@hakobu/hakobu 1.0.0
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/LICENSE +21 -0
- package/README.md +673 -0
- package/dictionary/angular-bridge.js +3 -0
- package/dictionary/any-promise.js +3 -0
- package/dictionary/async.js +3 -0
- package/dictionary/aws-sdk.js +7 -0
- package/dictionary/babel-core.js +3 -0
- package/dictionary/batch.js +3 -0
- package/dictionary/bcrypt.js +3 -0
- package/dictionary/better-sqlite3.js +3 -0
- package/dictionary/bignum.js +3 -0
- package/dictionary/bindings.js +3 -0
- package/dictionary/blessed.js +8 -0
- package/dictionary/body-parser.js +9 -0
- package/dictionary/browserify.js +7 -0
- package/dictionary/bson.js +3 -0
- package/dictionary/buffermaker.js +7 -0
- package/dictionary/bunyan.js +9 -0
- package/dictionary/busboy.js +7 -0
- package/dictionary/bytes.js +3 -0
- package/dictionary/callsites.js +3 -0
- package/dictionary/chokidar.js +3 -0
- package/dictionary/coffee-script.js +7 -0
- package/dictionary/colors.js +3 -0
- package/dictionary/compression.js +3 -0
- package/dictionary/compressjs.js +7 -0
- package/dictionary/connect-mongo.js +3 -0
- package/dictionary/connect-mongodb.js +3 -0
- package/dictionary/connect-redis.js +3 -0
- package/dictionary/connect.js +10 -0
- package/dictionary/consolidate.js +3 -0
- package/dictionary/cookie-parser.js +3 -0
- package/dictionary/cookie.js +3 -0
- package/dictionary/cors.js +3 -0
- package/dictionary/cron.js +3 -0
- package/dictionary/cross-env.js +11 -0
- package/dictionary/cross-spawn-async.js +3 -0
- package/dictionary/curve25519.js +3 -0
- package/dictionary/data-preflight.js +7 -0
- package/dictionary/debug.js +3 -0
- package/dictionary/denymount.js +3 -0
- package/dictionary/diff.js +3 -0
- package/dictionary/drivelist.js +22 -0
- package/dictionary/ed25519.js +3 -0
- package/dictionary/ejs.js +3 -0
- package/dictionary/elasticsearch.js +3 -0
- package/dictionary/electron.js +17 -0
- package/dictionary/emailjs.js +3 -0
- package/dictionary/engine.io.js +3 -0
- package/dictionary/epoll.js +3 -0
- package/dictionary/errorhandler.js +7 -0
- package/dictionary/errors.js +7 -0
- package/dictionary/eslint.js +7 -0
- package/dictionary/etcher-image-write.js +3 -0
- package/dictionary/exceljs.js +21 -0
- package/dictionary/exiftool.exe.js +13 -0
- package/dictionary/exiftool.pl.js +13 -0
- package/dictionary/express-load.js +12 -0
- package/dictionary/express-session.js +3 -0
- package/dictionary/express.js +14 -0
- package/dictionary/extender.js +11 -0
- package/dictionary/extsprintf.js +3 -0
- package/dictionary/faye-websocket.js +3 -0
- package/dictionary/feathers.js +3 -0
- package/dictionary/findup-sync.js +3 -0
- package/dictionary/floordate.js +3 -0
- package/dictionary/fmt.js +3 -0
- package/dictionary/formidable.js +3 -0
- package/dictionary/fs-extra.js +3 -0
- package/dictionary/fsevents.js +3 -0
- package/dictionary/geoip-lite.js +7 -0
- package/dictionary/github.js +7 -0
- package/dictionary/gm.js +3 -0
- package/dictionary/google-closure-compiler-java.js +13 -0
- package/dictionary/google-closure-compiler.js +13 -0
- package/dictionary/googleapis.js +7 -0
- package/dictionary/got.js +3 -0
- package/dictionary/graceful-fs.js +19 -0
- package/dictionary/grpc.js +11 -0
- package/dictionary/gulp.js +3 -0
- package/dictionary/hap-nodejs.js +3 -0
- package/dictionary/heapdump.js +3 -0
- package/dictionary/hoek.js +3 -0
- package/dictionary/homebridge.js +3 -0
- package/dictionary/http-proxy.js +3 -0
- package/dictionary/http-server.js +3 -0
- package/dictionary/image-size.js +3 -0
- package/dictionary/indexof.js +3 -0
- package/dictionary/inquirer.js +3 -0
- package/dictionary/j.js +16 -0
- package/dictionary/jade.js +3 -0
- package/dictionary/jsdom.js +3 -0
- package/dictionary/json-stringify-date.js +3 -0
- package/dictionary/json-stringify-safe.js +3 -0
- package/dictionary/jsonwebtoken.js +3 -0
- package/dictionary/kerberos.js +3 -0
- package/dictionary/knex.js +7 -0
- package/dictionary/later.js +7 -0
- package/dictionary/level.js +3 -0
- package/dictionary/leveldown.js +10 -0
- package/dictionary/levelup.js +3 -0
- package/dictionary/liftoff.js +12 -0
- package/dictionary/lodash.js +3 -0
- package/dictionary/log4js.js +7 -0
- package/dictionary/logform.js +7 -0
- package/dictionary/machinepack-urls.js +7 -0
- package/dictionary/markdown.js +3 -0
- package/dictionary/mdns.js +6 -0
- package/dictionary/method-override.js +3 -0
- package/dictionary/microjob.js +16 -0
- package/dictionary/mime-types.js +3 -0
- package/dictionary/mime.js +3 -0
- package/dictionary/minimatch.js +3 -0
- package/dictionary/minstache.js +3 -0
- package/dictionary/module-deps.js +3 -0
- package/dictionary/moment-timezone.js +3 -0
- package/dictionary/moment.js +7 -0
- package/dictionary/mongodb-core.js +22 -0
- package/dictionary/mongodb.js +7 -0
- package/dictionary/mongoose.js +7 -0
- package/dictionary/mongoskin.js +7 -0
- package/dictionary/ms.js +3 -0
- package/dictionary/msgpack.js +3 -0
- package/dictionary/multer.js +3 -0
- package/dictionary/muri.js +3 -0
- package/dictionary/native-or-bluebird.js +3 -0
- package/dictionary/natives.js +3 -0
- package/dictionary/nconf.js +7 -0
- package/dictionary/nedb.js +3 -0
- package/dictionary/negotiator.js +7 -0
- package/dictionary/newrelic.js +3 -0
- package/dictionary/nib.js +3 -0
- package/dictionary/nightmare.js +18 -0
- package/dictionary/node-forge.js +7 -0
- package/dictionary/node-libcurl.js +3 -0
- package/dictionary/node-notifier.js +30 -0
- package/dictionary/node-pre-gyp.js +7 -0
- package/dictionary/node-redis-pubsub.js +3 -0
- package/dictionary/node-sass.js +3 -0
- package/dictionary/node-uuid.js +3 -0
- package/dictionary/node-xlsx.js +3 -0
- package/dictionary/node-zookeeper-client.js +7 -0
- package/dictionary/nodegit.js +7 -0
- package/dictionary/nodemailer-sendmail-transport.js +3 -0
- package/dictionary/nodemailer.js +3 -0
- package/dictionary/npm-registry-client.js +7 -0
- package/dictionary/npm.js +7 -0
- package/dictionary/nssocket.js +3 -0
- package/dictionary/oauth2orize.js +7 -0
- package/dictionary/octobat.js +3 -0
- package/dictionary/open.js +13 -0
- package/dictionary/opn.js +4 -0
- package/dictionary/optimist.js +3 -0
- package/dictionary/passport-local.js +3 -0
- package/dictionary/passport.js +3 -0
- package/dictionary/pg-cursor.js +3 -0
- package/dictionary/pg-query-stream.js +3 -0
- package/dictionary/pg-types.js +7 -0
- package/dictionary/pg.js +7 -0
- package/dictionary/pg.js.js +7 -0
- package/dictionary/pgpass.js +7 -0
- package/dictionary/phantom.js +19 -0
- package/dictionary/phantomjs-prebuilt.js +16 -0
- package/dictionary/pkginfo.js +3 -0
- package/dictionary/pm2.js +7 -0
- package/dictionary/pmx.js +3 -0
- package/dictionary/pouchdb.js +3 -0
- package/dictionary/primus-emitter.js +3 -0
- package/dictionary/primus-spark-latency.js +3 -0
- package/dictionary/primus.js +3 -0
- package/dictionary/publicsuffixlist.js +15 -0
- package/dictionary/pug.js +3 -0
- package/dictionary/punt.js +3 -0
- package/dictionary/puppeteer.js +13 -0
- package/dictionary/pwd.js +3 -0
- package/dictionary/q.js +3 -0
- package/dictionary/raven.js +3 -0
- package/dictionary/rc.js +12 -0
- package/dictionary/readable-stream.js +3 -0
- package/dictionary/rechoir.js +3 -0
- package/dictionary/redis-parser.js +3 -0
- package/dictionary/redis.js +3 -0
- package/dictionary/regression.js +3 -0
- package/dictionary/reload.js +7 -0
- package/dictionary/request.js +3 -0
- package/dictionary/require-uncached.js +3 -0
- package/dictionary/require_optional.js +3 -0
- package/dictionary/s3.js +3 -0
- package/dictionary/safe_datejs.js +3 -0
- package/dictionary/sails.js +33 -0
- package/dictionary/sax.js +3 -0
- package/dictionary/scrypt.js +3 -0
- package/dictionary/semver.js +3 -0
- package/dictionary/sequelize.js +7 -0
- package/dictionary/serialport.js +3 -0
- package/dictionary/sha3.js +3 -0
- package/dictionary/sharp.js +11 -0
- package/dictionary/shelljs.js +7 -0
- package/dictionary/sinon.js +3 -0
- package/dictionary/socket.io-client.js +13 -0
- package/dictionary/socket.io.js +12 -0
- package/dictionary/sqip.js +7 -0
- package/dictionary/sqlite3.js +7 -0
- package/dictionary/steam-crypto.js +7 -0
- package/dictionary/steam-resources.js +19 -0
- package/dictionary/steam.js +3 -0
- package/dictionary/stripe-webhook-middleware.js +3 -0
- package/dictionary/stripe.js +3 -0
- package/dictionary/strong-globalize.js +3 -0
- package/dictionary/stylus.js +14 -0
- package/dictionary/supervisor.js +3 -0
- package/dictionary/svgo.js +8 -0
- package/dictionary/tabtab.js +3 -0
- package/dictionary/tesseract.js.js +7 -0
- package/dictionary/thread-stream.js +7 -0
- package/dictionary/throng.js +3 -0
- package/dictionary/time.js +3 -0
- package/dictionary/tinify.js +7 -0
- package/dictionary/tiny-worker.js +7 -0
- package/dictionary/tmp.js +3 -0
- package/dictionary/transformers.js +3 -0
- package/dictionary/uglify-js.js +9 -0
- package/dictionary/umd.js +19 -0
- package/dictionary/underscore.js +3 -0
- package/dictionary/union.js +3 -0
- package/dictionary/update-notifier.js +3 -0
- package/dictionary/usage.js +7 -0
- package/dictionary/v8flags.js +13 -0
- package/dictionary/verror.js +3 -0
- package/dictionary/voc.js +3 -0
- package/dictionary/webdriverio.js +7 -0
- package/dictionary/winston-uber.js +7 -0
- package/dictionary/winston.js +7 -0
- package/dictionary/ws.js +3 -0
- package/dictionary/xlsx.js +16 -0
- package/dictionary/xml2js.js +3 -0
- package/dictionary/yargs.js +3 -0
- package/dictionary/zeromq.js +13 -0
- package/lib-es5/addon-extract.js +164 -0
- package/lib-es5/analyzer.js +687 -0
- package/lib-es5/app-bundle.js +133 -0
- package/lib-es5/appdir.js +265 -0
- package/lib-es5/bin.js +304 -0
- package/lib-es5/bootstrap.js +179 -0
- package/lib-es5/bundler.js +496 -0
- package/lib-es5/chmod.js +15 -0
- package/lib-es5/colors.js +12 -0
- package/lib-es5/commands.js +376 -0
- package/lib-es5/common.js +328 -0
- package/lib-es5/compress_type.js +10 -0
- package/lib-es5/config.js +291 -0
- package/lib-es5/detector.js +421 -0
- package/lib-es5/esm-hooks.js +366 -0
- package/lib-es5/esm-resolver.js +292 -0
- package/lib-es5/esm-transformer.js +378 -0
- package/lib-es5/exports-resolver.js +184 -0
- package/lib-es5/external-artifacts.js +170 -0
- package/lib-es5/fabricator.js +137 -0
- package/lib-es5/follow.js +190 -0
- package/lib-es5/help.js +52 -0
- package/lib-es5/index.d.ts +2 -0
- package/lib-es5/index.js +552 -0
- package/lib-es5/log.js +7 -0
- package/lib-es5/mach-o.js +262 -0
- package/lib-es5/manifest.js +21 -0
- package/lib-es5/options.js +19 -0
- package/lib-es5/packager.js +1036 -0
- package/lib-es5/packer.js +137 -0
- package/lib-es5/pe-metadata.js +130 -0
- package/lib-es5/producer.js +378 -0
- package/lib-es5/refiner.js +87 -0
- package/lib-es5/resolver.js +142 -0
- package/lib-es5/runtime-diagnostics.js +172 -0
- package/lib-es5/sea.js +307 -0
- package/lib-es5/snapshot-fs-patch.js +396 -0
- package/lib-es5/snapshot-fs.js +255 -0
- package/lib-es5/snapshot-index.js +135 -0
- package/lib-es5/snapshot-path.js +168 -0
- package/lib-es5/types.js +9 -0
- package/lib-es5/walker.js +1019 -0
- package/lib-es5/windows-sign.js +139 -0
- package/package.json +99 -0
- package/prelude/bootstrap.js +2263 -0
- package/prelude/diagnostic.js +147 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.transformESMtoCJS = exports.rewriteMjsRequirePaths = void 0;
|
|
30
|
+
const babel = __importStar(require("@babel/parser"));
|
|
31
|
+
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
32
|
+
const esbuild = __importStar(require("esbuild"));
|
|
33
|
+
const log_1 = require("./log");
|
|
34
|
+
const common_1 = require("./common");
|
|
35
|
+
/**
|
|
36
|
+
* Wrapper for top-level await support
|
|
37
|
+
* Wraps code in an async IIFE to allow top-level await in CommonJS
|
|
38
|
+
*/
|
|
39
|
+
const ASYNC_IIFE_WRAPPER = {
|
|
40
|
+
prefix: '(async () => {\n',
|
|
41
|
+
suffix: '\n})()',
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Check if code contains import.meta usage
|
|
45
|
+
*
|
|
46
|
+
* @param code - The ESM source code to check
|
|
47
|
+
* @returns true if import.meta is used, false otherwise
|
|
48
|
+
*/
|
|
49
|
+
function hasImportMeta(code) {
|
|
50
|
+
try {
|
|
51
|
+
const ast = babel.parse(code, {
|
|
52
|
+
sourceType: 'module',
|
|
53
|
+
plugins: [],
|
|
54
|
+
});
|
|
55
|
+
if (!ast) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
let found = false;
|
|
59
|
+
(0, traverse_1.default)(ast, {
|
|
60
|
+
// Detect import.meta usage
|
|
61
|
+
MetaProperty(path) {
|
|
62
|
+
if (path.node.meta.name === 'import' &&
|
|
63
|
+
path.node.property.name === 'meta') {
|
|
64
|
+
found = true;
|
|
65
|
+
path.stop(); // Stop traversal once found
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
return found;
|
|
70
|
+
}
|
|
71
|
+
catch (_error) {
|
|
72
|
+
// If we can't parse, assume no import.meta
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Detect ESM features that require special handling or cannot be transformed
|
|
78
|
+
* These include:
|
|
79
|
+
* - Top-level await (can be handled with async IIFE wrapper)
|
|
80
|
+
*
|
|
81
|
+
* Note: import.meta is now supported via polyfills and is no longer in the unsupported list
|
|
82
|
+
*
|
|
83
|
+
* @param code - The ESM source code to check
|
|
84
|
+
* @param filename - The filename for error reporting
|
|
85
|
+
* @returns Object with arrays of features requiring special handling
|
|
86
|
+
*/
|
|
87
|
+
function detectESMFeatures(code, filename) {
|
|
88
|
+
try {
|
|
89
|
+
const ast = babel.parse(code, {
|
|
90
|
+
sourceType: 'module',
|
|
91
|
+
plugins: [],
|
|
92
|
+
});
|
|
93
|
+
if (!ast) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const topLevelAwait = [];
|
|
97
|
+
const unsupportedFeatures = [];
|
|
98
|
+
(0, traverse_1.default)(ast, {
|
|
99
|
+
// Detect top-level await - can be handled with async IIFE wrapper
|
|
100
|
+
AwaitExpression(path) {
|
|
101
|
+
// Check if await is at top level (not inside a function)
|
|
102
|
+
let parent = path.parentPath;
|
|
103
|
+
let isTopLevel = true;
|
|
104
|
+
while (parent) {
|
|
105
|
+
if (parent.isFunctionDeclaration() ||
|
|
106
|
+
parent.isFunctionExpression() ||
|
|
107
|
+
parent.isArrowFunctionExpression() ||
|
|
108
|
+
parent.isObjectMethod() ||
|
|
109
|
+
parent.isClassMethod()) {
|
|
110
|
+
isTopLevel = false;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
parent = parent.parentPath;
|
|
114
|
+
}
|
|
115
|
+
if (isTopLevel) {
|
|
116
|
+
topLevelAwait.push({
|
|
117
|
+
feature: 'top-level await',
|
|
118
|
+
line: path.node.loc?.start.line ?? null,
|
|
119
|
+
column: path.node.loc?.start.column ?? null,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
// Detect for-await-of at top level - can be handled with async IIFE wrapper
|
|
124
|
+
ForOfStatement(path) {
|
|
125
|
+
if (path.node.await) {
|
|
126
|
+
let parent = path.parentPath;
|
|
127
|
+
let isTopLevel = true;
|
|
128
|
+
while (parent) {
|
|
129
|
+
if (parent.isFunctionDeclaration() ||
|
|
130
|
+
parent.isFunctionExpression() ||
|
|
131
|
+
parent.isArrowFunctionExpression() ||
|
|
132
|
+
parent.isObjectMethod() ||
|
|
133
|
+
parent.isClassMethod()) {
|
|
134
|
+
isTopLevel = false;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
parent = parent.parentPath;
|
|
138
|
+
}
|
|
139
|
+
if (isTopLevel) {
|
|
140
|
+
topLevelAwait.push({
|
|
141
|
+
feature: 'top-level for-await-of',
|
|
142
|
+
line: path.node.loc?.start.line ?? null,
|
|
143
|
+
column: path.node.loc?.start.column ?? null,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
return { topLevelAwait, unsupportedFeatures };
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
// If we can't parse, return null to let the transform attempt proceed
|
|
153
|
+
log_1.log.debug(`Could not parse ${filename} to detect ESM features: ${error instanceof Error ? error.message : String(error)}`);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Replace esbuild's empty import_meta object with a proper implementation
|
|
159
|
+
*
|
|
160
|
+
* When esbuild transforms ESM to CJS, it converts `import.meta` to a `const import_meta = {}`.
|
|
161
|
+
* This function replaces that empty object with a proper implementation of import.meta properties.
|
|
162
|
+
*
|
|
163
|
+
* Shims provided:
|
|
164
|
+
* - import.meta.url: File URL of the current module
|
|
165
|
+
* - import.meta.dirname: Directory path of the current module (Node.js 20.11+)
|
|
166
|
+
* - import.meta.filename: File path of the current module (Node.js 20.11+)
|
|
167
|
+
*
|
|
168
|
+
* Based on approach from tsup and esbuild discussions
|
|
169
|
+
* @see https://github.com/egoist/tsup/blob/main/assets/cjs_shims.js
|
|
170
|
+
* @see https://github.com/evanw/esbuild/issues/3839
|
|
171
|
+
*
|
|
172
|
+
* @param code - The transformed CJS code from esbuild
|
|
173
|
+
* @returns Code with import_meta properly implemented
|
|
174
|
+
*/
|
|
175
|
+
function replaceImportMetaObject(code) {
|
|
176
|
+
// esbuild generates: const import_meta = {};
|
|
177
|
+
// We need to replace this with a proper implementation
|
|
178
|
+
// Note: We use getters to ensure values are computed at runtime in the correct context
|
|
179
|
+
const shimImplementation = `const import_meta = {
|
|
180
|
+
get url() {
|
|
181
|
+
return require('url').pathToFileURL(__filename).href;
|
|
182
|
+
},
|
|
183
|
+
get dirname() {
|
|
184
|
+
return __dirname;
|
|
185
|
+
},
|
|
186
|
+
get filename() {
|
|
187
|
+
return __filename;
|
|
188
|
+
}
|
|
189
|
+
};`;
|
|
190
|
+
// Replace esbuild's empty import_meta object with our implementation
|
|
191
|
+
// Match: const import_meta = {};
|
|
192
|
+
return code.replace(/const import_meta\s*=\s*\{\s*\};/, shimImplementation);
|
|
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;
|
|
209
|
+
/**
|
|
210
|
+
* Transform ESM code to CommonJS using esbuild
|
|
211
|
+
* This allows ESM modules to be compiled to bytecode via vm.Script
|
|
212
|
+
* Uses Babel parser for detecting unsupported ESM features, then esbuild for fast transformation
|
|
213
|
+
*
|
|
214
|
+
* @param code - The ESM source code to transform
|
|
215
|
+
* @param filename - The filename for error reporting
|
|
216
|
+
* @returns Object with transformed code and success flag
|
|
217
|
+
*/
|
|
218
|
+
function transformESMtoCJS(code, filename) {
|
|
219
|
+
// Skip files that are unlikely to be JavaScript (e.g., .d.ts, .json, .css)
|
|
220
|
+
// to avoid Babel parse errors
|
|
221
|
+
if ((0, common_1.unlikelyJavascript)(filename)) {
|
|
222
|
+
return {
|
|
223
|
+
code,
|
|
224
|
+
isTransformed: false,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
// First, check for ESM features that need special handling
|
|
228
|
+
const esmFeatures = detectESMFeatures(code, filename);
|
|
229
|
+
// Handle truly unsupported features (import.meta)
|
|
230
|
+
if (esmFeatures &&
|
|
231
|
+
esmFeatures.unsupportedFeatures &&
|
|
232
|
+
esmFeatures.unsupportedFeatures.length > 0) {
|
|
233
|
+
const featureList = esmFeatures.unsupportedFeatures
|
|
234
|
+
.map((f) => {
|
|
235
|
+
const location = f.line !== null ? ` at line ${f.line}` : '';
|
|
236
|
+
return ` - ${f.feature}${location}`;
|
|
237
|
+
})
|
|
238
|
+
.join('\n');
|
|
239
|
+
const errorMessage = [
|
|
240
|
+
`Cannot transform ESM module ${filename} to CommonJS:`,
|
|
241
|
+
`The following ESM features have no CommonJS equivalent:`,
|
|
242
|
+
featureList,
|
|
243
|
+
'',
|
|
244
|
+
'These features are not supported when compiling to bytecode.',
|
|
245
|
+
'Consider one of the following:',
|
|
246
|
+
' 1. Refactor to avoid these features',
|
|
247
|
+
' 2. Use --no-bytecode flag to keep the module as source code',
|
|
248
|
+
' 3. Mark the package as public to distribute with sources',
|
|
249
|
+
].join('\n');
|
|
250
|
+
log_1.log.warn(errorMessage);
|
|
251
|
+
// Return untransformed code rather than throwing
|
|
252
|
+
// This allows the file to be included as content instead of bytecode
|
|
253
|
+
return {
|
|
254
|
+
code,
|
|
255
|
+
isTransformed: false,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
// Check if we need to wrap in async IIFE for top-level await
|
|
259
|
+
const hasTopLevelAwait = esmFeatures &&
|
|
260
|
+
esmFeatures.topLevelAwait &&
|
|
261
|
+
esmFeatures.topLevelAwait.length > 0;
|
|
262
|
+
let codeToTransform = code;
|
|
263
|
+
// If top-level await is detected, we need to wrap in async IIFE
|
|
264
|
+
// But we must handle imports and exports specially
|
|
265
|
+
if (hasTopLevelAwait) {
|
|
266
|
+
try {
|
|
267
|
+
// Parse the code to check for exports and collect imports
|
|
268
|
+
const ast = babel.parse(code, {
|
|
269
|
+
sourceType: 'module',
|
|
270
|
+
plugins: [],
|
|
271
|
+
});
|
|
272
|
+
let hasExports = false;
|
|
273
|
+
const codeLines = code.split('\n');
|
|
274
|
+
const importLineIndices = new Set();
|
|
275
|
+
(0, traverse_1.default)(ast, {
|
|
276
|
+
ExportNamedDeclaration() {
|
|
277
|
+
hasExports = true;
|
|
278
|
+
},
|
|
279
|
+
ExportDefaultDeclaration() {
|
|
280
|
+
hasExports = true;
|
|
281
|
+
},
|
|
282
|
+
ExportAllDeclaration() {
|
|
283
|
+
hasExports = true;
|
|
284
|
+
},
|
|
285
|
+
ImportDeclaration(path) {
|
|
286
|
+
// Track import statements by line number
|
|
287
|
+
const { loc } = path.node;
|
|
288
|
+
if (loc) {
|
|
289
|
+
const { start, end } = loc;
|
|
290
|
+
for (let i = start.line; i <= end.line; i += 1) {
|
|
291
|
+
importLineIndices.add(i - 1); // Convert to 0-based index
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
if (hasExports) {
|
|
297
|
+
// If the file has exports, we can't wrap it in an IIFE
|
|
298
|
+
// because exports need to be synchronous and at the top level.
|
|
299
|
+
log_1.log.warn(`Module ${filename} has both top-level await and export statements. ` +
|
|
300
|
+
`This combination cannot be safely transformed to CommonJS in pkg's ESM transformer. ` +
|
|
301
|
+
`The original source code will be used as-is; depending on the package visibility and build configuration, ` +
|
|
302
|
+
`bytecode compilation may fail and the module may need to be loaded from source or be skipped.`);
|
|
303
|
+
return {
|
|
304
|
+
code,
|
|
305
|
+
isTransformed: false,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
// If there are imports, extract them to keep outside the async IIFE
|
|
309
|
+
if (importLineIndices.size > 0) {
|
|
310
|
+
const imports = [];
|
|
311
|
+
const rest = [];
|
|
312
|
+
codeLines.forEach((line, index) => {
|
|
313
|
+
if (importLineIndices.has(index)) {
|
|
314
|
+
imports.push(line);
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
rest.push(line);
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
// Reconstruct: imports at top, then async IIFE wrapping the rest
|
|
321
|
+
codeToTransform = `${imports.join('\n')}\n${ASYNC_IIFE_WRAPPER.prefix}${rest.join('\n')}${ASYNC_IIFE_WRAPPER.suffix}`;
|
|
322
|
+
log_1.log.debug(`Wrapping ${filename} in async IIFE with imports extracted to top level`);
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
// No imports, wrap everything
|
|
326
|
+
codeToTransform =
|
|
327
|
+
ASYNC_IIFE_WRAPPER.prefix + code + ASYNC_IIFE_WRAPPER.suffix;
|
|
328
|
+
log_1.log.debug(`Wrapping ${filename} in async IIFE to support top-level await`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
catch (parseError) {
|
|
332
|
+
// If we can't parse, wrap everything and hope for the best
|
|
333
|
+
codeToTransform =
|
|
334
|
+
ASYNC_IIFE_WRAPPER.prefix + code + ASYNC_IIFE_WRAPPER.suffix;
|
|
335
|
+
log_1.log.warn(`Could not parse ${filename} to detect exports/imports (${parseError instanceof Error ? parseError.message : String(parseError)}). ` +
|
|
336
|
+
`Wrapping entire code in async IIFE - this may fail if the module has export or import statements.`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
// Check if code uses import.meta before transformation
|
|
340
|
+
const usesImportMeta = hasImportMeta(code);
|
|
341
|
+
try {
|
|
342
|
+
// Build esbuild options
|
|
343
|
+
const esbuildOptions = {
|
|
344
|
+
loader: 'js',
|
|
345
|
+
format: 'cjs',
|
|
346
|
+
target: 'node20',
|
|
347
|
+
sourcemap: false,
|
|
348
|
+
minify: false,
|
|
349
|
+
keepNames: true,
|
|
350
|
+
};
|
|
351
|
+
const result = esbuild.transformSync(codeToTransform, esbuildOptions);
|
|
352
|
+
if (!result || !result.code) {
|
|
353
|
+
log_1.log.warn(`esbuild transform returned no code for ${filename}`);
|
|
354
|
+
return {
|
|
355
|
+
code,
|
|
356
|
+
isTransformed: false,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
// Inject import.meta shims after esbuild transformation if needed
|
|
360
|
+
let finalCode = result.code;
|
|
361
|
+
if (usesImportMeta) {
|
|
362
|
+
finalCode = replaceImportMetaObject(finalCode);
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
code: finalCode,
|
|
366
|
+
isTransformed: true,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
log_1.log.warn(`Failed to transform ESM to CJS for ${filename}: ${error instanceof Error ? error.message : String(error)}`);
|
|
371
|
+
return {
|
|
372
|
+
code,
|
|
373
|
+
isTransformed: false,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
exports.transformESMtoCJS = transformESMtoCJS;
|
|
378
|
+
//# sourceMappingURL=esm-transformer.js.map
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Node-compatible package.json exports/imports map resolution.
|
|
4
|
+
*
|
|
5
|
+
* Implements the PACKAGE_EXPORTS_RESOLVE and PACKAGE_IMPORTS_RESOLVE
|
|
6
|
+
* algorithms from the Node.js ESM specification, adapted for snapshot
|
|
7
|
+
* resolution.
|
|
8
|
+
*
|
|
9
|
+
* Reference: https://nodejs.org/api/esm.html#resolver-algorithm-specification
|
|
10
|
+
*
|
|
11
|
+
* Supported:
|
|
12
|
+
* - String exports: "exports": "./lib/index.js"
|
|
13
|
+
* - Object exports with "." key: "exports": { ".": "./lib/index.js" }
|
|
14
|
+
* - Subpath exports: "exports": { "./utils": "./lib/utils.js" }
|
|
15
|
+
* - Conditional exports: { "import": "...", "require": "...", "default": "..." }
|
|
16
|
+
* - Nested conditions: { ".": { "import": "...", "require": "..." } }
|
|
17
|
+
* - Pattern exports: "exports": { "./*": "./lib/*.js" }
|
|
18
|
+
* - Package imports (#-prefixed): "imports": { "#utils": "./src/utils.js" }
|
|
19
|
+
*
|
|
20
|
+
* Not yet supported:
|
|
21
|
+
* - Array fallback targets: ["./a.js", "./b.js"]
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.resolveImports = exports.resolveExports = exports.CJS_CONDITIONS = exports.ESM_CONDITIONS = void 0;
|
|
25
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
26
|
+
// Conditions
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
28
|
+
/**
|
|
29
|
+
* Default condition set for ESM resolution.
|
|
30
|
+
* Order matters — first match wins.
|
|
31
|
+
*/
|
|
32
|
+
exports.ESM_CONDITIONS = ['import', 'node', 'default'];
|
|
33
|
+
/**
|
|
34
|
+
* Default condition set for CJS resolution.
|
|
35
|
+
*/
|
|
36
|
+
exports.CJS_CONDITIONS = ['require', 'node', 'default'];
|
|
37
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
38
|
+
// Exports resolution
|
|
39
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
40
|
+
/**
|
|
41
|
+
* Resolve a subpath against a package's exports map.
|
|
42
|
+
*
|
|
43
|
+
* @param exports - The raw "exports" value from package.json
|
|
44
|
+
* @param subpath - The subpath to resolve (e.g., "." or "./utils")
|
|
45
|
+
* @param conditions - Condition names to match (e.g., ["import", "node", "default"])
|
|
46
|
+
* @returns Resolved relative path (e.g., "./lib/index.js") or null
|
|
47
|
+
*/
|
|
48
|
+
function resolveExports(exports, subpath, conditions) {
|
|
49
|
+
if (exports === null || exports === undefined)
|
|
50
|
+
return null;
|
|
51
|
+
// Simple string: "exports": "./lib/index.js"
|
|
52
|
+
// Only matches subpath "."
|
|
53
|
+
if (typeof exports === 'string') {
|
|
54
|
+
return subpath === '.' ? exports : null;
|
|
55
|
+
}
|
|
56
|
+
// Array fallback — not yet supported
|
|
57
|
+
if (Array.isArray(exports)) {
|
|
58
|
+
// Try each in order, return first valid
|
|
59
|
+
for (const target of exports) {
|
|
60
|
+
if (typeof target === 'string')
|
|
61
|
+
return subpath === '.' ? target : null;
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (typeof exports !== 'object')
|
|
66
|
+
return null;
|
|
67
|
+
const map = exports;
|
|
68
|
+
const keys = Object.keys(map);
|
|
69
|
+
// Check if this is a conditions object or a subpath map.
|
|
70
|
+
// A conditions object has keys that don't start with "."
|
|
71
|
+
// A subpath map has keys that start with "."
|
|
72
|
+
const isConditionsObject = keys.length > 0 && !keys[0].startsWith('.');
|
|
73
|
+
if (isConditionsObject) {
|
|
74
|
+
// This is a conditions object at the top level.
|
|
75
|
+
// Only valid for subpath "."
|
|
76
|
+
if (subpath !== '.')
|
|
77
|
+
return null;
|
|
78
|
+
return resolveConditions(map, conditions);
|
|
79
|
+
}
|
|
80
|
+
// Subpath map: { ".": ..., "./utils": ..., "./*": ... }
|
|
81
|
+
// Look for exact match first
|
|
82
|
+
if (subpath in map) {
|
|
83
|
+
return resolveTarget(map[subpath], conditions);
|
|
84
|
+
}
|
|
85
|
+
// Pattern matching: "./*" or "./lib/*"
|
|
86
|
+
for (const key of keys) {
|
|
87
|
+
if (!key.includes('*'))
|
|
88
|
+
continue;
|
|
89
|
+
const pattern = key;
|
|
90
|
+
const starIndex = pattern.indexOf('*');
|
|
91
|
+
const prefix = pattern.slice(0, starIndex);
|
|
92
|
+
const suffix = pattern.slice(starIndex + 1);
|
|
93
|
+
if (subpath.startsWith(prefix) && (suffix === '' || subpath.endsWith(suffix))) {
|
|
94
|
+
const matched = suffix
|
|
95
|
+
? subpath.slice(prefix.length, subpath.length - suffix.length)
|
|
96
|
+
: subpath.slice(prefix.length);
|
|
97
|
+
const target = resolveTarget(map[key], conditions);
|
|
98
|
+
if (target && typeof target === 'string') {
|
|
99
|
+
return target.replace('*', matched);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
exports.resolveExports = resolveExports;
|
|
106
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
107
|
+
// Imports resolution
|
|
108
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
109
|
+
/**
|
|
110
|
+
* Resolve a #-prefixed specifier against a package's imports map.
|
|
111
|
+
*
|
|
112
|
+
* @param imports - The raw "imports" value from package.json
|
|
113
|
+
* @param specifier - The full #-prefixed specifier (e.g., "#utils")
|
|
114
|
+
* @param conditions - Condition names to match
|
|
115
|
+
* @returns Resolved relative path or null
|
|
116
|
+
*/
|
|
117
|
+
function resolveImports(imports, specifier, conditions) {
|
|
118
|
+
if (!imports || typeof imports !== 'object')
|
|
119
|
+
return null;
|
|
120
|
+
if (!specifier.startsWith('#'))
|
|
121
|
+
return null;
|
|
122
|
+
const map = imports;
|
|
123
|
+
// Exact match
|
|
124
|
+
if (specifier in map) {
|
|
125
|
+
return resolveTarget(map[specifier], conditions);
|
|
126
|
+
}
|
|
127
|
+
// Pattern match: "#utils/*"
|
|
128
|
+
for (const key of Object.keys(map)) {
|
|
129
|
+
if (!key.includes('*'))
|
|
130
|
+
continue;
|
|
131
|
+
const starIndex = key.indexOf('*');
|
|
132
|
+
const prefix = key.slice(0, starIndex);
|
|
133
|
+
const suffix = key.slice(starIndex + 1);
|
|
134
|
+
if (specifier.startsWith(prefix) && (suffix === '' || specifier.endsWith(suffix))) {
|
|
135
|
+
const matched = suffix
|
|
136
|
+
? specifier.slice(prefix.length, specifier.length - suffix.length)
|
|
137
|
+
: specifier.slice(prefix.length);
|
|
138
|
+
const target = resolveTarget(map[key], conditions);
|
|
139
|
+
if (target && typeof target === 'string') {
|
|
140
|
+
return target.replace('*', matched);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
exports.resolveImports = resolveImports;
|
|
147
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
148
|
+
// Internal: condition + target resolution
|
|
149
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
150
|
+
/**
|
|
151
|
+
* Resolve a target value which may be a string, conditions object, or null.
|
|
152
|
+
*/
|
|
153
|
+
function resolveTarget(target, conditions) {
|
|
154
|
+
if (typeof target === 'string')
|
|
155
|
+
return target;
|
|
156
|
+
if (target === null)
|
|
157
|
+
return null;
|
|
158
|
+
if (Array.isArray(target)) {
|
|
159
|
+
for (const t of target) {
|
|
160
|
+
const result = resolveTarget(t, conditions);
|
|
161
|
+
if (result)
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
if (typeof target === 'object') {
|
|
167
|
+
return resolveConditions(target, conditions);
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Match a conditions object against the active condition set.
|
|
173
|
+
* First matching condition wins (order of the conditions array matters).
|
|
174
|
+
*/
|
|
175
|
+
function resolveConditions(obj, conditions) {
|
|
176
|
+
// Check each condition in priority order
|
|
177
|
+
for (const cond of conditions) {
|
|
178
|
+
if (cond in obj) {
|
|
179
|
+
return resolveTarget(obj[cond], conditions);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=exports-resolver.js.map
|