-
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.
feat: support to use import.meta.url on commonjs (#1)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new GitHub Actions workflow to publish on any commit or pull request. - Added a function to handle `import.meta.url` in CommonJS modules. - **Improvements** - Updated Node.js versions in workflows to include v22. - Enhanced `package.json` with updated paths for imports, exports, and types. - Improved file system operations and path handling within the project. - Updated TypeScript configurations for better code consistency and module handling. - **Bug Fixes** - Addressed a `SyntaxError` related to `import.meta.url` in CommonJS modules. - **Documentation** - Updated README with new instructions for auto-setting `package.json`. - **Tests** - Expanded test scenarios with additional asynchronous calls and configurations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
10 changed files
with
132 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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 |
---|---|---|
|
@@ -11,5 +11,3 @@ jobs: | |
secrets: | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
GIT_TOKEN: ${{ secrets.GIT_TOKEN }} | ||
with: | ||
checkTest: false |
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
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,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')); |
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 { 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)); | ||
} |
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,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" | ||
} | ||
} |
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,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" | ||
} | ||
} |
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