@knighted/module 1.0.0-alpha.2 → 1.0.0-alpha.4

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 CHANGED
@@ -89,8 +89,13 @@ type ModuleOptions = {
89
89
  /* Whether import/export and require/exports should be transformed. */
90
90
  modules?: boolean
91
91
  /* Whether to change specifier extensions to the assigned value. If omitted they are left alone. */
92
- specifiers?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts'
93
- /* What filepath to write the transformed source to. If omitted the transformed source is returned. */
92
+ specifier?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts'
93
+ /* What filepath to write the transformed source to. */
94
94
  out?: string
95
95
  }
96
96
  ```
97
+
98
+ ## Roadmap
99
+
100
+ - Support option `modules`.
101
+ - Remove `@knighted/specifier` and avoid double parsing.
@@ -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;
@@ -6,16 +6,14 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.transform = void 0;
7
7
  var _nodePath = require("node:path");
8
8
  var _promises = require("node:fs/promises");
9
+ var _specifier = require("@knighted/specifier");
9
10
  var _parse = require("./parse.cjs");
10
11
  var _format = require("./format.cjs");
11
- /**
12
- * Defaults to only transforming ES module globals to CommonJS.
13
- */
14
12
  const defaultOptions = {
15
13
  type: 'commonjs',
16
14
  out: undefined,
17
15
  modules: false,
18
- specifiers: undefined
16
+ specifier: undefined
19
17
  };
20
18
  const transform = async (filename, options = defaultOptions) => {
21
19
  const opts = {
@@ -25,8 +23,29 @@ const transform = async (filename, options = defaultOptions) => {
25
23
  const file = (0, _nodePath.resolve)(filename);
26
24
  const code = (await (0, _promises.readFile)(file)).toString();
27
25
  const ast = (0, _parse.parse)(code);
28
-
29
- // TODO: Support `out` option.
30
- return (0, _format.format)(code, ast, opts).toString();
26
+ let source = (0, _format.format)(code, ast, opts).toString();
27
+ if (options.specifier) {
28
+ const {
29
+ code,
30
+ error
31
+ } = await _specifier.specifier.updateSrc(source, ({
32
+ value
33
+ }) => {
34
+ // Collapse any BinaryExpression or NewExpression to test for a relative specifier
35
+ const collapsed = value.replace(/['"`+)\s]|new String\(/g, '');
36
+ const relative = /^(?:\.|\.\.)\//;
37
+ if (relative.test(collapsed)) {
38
+ // $2 is for any closing quotation/parens around BE or NE
39
+ return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"`]*)?$/, `$1${options.specifier}$2`);
40
+ }
41
+ });
42
+ if (code && !error) {
43
+ source = code;
44
+ }
45
+ }
46
+ if (opts.out) {
47
+ await (0, _promises.writeFile)((0, _nodePath.resolve)(opts.out), source);
48
+ }
49
+ return source;
31
50
  };
32
51
  exports.transform = transform;
@@ -1,7 +1,7 @@
1
1
  export type ModuleOptions = {
2
2
  type?: 'module' | 'commonjs';
3
3
  modules?: boolean;
4
- specifiers?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
4
+ specifier?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
5
5
  out?: string;
6
6
  };
7
7
  export type FormatterOptions = Omit<ModuleOptions, 'out'> & Required<Pick<ModuleOptions, 'type'>>;
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,15 +1,13 @@
1
1
  import { resolve } from 'node:path';
2
- import { readFile } from 'node:fs/promises';
2
+ import { readFile, writeFile } from 'node:fs/promises';
3
+ import { specifier } from '@knighted/specifier';
3
4
  import { parse } from './parse.js';
4
5
  import { format } from './format.js';
5
- /**
6
- * Defaults to only transforming ES module globals to CommonJS.
7
- */
8
6
  const defaultOptions = {
9
7
  type: 'commonjs',
10
8
  out: undefined,
11
9
  modules: false,
12
- specifiers: undefined
10
+ specifier: undefined
13
11
  };
14
12
  const transform = async (filename, options = defaultOptions) => {
15
13
  const opts = {
@@ -19,8 +17,29 @@ const transform = async (filename, options = defaultOptions) => {
19
17
  const file = resolve(filename);
20
18
  const code = (await readFile(file)).toString();
21
19
  const ast = parse(code);
22
-
23
- // TODO: Support `out` option.
24
- return format(code, ast, opts).toString();
20
+ let source = format(code, ast, opts).toString();
21
+ if (options.specifier) {
22
+ const {
23
+ code,
24
+ error
25
+ } = await specifier.updateSrc(source, ({
26
+ value
27
+ }) => {
28
+ // Collapse any BinaryExpression or NewExpression to test for a relative specifier
29
+ const collapsed = value.replace(/['"`+)\s]|new String\(/g, '');
30
+ const relative = /^(?:\.|\.\.)\//;
31
+ if (relative.test(collapsed)) {
32
+ // $2 is for any closing quotation/parens around BE or NE
33
+ return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"`]*)?$/, `$1${options.specifier}$2`);
34
+ }
35
+ });
36
+ if (code && !error) {
37
+ source = code;
38
+ }
39
+ }
40
+ if (opts.out) {
41
+ await writeFile(resolve(opts.out), source);
42
+ }
43
+ return source;
25
44
  };
26
45
  export { transform };
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export type ModuleOptions = {
2
2
  type?: 'module' | 'commonjs';
3
3
  modules?: boolean;
4
- specifiers?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
4
+ specifier?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
5
5
  out?: string;
6
6
  };
7
7
  export type FormatterOptions = Omit<ModuleOptions, 'out'> & Required<Pick<ModuleOptions, 'type'>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/module",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "Converts module differences in source files between ES and CommonJS.",
5
5
  "type": "module",
6
6
  "main": "dist/module.js",
@@ -68,13 +68,14 @@
68
68
  "eslint": "^9.3.0",
69
69
  "eslint-plugin-n": "^17.7.0",
70
70
  "prettier": "^3.2.5",
71
- "tsx": "^4.11.0",
71
+ "tsx": "^4.11.2",
72
72
  "typescript": "^5.4.5",
73
- "typescript-eslint": "^8.0.0-alpha.16"
73
+ "typescript-eslint": "^8.0.0-alpha.26"
74
74
  },
75
75
  "dependencies": {
76
76
  "@babel/parser": "^7.24.6",
77
77
  "@babel/traverse": "^7.24.6",
78
+ "@knighted/specifier": "^2.0.0-rc.1",
78
79
  "magic-string": "^0.30.10"
79
80
  },
80
81
  "prettier": {