@knighted/module 1.0.0 → 1.1.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/README.md +4 -6
- package/dist/ast.d.ts +39 -0
- package/dist/cjs/ast.d.cts +39 -0
- package/dist/cjs/exports.d.cts +4 -1
- package/dist/cjs/format.cjs +70 -30
- package/dist/cjs/formatters/assignmentExpression.cjs +1 -1
- package/dist/cjs/formatters/identifier.cjs +2 -2
- package/dist/cjs/formatters/memberExpression.cjs +1 -1
- package/dist/cjs/helpers/ast.cjs +29 -0
- package/dist/cjs/helpers/identifier.cjs +5 -3
- package/dist/cjs/module.cjs +5 -4
- package/dist/cjs/specifier.cjs +1 -1
- package/dist/cjs/types.d.cts +2 -0
- package/dist/cjs/utils/exports.cjs +1 -1
- package/dist/cjs/utils/identifiers.cjs +2 -2
- package/dist/exports.d.ts +4 -1
- package/dist/format.js +70 -30
- package/dist/formatters/assignmentExpression.js +1 -1
- package/dist/formatters/identifier.js +2 -2
- package/dist/formatters/memberExpression.js +1 -1
- package/dist/helpers/ast.d.ts +39 -0
- package/dist/helpers/ast.js +18 -0
- package/dist/helpers/identifier.js +5 -4
- package/dist/module.js +6 -5
- package/dist/specifier.js +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/{src/utils → utils}/exports.d.ts +4 -1
- package/dist/utils/exports.js +1 -1
- package/dist/utils/identifiers.js +2 -2
- package/package.json +9 -11
- package/dist/cjs/utils.cjs +0 -274
- package/dist/cjs/utils.d.cts +0 -23
- package/dist/src/format.d.ts +0 -9
- package/dist/src/module.d.ts +0 -3
- package/dist/src/parse.d.ts +0 -2
- package/dist/src/specifier.d.ts +0 -16
- package/dist/src/types.d.ts +0 -91
- package/dist/src/utils.d.ts +0 -23
- package/dist/src/walk.d.ts +0 -20
- package/dist/utils.d.ts +0 -23
- package/dist/utils.js +0 -265
- /package/dist/{src/formatters → formatters}/assignmentExpression.d.ts +0 -0
- /package/dist/{src/formatters → formatters}/expressionStatement.d.ts +0 -0
- /package/dist/{src/formatters → formatters}/identifier.d.ts +0 -0
- /package/dist/{src/formatters → formatters}/memberExpression.d.ts +0 -0
- /package/dist/{src/formatters → formatters}/metaProperty.d.ts +0 -0
- /package/dist/{src/helpers → helpers}/identifier.d.ts +0 -0
- /package/dist/{src/utils → utils}/identifiers.d.ts +0 -0
- /package/dist/{src/utils → utils}/lang.d.ts +0 -0
- /package/dist/{src/utils → utils}/scopeNodes.d.ts +0 -0
- /package/dist/{src/utils → utils}/url.d.ts +0 -0
package/README.md
CHANGED
|
@@ -130,7 +130,9 @@ type ModuleOptions = {
|
|
|
130
130
|
requireMainStrategy?: 'import-meta-main' | 'realpath'
|
|
131
131
|
detectCircularRequires?: 'off' | 'warn' | 'error'
|
|
132
132
|
requireSource?: 'builtin' | 'create-require'
|
|
133
|
+
importMetaPrelude?: 'off' | 'auto' | 'on'
|
|
133
134
|
cjsDefault?: 'module-exports' | 'auto' | 'none'
|
|
135
|
+
idiomaticExports?: 'off' | 'safe' | 'aggressive'
|
|
134
136
|
topLevelAwait?: 'error' | 'wrap' | 'preserve'
|
|
135
137
|
out?: string
|
|
136
138
|
inPlace?: boolean
|
|
@@ -149,11 +151,13 @@ type ModuleOptions = {
|
|
|
149
151
|
- `importMeta` (`shim`): rewrite `import.meta.*` to CommonJS equivalents.
|
|
150
152
|
- `importMetaMain` (`shim`): gate `import.meta.main` with shimming/warning/error when Node support is too old.
|
|
151
153
|
- `requireMainStrategy` (`import-meta-main`): use `import.meta.main` or the realpath-based `pathToFileURL(realpathSync(process.argv[1])).href` check.
|
|
154
|
+
- `importMetaPrelude` (`auto`): emit a no-op `void import.meta.filename;` touch. `on` always emits; `off` never emits; `auto` emits only when helpers that reference `import.meta.*` are synthesized (e.g., `__dirname`/`__filename` in CJS→ESM, require-main shims, createRequire helpers). Useful for bundlers/transpilers that do usage-based `import.meta` polyfilling.
|
|
152
155
|
- `detectCircularRequires` (`off`): optionally detect relative static require cycles and warn/throw.
|
|
153
156
|
- `topLevelAwait` (`error`): throw, wrap, or preserve when TLA appears in CommonJS output.
|
|
154
157
|
- `rewriteSpecifier` (off): rewrite relative specifiers to a chosen extension or via a callback. Precedence: the callback (if provided) runs first; if it returns a string, that wins. If it returns `undefined` or `null`, the appenders still apply.
|
|
155
158
|
- `requireSource` (`builtin`): whether `require` comes from Node or `createRequire`.
|
|
156
159
|
- `cjsDefault` (`auto`): bundler-style default interop vs direct `module.exports`.
|
|
160
|
+
- `idiomaticExports` (`safe`): when raising CJS to ESM, attempt to synthesize `export` statements directly when it is safe. `off` always uses the helper bag; `aggressive` currently matches `safe` heuristics.
|
|
157
161
|
- `out`/`inPlace`: write the transformed code to a file; otherwise the function returns the transformed string only.
|
|
158
162
|
- CommonJS → ESM lowering will throw on `with` statements and unshadowed `eval` calls to avoid unsound rewrites.
|
|
159
163
|
|
|
@@ -225,9 +229,3 @@ for (const file of files) {
|
|
|
225
229
|
```
|
|
226
230
|
|
|
227
231
|
This pre-`tsc` step removes the flagged globals in the compiled orientation; runtime semantics still match the target build.
|
|
228
|
-
|
|
229
|
-
## Roadmap
|
|
230
|
-
|
|
231
|
-
- Emit source maps and clearer diagnostics for transform choices.
|
|
232
|
-
- Broaden fixtures covering live-binding and top-level await edge cases across Node versions.
|
|
233
|
-
- Benchmark scope analysis choices: compare `periscopic`, `scope-analyzer`, and `eslint-scope` on fixtures and pick the final adapter.
|
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { CjsExport } from '../types.js';
|
|
3
|
+
export type IdentifierNode = Extract<Node, {
|
|
4
|
+
type: 'Identifier';
|
|
5
|
+
}>;
|
|
6
|
+
export type LiteralNode = Extract<Node, {
|
|
7
|
+
type: 'Literal';
|
|
8
|
+
value?: unknown;
|
|
9
|
+
}>;
|
|
10
|
+
export type MemberExpressionNode = Extract<Node, {
|
|
11
|
+
type: 'MemberExpression';
|
|
12
|
+
}>;
|
|
13
|
+
export type CallExpressionNode = Extract<Node, {
|
|
14
|
+
type: 'CallExpression';
|
|
15
|
+
}>;
|
|
16
|
+
export type ProgramNode = Extract<Node, {
|
|
17
|
+
type: 'Program';
|
|
18
|
+
body: Node[];
|
|
19
|
+
}>;
|
|
20
|
+
export type ModuleExportNameNode = Extract<Node, {
|
|
21
|
+
type: 'Identifier' | 'Literal';
|
|
22
|
+
}>;
|
|
23
|
+
export type ImportDefaultSpecifierNode = Extract<Node, {
|
|
24
|
+
type: 'ImportDefaultSpecifier';
|
|
25
|
+
}>;
|
|
26
|
+
export type ImportNamespaceSpecifierNode = Extract<Node, {
|
|
27
|
+
type: 'ImportNamespaceSpecifier';
|
|
28
|
+
}>;
|
|
29
|
+
export type ImportSpecifierNode = Extract<Node, {
|
|
30
|
+
type: 'ImportSpecifier';
|
|
31
|
+
}>;
|
|
32
|
+
export type ExportsMap = Map<string, CjsExport> & {
|
|
33
|
+
hasUnsupportedExportWrite?: boolean;
|
|
34
|
+
};
|
|
35
|
+
export declare const isAstNode: (value: unknown) => value is Node;
|
|
36
|
+
export declare const isIdentifierNode: (node: Node | null | undefined) => node is IdentifierNode;
|
|
37
|
+
export declare const isMemberExpressionNode: (node: Node | null | undefined) => node is MemberExpressionNode;
|
|
38
|
+
export declare const isCallExpressionNode: (node: Node | null | undefined) => node is CallExpressionNode;
|
|
39
|
+
export declare const getModuleExportName: (name: ModuleExportNameNode | null | undefined) => string | null;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { CjsExport } from '../types.cjs';
|
|
3
|
+
export type IdentifierNode = Extract<Node, {
|
|
4
|
+
type: 'Identifier';
|
|
5
|
+
}>;
|
|
6
|
+
export type LiteralNode = Extract<Node, {
|
|
7
|
+
type: 'Literal';
|
|
8
|
+
value?: unknown;
|
|
9
|
+
}>;
|
|
10
|
+
export type MemberExpressionNode = Extract<Node, {
|
|
11
|
+
type: 'MemberExpression';
|
|
12
|
+
}>;
|
|
13
|
+
export type CallExpressionNode = Extract<Node, {
|
|
14
|
+
type: 'CallExpression';
|
|
15
|
+
}>;
|
|
16
|
+
export type ProgramNode = Extract<Node, {
|
|
17
|
+
type: 'Program';
|
|
18
|
+
body: Node[];
|
|
19
|
+
}>;
|
|
20
|
+
export type ModuleExportNameNode = Extract<Node, {
|
|
21
|
+
type: 'Identifier' | 'Literal';
|
|
22
|
+
}>;
|
|
23
|
+
export type ImportDefaultSpecifierNode = Extract<Node, {
|
|
24
|
+
type: 'ImportDefaultSpecifier';
|
|
25
|
+
}>;
|
|
26
|
+
export type ImportNamespaceSpecifierNode = Extract<Node, {
|
|
27
|
+
type: 'ImportNamespaceSpecifier';
|
|
28
|
+
}>;
|
|
29
|
+
export type ImportSpecifierNode = Extract<Node, {
|
|
30
|
+
type: 'ImportSpecifier';
|
|
31
|
+
}>;
|
|
32
|
+
export type ExportsMap = Map<string, CjsExport> & {
|
|
33
|
+
hasUnsupportedExportWrite?: boolean;
|
|
34
|
+
};
|
|
35
|
+
export declare const isAstNode: (value: unknown) => value is Node;
|
|
36
|
+
export declare const isIdentifierNode: (node: Node | null | undefined) => node is IdentifierNode;
|
|
37
|
+
export declare const isMemberExpressionNode: (node: Node | null | undefined) => node is MemberExpressionNode;
|
|
38
|
+
export declare const isCallExpressionNode: (node: Node | null | undefined) => node is CallExpressionNode;
|
|
39
|
+
export declare const getModuleExportName: (name: ModuleExportNameNode | null | undefined) => string | null;
|
package/dist/cjs/exports.d.cts
CHANGED
|
@@ -2,5 +2,8 @@ import type { Node } from 'oxc-parser';
|
|
|
2
2
|
import type { CjsExport } from '../types.cjs';
|
|
3
3
|
declare const exportsRename = "__exports";
|
|
4
4
|
declare const requireMainRgx: RegExp;
|
|
5
|
-
|
|
5
|
+
type ExportsMap = Map<string, CjsExport> & {
|
|
6
|
+
hasUnsupportedExportWrite?: boolean;
|
|
7
|
+
};
|
|
8
|
+
declare const collectCjsExports: (ast: Node) => Promise<ExportsMap>;
|
|
6
9
|
export { exportsRename, requireMainRgx, collectCjsExports };
|
package/dist/cjs/format.cjs
CHANGED
|
@@ -4,33 +4,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.format = void 0;
|
|
7
|
+
var _ast = require("./helpers/ast.cjs");
|
|
7
8
|
var _magicString = _interopRequireDefault(require("magic-string"));
|
|
8
|
-
var _identifier = require("
|
|
9
|
-
var _metaProperty = require("
|
|
10
|
-
var _memberExpression = require("
|
|
11
|
-
var _assignmentExpression = require("
|
|
12
|
-
var _url = require("
|
|
13
|
-
var _exports = require("
|
|
14
|
-
var _identifiers = require("
|
|
15
|
-
var _identifier2 = require("
|
|
16
|
-
var _walk = require("
|
|
9
|
+
var _identifier = require("./formatters/identifier.cjs");
|
|
10
|
+
var _metaProperty = require("./formatters/metaProperty.cjs");
|
|
11
|
+
var _memberExpression = require("./formatters/memberExpression.cjs");
|
|
12
|
+
var _assignmentExpression = require("./formatters/assignmentExpression.cjs");
|
|
13
|
+
var _url = require("./utils/url.cjs");
|
|
14
|
+
var _exports = require("./utils/exports.cjs");
|
|
15
|
+
var _identifiers = require("./utils/identifiers.cjs");
|
|
16
|
+
var _identifier2 = require("./helpers/identifier.cjs");
|
|
17
|
+
var _walk = require("./walk.cjs");
|
|
17
18
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
19
|
const isValidIdent = name => /^[$A-Z_a-z][$\w]*$/.test(name);
|
|
19
20
|
const expressionHasRequireCall = (node, shadowed) => {
|
|
20
21
|
let found = false;
|
|
21
22
|
const walkNode = n => {
|
|
22
|
-
if (!n || found) return;
|
|
23
|
-
if (n
|
|
23
|
+
if (!(0, _ast.isAstNode)(n) || found) return;
|
|
24
|
+
if ((0, _ast.isCallExpressionNode)(n) && (0, _ast.isIdentifierNode)(n.callee) && n.callee.name === 'require' && !shadowed.has('require')) {
|
|
24
25
|
found = true;
|
|
25
26
|
return;
|
|
26
27
|
}
|
|
27
|
-
if (n
|
|
28
|
+
if ((0, _ast.isCallExpressionNode)(n) && (0, _ast.isMemberExpressionNode)(n.callee) && (0, _ast.isIdentifierNode)(n.callee.object) && n.callee.object.name === 'require' && !shadowed.has('require')) {
|
|
28
29
|
found = true;
|
|
29
30
|
return;
|
|
30
31
|
}
|
|
31
|
-
const
|
|
32
|
+
const record = n;
|
|
33
|
+
const keys = Object.keys(record);
|
|
32
34
|
for (const key of keys) {
|
|
33
|
-
const value =
|
|
35
|
+
const value = record[key];
|
|
34
36
|
if (!value) continue;
|
|
35
37
|
if (Array.isArray(value)) {
|
|
36
38
|
for (const item of value) {
|
|
@@ -87,6 +89,10 @@ const lowerCjsRequireToImports = (program, code, shadowed) => {
|
|
|
87
89
|
if (allStatic) {
|
|
88
90
|
for (const decl of decls) {
|
|
89
91
|
const init = decl.init;
|
|
92
|
+
if (!init || !(0, _ast.isCallExpressionNode)(init)) {
|
|
93
|
+
needsCreateRequire = true;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
90
96
|
const arg = init.arguments[0];
|
|
91
97
|
const source = code.slice(arg.start, arg.end);
|
|
92
98
|
const value = arg.value;
|
|
@@ -123,6 +129,10 @@ const lowerCjsRequireToImports = (program, code, shadowed) => {
|
|
|
123
129
|
if (stmt.type === 'ExpressionStatement') {
|
|
124
130
|
const expr = stmt.expression;
|
|
125
131
|
if (expr && isStaticRequire(expr, shadowed)) {
|
|
132
|
+
if (!(0, _ast.isCallExpressionNode)(expr)) {
|
|
133
|
+
needsCreateRequire = true;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
126
136
|
const arg = expr.arguments[0];
|
|
127
137
|
const source = code.slice(arg.start, arg.end);
|
|
128
138
|
const value = arg.value;
|
|
@@ -149,11 +159,12 @@ const lowerCjsRequireToImports = (program, code, shadowed) => {
|
|
|
149
159
|
needsInteropHelper
|
|
150
160
|
};
|
|
151
161
|
};
|
|
152
|
-
const isRequireMainMember = (node, shadowed) => node
|
|
162
|
+
const isRequireMainMember = (node, shadowed) => node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'require' && !shadowed.has('require') && node.property.type === 'Identifier' && node.property.name === 'main';
|
|
153
163
|
const hasTopLevelAwait = program => {
|
|
154
164
|
let found = false;
|
|
155
165
|
const walkNode = (node, inFunction) => {
|
|
156
166
|
if (found) return;
|
|
167
|
+
if (!(0, _ast.isAstNode)(node)) return;
|
|
157
168
|
switch (node.type) {
|
|
158
169
|
case 'FunctionDeclaration':
|
|
159
170
|
case 'FunctionExpression':
|
|
@@ -167,9 +178,10 @@ const hasTopLevelAwait = program => {
|
|
|
167
178
|
found = true;
|
|
168
179
|
return;
|
|
169
180
|
}
|
|
170
|
-
const
|
|
181
|
+
const record = node;
|
|
182
|
+
const keys = Object.keys(record);
|
|
171
183
|
for (const key of keys) {
|
|
172
|
-
const value =
|
|
184
|
+
const value = record[key];
|
|
173
185
|
if (!value) continue;
|
|
174
186
|
if (Array.isArray(value)) {
|
|
175
187
|
for (const item of value) {
|
|
@@ -250,7 +262,8 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
250
262
|
}
|
|
251
263
|
if (namedSpecs.length) {
|
|
252
264
|
const pairs = namedSpecs.map(s => {
|
|
253
|
-
const imported = s.imported
|
|
265
|
+
const imported = (0, _ast.getModuleExportName)(s.imported);
|
|
266
|
+
if (!imported) return s.local.name;
|
|
254
267
|
const local = s.local.name;
|
|
255
268
|
return imported === local ? imported : `${imported}: ${local}`;
|
|
256
269
|
});
|
|
@@ -275,7 +288,7 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
275
288
|
exportedNames.push(d.id.name);
|
|
276
289
|
}
|
|
277
290
|
}
|
|
278
|
-
} else if (decl.id?.type === 'Identifier') {
|
|
291
|
+
} else if ('id' in decl && decl.id?.type === 'Identifier') {
|
|
279
292
|
exportedNames.push(decl.id.name);
|
|
280
293
|
}
|
|
281
294
|
const exportLines = exportedNames.map(name => exportAssignment(name, name, live));
|
|
@@ -295,8 +308,9 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
295
308
|
const lines = [`const ${modIdent} = require(${srcLiteral});`];
|
|
296
309
|
for (const spec of node.specifiers) {
|
|
297
310
|
if (spec.type !== 'ExportSpecifier') continue;
|
|
298
|
-
const exported = spec.exported
|
|
299
|
-
const imported = spec.local
|
|
311
|
+
const exported = (0, _ast.getModuleExportName)(spec.exported);
|
|
312
|
+
const imported = (0, _ast.getModuleExportName)(spec.local);
|
|
313
|
+
if (!exported || !imported) continue;
|
|
300
314
|
let rhs = `${modIdent}.${imported}`;
|
|
301
315
|
if (imported === 'default') {
|
|
302
316
|
rhs = `${defaultInteropName}(${modIdent})`;
|
|
@@ -314,8 +328,9 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
314
328
|
const lines = [];
|
|
315
329
|
for (const spec of node.specifiers) {
|
|
316
330
|
if (spec.type !== 'ExportSpecifier') continue;
|
|
317
|
-
const exported = spec.exported
|
|
318
|
-
const local = spec.local
|
|
331
|
+
const exported = (0, _ast.getModuleExportName)(spec.exported);
|
|
332
|
+
const local = (0, _ast.getModuleExportName)(spec.local);
|
|
333
|
+
if (!exported || !local) continue;
|
|
319
334
|
lines.push(exportAssignment(exported, local, live));
|
|
320
335
|
}
|
|
321
336
|
exportTransforms.push({
|
|
@@ -359,8 +374,11 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
359
374
|
}
|
|
360
375
|
if (node.type === 'ExportAllDeclaration') {
|
|
361
376
|
const srcLiteral = code.slice(node.source.start, node.source.end);
|
|
362
|
-
if (node.exported) {
|
|
363
|
-
const exported = node.exported
|
|
377
|
+
if ('exported' in node && node.exported) {
|
|
378
|
+
const exported = (0, _ast.getModuleExportName)(node.exported);
|
|
379
|
+
if (!exported) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
364
382
|
const modIdent = `__mod${importIndex++}`;
|
|
365
383
|
const lines = [`const ${modIdent} = require(${srcLiteral});`, exportAssignment(exported, modIdent, live)];
|
|
366
384
|
exportTransforms.push({
|
|
@@ -607,6 +625,8 @@ const format = async (src, ast, opts) => {
|
|
|
607
625
|
let requireMainNeedsRealpath = false;
|
|
608
626
|
let needsRequireResolveHelper = false;
|
|
609
627
|
const nestedRequireStrategy = opts.nestedRequireStrategy ?? 'create-require';
|
|
628
|
+
const importMetaPreludeMode = opts.importMetaPrelude ?? 'auto';
|
|
629
|
+
let importMetaRef = false;
|
|
610
630
|
const shouldLowerCjs = opts.target === 'commonjs' && fullTransform;
|
|
611
631
|
const shouldRaiseEsm = opts.target === 'module' && fullTransform;
|
|
612
632
|
let hoistedImports = [];
|
|
@@ -654,6 +674,10 @@ const format = async (src, ast, opts) => {
|
|
|
654
674
|
const mainExpr = requireMainStrategy === 'import-meta-main' ? 'import.meta.main' : 'import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href';
|
|
655
675
|
if (requireMainStrategy === 'realpath') {
|
|
656
676
|
requireMainNeedsRealpath = true;
|
|
677
|
+
importMetaRef = true;
|
|
678
|
+
}
|
|
679
|
+
if (requireMainStrategy === 'import-meta-main') {
|
|
680
|
+
importMetaRef = true;
|
|
657
681
|
}
|
|
658
682
|
code.update(node.start, node.end, negate ? `!(${mainExpr})` : mainExpr);
|
|
659
683
|
return;
|
|
@@ -772,6 +796,9 @@ const format = async (src, ast, opts) => {
|
|
|
772
796
|
}
|
|
773
797
|
}
|
|
774
798
|
if ((0, _identifier2.isIdentifierName)(node)) {
|
|
799
|
+
if (shouldRaiseEsm && node.type === 'Identifier' && (node.name === '__dirname' || node.name === '__filename')) {
|
|
800
|
+
importMetaRef = true;
|
|
801
|
+
}
|
|
775
802
|
(0, _identifier.identifier)({
|
|
776
803
|
node,
|
|
777
804
|
ancestors,
|
|
@@ -827,8 +854,14 @@ const format = async (src, ast, opts) => {
|
|
|
827
854
|
const asExportName = name => isValidExportName(name) ? name : JSON.stringify(name);
|
|
828
855
|
const accessProp = name => isValidExportName(name) ? `${_exports.exportsRename}.${name}` : `${_exports.exportsRename}[${JSON.stringify(name)}]`;
|
|
829
856
|
const exportValueFor = name => {
|
|
830
|
-
if (name === '__dirname')
|
|
831
|
-
|
|
857
|
+
if (name === '__dirname') {
|
|
858
|
+
importMetaRef = true;
|
|
859
|
+
return 'import.meta.dirname';
|
|
860
|
+
}
|
|
861
|
+
if (name === '__filename') {
|
|
862
|
+
importMetaRef = true;
|
|
863
|
+
return 'import.meta.filename';
|
|
864
|
+
}
|
|
832
865
|
return name;
|
|
833
866
|
};
|
|
834
867
|
const tempNameFor = name => {
|
|
@@ -884,6 +917,9 @@ const format = async (src, ast, opts) => {
|
|
|
884
917
|
}
|
|
885
918
|
if (shouldRaiseEsm && fullTransform) {
|
|
886
919
|
const importPrelude = [];
|
|
920
|
+
if (needsCreateRequire || needsRequireResolveHelper) {
|
|
921
|
+
importMetaRef = true;
|
|
922
|
+
}
|
|
887
923
|
if (needsCreateRequire || needsRequireResolveHelper) {
|
|
888
924
|
importPrelude.push('import { createRequire } from "node:module";\n');
|
|
889
925
|
}
|
|
@@ -916,9 +952,13 @@ const format = async (src, ast, opts) => {
|
|
|
916
952
|
const exportsBagInit = useExportsBag ? `let ${_exports.exportsRename} = {};
|
|
917
953
|
` : '';
|
|
918
954
|
const modulePrelude = '';
|
|
919
|
-
const prelude = `${importPrelude.join('')}${importPrelude.length ? '\n' : ''}${setupPrelude.join('')}${setupPrelude.length ? '\n' : ''}${requireInit}${requireResolveInit}${exportsBagInit}${modulePrelude}
|
|
920
|
-
|
|
921
|
-
|
|
955
|
+
const prelude = `${importPrelude.join('')}${importPrelude.length ? '\n' : ''}${setupPrelude.join('')}${setupPrelude.length ? '\n' : ''}${requireInit}${requireResolveInit}${exportsBagInit}${modulePrelude}`;
|
|
956
|
+
const importMetaTouch = (() => {
|
|
957
|
+
if (importMetaPreludeMode === 'on') return 'void import.meta.filename;\n';
|
|
958
|
+
if (importMetaPreludeMode === 'off') return '';
|
|
959
|
+
return importMetaRef ? 'void import.meta.filename;\n' : '';
|
|
960
|
+
})();
|
|
961
|
+
code.prepend(`${prelude}${importMetaTouch}`);
|
|
922
962
|
}
|
|
923
963
|
if (opts.target === 'commonjs' && fullTransform && containsTopLevelAwait) {
|
|
924
964
|
const body = code.toString();
|
|
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.identifier = void 0;
|
|
7
|
-
var _exports = require("
|
|
8
|
-
var _identifier = require("
|
|
7
|
+
var _exports = require("../utils/exports.cjs");
|
|
8
|
+
var _identifier = require("../helpers/identifier.cjs");
|
|
9
9
|
const identifier = ({
|
|
10
10
|
node,
|
|
11
11
|
ancestors,
|
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.memberExpression = void 0;
|
|
7
|
-
var _exports = require("
|
|
7
|
+
var _exports = require("../utils/exports.cjs");
|
|
8
8
|
const memberExpression = (node, parent, src, options, shadowed, extras, useExportsBag = true, rewriteExports = true) => {
|
|
9
9
|
if (options.target === 'module') {
|
|
10
10
|
if (rewriteExports && !useExportsBag) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isMemberExpressionNode = exports.isIdentifierNode = exports.isCallExpressionNode = exports.isAstNode = exports.getModuleExportName = void 0;
|
|
7
|
+
const isAstNode = value => {
|
|
8
|
+
return Boolean(value) && typeof value === 'object' && 'type' in value;
|
|
9
|
+
};
|
|
10
|
+
exports.isAstNode = isAstNode;
|
|
11
|
+
const isIdentifierNode = node => {
|
|
12
|
+
return node?.type === 'Identifier';
|
|
13
|
+
};
|
|
14
|
+
exports.isIdentifierNode = isIdentifierNode;
|
|
15
|
+
const isMemberExpressionNode = node => {
|
|
16
|
+
return node?.type === 'MemberExpression';
|
|
17
|
+
};
|
|
18
|
+
exports.isMemberExpressionNode = isMemberExpressionNode;
|
|
19
|
+
const isCallExpressionNode = node => {
|
|
20
|
+
return node?.type === 'CallExpression';
|
|
21
|
+
};
|
|
22
|
+
exports.isCallExpressionNode = isCallExpressionNode;
|
|
23
|
+
const getModuleExportName = name => {
|
|
24
|
+
if (!name) return null;
|
|
25
|
+
if (name.type === 'Identifier') return name.name;
|
|
26
|
+
if (name.type === 'Literal' && typeof name.value === 'string') return name.value;
|
|
27
|
+
return null;
|
|
28
|
+
};
|
|
29
|
+
exports.getModuleExportName = getModuleExportName;
|
|
@@ -127,14 +127,16 @@ const identifier = exports.identifier = {
|
|
|
127
127
|
const node = ancestors[ancestors.length - 1];
|
|
128
128
|
const varBoundScopes = ['ClassDeclaration', 'ClassExpression', 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'];
|
|
129
129
|
const declaratorIndex = ancestors.findIndex(ancestor => {
|
|
130
|
-
|
|
130
|
+
if (ancestor.type !== 'VariableDeclarator') return false;
|
|
131
|
+
return ancestor === node || isInBindingPattern(ancestor.id, node);
|
|
131
132
|
});
|
|
132
133
|
if (declaratorIndex === -1) return false;
|
|
133
|
-
const
|
|
134
|
+
const declaratorNode = ancestors[declaratorIndex];
|
|
134
135
|
const declaration = ancestors[declaratorIndex - 1];
|
|
136
|
+
if (declaratorNode?.type !== 'VariableDeclarator') return false;
|
|
135
137
|
return declaration?.type === 'VariableDeclaration' && declaration.kind === 'var' && ancestors.every(ancestor => {
|
|
136
138
|
return !varBoundScopes.includes(ancestor.type);
|
|
137
|
-
}) && (
|
|
139
|
+
}) && (declaratorNode.id === node || isInBindingPattern(declaratorNode.id, node));
|
|
138
140
|
},
|
|
139
141
|
isIife(ancestors) {
|
|
140
142
|
const parent = ancestors[ancestors.length - 2];
|
package/dist/cjs/module.cjs
CHANGED
|
@@ -7,11 +7,11 @@ exports.transform = void 0;
|
|
|
7
7
|
var _nodePath = require("node:path");
|
|
8
8
|
var _promises = require("node:fs/promises");
|
|
9
9
|
var _specifier = require("./specifier.cjs");
|
|
10
|
-
var _parse = require("
|
|
11
|
-
var _format = require("
|
|
12
|
-
var _lang = require("
|
|
10
|
+
var _parse = require("./parse.cjs");
|
|
11
|
+
var _format = require("./format.cjs");
|
|
12
|
+
var _lang = require("./utils/lang.cjs");
|
|
13
13
|
var _nodeModule = require("node:module");
|
|
14
|
-
var _walk = require("
|
|
14
|
+
var _walk = require("./walk.cjs");
|
|
15
15
|
const collapseSpecifier = value => value.replace(/['"`+)\s]|new String\(/g, '');
|
|
16
16
|
const builtinSpecifiers = new Set(_nodeModule.builtinModules.map(mod => mod.startsWith('node:') ? mod.slice(5) : mod).flatMap(mod => {
|
|
17
17
|
const parts = mod.split('/');
|
|
@@ -161,6 +161,7 @@ const defaultOptions = {
|
|
|
161
161
|
nestedRequireStrategy: 'create-require',
|
|
162
162
|
cjsDefault: 'auto',
|
|
163
163
|
idiomaticExports: 'safe',
|
|
164
|
+
importMetaPrelude: 'auto',
|
|
164
165
|
topLevelAwait: 'error',
|
|
165
166
|
out: undefined,
|
|
166
167
|
inPlace: false
|
package/dist/cjs/specifier.cjs
CHANGED
|
@@ -8,7 +8,7 @@ var _nodePath = require("node:path");
|
|
|
8
8
|
var _promises = require("node:fs/promises");
|
|
9
9
|
var _magicString = _interopRequireDefault(require("magic-string"));
|
|
10
10
|
var _oxcParser = require("oxc-parser");
|
|
11
|
-
var _walk = require("
|
|
11
|
+
var _walk = require("./walk.cjs");
|
|
12
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const isStringLiteral = node => {
|
|
14
14
|
return node.type === 'Literal' && typeof node.value === 'string';
|
package/dist/cjs/types.d.cts
CHANGED
|
@@ -40,6 +40,8 @@ export type ModuleOptions = {
|
|
|
40
40
|
cjsDefault?: 'module-exports' | 'auto' | 'none';
|
|
41
41
|
/** Emit idiomatic exports when raising CJS to ESM. */
|
|
42
42
|
idiomaticExports?: 'off' | 'safe' | 'aggressive';
|
|
43
|
+
/** Control whether a no-op import.meta prelude is emitted. */
|
|
44
|
+
importMetaPrelude?: 'off' | 'auto' | 'on';
|
|
43
45
|
/** Handling for top-level await constructs. */
|
|
44
46
|
topLevelAwait?: 'error' | 'wrap' | 'preserve';
|
|
45
47
|
/** Optional diagnostics sink for warnings/errors emitted during transform. */
|
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.requireMainRgx = exports.exportsRename = exports.collectCjsExports = void 0;
|
|
7
|
-
var _walk = require("
|
|
7
|
+
var _walk = require("../walk.cjs");
|
|
8
8
|
const exportsRename = exports.exportsRename = '__exports';
|
|
9
9
|
const requireMainRgx = exports.requireMainRgx = /(require\.main\s*===\s*module|module\s*===\s*require\.main)/g;
|
|
10
10
|
const literalPropName = (prop, literals) => {
|
|
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.collectScopeIdentifiers = exports.collectModuleIdentifiers = void 0;
|
|
7
|
-
var _walk = require("
|
|
8
|
-
var _identifier = require("
|
|
7
|
+
var _walk = require("../walk.cjs");
|
|
8
|
+
var _identifier = require("../helpers/identifier.cjs");
|
|
9
9
|
var _scopeNodes = require("./scopeNodes.cjs");
|
|
10
10
|
const addBindingNames = (pattern, into) => {
|
|
11
11
|
if (!pattern) return;
|
package/dist/exports.d.ts
CHANGED
|
@@ -2,5 +2,8 @@ import type { Node } from 'oxc-parser';
|
|
|
2
2
|
import type { CjsExport } from '../types.js';
|
|
3
3
|
declare const exportsRename = "__exports";
|
|
4
4
|
declare const requireMainRgx: RegExp;
|
|
5
|
-
|
|
5
|
+
type ExportsMap = Map<string, CjsExport> & {
|
|
6
|
+
hasUnsupportedExportWrite?: boolean;
|
|
7
|
+
};
|
|
8
|
+
declare const collectCjsExports: (ast: Node) => Promise<ExportsMap>;
|
|
6
9
|
export { exportsRename, requireMainRgx, collectCjsExports };
|