-
Notifications
You must be signed in to change notification settings - Fork 282
/
relocate-loader.js
594 lines (547 loc) · 24.9 KB
/
relocate-loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
const path = require('path');
const fs = require('graceful-fs');
const { walk } = require('estree-walker');
const MagicString = require('magic-string');
const { attachScopes } = require('rollup-pluginutils');
const evaluate = require('static-eval');
const acorn = require('acorn');
const bindings = require('bindings');
const getUniqueAssetName = require('../utils/dedupe-names');
const { getOptions } = require('loader-utils');
const sharedlibEmit = require('../utils/sharedlib-emit');
const glob = require('glob');
const getPackageBase = require('../utils/get-package-base');
// binary support for inlining logic from - node-pre-gyp/lib/pre-binding.js
function isPregypId (id) {
return id === 'node-pre-gyp' ||
id === 'node-pre-gyp/lib/pre-binding' ||
id === 'node-pre-gyp/lib/pre-binding.js';
}
const versioning = require('node-pre-gyp/lib/util/versioning.js');
const napi = require('node-pre-gyp/lib/util/napi.js');
const pregyp = {
find (package_json_path, opts) {
const package_json = JSON.parse(fs.readFileSync(package_json_path).toString());
versioning.validate_config(package_json, opts);
var napi_build_version;
if (napi.get_napi_build_versions (package_json, opts)) {
napi_build_version = napi.get_best_napi_build_version(package_json, opts);
}
opts = opts || {};
if (!opts.module_root) opts.module_root = path.dirname(package_json_path);
var meta = versioning.evaluate(package_json,opts,napi_build_version);
return meta.module;
}
};
function isExpressionReference(node, parent) {
if (parent.type === 'MemberExpression') return parent.computed || node === parent.object;
// disregard the `bar` in { bar: foo }
if (parent.type === 'Property' && node !== parent.value) return false;
// disregard the `bar` in `class Foo { bar () {...} }`
if (parent.type === 'MethodDefinition') return false;
// disregard the `bar` in `export { foo as bar }`
if (parent.type === 'ExportSpecifier' && node !== parent.local) return false;
// disregard the `bar` in var bar = asdf
if (parent.type === 'VariableDeclarator' && node.id === node) return false;
return true;
}
// Wrapper detections for require extraction handles:
//
// When.js-style AMD wrapper:
// (function (define) { 'use strict' define(function (require) { ... }) })
// (typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); })
// ->
// (function (define) { 'use strict' define(function () { ... }) })
// (typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); })
//
// Browserify-style wrapper
// (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bugsnag = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({
// 1:[function(require,module,exports){
// ...code...
// },{"external":undefined}], 2: ...
// },{},[24])(24)
// });
// ->
// (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bugsnag = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({
// 1:[function(require,module,exports){
// ...code...
// },{"external":undefined}], 2: ...
// },{
// "external": { exports: require('external') }
// },[24])(24)
// });
//
function handleWrappers (ast, scope, magicString, len) {
let transformed = false;
if (ast.body.length === 1 &&
ast.body[0].type === 'ExpressionStatement' &&
ast.body[0].expression.type === 'CallExpression' &&
ast.body[0].expression.callee.type === 'FunctionExpression' &&
ast.body[0].expression.arguments.length === 1) {
// When.js wrapper
const arg = ast.body[0].expression.arguments[0];
if (arg.type === 'ConditionalExpression' &&
arg.test.type === 'LogicalExpression' &&
arg.test.operator === '&&' &&
arg.test.left.type === 'BinaryExpression' &&
arg.test.left.operator === '===' &&
arg.test.left.left.type === 'UnaryExpression' &&
arg.test.left.left.operator === 'typeof' &&
arg.test.left.left.argument.name === 'define' &&
arg.test.left.right.type === 'Literal' &&
arg.test.left.right.value === 'function' &&
arg.test.right.type === 'MemberExpression' &&
arg.test.right.object.type === 'Identifier' &&
arg.test.right.property.type === 'Identifier' &&
arg.test.right.property.name === 'amd' &&
arg.test.right.computed === false &&
arg.alternate.type === 'FunctionExpression' &&
arg.alternate.params.length === 1 &&
arg.alternate.params[0].type === 'Identifier' &&
arg.alternate.body.body.length === 1 &&
arg.alternate.body.body[0].type === 'ExpressionStatement' &&
arg.alternate.body.body[0].expression.type === 'AssignmentExpression' &&
arg.alternate.body.body[0].expression.left.type === 'MemberExpression' &&
arg.alternate.body.body[0].expression.left.object.type === 'Identifier' &&
arg.alternate.body.body[0].expression.left.object.name === 'module' &&
arg.alternate.body.body[0].expression.left.property.type === 'Identifier' &&
arg.alternate.body.body[0].expression.left.property.name === 'exports' &&
arg.alternate.body.body[0].expression.left.computed === false &&
arg.alternate.body.body[0].expression.right.type === 'CallExpression' &&
arg.alternate.body.body[0].expression.right.callee.type === 'Identifier' &&
arg.alternate.body.body[0].expression.right.callee.name === arg.alternate.params[0].name &&
arg.alternate.body.body[0].expression.right.arguments.length === 1 &&
arg.alternate.body.body[0].expression.right.arguments[0].type === 'Identifier' &&
arg.alternate.body.body[0].expression.right.arguments[0].name === 'require') {
let iifeBody = ast.body[0].expression.callee.body.body;
if (iifeBody[0].type === 'ExpressionStatement' &&
iifeBody[0].expression.type === 'Literal' &&
iifeBody[0].expression.value === 'use strict') {
iifeBody = iifeBody.slice(1);
}
if (iifeBody.length === 1 &&
iifeBody[0].type === 'ExpressionStatement' &&
iifeBody[0].expression.type === 'CallExpression' &&
iifeBody[0].expression.callee.type === 'Identifier' &&
iifeBody[0].expression.callee.name === arg.test.right.object.name &&
iifeBody[0].expression.arguments.length === 1 &&
iifeBody[0].expression.arguments[0].type === 'FunctionExpression' &&
iifeBody[0].expression.arguments[0].params.length === 1 &&
iifeBody[0].expression.arguments[0].params[0].type === 'Identifier' &&
iifeBody[0].expression.arguments[0].params[0].name === 'require') {
magicString.remove(iifeBody[0].expression.arguments[0].params[0].start, iifeBody[0].expression.arguments[0].params[0].end);
transformed = true;
}
}
// browserify wrapper
else if (arg.type === 'FunctionExpression' &&
arg.params.length === 0 &&
arg.body.body.length === 2 &&
arg.body.body[0].type === 'VariableDeclaration' &&
arg.body.body[0].declarations.length === 3 &&
arg.body.body[0].declarations.every(decl => decl.init === null && decl.id.type === 'Identifier') &&
arg.body.body[1].type === 'ReturnStatement' &&
arg.body.body[1].argument.type === 'CallExpression' &&
arg.body.body[1].argument.callee.type === 'CallExpression' &&
arg.body.body[1].argument.arguments.length &&
arg.body.body[1].argument.arguments.every(arg => arg.type === 'Literal' && typeof arg.value === 'number') &&
arg.body.body[1].argument.callee.callee.type === 'CallExpression' &&
arg.body.body[1].argument.callee.callee.callee.type === 'FunctionExpression' &&
arg.body.body[1].argument.callee.callee.arguments.length === 0 &&
// (dont go deeper into browserify loader internals than this)
arg.body.body[1].argument.callee.arguments.length === 3 &&
arg.body.body[1].argument.callee.arguments[0].type === 'ObjectExpression' &&
arg.body.body[1].argument.callee.arguments[1].type === 'ObjectExpression' &&
arg.body.body[1].argument.callee.arguments[2].type === 'ArrayExpression') {
const modules = arg.body.body[1].argument.callee.arguments[0].properties;
// verify modules is the expected data structure
// in the process, extract external requires
const externals = {};
if (modules.every(m => {
if (m.type !== 'Property' ||
m.computed !== false ||
m.key.type !== 'Literal' ||
typeof m.key.value !== 'number' ||
m.value.type !== 'ArrayExpression' ||
m.value.elements.length !== 2 ||
m.value.elements[0].type !== 'FunctionExpression' ||
m.value.elements[1].type !== 'ObjectExpression')
return false;
// detect externals from undefined moduleMap values
const moduleMap = m.value.elements[1].properties;
for (const prop of moduleMap) {
if (prop.type !== 'Property' ||
(prop.value.type !== 'Identifier' && prop.value.type !== 'Literal') ||
prop.key.type !== 'Literal' ||
typeof prop.key.value !== 'string' ||
prop.computed)
return false;
if (prop.value.type === 'Identifier' && prop.value.name === 'undefined')
externals[prop.key.value] = true;
}
return true;
})) {
// if we have externals, inline them into the browserify cache for webpack to pick up
const externalIds = Object.keys(externals);
if (externalIds.length) {
const cache = arg.body.body[1].argument.callee.arguments[1];
const renderedExternals = externalIds.map(ext => `"${ext}": { exports: require("${ext}") }`).join(',\n ');
magicString.appendRight(cache.end - 1, renderedExternals);
transformed = true;
}
}
}
}
return { ast, scope, transformed };
}
const relocateRegEx = /_\_dirname|_\_filename|require\.main|node-pre-gyp|bindings|define/;
module.exports = function (code) {
this.async();
const id = this.resourcePath;
if (id.endsWith('.json') || !code.match(relocateRegEx))
return this.callback(null, code);
const options = getOptions(this);
const emitAsset = (assetPath) => {
// JS assets to support require(assetPath) and not fs-based handling
// NB package.json is ambiguous here...
if (assetPath.endsWith('.js') || assetPath.endsWith('.mjs'))
return;
if (options.assets[assetPath])
return "__dirname + '/" + JSON.stringify(options.assets[assetPath]).slice(1, -1) + "'";
let outName = path.basename(assetPath);
if (assetPath.endsWith('.node')) {
// retain directory depth structure for binaries for rpath to work out
if (pkgBase)
outName = assetPath.substr(pkgBase.length);
// If the asset is a ".node" binary, then glob for possible shared
// libraries that should also be included
if (!options.assetNames[`sharedlibs:${pkgBase}`]) {
options.assetNames[`sharedlibs:${pkgBase}`] = true;
assetEmissionPromises = assetEmissionPromises.then(sharedlibEmit(pkgBase, this.emitFile));
}
}
const name = getUniqueAssetName(outName, assetPath, options.assetNames);
options.assets[assetPath] = name;
// console.log('Emitting ' + assetPath + ' for module ' + id);
assetEmissionPromises = assetEmissionPromises.then(async () => {
const source = await new Promise((resolve, reject) =>
fs.readFile(assetPath, (err, source) => err ? reject(err) : resolve(source))
);
this.emitFile(name, source);
});
return "__dirname + '/" + JSON.stringify(name).slice(1, -1) + "'";
};
const emitAssetDirectory = (assetDirPath) => {
if (options.assets[assetDirPath])
return "__dirname + '/" + JSON.stringify(options.assets[assetDirPath]).slice(1, -1) + "'";
const dirName = path.basename(assetDirPath);
const name = getUniqueAssetName(dirName, assetDirPath, options.assetNames);
options.assets[assetDirPath] = name;
assetEmissionPromises = assetEmissionPromises.then(async () => {
const files = await new Promise((resolve, reject) =>
glob(assetDirPath + '/**/*', { mark: true, ignore: 'node_modules/**/*' }, (err, files) => err ? reject(err) : resolve(files))
);
await Promise.all(files.map(async file => {
// dont emit empty directories
if (file.endsWith('/'))
return;
const source = await new Promise((resolve, reject) =>
fs.readFile(file, (err, source) => err ? reject(err) : resolve(source))
);
this.emitFile(name + file.substr(assetDirPath.length), source);
}));
});
return "__dirname + '/" + JSON.stringify(name).slice(1, -1) + "'";
};
let assetEmissionPromises = Promise.resolve();
const magicString = new MagicString(code);
let ast, isESM;
try {
ast = acorn.parse(code, { allowReturnOutsideFunction: true });
isESM = false;
}
catch (e) {}
if (!ast) {
ast = acorn.parse(code, { sourceType: 'module' });
isESM = true;
}
let scope = attachScopes(ast, 'scope');
let pathId, pathImportIds = {};
let pregypId, bindingsId;
const shadowDepths = Object.create(null);
shadowDepths.__filename = 0;
shadowDepths.__dirname = 0;
if (!isESM) {
shadowDepths.require = 0;
}
else {
for (const decl of ast.body) {
// Detects:
// import * as path from 'path';
// import path from 'path';
// import { join } from 'path';
if (decl.type === 'ImportDeclaration') {
const source = decl.source.value;
if (source === 'path') {
for (const impt of decl.specifiers) {
if (impt.type === 'ImportNamespaceSpecifier' || impt.type === 'ImportDefaultSpecifier') {
pathId = impt.local.name;
shadowDepths[pathId] = 0;
}
else if (impt.type === 'ImportSpecifier') {
pathImportIds[impt.local.name] = impt.imported.name;
shadowDepths[impt.local.name] = 0;
}
}
}
// import binary from 'node-pre-gyp';
// import * as binary from 'node-pre-gyp';
// import { find } from 'node-pre-gyp' not yet implemented
else if (isPregypId(source)) {
for (const impt of decl.specifiers) {
if (impt.type === 'ImportNamespaceSpecifier' || impt.type === 'ImportDefaultSpecifier') {
pregypId = impt.local.name;
shadowDepths[pregypId] = 0;
}
}
}
// import bindings from 'bindings';
else if (source === 'bindings') {
for (const impt of decl.specifiers) {
if (impt.type === 'ImportDefaultSpecifier') {
bindingsId = impt.local.name;
shadowDepths[bindingsId] = 0;
}
}
}
}
}
}
let transformed = false;
let staticBindingsInstance = false;
// calculate the base-level package folder to load bindings from
const pkgBase = getPackageBase(id);
function createBindings () {
return (opts = {}) => {
if (typeof opts === 'string')
opts = { bindings: opts };
if (!opts.path) {
opts.path = true;
staticBindingsInstance = true;
}
opts.module_root = pkgBase;
return bindings(opts);
};
}
function computeStaticValue (expr, bindingsReq) {
staticBindingsInstance = false;
// function expression analysis disabled due to static-eval locals bug
if (expr.type === 'FunctionExpression')
return;
const vars = {};
if (shadowDepths.__dirname === 0)
vars.__dirname = path.resolve(id, '..');
if (shadowDepths.__filename === 0)
vars.__filename = id;
if (pathId) {
if (shadowDepths[pathId] === 0)
vars[pathId] = path;
}
if (pregypId) {
if (shadowDepths[pregypId] === 0)
vars[pregypId] = pregyp;
}
if (bindingsId) {
if (shadowDepths[bindingsId] === 0)
vars[bindingsId] = createBindings();
}
for (const pathFn of Object.keys(pathImportIds)) {
if (shadowDepths[pathFn] === 0)
vars[pathFn] = path[pathImportIds[pathFn]];
}
if (bindingsReq && shadowDepths.require === 0)
vars.require = function (reqId) {
if (reqId === 'bindings')
return createBindings();
};
// evaluate returns undefined for non-statically-analyzable
return evaluate(expr, vars);
}
// statically determinable leaves are tracked, and inlined when the
// greatest parent statically known leaf computation corresponds to an asset path
let staticChildNode, staticChildValue, staticChildValueBindingsInstance;
// detect require('asdf');
function isStaticRequire (node) {
return node &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
shadowDepths.require === 0 &&
node.arguments.length === 1 &&
node.arguments[0].type === 'Literal';
}
({ ast, scope, transformed } = handleWrappers(ast, scope, magicString, code.length));
walk(ast, {
enter (node, parent) {
if (node.scope) {
scope = node.scope;
for (const id in node.scope.declarations) {
if (id in shadowDepths)
shadowDepths[id]++;
}
}
if (staticChildNode)
return this.skip();
// detect asset leaf expression triggers (if not already)
// __dirname, __filename, binary only currently as well as require('bindings')(...)
// Can add require.resolve, import.meta.url, even path-like environment variables
if (node.type === 'Identifier' && isExpressionReference(node, parent)) {
if (!shadowDepths[node.name] &&
(node.name === '__dirname' || node.name === '__filename' ||
node.name === pregypId || node.name === bindingsId)) {
staticChildValue = computeStaticValue(node, false);
// if it computes, then we start backtracking
if (staticChildValue) {
staticChildNode = node;
staticChildValueBindingsInstance = staticBindingsInstance;
return this.skip();
}
}
}
// require('bindings')('asdf')
else if (node.type === 'CallExpression' &&
!isESM && isStaticRequire(node.callee) &&
node.callee.arguments[0].value === 'bindings') {
staticChildValue = computeStaticValue(node, true);
if (staticChildValue) {
staticChildNode = node;
staticChildValueBindingsInstance = staticBindingsInstance;
return this.skip();
}
}
// require.main -> __non_webpack_require__.main
else if (!isESM && node.type === 'MemberExpression' &&
node.object.type === 'Identifier' &&
node.object.name === 'require' &&
!shadowDepths.require &&
node.property.type === 'Identifier' &&
node.property.name === 'main' &&
!node.computed) {
magicString.overwrite(node.object.start, node.object.end, '__non_webpack_require__');
transformed = true;
}
// for now we only support top-level variable declarations
// so "var { join } = require('path')" will only detect in the top scope.
// Intermediate scope handling for these requires is straightforward, but
// would need nested shadow depth handling of the pathIds.
else if (parent === ast && node.type === 'VariableDeclaration') {
for (const decl of node.declarations) {
// var path = require('path')
if (decl.id.type === 'Identifier' &&
!isESM && isStaticRequire(decl.init)) {
if (decl.init.arguments[0].value === 'path') {
pathId = decl.id.name;
shadowDepths[pathId] = 0;
return this.skip();
}
// var binary = require('node-pre-gyp')
else if (isPregypId(decl.init.arguments[0].value)) {
pregypId = decl.id.name;
shadowDepths[pregypId] = 0;
return this.skip();
}
// var bindings = require('bindings')
else if (decl.init.arguments[0].value === 'bindings') {
bindingsId = decl.id.name;
shadowDepths[bindingsId] = 0;
return this.skip();
}
}
// var { join } = path | require('path');
else if (decl.id.type === 'ObjectPattern' && decl.init &&
(decl.init.type === 'Identifier' && decl.init.name === pathId && shadowDepths[pathId] === 0) ||
!isESM && isStaticRequire(decl.init) && decl.init.arguments[0].value === 'path') {
for (const prop of decl.id.properties) {
if (prop.type !== 'Property' ||
prop.key.type !== 'Identifier' ||
prop.value.type !== 'Identifier')
continue;
pathImportIds[prop.value.name] = prop.key.name;
shadowDepths[prop.key.name] = 0;
return this.skip();
}
}
// var join = path.join
else if (decl.id.type === 'Identifier' &&
decl.init &&
decl.init.type === 'MemberExpression' &&
decl.init.object.type === 'Identifier' &&
decl.init.object.name === pathId &&
shadowDepths[decl.init.object.name] === 0 &&
decl.init.computed === false &&
decl.init.property.type === 'Identifier') {
pathImportIds[decl.init.property.name] = decl.id.name;
shadowDepths[decl.id.name] = 0;
return this.skip();
}
}
}
},
leave (node, parent) {
if (node.scope) {
scope = scope.parent;
for (const id in node.scope.declarations) {
if (id in shadowDepths)
shadowDepths[id]--;
}
}
// computing a static expression outward
// -> compute and backtrack
if (staticChildNode) {
const curStaticValue = computeStaticValue(node, false);
if (curStaticValue) {
staticChildValue = curStaticValue;
staticChildNode = node;
staticChildValueBindingsInstance = staticBindingsInstance;
return;
}
// no static value -> see if we should emit the asset if it exists
// Currently we only handle files. In theory whole directories could also be emitted if necessary.
let stats;
if (typeof staticChildValue === 'string') {
try {
stats = fs.statSync(staticChildValue);
}
catch (e) {}
}
if (stats && stats.isFile()) {
let replacement = emitAsset(path.resolve(staticChildValue));
// require('bindings')(...)
// -> require(require('bindings')(...))
if (staticChildValueBindingsInstance) {
replacement = '__non_webpack_require__(' + replacement + ')';
}
if (replacement) {
transformed = true;
magicString.overwrite(staticChildNode.start, staticChildNode.end, replacement);
}
}
else if (stats && stats.isDirectory()) {
let replacement = emitAssetDirectory(path.resolve(staticChildValue));
if (replacement) {
transformed = true;
magicString.overwrite(staticChildNode.start, staticChildNode.end, replacement);
}
}
staticChildNode = staticChildValue = undefined;
}
}
});
if (!transformed)
return this.callback(null, code);
assetEmissionPromises.then(() => {
code = magicString.toString();
const map = magicString.generateMap();
this.callback(null, code, map);
});
};