@@ -10,7 +10,6 @@ const getUniqueAssetName = require('../utils/dedupe-names');
10
10
const sharedlibEmit = require ( '../utils/sharedlib-emit' ) ;
11
11
const glob = require ( 'glob' ) ;
12
12
const getPackageBase = require ( '../utils/get-package-base' ) ;
13
- const { getOptions } = require ( 'loader-utils' ) ;
14
13
15
14
// binary support for inlining logic from - node-pre-gyp/lib/pre-binding.js
16
15
function isPregypId ( id ) {
@@ -114,9 +113,6 @@ function isExpressionReference(node, parent) {
114
113
// disregard the `bar` in var bar = asdf
115
114
if ( parent . type === 'VariableDeclarator' && node . id === node ) return false ;
116
115
117
- // disregard the `x` in import/export 'x';
118
- if ( parent . type === 'ImportDeclaration' || parent . type === 'ExportNamedDeclaration' || parent . type === 'ExportAllDeclaration' ) return false ;
119
-
120
116
return true ;
121
117
}
122
118
@@ -275,14 +271,13 @@ function handleWrappers (ast, scope, magicString, len) {
275
271
return { ast, scope, transformed } ;
276
272
}
277
273
278
- const relocateRegEx = / _ \_ d i r n a m e | _ \_ f i l e n a m e | r e q u i r e \. m a i n | n o d e - p r e - g y p | b i n d i n g s | d e f i n e | [ ' " ] \. \. ? \/ / ;
274
+ const relocateRegEx = / _ \_ d i r n a m e | _ \_ f i l e n a m e | r e q u i r e \. m a i n | n o d e - p r e - g y p | b i n d i n g s | d e f i n e | r e q u i r e \( \s * [ ^ ' " ] / ;
279
275
280
276
module . exports = function ( code ) {
281
277
if ( this . cacheable )
282
278
this . cacheable ( ) ;
283
279
this . async ( ) ;
284
280
const id = this . resourcePath ;
285
- const { cwd } = getOptions ( this ) ;
286
281
287
282
if ( id . endsWith ( '.json' ) || ! code . match ( relocateRegEx ) )
288
283
return this . callback ( null , code ) ;
@@ -497,8 +492,8 @@ module.exports = function (code) {
497
492
node . arguments [ 0 ] . type === 'Literal' ;
498
493
}
499
494
500
- // detect require(...) || require.resolve(...);
501
- function isRequire ( node ) {
495
+ // detect require(...)
496
+ function isRequire ( node , requireResolve ) {
502
497
return node &&
503
498
node . type === 'CallExpression' &&
504
499
( node . callee . type === 'Identifier' &&
@@ -507,8 +502,16 @@ module.exports = function (code) {
507
502
node . callee . type === 'MemberExpression' &&
508
503
node . callee . object . type === 'Identifier' &&
509
504
node . callee . object . name === 'require' &&
505
+ ( ! requireResolve ||
510
506
node . callee . property . type === 'Identifier' &&
511
- node . callee . property . name === 'resolve' ) ;
507
+ node . callee . property . name === 'resolve' ) ) ;
508
+ }
509
+
510
+ function isAnalyzableRequire ( expression ) {
511
+ if ( expression . type === 'Identifier' || expression . type === 'MemberExpression' )
512
+ return false ;
513
+ // "possibly" analyzable (this can be further restricted over time)
514
+ return true ;
512
515
}
513
516
514
517
( { ast, scope, transformed } = handleWrappers ( ast , scope , magicString , code . length ) ) ;
@@ -543,18 +546,9 @@ module.exports = function (code) {
543
546
}
544
547
}
545
548
}
546
- // special trigger for './asset.txt' references
547
- else if ( node . type === 'Literal' && typeof node . value === 'string' &&
548
- ( node . value . startsWith ( './' ) || node . value . startsWith ( '../' ) ) &&
549
- isExpressionReference ( node , parent ) ) {
550
- staticChildValue = node . value ;
551
- staticChildNode = node ;
552
- staticChildValueBindingsInstance = staticBindingsInstance ;
553
- return this . skip ( ) ;
554
- }
555
549
// require('bindings')('asdf')
556
- else if ( node . type === 'CallExpression' &&
557
- ! isESM && isStaticRequire ( node . callee ) &&
550
+ else if ( node . type === 'CallExpression' && ! isESM &&
551
+ isStaticRequire ( node . callee ) &&
558
552
node . callee . arguments [ 0 ] . value === 'bindings' ) {
559
553
staticChildValue = computeStaticValue ( node , true ) ;
560
554
if ( staticChildValue ) {
@@ -563,6 +557,12 @@ module.exports = function (code) {
563
557
return this . skip ( ) ;
564
558
}
565
559
}
560
+ // require(dynamic) -> __non_webpack_require__(dynamic)
561
+ else if ( isRequire ( node ) && ! isAnalyzableRequire ( node . arguments [ 0 ] ) ) {
562
+ transformed = true ;
563
+ magicString . overwrite ( node . callee . start , node . callee . end , "__non_webpack_require__" ) ;
564
+ return this . skip ( ) ;
565
+ }
566
566
// nbind.init(...) -> require('./resolved.node')
567
567
else if ( nbindId && node . type === 'CallExpression' &&
568
568
node . callee . type === 'MemberExpression' &&
@@ -701,12 +701,9 @@ module.exports = function (code) {
701
701
return ;
702
702
}
703
703
// no static value -> see if we should emit the asset if it exists
704
- // never inline an asset path into a require statement though
704
+ // Currently we only handle files. In theory whole directories could also be emitted if necessary.
705
705
let stats ;
706
- if ( typeof staticChildValue === 'string' && ! isRequire ( node ) ) {
707
- if ( staticChildValue . startsWith ( './' ) || staticChildValue . startsWith ( '../' ) ) {
708
- staticChildValue = path . resolve ( cwd , staticChildValue ) ;
709
- }
706
+ if ( typeof staticChildValue === 'string' ) {
710
707
try {
711
708
stats = fs . statSync ( staticChildValue ) ;
712
709
}
0 commit comments