-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: renamed option specifiers to specifier.
- Loading branch information
1 parent
077f985
commit a034fd6
Showing
11 changed files
with
189 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,48 @@ | ||
import { resolve } from 'node:path' | ||
import { readFile } from 'node:fs/promises' | ||
import { readFile, writeFile } from 'node:fs/promises' | ||
|
||
import { specifier } from '@knighted/specifier' | ||
|
||
import { parse } from './parse.js' | ||
import { format } from './format.js' | ||
import type { ModuleOptions } from './types.js' | ||
|
||
/** | ||
* Defaults to only transforming ES module globals to CommonJS. | ||
*/ | ||
const defaultOptions = { | ||
type: 'commonjs', | ||
out: undefined, | ||
modules: false, | ||
specifiers: undefined, | ||
specifier: undefined, | ||
} satisfies ModuleOptions | ||
|
||
const transform = async (filename: string, options: ModuleOptions = defaultOptions) => { | ||
const opts = { ...defaultOptions, ...options } | ||
const file = resolve(filename) | ||
const code = (await readFile(file)).toString() | ||
const ast = parse(code) | ||
let source = format(code, ast, opts).toString() | ||
|
||
if (options.specifier) { | ||
const { code, error } = await specifier.updateSrc(source, ({ value }) => { | ||
// Collapse any BinaryExpression or NewExpression to test for a relative specifier | ||
const collapsed = value.replace(/['"`+)\s]|new String\(/g, '') | ||
const relative = /^(?:\.|\.\.)\// | ||
|
||
if (relative.test(collapsed)) { | ||
// $2 is for any closing quotation/parens around BE or NE | ||
return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"`]*)?$/, `$1${options.specifier}$2`) | ||
} | ||
}) | ||
|
||
if (code && !error) { | ||
source = code | ||
} | ||
} | ||
|
||
if (opts.out) { | ||
await writeFile(resolve(opts.out), source) | ||
} | ||
|
||
// TODO: Support `out` option. | ||
return format(code, ast, opts).toString() | ||
return source | ||
} | ||
|
||
export { transform } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
// CommonJS was like the Wild Wild West (require.extensions is deprecated so meh). | ||
// Too much implied behavior without requiring explicitness. | ||
|
||
const { foo } = require('./test.js') | ||
const { esmodule } = require('./file.mjs') | ||
|
||
__filename | ||
__dirname | ||
require.main | ||
require.cache | ||
require.resolve('./test.js') | ||
require.resolve('./file.mjs') | ||
|
||
module | ||
module.exports | ||
module.exports = foo | ||
|
||
exports | ||
exports.commonjs = true | ||
exports.esmodule = esmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('./file.cjs') | ||
require.resolve('./file.cjs') | ||
import('./file.cjs') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import foo from './file.mts' | ||
import bar from './file.cts' | ||
import baz from './file.ts' | ||
|
||
import(`./${foo}${bar}${baz}.cjs`) | ||
import(new String('not-relative.js')) | ||
import(new String('./file.mjs')) | ||
|
||
import.meta.resolve('./file.cjs') | ||
import.meta.resolve('./file.js') |
Oops, something went wrong.