@knighted/module 1.0.0-alpha.3 → 1.0.0-alpha.5
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/dist/cjs/format.cjs +4 -4
- package/dist/cjs/formatters/identifier.cjs +48 -0
- package/dist/cjs/identifier.d.cts +5 -0
- package/dist/cjs/module.cjs +17 -7
- package/dist/format.js +4 -4
- package/dist/formatters/identifier.d.ts +5 -0
- package/dist/formatters/identifier.js +41 -0
- package/dist/identifier.d.ts +5 -0
- package/dist/module.js +18 -8
- package/package.json +14 -14
package/dist/cjs/format.cjs
CHANGED
|
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.format = void 0;
|
|
7
7
|
var _magicString = _interopRequireDefault(require("magic-string"));
|
|
8
8
|
var _traverse2 = _interopRequireDefault(require("@babel/traverse"));
|
|
9
|
+
var _identifier = require("./formatters/identifier.cjs");
|
|
9
10
|
var _metaProperty = require("./formatters/metaProperty.cjs");
|
|
10
11
|
var _memberExpression = require("./formatters/memberExpression.cjs");
|
|
11
|
-
var _expressionStatement = require("./formatters/expressionStatement.cjs");
|
|
12
12
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
13
|
const traverse = _traverse2.default.default;
|
|
14
14
|
|
|
@@ -19,12 +19,12 @@ const traverse = _traverse2.default.default;
|
|
|
19
19
|
const format = (code, ast, options) => {
|
|
20
20
|
const src = new _magicString.default(code);
|
|
21
21
|
traverse(ast, {
|
|
22
|
+
Identifier(path) {
|
|
23
|
+
(0, _identifier.identifier)(path, src, options);
|
|
24
|
+
},
|
|
22
25
|
MetaProperty(path) {
|
|
23
26
|
(0, _metaProperty.metaProperty)(path, src, options);
|
|
24
27
|
},
|
|
25
|
-
ExpressionStatement(path) {
|
|
26
|
-
(0, _expressionStatement.expressionStatement)(path, src, options);
|
|
27
|
-
},
|
|
28
28
|
MemberExpression(path) {
|
|
29
29
|
(0, _memberExpression.memberExpression)(path, src, options);
|
|
30
30
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.identifier = void 0;
|
|
7
|
+
const identifier = (nodePath, src, options) => {
|
|
8
|
+
if (options.type === 'module') {
|
|
9
|
+
const {
|
|
10
|
+
node
|
|
11
|
+
} = nodePath;
|
|
12
|
+
const {
|
|
13
|
+
start,
|
|
14
|
+
end
|
|
15
|
+
} = node;
|
|
16
|
+
if (typeof start === 'number' && typeof end === 'number' && node.type === 'Identifier') {
|
|
17
|
+
const {
|
|
18
|
+
name
|
|
19
|
+
} = node;
|
|
20
|
+
const isMemberExpression = Boolean(nodePath.findParent(path => path.isMemberExpression()));
|
|
21
|
+
|
|
22
|
+
// CommonJS globals in expression/statement
|
|
23
|
+
switch (name) {
|
|
24
|
+
case 'module':
|
|
25
|
+
{
|
|
26
|
+
if (!isMemberExpression) {
|
|
27
|
+
src.update(start, end, 'import.meta');
|
|
28
|
+
}
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case 'exports':
|
|
32
|
+
{
|
|
33
|
+
if (!isMemberExpression) {
|
|
34
|
+
src.update(start, end, '{}');
|
|
35
|
+
}
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case '__filename':
|
|
39
|
+
src.update(start, end, 'import.meta.filename');
|
|
40
|
+
break;
|
|
41
|
+
case '__dirname':
|
|
42
|
+
src.update(start, end, 'import.meta.dirname');
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.identifier = identifier;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import MagicString from 'magic-string';
|
|
2
|
+
import type { NodePath } from '@babel/traverse';
|
|
3
|
+
import type { Identifier } from '@babel/types';
|
|
4
|
+
import type { FormatterOptions } from '../types.cjs';
|
|
5
|
+
export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
|
package/dist/cjs/module.cjs
CHANGED
|
@@ -15,6 +15,21 @@ const defaultOptions = {
|
|
|
15
15
|
modules: false,
|
|
16
16
|
specifier: undefined
|
|
17
17
|
};
|
|
18
|
+
const getLangFromExt = filename => {
|
|
19
|
+
const ext = (0, _nodePath.extname)(filename);
|
|
20
|
+
if (/\.js$/.test(ext)) {
|
|
21
|
+
return 'js';
|
|
22
|
+
}
|
|
23
|
+
if (/\.ts$/.test(ext)) {
|
|
24
|
+
return 'ts';
|
|
25
|
+
}
|
|
26
|
+
if (ext === '.tsx') {
|
|
27
|
+
return 'tsx';
|
|
28
|
+
}
|
|
29
|
+
if (ext === '.jsx') {
|
|
30
|
+
return 'jsx';
|
|
31
|
+
}
|
|
32
|
+
};
|
|
18
33
|
const transform = async (filename, options = defaultOptions) => {
|
|
19
34
|
const opts = {
|
|
20
35
|
...defaultOptions,
|
|
@@ -25,10 +40,7 @@ const transform = async (filename, options = defaultOptions) => {
|
|
|
25
40
|
const ast = (0, _parse.parse)(code);
|
|
26
41
|
let source = (0, _format.format)(code, ast, opts).toString();
|
|
27
42
|
if (options.specifier) {
|
|
28
|
-
const {
|
|
29
|
-
code,
|
|
30
|
-
error
|
|
31
|
-
} = await _specifier.specifier.updateSrc(source, ({
|
|
43
|
+
const code = await _specifier.specifier.updateSrc(source, getLangFromExt(filename), ({
|
|
32
44
|
value
|
|
33
45
|
}) => {
|
|
34
46
|
// Collapse any BinaryExpression or NewExpression to test for a relative specifier
|
|
@@ -39,9 +51,7 @@ const transform = async (filename, options = defaultOptions) => {
|
|
|
39
51
|
return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"`]*)?$/, `$1${options.specifier}$2`);
|
|
40
52
|
}
|
|
41
53
|
});
|
|
42
|
-
|
|
43
|
-
source = code;
|
|
44
|
-
}
|
|
54
|
+
source = code;
|
|
45
55
|
}
|
|
46
56
|
if (opts.out) {
|
|
47
57
|
await (0, _promises.writeFile)((0, _nodePath.resolve)(opts.out), source);
|
package/dist/format.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import MagicString from 'magic-string';
|
|
2
2
|
import _traverse from '@babel/traverse';
|
|
3
|
+
import { identifier } from './formatters/identifier.js';
|
|
3
4
|
import { metaProperty } from './formatters/metaProperty.js';
|
|
4
5
|
import { memberExpression } from './formatters/memberExpression.js';
|
|
5
|
-
import { expressionStatement } from './formatters/expressionStatement.js';
|
|
6
6
|
const traverse = _traverse.default;
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -12,12 +12,12 @@ const traverse = _traverse.default;
|
|
|
12
12
|
export const format = (code, ast, options) => {
|
|
13
13
|
const src = new MagicString(code);
|
|
14
14
|
traverse(ast, {
|
|
15
|
+
Identifier(path) {
|
|
16
|
+
identifier(path, src, options);
|
|
17
|
+
},
|
|
15
18
|
MetaProperty(path) {
|
|
16
19
|
metaProperty(path, src, options);
|
|
17
20
|
},
|
|
18
|
-
ExpressionStatement(path) {
|
|
19
|
-
expressionStatement(path, src, options);
|
|
20
|
-
},
|
|
21
21
|
MemberExpression(path) {
|
|
22
22
|
memberExpression(path, src, options);
|
|
23
23
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import MagicString from 'magic-string';
|
|
2
|
+
import type { NodePath } from '@babel/traverse';
|
|
3
|
+
import type { Identifier } from '@babel/types';
|
|
4
|
+
import type { FormatterOptions } from '../types.js';
|
|
5
|
+
export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const identifier = (nodePath, src, options) => {
|
|
2
|
+
if (options.type === 'module') {
|
|
3
|
+
const {
|
|
4
|
+
node
|
|
5
|
+
} = nodePath;
|
|
6
|
+
const {
|
|
7
|
+
start,
|
|
8
|
+
end
|
|
9
|
+
} = node;
|
|
10
|
+
if (typeof start === 'number' && typeof end === 'number' && node.type === 'Identifier') {
|
|
11
|
+
const {
|
|
12
|
+
name
|
|
13
|
+
} = node;
|
|
14
|
+
const isMemberExpression = Boolean(nodePath.findParent(path => path.isMemberExpression()));
|
|
15
|
+
|
|
16
|
+
// CommonJS globals in expression/statement
|
|
17
|
+
switch (name) {
|
|
18
|
+
case 'module':
|
|
19
|
+
{
|
|
20
|
+
if (!isMemberExpression) {
|
|
21
|
+
src.update(start, end, 'import.meta');
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case 'exports':
|
|
26
|
+
{
|
|
27
|
+
if (!isMemberExpression) {
|
|
28
|
+
src.update(start, end, '{}');
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case '__filename':
|
|
33
|
+
src.update(start, end, 'import.meta.filename');
|
|
34
|
+
break;
|
|
35
|
+
case '__dirname':
|
|
36
|
+
src.update(start, end, 'import.meta.dirname');
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import MagicString from 'magic-string';
|
|
2
|
+
import type { NodePath } from '@babel/traverse';
|
|
3
|
+
import type { Identifier } from '@babel/types';
|
|
4
|
+
import type { FormatterOptions } from '../types.js';
|
|
5
|
+
export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
|
package/dist/module.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolve } from 'node:path';
|
|
1
|
+
import { resolve, extname } from 'node:path';
|
|
2
2
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { specifier } from '@knighted/specifier';
|
|
4
4
|
import { parse } from './parse.js';
|
|
@@ -9,6 +9,21 @@ const defaultOptions = {
|
|
|
9
9
|
modules: false,
|
|
10
10
|
specifier: undefined
|
|
11
11
|
};
|
|
12
|
+
const getLangFromExt = filename => {
|
|
13
|
+
const ext = extname(filename);
|
|
14
|
+
if (/\.js$/.test(ext)) {
|
|
15
|
+
return 'js';
|
|
16
|
+
}
|
|
17
|
+
if (/\.ts$/.test(ext)) {
|
|
18
|
+
return 'ts';
|
|
19
|
+
}
|
|
20
|
+
if (ext === '.tsx') {
|
|
21
|
+
return 'tsx';
|
|
22
|
+
}
|
|
23
|
+
if (ext === '.jsx') {
|
|
24
|
+
return 'jsx';
|
|
25
|
+
}
|
|
26
|
+
};
|
|
12
27
|
const transform = async (filename, options = defaultOptions) => {
|
|
13
28
|
const opts = {
|
|
14
29
|
...defaultOptions,
|
|
@@ -19,10 +34,7 @@ const transform = async (filename, options = defaultOptions) => {
|
|
|
19
34
|
const ast = parse(code);
|
|
20
35
|
let source = format(code, ast, opts).toString();
|
|
21
36
|
if (options.specifier) {
|
|
22
|
-
const {
|
|
23
|
-
code,
|
|
24
|
-
error
|
|
25
|
-
} = await specifier.updateSrc(source, ({
|
|
37
|
+
const code = await specifier.updateSrc(source, getLangFromExt(filename), ({
|
|
26
38
|
value
|
|
27
39
|
}) => {
|
|
28
40
|
// Collapse any BinaryExpression or NewExpression to test for a relative specifier
|
|
@@ -33,9 +45,7 @@ const transform = async (filename, options = defaultOptions) => {
|
|
|
33
45
|
return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"`]*)?$/, `$1${options.specifier}$2`);
|
|
34
46
|
}
|
|
35
47
|
});
|
|
36
|
-
|
|
37
|
-
source = code;
|
|
38
|
-
}
|
|
48
|
+
source = code;
|
|
39
49
|
}
|
|
40
50
|
if (opts.out) {
|
|
41
51
|
await writeFile(resolve(opts.out), source);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@knighted/module",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.5",
|
|
4
4
|
"description": "Converts module differences in source files between ES and CommonJS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/module.js",
|
|
@@ -57,25 +57,25 @@
|
|
|
57
57
|
"url": "https://github.com/knightedcodemonkey/module/issues"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@babel/preset-env": "^7.
|
|
61
|
-
"@babel/preset-typescript": "^7.
|
|
62
|
-
"@babel/types": "^7.
|
|
60
|
+
"@babel/preset-env": "^7.26.9",
|
|
61
|
+
"@babel/preset-typescript": "^7.27.0",
|
|
62
|
+
"@babel/types": "^7.27.0",
|
|
63
63
|
"@eslint/js": "^9.3.0",
|
|
64
64
|
"@types/babel__traverse": "^7.20.6",
|
|
65
|
-
"@types/node": "^
|
|
66
|
-
"babel-dual-package": "^1.1.
|
|
67
|
-
"c8": "^
|
|
68
|
-
"eslint": "^9.
|
|
65
|
+
"@types/node": "^22.13.17",
|
|
66
|
+
"babel-dual-package": "^1.1.4",
|
|
67
|
+
"c8": "^10.1.3",
|
|
68
|
+
"eslint": "^9.23.0",
|
|
69
69
|
"eslint-plugin-n": "^17.7.0",
|
|
70
70
|
"prettier": "^3.2.5",
|
|
71
|
-
"tsx": "^4.
|
|
72
|
-
"typescript": "^5.
|
|
73
|
-
"typescript-eslint": "^8.
|
|
71
|
+
"tsx": "^4.19.3",
|
|
72
|
+
"typescript": "^5.8.2",
|
|
73
|
+
"typescript-eslint": "^8.29.0"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@babel/parser": "^7.
|
|
77
|
-
"@babel/traverse": "^7.
|
|
78
|
-
"@knighted/specifier": "^2.0.0-rc.
|
|
76
|
+
"@babel/parser": "^7.27.0",
|
|
77
|
+
"@babel/traverse": "^7.27.0",
|
|
78
|
+
"@knighted/specifier": "^2.0.0-rc.4",
|
|
79
79
|
"magic-string": "^0.30.10"
|
|
80
80
|
},
|
|
81
81
|
"prettier": {
|