@yao-pkg/pkg 6.14.0 → 6.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib-es5/esm-transformer.js +17 -2
- package/lib-es5/walker.js +11 -2
- package/package.json +1 -1
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.transformESMtoCJS = void 0;
|
|
29
|
+
exports.transformESMtoCJS = exports.rewriteMjsRequirePaths = void 0;
|
|
30
30
|
const babel = __importStar(require("@babel/parser"));
|
|
31
31
|
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
32
32
|
const esbuild = __importStar(require("esbuild"));
|
|
@@ -191,6 +191,21 @@ function replaceImportMetaObject(code) {
|
|
|
191
191
|
// Match: const import_meta = {};
|
|
192
192
|
return code.replace(/const import_meta\s*=\s*\{\s*\};/, shimImplementation);
|
|
193
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Rewrite relative `.mjs` require paths to `.js` in CJS output
|
|
196
|
+
*
|
|
197
|
+
* When esbuild transforms ESM to CJS, it converts `import './foo.mjs'` to `require('./foo.mjs')`.
|
|
198
|
+
* Since the packer renames `.mjs` files to `.js` in the snapshot, the require paths must be
|
|
199
|
+
* updated to match. This handles the rewriting at build time.
|
|
200
|
+
*
|
|
201
|
+
* @param code - The CJS code after esbuild transformation
|
|
202
|
+
* @returns Code with relative .mjs require paths rewritten to .js
|
|
203
|
+
*/
|
|
204
|
+
function rewriteMjsRequirePaths(code) {
|
|
205
|
+
// Match require("./path.mjs") or require('../path.mjs') with relative paths only
|
|
206
|
+
return code.replace(/require\((["'])(\.\.?\/[^"']*?)\.mjs\1\)/g, 'require($1$2.js$1)');
|
|
207
|
+
}
|
|
208
|
+
exports.rewriteMjsRequirePaths = rewriteMjsRequirePaths;
|
|
194
209
|
/**
|
|
195
210
|
* Transform ESM code to CommonJS using esbuild
|
|
196
211
|
* This allows ESM modules to be compiled to bytecode via vm.Script
|
|
@@ -344,7 +359,7 @@ function transformESMtoCJS(code, filename) {
|
|
|
344
359
|
// Inject import.meta shims after esbuild transformation if needed
|
|
345
360
|
let finalCode = result.code;
|
|
346
361
|
if (usesImportMeta) {
|
|
347
|
-
finalCode = replaceImportMetaObject(
|
|
362
|
+
finalCode = replaceImportMetaObject(finalCode);
|
|
348
363
|
}
|
|
349
364
|
return {
|
|
350
365
|
code: finalCode,
|
package/lib-es5/walker.js
CHANGED
|
@@ -48,6 +48,9 @@ const options_1 = __importDefault(require("./options"));
|
|
|
48
48
|
// performance hit.
|
|
49
49
|
const strictVerify = Boolean(process.env.PKG_STRICT_VER);
|
|
50
50
|
const win32 = process.platform === 'win32';
|
|
51
|
+
// Extensions to try when resolving modules
|
|
52
|
+
// Includes .mjs to support ESM files that get transformed to .js
|
|
53
|
+
const MODULE_RESOLVE_EXTENSIONS = ['.js', '.json', '.node', '.mjs'];
|
|
51
54
|
/**
|
|
52
55
|
* Checks if a module is a core module
|
|
53
56
|
* module.isBuiltin is available in Node.js 16.17.0 or later. Use that if available
|
|
@@ -632,7 +635,8 @@ class Walker {
|
|
|
632
635
|
// it is not enough because 'typos.json'
|
|
633
636
|
// is not taken in require('./typos')
|
|
634
637
|
// in 'normalize-package-data/lib/fixer.js'
|
|
635
|
-
|
|
638
|
+
// Also include .mjs to support ESM files that get transformed to .js
|
|
639
|
+
extensions: MODULE_RESOLVE_EXTENSIONS,
|
|
636
640
|
catchReadFile,
|
|
637
641
|
catchPackageFilter,
|
|
638
642
|
});
|
|
@@ -665,7 +669,7 @@ class Walker {
|
|
|
665
669
|
try {
|
|
666
670
|
newFile2 = await (0, follow_1.follow)(derivative.alias, {
|
|
667
671
|
basedir: path_1.default.dirname(record.file),
|
|
668
|
-
extensions:
|
|
672
|
+
extensions: MODULE_RESOLVE_EXTENSIONS,
|
|
669
673
|
ignoreFile: newPackage.packageJson,
|
|
670
674
|
});
|
|
671
675
|
if (strictVerify) {
|
|
@@ -867,6 +871,11 @@ class Walker {
|
|
|
867
871
|
const derivatives2 = [];
|
|
868
872
|
stepDetect(record, marker, derivatives2);
|
|
869
873
|
await this.stepDerivatives(record, marker, derivatives2);
|
|
874
|
+
// After dependencies are resolved, rewrite .mjs require paths to .js
|
|
875
|
+
// since the packer renames .mjs files to .js in the snapshot
|
|
876
|
+
if (record.wasTransformed && record.body) {
|
|
877
|
+
record.body = Buffer.from((0, esm_transformer_1.rewriteMjsRequirePaths)(record.body.toString('utf8')), 'utf8');
|
|
878
|
+
}
|
|
870
879
|
}
|
|
871
880
|
}
|
|
872
881
|
record[store] = true;
|