@knighted/module 1.1.1 → 1.2.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/README.md CHANGED
@@ -135,6 +135,7 @@ type ModuleOptions = {
135
135
  idiomaticExports?: 'off' | 'safe' | 'aggressive'
136
136
  topLevelAwait?: 'error' | 'wrap' | 'preserve'
137
137
  out?: string
138
+ cwd?: string
138
139
  inPlace?: boolean
139
140
  }
140
141
  ```
@@ -159,7 +160,7 @@ type ModuleOptions = {
159
160
  - `cjsDefault` (`auto`): bundler-style default interop vs direct `module.exports`.
160
161
  - `idiomaticExports` (`safe`): when raising CJS to ESM, attempt to synthesize `export` statements directly when it is safe. `off` always uses the helper bag; `aggressive` currently matches `safe` heuristics.
161
162
  - `out`/`inPlace`: write the transformed code to a file; otherwise the function returns the transformed string only.
162
- - CommonJS ESM lowering will throw on `with` statements and unshadowed `eval` calls to avoid unsound rewrites.
163
+ - `cwd` (`process.cwd()`): Base directory used to resolve relative `out` paths.
163
164
 
164
165
  > [!NOTE]
165
166
  > Package-level metadata (`package.json` updates such as setting `"type": "module"` or authoring `exports`) is not edited by this tool today; plan that change outside the per-file transform.
@@ -163,6 +163,7 @@ const defaultOptions = {
163
163
  idiomaticExports: 'safe',
164
164
  importMetaPrelude: 'auto',
165
165
  topLevelAwait: 'error',
166
+ cwd: undefined,
166
167
  out: undefined,
167
168
  inPlace: false
168
169
  };
@@ -172,10 +173,11 @@ const transform = async (filename, options = defaultOptions) => {
172
173
  ...options,
173
174
  filePath: filename
174
175
  };
176
+ const cwdBase = opts.cwd ? (0, _nodePath.resolve)(opts.cwd) : process.cwd();
175
177
  const appendMode = options?.appendJsExtension ?? (opts.target === 'module' ? 'relative-only' : 'off');
176
178
  const dirIndex = opts.appendDirectoryIndex === undefined ? 'index.js' : opts.appendDirectoryIndex;
177
179
  const detectCycles = opts.detectCircularRequires ?? 'off';
178
- const file = (0, _nodePath.resolve)(filename);
180
+ const file = (0, _nodePath.resolve)(cwdBase, filename);
179
181
  const code = (await (0, _promises.readFile)(file)).toString();
180
182
  const ast = (0, _parse.parse)(filename, code);
181
183
  let source = await (0, _format.format)(code, ast, opts);
@@ -192,7 +194,7 @@ const transform = async (filename, options = defaultOptions) => {
192
194
  if (detectCycles !== 'off' && opts.target === 'module' && opts.transformSyntax) {
193
195
  await detectCircularRequireGraph(file, detectCycles, dirIndex || 'index.js');
194
196
  }
195
- const outputPath = opts.inPlace ? file : opts.out ? (0, _nodePath.resolve)(opts.out) : undefined;
197
+ const outputPath = opts.inPlace ? file : opts.out ? (0, _nodePath.resolve)(cwdBase, opts.out) : undefined;
196
198
  if (outputPath) {
197
199
  await (0, _promises.writeFile)(outputPath, source);
198
200
  }
@@ -48,6 +48,8 @@ export type ModuleOptions = {
48
48
  diagnostics?: (diag: Diagnostic) => void;
49
49
  /** Optional source file path used for diagnostics context. */
50
50
  filePath?: string;
51
+ /** Base directory used to resolve relative `out` paths; defaults to process.cwd(). */
52
+ cwd?: string;
51
53
  /** Output directory or file path when writing. */
52
54
  out?: string;
53
55
  /** Overwrite input files instead of writing to out. */
package/dist/module.js CHANGED
@@ -160,6 +160,7 @@ const defaultOptions = {
160
160
  idiomaticExports: 'safe',
161
161
  importMetaPrelude: 'auto',
162
162
  topLevelAwait: 'error',
163
+ cwd: undefined,
163
164
  out: undefined,
164
165
  inPlace: false
165
166
  };
@@ -169,10 +170,11 @@ const transform = async (filename, options = defaultOptions) => {
169
170
  ...options,
170
171
  filePath: filename
171
172
  };
173
+ const cwdBase = opts.cwd ? resolve(opts.cwd) : process.cwd();
172
174
  const appendMode = options?.appendJsExtension ?? (opts.target === 'module' ? 'relative-only' : 'off');
173
175
  const dirIndex = opts.appendDirectoryIndex === undefined ? 'index.js' : opts.appendDirectoryIndex;
174
176
  const detectCycles = opts.detectCircularRequires ?? 'off';
175
- const file = resolve(filename);
177
+ const file = resolve(cwdBase, filename);
176
178
  const code = (await readFile(file)).toString();
177
179
  const ast = parse(filename, code);
178
180
  let source = await format(code, ast, opts);
@@ -189,7 +191,7 @@ const transform = async (filename, options = defaultOptions) => {
189
191
  if (detectCycles !== 'off' && opts.target === 'module' && opts.transformSyntax) {
190
192
  await detectCircularRequireGraph(file, detectCycles, dirIndex || 'index.js');
191
193
  }
192
- const outputPath = opts.inPlace ? file : opts.out ? resolve(opts.out) : undefined;
194
+ const outputPath = opts.inPlace ? file : opts.out ? resolve(cwdBase, opts.out) : undefined;
193
195
  if (outputPath) {
194
196
  await writeFile(outputPath, source);
195
197
  }
package/dist/types.d.ts CHANGED
@@ -48,6 +48,8 @@ export type ModuleOptions = {
48
48
  diagnostics?: (diag: Diagnostic) => void;
49
49
  /** Optional source file path used for diagnostics context. */
50
50
  filePath?: string;
51
+ /** Base directory used to resolve relative `out` paths; defaults to process.cwd(). */
52
+ cwd?: string;
51
53
  /** Output directory or file path when writing. */
52
54
  out?: string;
53
55
  /** Overwrite input files instead of writing to out. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knighted/module",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Bidirectional transform for ES modules and CommonJS.",
5
5
  "type": "module",
6
6
  "main": "dist/module.js",
@@ -27,7 +27,7 @@
27
27
  "prettier": "prettier -w .",
28
28
  "prettier:check": "prettier -c .",
29
29
  "lint": "oxlint --config oxlint.json .",
30
- "prepare": "husky install",
30
+ "prepare": "husky",
31
31
  "test": "c8 --reporter=text --reporter=text-summary --reporter=lcov tsx --test --test-reporter=spec test/*.ts",
32
32
  "build:types": "tsc --emitDeclarationOnly",
33
33
  "build:dual": "babel-dual-package src --extensions .ts",
@@ -72,7 +72,6 @@
72
72
  },
73
73
  "dependencies": {
74
74
  "magic-string": "^0.30.21",
75
- "node-module-type": "^1.0.4",
76
75
  "oxc-parser": "^0.105.0",
77
76
  "periscopic": "^4.0.2"
78
77
  },