Skip to content

Commit

Permalink
feat: auto fix import.meta.resolve SyntaxError on CJS
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 24, 2024
1 parent 047ce04 commit ddd4836
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ jobs:
with:
os: 'ubuntu-latest, windows-latest'
version: '16, 18, 20, 22, 23'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ dist
.pnp.*

.tshy
package-lock.json
.package-lock.json
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Node.js CI](https://github.com/node-modules/tshy-after/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/tshy-after/actions/workflows/nodejs.yml)
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/tshy-after.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/tshy-after.svg?style=flat-square
[npm-url]: https://npmjs.org/package/tshy-after
Expand All @@ -16,7 +17,7 @@ Auto set package.json after [tshy](https://github.com/isaacs/tshy) run

Set `package.types` to `package.exports['.'].require.types`

## Auto fix `import.meta.url` SyntaxError on CJS
## Auto fix `import.meta.url` and `import.meta.resolve` SyntaxError on CJS

```bash
SyntaxError: Cannot use 'import.meta' outside a module
Expand Down
24 changes: 19 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ writeFileSync('dist/package.json', JSON.stringify({
const IMPORT_META_URL = '(' + 'import.meta.url' + ')';
const IMPORT_META_URL_PLACE_HOLDER = '(\'import_meta_url_placeholder_by_tshy_after\')';

// Replace all `import.meta.resolve (xxx)` into `require.resolve(xxx)` on commonjs.
const IMPORT_META_RESOLVE = 'import.meta.resolve' + '(';
const IMPORT_META_RESOLVE_PLACE_HOLDER = 'require.resolve(';

function replaceImportMetaUrl(baseDir: string) {
const names = readdirSync(baseDir);
for (const name of names) {
Expand All @@ -31,12 +35,22 @@ function replaceImportMetaUrl(baseDir: string) {
if (!filepath.endsWith('.js')) {
continue;
}
const content = readFileSync(filepath, 'utf-8');
if (!content.includes(IMPORT_META_URL)) {
continue;

let content = readFileSync(filepath, 'utf-8');
let changed = false;
if (content.includes(IMPORT_META_URL)) {
changed = true;
content = content.replaceAll(IMPORT_META_URL, IMPORT_META_URL_PLACE_HOLDER);
console.log('Auto fix "import.meta.url" on %s', filepath.replace(cwd, ''));
}
if (content.includes(IMPORT_META_RESOLVE)) {
changed = true;
content = content.replaceAll(IMPORT_META_RESOLVE, IMPORT_META_RESOLVE_PLACE_HOLDER);
console.log('Auto fix "import.meta.resolve" on %s', filepath.replace(cwd, ''));
}
if (changed) {
writeFileSync(filepath, content);
}
writeFileSync(filepath, content.replaceAll(IMPORT_META_URL, IMPORT_META_URL_PLACE_HOLDER));
console.log('Auto fix "import.meta.url" on %s', filepath.replace(cwd, ''));
}
}
replaceImportMetaUrl(path.join(cwd, 'dist/commonjs'));
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/demo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ export function getDirname() {
// @ts-ignore
return path.dirname(fileURLToPath(import.meta.url));
}

export function resolve(filename: string) {
if (typeof require === 'function') {
return require.resolve(filename);
}
// @ts-ignore
return import.meta.resolve(filename);
}

0 comments on commit ddd4836

Please sign in to comment.