@knighted/module 1.0.0-alpha.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 KCM
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # [`@knighted/module`](https://www.npmjs.com/package/@knighted/module)
2
+
3
+ ![CI](https://github.com/knightedcodemonkey/module/actions/workflows/ci.yml/badge.svg)
4
+ [![codecov](https://codecov.io/gh/knightedcodemonkey/module/graph/badge.svg?token=AjayQQxghy)](https://codecov.io/gh/knightedcodemonkey/module)
5
+ [![NPM version](https://img.shields.io/npm/v/@knighted/module.svg)](https://www.npmjs.com/package/@knighted/module)
6
+
7
+ Node.js utility for transforming JavaScript or TypeScript files from one module system to another.
8
+
9
+ - ES module ➡️ CommonJS
10
+ - CommonJS ➡️ ES module
11
+
12
+ By default `@knighted/module` transforms globals from one module scope to another and returns the modified source, but it accepts options that allow
13
+
14
+ - Module loading transforms, i.e, `import`/`export` converted to `require`/`exports`
15
+ - Extensions to be updated in relative specifiers
16
+ - Write transformed source code to a filename
17
+
18
+ ## Requirements
19
+
20
+ - Node >= 20.11.0
21
+
22
+ ## Example
23
+
24
+ Given an ES module
25
+
26
+ **file.js**
27
+
28
+ ```js
29
+ import { argv } from 'node:process'
30
+ import { pathToFileURL } from 'node:url'
31
+ import { realpath } from 'node:fs/promises'
32
+
33
+ const detectCalledFromCli = async path => {
34
+ const realPath = await realpath(path)
35
+
36
+ if (import.meta.url === pathToFileURL(realPath).href) {
37
+ console.log('invoked directly by node')
38
+ }
39
+ }
40
+
41
+ detectCalledFromCli(argv[1])
42
+ ```
43
+
44
+ You can use transform it to the equivalent CommonJS module
45
+
46
+ ```js
47
+ import { transform } from '@knighted/module'
48
+
49
+ await transform('./file.js', {
50
+ type: 'commonjs'
51
+ moduleLoading: true,
52
+ out: './file.cjs'
53
+ })
54
+ ```
55
+
56
+ Which produces
57
+
58
+ **file.js**
59
+
60
+ ```js
61
+ const { argv } = require('node:process')
62
+ const { pathToFileURL } = require('node:url')
63
+ const { realpath } = require('node:fs/promises')
64
+
65
+ const detectCalledFromCli = async path => {
66
+ const realPath = await realpath(path)
67
+
68
+ if (require('node:url').pathToFileURL(__filename).toString() === pathToFileURL(realPath).href) {
69
+ console.log('invoked directly by node')
70
+ }
71
+ }
72
+
73
+ detectCalledFromCli(argv[1])
74
+ ```
75
+
76
+ When executed from the CLI
77
+
78
+ ```console
79
+ use@computer: $ node file.cjs
80
+ invoked directly by node
81
+ ```
82
+
83
+ ## Options
84
+
85
+ ```ts
86
+ type ModuleOptions = {
87
+ /* What module system to convert to. */
88
+ type?: 'module' | 'commonjs'
89
+ /* Whether import/export and require/exports should be transformed. */
90
+ moduleLoading?: boolean
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. */
94
+ out?: string
95
+ }
96
+ ```
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.format = void 0;
7
+ var _magicString = _interopRequireDefault(require("magic-string"));
8
+ var _traverse2 = _interopRequireDefault(require("@babel/traverse"));
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ const traverse = _traverse2.default.default;
11
+ const format = (code, ast, options) => {
12
+ const src = new _magicString.default(code);
13
+ const {
14
+ type = 'commonjs'
15
+ } = options;
16
+ traverse(ast, {
17
+ MetaProperty(metaPropertyPath) {
18
+ if (type === 'commonjs') {
19
+ const path = metaPropertyPath.findParent(path => path.isMemberExpression());
20
+ if (path) {
21
+ const {
22
+ node
23
+ } = path;
24
+ const {
25
+ start,
26
+ end
27
+ } = node;
28
+ if (node.type === 'MemberExpression' && node.property.type === 'Identifier' && typeof start == 'number' && typeof end === 'number') {
29
+ const name = node.property.name;
30
+ switch (name) {
31
+ case 'url':
32
+ src.update(start, end, 'require("node:url").pathToFileURL(__filename).toString()');
33
+ break;
34
+ case 'filename':
35
+ src.update(start, end, '__filename');
36
+ break;
37
+ case 'dirname':
38
+ src.update(start, end, '__dirname');
39
+ break;
40
+ case 'resolve':
41
+ src.update(start, end, 'require.resolve');
42
+ break;
43
+ }
44
+ }
45
+ }
46
+ }
47
+ },
48
+ ExpressionStatement(expressionStatementPath) {
49
+ if (type === 'module') {
50
+ const {
51
+ node
52
+ } = expressionStatementPath;
53
+ const {
54
+ start,
55
+ end
56
+ } = node;
57
+ if (node.expression.type === 'Identifier' && typeof start === 'number' && typeof end === 'number') {
58
+ const name = node.expression.name;
59
+ switch (name) {
60
+ case '__filename':
61
+ src.update(start, end, 'import.meta.filename');
62
+ break;
63
+ case '__dirname':
64
+ src.update(start, end, 'import.meta.dirname');
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ },
70
+ MemberExpression(memberExpressionPath) {
71
+ if (type === 'module') {
72
+ const {
73
+ node
74
+ } = memberExpressionPath;
75
+ const {
76
+ start,
77
+ end
78
+ } = node;
79
+
80
+ // Update require.resolve to import.meta.resolve
81
+ if (node.object.type === 'Identifier' && node.object.name === 'require' && node.property.type === 'Identifier' && node.property.name == 'resolve' && typeof start === 'number' && typeof end === 'number') {
82
+ src.update(start, end, 'import.meta.resolve');
83
+ }
84
+ }
85
+ }
86
+ });
87
+ return src;
88
+ };
89
+ exports.format = format;
@@ -0,0 +1,7 @@
1
+ import MagicString from 'magic-string';
2
+ import type { ParseResult } from '@babel/parser';
3
+ import type { File } from '@babel/types';
4
+ import type { ModuleOptions } from './types.cjs';
5
+ type FormatOptions = Omit<ModuleOptions, 'out'>;
6
+ export declare const format: (code: string, ast: ParseResult<File>, options: FormatOptions) => MagicString;
7
+ export {};
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.transform = void 0;
7
+ var _nodePath = require("node:path");
8
+ var _promises = require("node:fs/promises");
9
+ var _parse = require("./parse.cjs");
10
+ var _format = require("./format.cjs");
11
+ const defaultOptions = {
12
+ type: 'commonjs',
13
+ out: undefined,
14
+ moduleLoading: false,
15
+ specifiers: undefined
16
+ };
17
+
18
+ /**
19
+ * Transforms a file from one Node.js module system to another based on options.
20
+ * Module globals, for example import.meta, __dirname, __filename, etc. are always transformed.
21
+ * However, the CommonJS `module` object is not transformed, for instance `module.path`, `module.children`, etc.,
22
+ * with the exception of `module.exports` which is converted when `loading` is set to `true`.
23
+ */
24
+ const transform = async (filename, options = defaultOptions) => {
25
+ const opts = {
26
+ ...defaultOptions,
27
+ ...options
28
+ };
29
+ const file = (0, _nodePath.resolve)(filename);
30
+ const code = (await (0, _promises.readFile)(file)).toString();
31
+ const ast = (0, _parse.parse)(code);
32
+ return (0, _format.format)(code, ast, opts).toString();
33
+ };
34
+ exports.transform = transform;
@@ -0,0 +1,9 @@
1
+ import type { ModuleOptions } from './types.cjs';
2
+ /**
3
+ * Transforms a file from one Node.js module system to another based on options.
4
+ * Module globals, for example import.meta, __dirname, __filename, etc. are always transformed.
5
+ * However, the CommonJS `module` object is not transformed, for instance `module.path`, `module.children`, etc.,
6
+ * with the exception of `module.exports` which is converted when `loading` is set to `true`.
7
+ */
8
+ declare const transform: (filename: string, options?: ModuleOptions) => Promise<string>;
9
+ export { transform };
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parse = void 0;
7
+ var _parser = require("@babel/parser");
8
+ const parse = (source, dts = false) => {
9
+ const ast = (0, _parser.parse)(source, {
10
+ sourceType: 'module',
11
+ allowAwaitOutsideFunction: true,
12
+ allowReturnOutsideFunction: true,
13
+ allowImportExportEverywhere: true,
14
+ plugins: ['jsx', ['importAttributes', {
15
+ deprecatedAssertSyntax: true
16
+ }], ['typescript', {
17
+ dts
18
+ }]]
19
+ });
20
+ return ast;
21
+ };
22
+ exports.parse = parse;
@@ -0,0 +1,2 @@
1
+ declare const parse: (source: string, dts?: boolean) => import("@babel/parser").ParseResult<import("@babel/types").File>;
2
+ export { parse };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,6 @@
1
+ export type ModuleOptions = {
2
+ type?: 'module' | 'commonjs';
3
+ moduleLoading?: boolean;
4
+ specifiers?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
5
+ out?: string;
6
+ };
@@ -0,0 +1,7 @@
1
+ import MagicString from 'magic-string';
2
+ import type { ParseResult } from '@babel/parser';
3
+ import type { File } from '@babel/types';
4
+ import type { ModuleOptions } from './types.js';
5
+ type FormatOptions = Omit<ModuleOptions, 'out'>;
6
+ export declare const format: (code: string, ast: ParseResult<File>, options: FormatOptions) => MagicString;
7
+ export {};
package/dist/format.js ADDED
@@ -0,0 +1,81 @@
1
+ import MagicString from 'magic-string';
2
+ import _traverse from '@babel/traverse';
3
+ const traverse = _traverse.default;
4
+ export const format = (code, ast, options) => {
5
+ const src = new MagicString(code);
6
+ const {
7
+ type = 'commonjs'
8
+ } = options;
9
+ traverse(ast, {
10
+ MetaProperty(metaPropertyPath) {
11
+ if (type === 'commonjs') {
12
+ const path = metaPropertyPath.findParent(path => path.isMemberExpression());
13
+ if (path) {
14
+ const {
15
+ node
16
+ } = path;
17
+ const {
18
+ start,
19
+ end
20
+ } = node;
21
+ if (node.type === 'MemberExpression' && node.property.type === 'Identifier' && typeof start == 'number' && typeof end === 'number') {
22
+ const name = node.property.name;
23
+ switch (name) {
24
+ case 'url':
25
+ src.update(start, end, 'require("node:url").pathToFileURL(__filename).toString()');
26
+ break;
27
+ case 'filename':
28
+ src.update(start, end, '__filename');
29
+ break;
30
+ case 'dirname':
31
+ src.update(start, end, '__dirname');
32
+ break;
33
+ case 'resolve':
34
+ src.update(start, end, 'require.resolve');
35
+ break;
36
+ }
37
+ }
38
+ }
39
+ }
40
+ },
41
+ ExpressionStatement(expressionStatementPath) {
42
+ if (type === 'module') {
43
+ const {
44
+ node
45
+ } = expressionStatementPath;
46
+ const {
47
+ start,
48
+ end
49
+ } = node;
50
+ if (node.expression.type === 'Identifier' && typeof start === 'number' && typeof end === 'number') {
51
+ const name = node.expression.name;
52
+ switch (name) {
53
+ case '__filename':
54
+ src.update(start, end, 'import.meta.filename');
55
+ break;
56
+ case '__dirname':
57
+ src.update(start, end, 'import.meta.dirname');
58
+ break;
59
+ }
60
+ }
61
+ }
62
+ },
63
+ MemberExpression(memberExpressionPath) {
64
+ if (type === 'module') {
65
+ const {
66
+ node
67
+ } = memberExpressionPath;
68
+ const {
69
+ start,
70
+ end
71
+ } = node;
72
+
73
+ // Update require.resolve to import.meta.resolve
74
+ if (node.object.type === 'Identifier' && node.object.name === 'require' && node.property.type === 'Identifier' && node.property.name == 'resolve' && typeof start === 'number' && typeof end === 'number') {
75
+ src.update(start, end, 'import.meta.resolve');
76
+ }
77
+ }
78
+ }
79
+ });
80
+ return src;
81
+ };
@@ -0,0 +1,9 @@
1
+ import type { ModuleOptions } from './types.js';
2
+ /**
3
+ * Transforms a file from one Node.js module system to another based on options.
4
+ * Module globals, for example import.meta, __dirname, __filename, etc. are always transformed.
5
+ * However, the CommonJS `module` object is not transformed, for instance `module.path`, `module.children`, etc.,
6
+ * with the exception of `module.exports` which is converted when `loading` is set to `true`.
7
+ */
8
+ declare const transform: (filename: string, options?: ModuleOptions) => Promise<string>;
9
+ export { transform };
package/dist/module.js ADDED
@@ -0,0 +1,28 @@
1
+ import { resolve } from 'node:path';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { parse } from './parse.js';
4
+ import { format } from './format.js';
5
+ const defaultOptions = {
6
+ type: 'commonjs',
7
+ out: undefined,
8
+ moduleLoading: false,
9
+ specifiers: undefined
10
+ };
11
+
12
+ /**
13
+ * Transforms a file from one Node.js module system to another based on options.
14
+ * Module globals, for example import.meta, __dirname, __filename, etc. are always transformed.
15
+ * However, the CommonJS `module` object is not transformed, for instance `module.path`, `module.children`, etc.,
16
+ * with the exception of `module.exports` which is converted when `loading` is set to `true`.
17
+ */
18
+ const transform = async (filename, options = defaultOptions) => {
19
+ const opts = {
20
+ ...defaultOptions,
21
+ ...options
22
+ };
23
+ const file = resolve(filename);
24
+ const code = (await readFile(file)).toString();
25
+ const ast = parse(code);
26
+ return format(code, ast, opts).toString();
27
+ };
28
+ export { transform };
@@ -0,0 +1,2 @@
1
+ declare const parse: (source: string, dts?: boolean) => import("@babel/parser").ParseResult<import("@babel/types").File>;
2
+ export { parse };
package/dist/parse.js ADDED
@@ -0,0 +1,16 @@
1
+ import { parse as babelParse } from '@babel/parser';
2
+ const parse = (source, dts = false) => {
3
+ const ast = babelParse(source, {
4
+ sourceType: 'module',
5
+ allowAwaitOutsideFunction: true,
6
+ allowReturnOutsideFunction: true,
7
+ allowImportExportEverywhere: true,
8
+ plugins: ['jsx', ['importAttributes', {
9
+ deprecatedAssertSyntax: true
10
+ }], ['typescript', {
11
+ dts
12
+ }]]
13
+ });
14
+ return ast;
15
+ };
16
+ export { parse };
@@ -0,0 +1,6 @@
1
+ export type ModuleOptions = {
2
+ type?: 'module' | 'commonjs';
3
+ moduleLoading?: boolean;
4
+ specifiers?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
5
+ out?: string;
6
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@knighted/module",
3
+ "version": "1.0.0-alpha.0",
4
+ "description": "Converts module differences in source files between ES and CommonJS.",
5
+ "type": "module",
6
+ "main": "dist/module.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": {
10
+ "types": "./dist/types.d.ts",
11
+ "default": "./dist/module.js"
12
+ },
13
+ "require": {
14
+ "types": "./dist/cjs/types.d.cts",
15
+ "default": "./dist/cjs/module.cjs"
16
+ },
17
+ "default": "./dist/module.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "engines": {
22
+ "node": ">=20.11.0"
23
+ },
24
+ "engineStrict": true,
25
+ "scripts": {
26
+ "prettier": "prettier -w .",
27
+ "lint": "eslint --ignore-pattern dist .",
28
+ "test": "c8 --reporter=text --reporter=text-summary --reporter=lcov tsx --test --test-reporter=spec test/*.ts",
29
+ "build:types": "tsc --emitDeclarationOnly",
30
+ "build:dual": "babel-dual-package src --extensions .ts",
31
+ "build": "npm run build:types && npm run build:dual",
32
+ "prepack": "npm run build"
33
+ },
34
+ "keywords": [
35
+ "transform",
36
+ "es module",
37
+ "commonjs",
38
+ "module",
39
+ "require",
40
+ "import.meta",
41
+ "__dirname",
42
+ "__filename",
43
+ "conversion",
44
+ "tool",
45
+ "build"
46
+ ],
47
+ "files": [
48
+ "dist"
49
+ ],
50
+ "author": "KCM <knightedcodemonkey@gmail.com>",
51
+ "license": "MIT",
52
+ "devDependencies": {
53
+ "@babel/preset-env": "^7.24.6",
54
+ "@babel/preset-typescript": "^7.24.6",
55
+ "@babel/types": "^7.24.6",
56
+ "@eslint/js": "^9.3.0",
57
+ "@types/babel__traverse": "^7.20.6",
58
+ "@types/node": "^20.12.12",
59
+ "babel-dual-package": "^1.1.3",
60
+ "c8": "^9.1.0",
61
+ "eslint": "^9.3.0",
62
+ "eslint-plugin-n": "^17.7.0",
63
+ "prettier": "^3.2.5",
64
+ "tsx": "^4.11.0",
65
+ "typescript": "^5.4.5",
66
+ "typescript-eslint": "^8.0.0-alpha.16"
67
+ },
68
+ "dependencies": {
69
+ "@babel/parser": "^7.24.6",
70
+ "@babel/traverse": "^7.24.6",
71
+ "magic-string": "^0.30.10"
72
+ },
73
+ "prettier": {
74
+ "arrowParens": "avoid",
75
+ "printWidth": 119,
76
+ "semi": false,
77
+ "singleQuote": true
78
+ }
79
+ }