From 6039a603ec6ee4b11bc37dec4ce074e810bd2c2c Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 7 Jul 2024 13:43:58 +0800 Subject: [PATCH] feat: support to use import.meta.url on commonjs --- .github/workflows/nodejs.yml | 2 +- .github/workflows/pkg.pr.new.yml | 23 +++++++++++++++++++++++ .github/workflows/release.yml | 2 -- README.md | 23 +++++++++++++++++++++++ package.json | 7 ++++++- src/index.ts | 26 ++++++++++++++++++++++++-- test/fixtures/demo/src/index.ts | 10 ++++++++++ test/fixtures/demo/src/tsconfig.json | 16 ++++++++++++++++ test/fixtures/demo/tsconfig.json | 18 ++++++++++++++++++ test/index.test.ts | 11 +++++++++++ 10 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/pkg.pr.new.yml create mode 100644 test/fixtures/demo/src/index.ts create mode 100644 test/fixtures/demo/src/tsconfig.json create mode 100644 test/fixtures/demo/tsconfig.json diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 58f05ed..1b3ace9 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,4 +13,4 @@ jobs: uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest' - version: '16, 18, 20' + version: '16, 18, 20, 22' diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml new file mode 100644 index 0000000..bac3fac --- /dev/null +++ b/.github/workflows/pkg.pr.new.yml @@ -0,0 +1,23 @@ +name: Publish Any Commit +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npm install + + - name: Build + run: npm run prepublishOnly --if-present + + - run: npx pkg-pr-new publish diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a4e1158..1c6cbb1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,5 +11,3 @@ jobs: secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - with: - checkTest: false diff --git a/README.md b/README.md index af3de92..7a94dee 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,26 @@ Auto set package.json after [tshy](https://github.com/isaacs/tshy) run ## keep `types` Set `package.types` to `package.exports['.'].require.types` + +## Auto fix `import.meta.url` SyntaxError on CJS + +```bash +SyntaxError: Cannot use 'import.meta' outside a module +``` + +e.g.: Get the file's dirname + +```ts +// src/index.ts + +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; + +export function getDirname() { + if (typeof __dirname !== 'undefined') { + return __dirname; + } + // @ts-ignore + return path.dirname(fileURLToPath(import.meta.url)); +} +``` diff --git a/package.json b/package.json index 210342d..1db35fd 100644 --- a/package.json +++ b/package.json @@ -43,13 +43,18 @@ "./package.json": "./package.json", ".": { "import": { + "source": "./src/index.ts", "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.js" }, "require": { + "source": "./src/index.ts", "types": "./dist/commonjs/index.d.ts", "default": "./dist/commonjs/index.js" } } - } + }, + "main": "./dist/commonjs/index.js", + "types": "./dist/commonjs/index.d.ts", + "module": "./dist/esm/index.js" } diff --git a/src/index.ts b/src/index.ts index 6beca80..a85ba2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,31 @@ #!/usr/bin/env node -import { writeFileSync, readFileSync } from 'node:fs' +import { writeFileSync, readFileSync, readdirSync, statSync } from 'node:fs' +import path from 'node:path' -const pkg = JSON.parse(readFileSync('package.json', 'utf-8')); +const cwd = process.cwd(); + +const pkg = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf-8')); // make sure commonjs *.d.ts can import types pkg.types = pkg.exports['.'].require.types; writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n') + +// Replace all `import.meta.url` into 'import.meta.url' string placeholder on commonjs. +function replaceImportMetaUrl(baseDir: string) { + const names = readdirSync(baseDir); + for (const name of names) { + const filepath = path.join(baseDir, name); + const stat = statSync(filepath); + if (stat.isDirectory()) { + replaceImportMetaUrl(filepath); + continue; + } + if (!filepath.endsWith('.js')) { + continue; + } + writeFileSync(filepath, readFileSync(filepath, 'utf-8').replaceAll('import.meta.url', '"import_meta_url_placeholder_by_tshy_after"')); + console.log('Auto fix "import.meta.url" on %s', filepath); + } +} +replaceImportMetaUrl(path.join(cwd, 'dist/commonjs')); diff --git a/test/fixtures/demo/src/index.ts b/test/fixtures/demo/src/index.ts new file mode 100644 index 0000000..83fe20a --- /dev/null +++ b/test/fixtures/demo/src/index.ts @@ -0,0 +1,10 @@ +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; + +export function getDirname() { + if (typeof __dirname !== 'undefined') { + return __dirname; + } + // @ts-ignore + return path.dirname(fileURLToPath(import.meta.url)); +} diff --git a/test/fixtures/demo/src/tsconfig.json b/test/fixtures/demo/src/tsconfig.json new file mode 100644 index 0000000..b02cfc2 --- /dev/null +++ b/test/fixtures/demo/src/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "inlineSources": true, + "jsx": "react", + "module": "nodenext", + "moduleResolution": "nodenext", + "noUncheckedIndexedAccess": true, + "resolveJsonModule": true, + "skipLibCheck": false, + "sourceMap": true, + "strict": true, + "target": "es2022" + } +} diff --git a/test/fixtures/demo/tsconfig.json b/test/fixtures/demo/tsconfig.json new file mode 100644 index 0000000..7f39495 --- /dev/null +++ b/test/fixtures/demo/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "inlineSources": true, + "jsx": "react", + "module": "nodenext", + "moduleResolution": "nodenext", + "noUncheckedIndexedAccess": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "sourceMap": true, + "strict": true, + "target": "es2022" + } +} diff --git a/test/index.test.ts b/test/index.test.ts index d8acf7c..c156b3d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -20,10 +20,21 @@ describe('test/index.test.ts', () => { }); it('should work', async () => { + await coffee.spawn('tshy', { cwd }) + .debug() + .expect('code', 0) + .end(); await coffee.fork(bin, { cwd }) .debug() .expect('code', 0) .end(); + await coffee.spawn('node', [ + '--require', './dist/commonjs/index.js', + '-p', '123123', + ], { cwd }) + .debug() + .expect('code', 0) + .end(); const pkg = JSON.parse(fs.readFileSync(packageFile, 'utf-8')); assert.equal(pkg.types, './dist/commonjs/index.d.ts'); });