Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto fix import.meta.resolve SyntaxError on CJS #4

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading