node_modules/browser-pack-flat/index.js
// Parse the module and collect require() calls and exports assignments.
Parse the module and collect require() calls and exports assignments.
Parse the module and collect require () calls and exports assignments .
function parseModule (row, index, rows, opts) {
  var moduleExportsName = toIdentifier('_$' + getModuleName(row.file || '') + '_' + row.id)
  if (dedupedRx.test(row.source)) {
    var n = row.source.match(dedupedRx)[1]
    var dedup = rows.filter(function (other) {
      return String(other.id) === n
    })[0]
    row.source = dedup.source
  }
  var requireCalls = []
  var orderOfExecution = new Map()
  var ast
  var globalScope = {
    type: 'BrowserPackFlatWrapper',
    parent: null
  }
  scan.createScope(globalScope, ['require', 'module', 'exports'])
  var source = row.source
  function isOrderUnpredictable (node) {
    while ((node = node.parent)) {
      if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
        if (node.parent && node.parent.type === 'CallExpression' && node.parent.callee === node) {
          continue
        }
        if (node.parent && node.parent.type === 'MemberExpression' &&
            node.parent.object === node &&
            (node.parent.property.name === 'call' || node.parent.property.name === 'apply') &&
            node.parent.parent && node.parent.parent.type === 'CallExpression') {
          continue
        }
      }
      if (node.parent && node.parent.type === 'IfStatement' && node.parent.test === node) {
        node = node.parent
        continue
      }
      if (node.type === 'IfStatement' || 
          node.type === 'WhileStatement' || 
          node.type === 'ForStatement' || 
          node.type === 'ForInStatement' || 
          node.type === 'FunctionExpression' || 
          node.type === 'FunctionDeclaration' || 
          node.type === 'ArrowFunctionExpression' 
      ) {
        return true
      }
    }
    return false
  }
  var alreadyPredictablyEvaluated = new Set()
  var magicString = transformAst(source, {
    ecmaVersion: 9,
    inputFilename: row.sourceFile,
    sourceType: opts.sourceType || 'script'
  }, function (node) {
    if (node.type === 'Program') ast = node
    scan.visitScope(node)
    if (isRequire(node)) {
      var argument = node.arguments[0]
      var required = argument.type === 'Literal' ? argument.value : null
      if (required !== null && moduleExists(row.deps[required])) {
        var other = rows.byId[row.deps[required]]
        if (isOrderUnpredictable(node)) {
          if (!alreadyPredictablyEvaluated.has(other)) {
            other[kEvaluateOnDemand] = true
          }
        } else {
          alreadyPredictablyEvaluated.add(other)
        }
        requireCalls.push({
          id: row.deps[required],
          node: node,
          requiredModule: other
        })
      } else if (required !== null) {
        requireCalls.push({
          external: true,
          id: row.deps[required] || required,
          node: node
        })
      }
      if (required !== null) {
        orderOfExecution.set(row.deps[required] || required, node.end)
      }
      function moduleExists (id) {
        return id != null && !!rows.byId[id]
      }
    }
  })
  magicString.walk(function (node) {
    ast.parent = globalScope
    scan.visitBinding(node)
  })
  var requireList = scan.scope(globalScope).getReferences('require')
  var moduleExportsList = scan.scope(globalScope).getReferences('module')
    .map(function (node) { return node.parent })
    .filter(isModuleExports)
  var exportsList = scan.scope(globalScope).getReferences('exports')
  var moduleList = scan.scope(globalScope).getReferences('module')
    .filter(function (node) { return !isModuleExports(node.parent) })
  var isSimpleExport = false
  if (moduleExportsList.length === 1 && exportsList.length === 0 && moduleList.length === 0) {
    var node = moduleExportsList[0]
    if (node.parent.type === 'AssignmentExpression' && node.parent.left === node &&
        node.parent.parent.type === 'ExpressionStatement') {
      isSimpleExport = scan.nearestScope(node.object, false) === ast
      var name = getNodeName(node.parent.right)
      if (name) {
        moduleExportsName = toIdentifier('_$' + name + '_' + row.id)
      }
    }
  }
  row[kAst] = ast
  row[kIsSimpleExport] = isSimpleExport
  row[kExportsName] = moduleExportsName
  row.hasExports = (moduleExportsList.length + exportsList.length) > 0
  row[kRequireCalls] = requireCalls
  row[kDependencyOrder] = orderOfExecution
  row[kReferences] = {
    require: requireList,
    module: moduleList,
    exports: exportsList,
    'module.exports': moduleExportsList
  }
  row[kMagicString] = magicString
}
function parseModule ( row , index , rows , opts ) { var moduleExportsName = toIdentifier ( '_$' + getModuleName ( row . file || '' ) + '_' + row . id ) if ( dedupedRx . test ( row . source ) ) { var n = row . source . match ( dedupedRx ) [ 1 ] var dedup = rows . filter ( function ( other ) { return String ( other . id ) === n } ) [ 0 ] row . source = dedup . source } var requireCalls = [ ] var orderOfExecution = new Map ( ) var ast var globalScope = { type : 'BrowserPackFlatWrapper' , parent : null } scan . createScope ( globalScope , [ 'require' , 'module' , 'exports' ] ) var source = row . source function isOrderUnpredictable ( node ) { while ( ( node = node . parent ) ) { if ( node . type === 'FunctionExpression' || node . type === 'ArrowFunctionExpression' ) { if ( node . parent && node . parent . type === 'CallExpression' && node . parent . callee === node ) { continue } if ( node . parent && node . parent . type === 'MemberExpression' && node . parent . object === node && ( node . parent . property . name === 'call' || node . parent . property . name === 'apply' ) && node . parent . parent && node . parent . parent . type === 'CallExpression' ) { continue } } if ( node . parent && node . parent . type === 'IfStatement' && node . parent . test === node ) { node = node . parent continue } if ( node . type === 'IfStatement' || node . type === 'WhileStatement' || node . type === 'ForStatement' || node . type === 'ForInStatement' || node . type === 'FunctionExpression' || node . type === 'FunctionDeclaration' || node . type === 'ArrowFunctionExpression' ) { return true } } return false } var alreadyPredictablyEvaluated = new Set ( ) var magicString = transformAst ( source , { ecmaVersion : 9 , inputFilename : row . sourceFile , sourceType : opts . sourceType || 'script' } , function ( node ) { if ( node . type === 'Program' ) ast = node scan . visitScope ( node ) if ( isRequire ( node ) ) { var argument = node . arguments [ 0 ] var required = argument . type === 'Literal' ? argument . value : null if ( required !== null && moduleExists ( row . deps [ required ] ) ) { var other = rows . byId [ row . deps [ required ] ] if ( isOrderUnpredictable ( node ) ) { if ( ! alreadyPredictablyEvaluated . has ( other ) ) { other [ kEvaluateOnDemand ] = true } } else { alreadyPredictablyEvaluated . add ( other ) } requireCalls . push ( { id : row . deps [ required ] , node : node , requiredModule : other } ) } else if ( required !== null ) { requireCalls . push ( { external : true , id : row . deps [ required ] || required , node : node } ) } if ( required !== null ) { orderOfExecution . set ( row . deps [ required ] || required , node . end ) } function moduleExists ( id ) { return id != null && ! ! rows . byId [ id ] } } } ) magicString . walk ( function ( node ) { ast . parent = globalScope scan . visitBinding ( node ) } ) var requireList = scan . scope ( globalScope ) . getReferences ( 'require' ) var moduleExportsList = scan . scope ( globalScope ) . getReferences ( 'module' ) . map ( function ( node ) { return node . parent } ) . filter ( isModuleExports ) var exportsList = scan . scope ( globalScope ) . getReferences ( 'exports' ) var moduleList = scan . scope ( globalScope ) . getReferences ( 'module' ) . filter ( function ( node ) { return ! isModuleExports ( node . parent ) } ) var isSimpleExport = false if ( moduleExportsList . length === 1 && exportsList . length === 0 && moduleList . length === 0 ) { var node = moduleExportsList [ 0 ] if ( node . parent . type === 'AssignmentExpression' && node . parent . left === node && node . parent . parent . type === 'ExpressionStatement' ) { isSimpleExport = scan . nearestScope ( node . object , false ) === ast var name = getNodeName ( node . parent . right ) if ( name ) { moduleExportsName = toIdentifier ( '_$' + name + '_' + row . id ) } } } row [ kAst ] = ast row [ kIsSimpleExport ] = isSimpleExport row [ kExportsName ] = moduleExportsName row . hasExports = ( moduleExportsList . length + exportsList . length ) > 0 row [ kRequireCalls ] = requireCalls row [ kDependencyOrder ] = orderOfExecution row [ kReferences ] = { require : requireList , module : moduleList , exports : exportsList , 'module.exports' : moduleExportsList } row [ kMagicString ] = magicString }
Parse the module and collect require() calls and exports assignments.
Parse the module and collect require () calls and exports assignments .
// Holds the `module.exports` variable name. // browserify is clever about deduping modules with the same source code, // but it needs the browser-pack runtime in order to do so. // we don't have that runtime so this … re-dupes those modules. // hack to keep track of `module`/`exports` references correctly. // in node.js they're defined by a function wrapper, so their scope is // one level higher-ish than the module scope. this emulates that. // Determine if a require() call may not be evaluated according to its linear source position // This happens primarily if the call is inside a function, or inside a conditional branch // Special-case for IIFE, behaviour is the same as evaluating inline // (() => {})() // (function(){}).call(null) // this form is added by browserify to inject globals // if(require('d')) is still in-order // the same is technically true for while(require()) and for(require();;) but those are more rare // Skip the IfStatement // if(false) require() // while(false) require() // for(;false;) require() // for(x in []) require() // setTimeout(function(){ require() }) // function neverCalled(){ require() } // setTimeout(()=> require()) // Keep track of modules that were already evaluated in a predictable order so that multiple // require('a') occurences don't force 'a' to become on-demand // we'll do two walks along the AST in order to detect variables and their references. // we initialise the scopes and declarations in the first one here, and then collect // references in the second. // also collect requires while we're here // transform-ast has set this to `undefined` // Detect simple exports that are just `module.exports = xyz`, we can compile them to a single // variable assignment.