@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/dist/format.js
CHANGED
|
@@ -1,29 +1,31 @@
|
|
|
1
|
+
import { getModuleExportName, isAstNode, isCallExpressionNode, isIdentifierNode, isMemberExpressionNode } from './helpers/ast.js';
|
|
1
2
|
import MagicString from 'magic-string';
|
|
2
|
-
import { identifier } from '
|
|
3
|
-
import { metaProperty } from '
|
|
4
|
-
import { memberExpression } from '
|
|
5
|
-
import { assignmentExpression } from '
|
|
6
|
-
import { isValidUrl } from '
|
|
7
|
-
import { exportsRename, collectCjsExports } from '
|
|
8
|
-
import { collectModuleIdentifiers } from '
|
|
9
|
-
import { isIdentifierName } from '
|
|
10
|
-
import { ancestorWalk } from '
|
|
3
|
+
import { identifier } from './formatters/identifier.js';
|
|
4
|
+
import { metaProperty } from './formatters/metaProperty.js';
|
|
5
|
+
import { memberExpression } from './formatters/memberExpression.js';
|
|
6
|
+
import { assignmentExpression } from './formatters/assignmentExpression.js';
|
|
7
|
+
import { isValidUrl } from './utils/url.js';
|
|
8
|
+
import { exportsRename, collectCjsExports } from './utils/exports.js';
|
|
9
|
+
import { collectModuleIdentifiers } from './utils/identifiers.js';
|
|
10
|
+
import { isIdentifierName } from './helpers/identifier.js';
|
|
11
|
+
import { ancestorWalk } from './walk.js';
|
|
11
12
|
const isValidIdent = name => /^[$A-Z_a-z][$\w]*$/.test(name);
|
|
12
13
|
const expressionHasRequireCall = (node, shadowed) => {
|
|
13
14
|
let found = false;
|
|
14
15
|
const walkNode = n => {
|
|
15
|
-
if (!n || found) return;
|
|
16
|
-
if (n
|
|
16
|
+
if (!isAstNode(n) || found) return;
|
|
17
|
+
if (isCallExpressionNode(n) && isIdentifierNode(n.callee) && n.callee.name === 'require' && !shadowed.has('require')) {
|
|
17
18
|
found = true;
|
|
18
19
|
return;
|
|
19
20
|
}
|
|
20
|
-
if (n
|
|
21
|
+
if (isCallExpressionNode(n) && isMemberExpressionNode(n.callee) && isIdentifierNode(n.callee.object) && n.callee.object.name === 'require' && !shadowed.has('require')) {
|
|
21
22
|
found = true;
|
|
22
23
|
return;
|
|
23
24
|
}
|
|
24
|
-
const
|
|
25
|
+
const record = n;
|
|
26
|
+
const keys = Object.keys(record);
|
|
25
27
|
for (const key of keys) {
|
|
26
|
-
const value =
|
|
28
|
+
const value = record[key];
|
|
27
29
|
if (!value) continue;
|
|
28
30
|
if (Array.isArray(value)) {
|
|
29
31
|
for (const item of value) {
|
|
@@ -80,6 +82,10 @@ const lowerCjsRequireToImports = (program, code, shadowed) => {
|
|
|
80
82
|
if (allStatic) {
|
|
81
83
|
for (const decl of decls) {
|
|
82
84
|
const init = decl.init;
|
|
85
|
+
if (!init || !isCallExpressionNode(init)) {
|
|
86
|
+
needsCreateRequire = true;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
83
89
|
const arg = init.arguments[0];
|
|
84
90
|
const source = code.slice(arg.start, arg.end);
|
|
85
91
|
const value = arg.value;
|
|
@@ -116,6 +122,10 @@ const lowerCjsRequireToImports = (program, code, shadowed) => {
|
|
|
116
122
|
if (stmt.type === 'ExpressionStatement') {
|
|
117
123
|
const expr = stmt.expression;
|
|
118
124
|
if (expr && isStaticRequire(expr, shadowed)) {
|
|
125
|
+
if (!isCallExpressionNode(expr)) {
|
|
126
|
+
needsCreateRequire = true;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
119
129
|
const arg = expr.arguments[0];
|
|
120
130
|
const source = code.slice(arg.start, arg.end);
|
|
121
131
|
const value = arg.value;
|
|
@@ -142,11 +152,12 @@ const lowerCjsRequireToImports = (program, code, shadowed) => {
|
|
|
142
152
|
needsInteropHelper
|
|
143
153
|
};
|
|
144
154
|
};
|
|
145
|
-
const isRequireMainMember = (node, shadowed) => node
|
|
155
|
+
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';
|
|
146
156
|
const hasTopLevelAwait = program => {
|
|
147
157
|
let found = false;
|
|
148
158
|
const walkNode = (node, inFunction) => {
|
|
149
159
|
if (found) return;
|
|
160
|
+
if (!isAstNode(node)) return;
|
|
150
161
|
switch (node.type) {
|
|
151
162
|
case 'FunctionDeclaration':
|
|
152
163
|
case 'FunctionExpression':
|
|
@@ -160,9 +171,10 @@ const hasTopLevelAwait = program => {
|
|
|
160
171
|
found = true;
|
|
161
172
|
return;
|
|
162
173
|
}
|
|
163
|
-
const
|
|
174
|
+
const record = node;
|
|
175
|
+
const keys = Object.keys(record);
|
|
164
176
|
for (const key of keys) {
|
|
165
|
-
const value =
|
|
177
|
+
const value = record[key];
|
|
166
178
|
if (!value) continue;
|
|
167
179
|
if (Array.isArray(value)) {
|
|
168
180
|
for (const item of value) {
|
|
@@ -243,7 +255,8 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
243
255
|
}
|
|
244
256
|
if (namedSpecs.length) {
|
|
245
257
|
const pairs = namedSpecs.map(s => {
|
|
246
|
-
const imported = s.imported
|
|
258
|
+
const imported = getModuleExportName(s.imported);
|
|
259
|
+
if (!imported) return s.local.name;
|
|
247
260
|
const local = s.local.name;
|
|
248
261
|
return imported === local ? imported : `${imported}: ${local}`;
|
|
249
262
|
});
|
|
@@ -268,7 +281,7 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
268
281
|
exportedNames.push(d.id.name);
|
|
269
282
|
}
|
|
270
283
|
}
|
|
271
|
-
} else if (decl.id?.type === 'Identifier') {
|
|
284
|
+
} else if ('id' in decl && decl.id?.type === 'Identifier') {
|
|
272
285
|
exportedNames.push(decl.id.name);
|
|
273
286
|
}
|
|
274
287
|
const exportLines = exportedNames.map(name => exportAssignment(name, name, live));
|
|
@@ -288,8 +301,9 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
288
301
|
const lines = [`const ${modIdent} = require(${srcLiteral});`];
|
|
289
302
|
for (const spec of node.specifiers) {
|
|
290
303
|
if (spec.type !== 'ExportSpecifier') continue;
|
|
291
|
-
const exported = spec.exported
|
|
292
|
-
const imported = spec.local
|
|
304
|
+
const exported = getModuleExportName(spec.exported);
|
|
305
|
+
const imported = getModuleExportName(spec.local);
|
|
306
|
+
if (!exported || !imported) continue;
|
|
293
307
|
let rhs = `${modIdent}.${imported}`;
|
|
294
308
|
if (imported === 'default') {
|
|
295
309
|
rhs = `${defaultInteropName}(${modIdent})`;
|
|
@@ -307,8 +321,9 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
307
321
|
const lines = [];
|
|
308
322
|
for (const spec of node.specifiers) {
|
|
309
323
|
if (spec.type !== 'ExportSpecifier') continue;
|
|
310
|
-
const exported = spec.exported
|
|
311
|
-
const local = spec.local
|
|
324
|
+
const exported = getModuleExportName(spec.exported);
|
|
325
|
+
const local = getModuleExportName(spec.local);
|
|
326
|
+
if (!exported || !local) continue;
|
|
312
327
|
lines.push(exportAssignment(exported, local, live));
|
|
313
328
|
}
|
|
314
329
|
exportTransforms.push({
|
|
@@ -352,8 +367,11 @@ const lowerEsmToCjs = (program, code, opts, containsTopLevelAwait) => {
|
|
|
352
367
|
}
|
|
353
368
|
if (node.type === 'ExportAllDeclaration') {
|
|
354
369
|
const srcLiteral = code.slice(node.source.start, node.source.end);
|
|
355
|
-
if (node.exported) {
|
|
356
|
-
const exported = node.exported
|
|
370
|
+
if ('exported' in node && node.exported) {
|
|
371
|
+
const exported = getModuleExportName(node.exported);
|
|
372
|
+
if (!exported) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
357
375
|
const modIdent = `__mod${importIndex++}`;
|
|
358
376
|
const lines = [`const ${modIdent} = require(${srcLiteral});`, exportAssignment(exported, modIdent, live)];
|
|
359
377
|
exportTransforms.push({
|
|
@@ -600,6 +618,8 @@ const format = async (src, ast, opts) => {
|
|
|
600
618
|
let requireMainNeedsRealpath = false;
|
|
601
619
|
let needsRequireResolveHelper = false;
|
|
602
620
|
const nestedRequireStrategy = opts.nestedRequireStrategy ?? 'create-require';
|
|
621
|
+
const importMetaPreludeMode = opts.importMetaPrelude ?? 'auto';
|
|
622
|
+
let importMetaRef = false;
|
|
603
623
|
const shouldLowerCjs = opts.target === 'commonjs' && fullTransform;
|
|
604
624
|
const shouldRaiseEsm = opts.target === 'module' && fullTransform;
|
|
605
625
|
let hoistedImports = [];
|
|
@@ -647,6 +667,10 @@ const format = async (src, ast, opts) => {
|
|
|
647
667
|
const mainExpr = requireMainStrategy === 'import-meta-main' ? 'import.meta.main' : 'import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href';
|
|
648
668
|
if (requireMainStrategy === 'realpath') {
|
|
649
669
|
requireMainNeedsRealpath = true;
|
|
670
|
+
importMetaRef = true;
|
|
671
|
+
}
|
|
672
|
+
if (requireMainStrategy === 'import-meta-main') {
|
|
673
|
+
importMetaRef = true;
|
|
650
674
|
}
|
|
651
675
|
code.update(node.start, node.end, negate ? `!(${mainExpr})` : mainExpr);
|
|
652
676
|
return;
|
|
@@ -765,6 +789,9 @@ const format = async (src, ast, opts) => {
|
|
|
765
789
|
}
|
|
766
790
|
}
|
|
767
791
|
if (isIdentifierName(node)) {
|
|
792
|
+
if (shouldRaiseEsm && node.type === 'Identifier' && (node.name === '__dirname' || node.name === '__filename')) {
|
|
793
|
+
importMetaRef = true;
|
|
794
|
+
}
|
|
768
795
|
identifier({
|
|
769
796
|
node,
|
|
770
797
|
ancestors,
|
|
@@ -820,8 +847,14 @@ const format = async (src, ast, opts) => {
|
|
|
820
847
|
const asExportName = name => isValidExportName(name) ? name : JSON.stringify(name);
|
|
821
848
|
const accessProp = name => isValidExportName(name) ? `${exportsRename}.${name}` : `${exportsRename}[${JSON.stringify(name)}]`;
|
|
822
849
|
const exportValueFor = name => {
|
|
823
|
-
if (name === '__dirname')
|
|
824
|
-
|
|
850
|
+
if (name === '__dirname') {
|
|
851
|
+
importMetaRef = true;
|
|
852
|
+
return 'import.meta.dirname';
|
|
853
|
+
}
|
|
854
|
+
if (name === '__filename') {
|
|
855
|
+
importMetaRef = true;
|
|
856
|
+
return 'import.meta.filename';
|
|
857
|
+
}
|
|
825
858
|
return name;
|
|
826
859
|
};
|
|
827
860
|
const tempNameFor = name => {
|
|
@@ -877,6 +910,9 @@ const format = async (src, ast, opts) => {
|
|
|
877
910
|
}
|
|
878
911
|
if (shouldRaiseEsm && fullTransform) {
|
|
879
912
|
const importPrelude = [];
|
|
913
|
+
if (needsCreateRequire || needsRequireResolveHelper) {
|
|
914
|
+
importMetaRef = true;
|
|
915
|
+
}
|
|
880
916
|
if (needsCreateRequire || needsRequireResolveHelper) {
|
|
881
917
|
importPrelude.push('import { createRequire } from "node:module";\n');
|
|
882
918
|
}
|
|
@@ -909,9 +945,13 @@ const format = async (src, ast, opts) => {
|
|
|
909
945
|
const exportsBagInit = useExportsBag ? `let ${exportsRename} = {};
|
|
910
946
|
` : '';
|
|
911
947
|
const modulePrelude = '';
|
|
912
|
-
const prelude = `${importPrelude.join('')}${importPrelude.length ? '\n' : ''}${setupPrelude.join('')}${setupPrelude.length ? '\n' : ''}${requireInit}${requireResolveInit}${exportsBagInit}${modulePrelude}
|
|
913
|
-
|
|
914
|
-
|
|
948
|
+
const prelude = `${importPrelude.join('')}${importPrelude.length ? '\n' : ''}${setupPrelude.join('')}${setupPrelude.length ? '\n' : ''}${requireInit}${requireResolveInit}${exportsBagInit}${modulePrelude}`;
|
|
949
|
+
const importMetaTouch = (() => {
|
|
950
|
+
if (importMetaPreludeMode === 'on') return 'void import.meta.filename;\n';
|
|
951
|
+
if (importMetaPreludeMode === 'off') return '';
|
|
952
|
+
return importMetaRef ? 'void import.meta.filename;\n' : '';
|
|
953
|
+
})();
|
|
954
|
+
code.prepend(`${prelude}${importMetaTouch}`);
|
|
915
955
|
}
|
|
916
956
|
if (opts.target === 'commonjs' && fullTransform && containsTopLevelAwait) {
|
|
917
957
|
const body = code.toString();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { exportsRename } from '
|
|
2
|
-
import { identifier as ident } from '
|
|
1
|
+
import { exportsRename } from '../utils/exports.js';
|
|
2
|
+
import { identifier as ident } from '../helpers/identifier.js';
|
|
3
3
|
export const identifier = ({
|
|
4
4
|
node,
|
|
5
5
|
ancestors,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { exportsRename } from '
|
|
1
|
+
import { exportsRename } from '../utils/exports.js';
|
|
2
2
|
export const memberExpression = (node, parent, src, options, shadowed, extras, useExportsBag = true, rewriteExports = true) => {
|
|
3
3
|
if (options.target === 'module') {
|
|
4
4
|
if (rewriteExports && !useExportsBag) {
|
|
@@ -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,18 @@
|
|
|
1
|
+
export const isAstNode = value => {
|
|
2
|
+
return Boolean(value) && typeof value === 'object' && 'type' in value;
|
|
3
|
+
};
|
|
4
|
+
export const isIdentifierNode = node => {
|
|
5
|
+
return node?.type === 'Identifier';
|
|
6
|
+
};
|
|
7
|
+
export const isMemberExpressionNode = node => {
|
|
8
|
+
return node?.type === 'MemberExpression';
|
|
9
|
+
};
|
|
10
|
+
export const isCallExpressionNode = node => {
|
|
11
|
+
return node?.type === 'CallExpression';
|
|
12
|
+
};
|
|
13
|
+
export const getModuleExportName = name => {
|
|
14
|
+
if (!name) return null;
|
|
15
|
+
if (name.type === 'Identifier') return name.name;
|
|
16
|
+
if (name.type === 'Literal' && typeof name.value === 'string') return name.value;
|
|
17
|
+
return null;
|
|
18
|
+
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { analyze } from 'periscopic';
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* Focus exclusively on IdentifierName type as it has the name property,
|
|
5
4
|
* which is what the identifer utilities are interested in.
|
|
@@ -121,14 +120,16 @@ const identifier = {
|
|
|
121
120
|
const node = ancestors[ancestors.length - 1];
|
|
122
121
|
const varBoundScopes = ['ClassDeclaration', 'ClassExpression', 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'];
|
|
123
122
|
const declaratorIndex = ancestors.findIndex(ancestor => {
|
|
124
|
-
|
|
123
|
+
if (ancestor.type !== 'VariableDeclarator') return false;
|
|
124
|
+
return ancestor === node || isInBindingPattern(ancestor.id, node);
|
|
125
125
|
});
|
|
126
126
|
if (declaratorIndex === -1) return false;
|
|
127
|
-
const
|
|
127
|
+
const declaratorNode = ancestors[declaratorIndex];
|
|
128
128
|
const declaration = ancestors[declaratorIndex - 1];
|
|
129
|
+
if (declaratorNode?.type !== 'VariableDeclarator') return false;
|
|
129
130
|
return declaration?.type === 'VariableDeclaration' && declaration.kind === 'var' && ancestors.every(ancestor => {
|
|
130
131
|
return !varBoundScopes.includes(ancestor.type);
|
|
131
|
-
}) && (
|
|
132
|
+
}) && (declaratorNode.id === node || isInBindingPattern(declaratorNode.id, node));
|
|
132
133
|
},
|
|
133
134
|
isIife(ancestors) {
|
|
134
135
|
const parent = ancestors[ancestors.length - 2];
|
package/dist/module.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { resolve } from 'node:path';
|
|
2
2
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { specifier } from './specifier.js';
|
|
4
|
-
import { parse } from '
|
|
5
|
-
import { format } from '
|
|
6
|
-
import { getLangFromExt } from '
|
|
4
|
+
import { parse } from './parse.js';
|
|
5
|
+
import { format } from './format.js';
|
|
6
|
+
import { getLangFromExt } from './utils/lang.js';
|
|
7
7
|
import { builtinModules } from 'node:module';
|
|
8
8
|
import { resolve as pathResolve, dirname as pathDirname, extname, join } from 'node:path';
|
|
9
9
|
import { readFile as fsReadFile, stat } from 'node:fs/promises';
|
|
10
|
-
import { parse as parseModule } from '
|
|
11
|
-
import { walk } from '
|
|
10
|
+
import { parse as parseModule } from './parse.js';
|
|
11
|
+
import { walk } from './walk.js';
|
|
12
12
|
const collapseSpecifier = value => value.replace(/['"`+)\s]|new String\(/g, '');
|
|
13
13
|
const builtinSpecifiers = new Set(builtinModules.map(mod => mod.startsWith('node:') ? mod.slice(5) : mod).flatMap(mod => {
|
|
14
14
|
const parts = mod.split('/');
|
|
@@ -158,6 +158,7 @@ const defaultOptions = {
|
|
|
158
158
|
nestedRequireStrategy: 'create-require',
|
|
159
159
|
cjsDefault: 'auto',
|
|
160
160
|
idiomaticExports: 'safe',
|
|
161
|
+
importMetaPrelude: 'auto',
|
|
161
162
|
topLevelAwait: 'error',
|
|
162
163
|
out: undefined,
|
|
163
164
|
inPlace: false
|
package/dist/specifier.js
CHANGED
|
@@ -2,7 +2,7 @@ import { resolve } from 'node:path';
|
|
|
2
2
|
import { stat, readFile } from 'node:fs/promises';
|
|
3
3
|
import MagicString from 'magic-string';
|
|
4
4
|
import { parseSync } from 'oxc-parser';
|
|
5
|
-
import { walk } from '
|
|
5
|
+
import { walk } from './walk.js';
|
|
6
6
|
const isStringLiteral = node => {
|
|
7
7
|
return node.type === 'Literal' && typeof node.value === 'string';
|
|
8
8
|
};
|
package/dist/types.d.ts
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. */
|
|
@@ -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 };
|
package/dist/utils/exports.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ancestorWalk } from '
|
|
2
|
-
import { identifier } from '
|
|
1
|
+
import { ancestorWalk } from '../walk.js';
|
|
2
|
+
import { identifier } from '../helpers/identifier.js';
|
|
3
3
|
import { scopeNodes } from './scopeNodes.js';
|
|
4
4
|
const addBindingNames = (pattern, into) => {
|
|
5
5
|
if (!pattern) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knighted/module",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Bidirectional transform for ES modules and CommonJS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/module.js",
|
|
@@ -18,14 +18,6 @@
|
|
|
18
18
|
},
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
|
-
"imports": {
|
|
22
|
-
"#parse": "./src/parse.js",
|
|
23
|
-
"#format": "./src/format.js",
|
|
24
|
-
"#utils/*.js": "./src/utils/*.js",
|
|
25
|
-
"#walk": "./src/walk.js",
|
|
26
|
-
"#helpers/*.js": "./src/helpers/*.js",
|
|
27
|
-
"#formatters/*.js": "./src/formatters/*.js"
|
|
28
|
-
},
|
|
29
21
|
"engines": {
|
|
30
22
|
"node": ">=22.21.1 <23 || >=24 <25"
|
|
31
23
|
},
|
|
@@ -40,8 +32,8 @@
|
|
|
40
32
|
"build:types": "tsc --emitDeclarationOnly",
|
|
41
33
|
"build:dual": "babel-dual-package src --extensions .ts",
|
|
42
34
|
"build": "npm run build:types && npm run build:dual",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
35
|
+
"cycles": "madge --circular src --extensions ts,js --ts-config tsconfig.json",
|
|
36
|
+
"prepack": "npm run build"
|
|
45
37
|
},
|
|
46
38
|
"keywords": [
|
|
47
39
|
"esm",
|
|
@@ -72,6 +64,7 @@
|
|
|
72
64
|
"c8": "^10.1.3",
|
|
73
65
|
"husky": "^9.1.7",
|
|
74
66
|
"lint-staged": "^16.2.7",
|
|
67
|
+
"madge": "^8.0.0",
|
|
75
68
|
"oxlint": "^1.35.0",
|
|
76
69
|
"prettier": "^3.7.4",
|
|
77
70
|
"tsx": "^4.21.0",
|
|
@@ -83,6 +76,11 @@
|
|
|
83
76
|
"oxc-parser": "^0.105.0",
|
|
84
77
|
"periscopic": "^4.0.2"
|
|
85
78
|
},
|
|
79
|
+
"overrides": {
|
|
80
|
+
"module-lookup-amd": {
|
|
81
|
+
"glob": "^9.0.0"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
86
84
|
"prettier": {
|
|
87
85
|
"arrowParens": "avoid",
|
|
88
86
|
"printWidth": 90,
|