From ddd4836fcdafe994498d2bfcfd4370f88dd31144 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 24 Dec 2024 20:48:57 +0800 Subject: [PATCH] feat: auto fix import.meta.resolve SyntaxError on CJS --- .github/workflows/nodejs.yml | 2 ++ .gitignore | 2 ++ README.md | 3 ++- src/index.ts | 24 +++++++++++++++++++----- test/fixtures/demo/src/index.ts | 8 ++++++++ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 625e294..49972d9 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,3 +13,5 @@ jobs: with: os: 'ubuntu-latest, windows-latest' version: '16, 18, 20, 22, 23' + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore index 7017bef..90ebfe6 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,5 @@ dist .pnp.* .tshy +package-lock.json +.package-lock.json diff --git a/README.md b/README.md index 1b50018..3cb1932 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/index.ts b/src/index.ts index 0161bf9..ba50825 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) { @@ -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')); diff --git a/test/fixtures/demo/src/index.ts b/test/fixtures/demo/src/index.ts index 83fe20a..10364af 100644 --- a/test/fixtures/demo/src/index.ts +++ b/test/fixtures/demo/src/index.ts @@ -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); +}