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

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,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/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.3",
4
4
  "description": "Converts module differences in source files between ES and CommonJS.",
5
5
  "type": "module",
6
6
  "main": "dist/module.js",
@@ -70,11 +70,12 @@
70
70
  "prettier": "^3.2.5",
71
71
  "tsx": "^4.11.0",
72
72
  "typescript": "^5.4.5",
73
- "typescript-eslint": "^8.0.0-alpha.16"
73
+ "typescript-eslint": "^8.0.0-alpha.18"
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": {