From c3bf57bbb1b983e454f5df664c4ce555e22ffaa9 Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 11 Nov 2024 14:38:43 +0800 Subject: [PATCH] refactor(build): switch to Rslib --- client/package.json | 3 - index.cjs | 10 +++ jest.config.js => jest.config.cjs | 0 package.json | 23 +++---- pnpm-lock.yaml | 104 ++++++++++++++++++++++++++++++ rslib.config.ts | 11 ++++ src/index.ts | 7 +- test/package.json | 3 + tsconfig.build.json | 7 -- tsconfig.json | 21 +++--- 10 files changed, 153 insertions(+), 36 deletions(-) delete mode 100644 client/package.json create mode 100644 index.cjs rename jest.config.js => jest.config.cjs (100%) create mode 100644 rslib.config.ts create mode 100644 test/package.json delete mode 100644 tsconfig.build.json diff --git a/client/package.json b/client/package.json deleted file mode 100644 index 3dbc1ca..0000000 --- a/client/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "type": "module" -} diff --git a/index.cjs b/index.cjs new file mode 100644 index 0000000..b4ba297 --- /dev/null +++ b/index.cjs @@ -0,0 +1,10 @@ +/** + * Compatible with CommonJS + * ```js + * const { PreactRefreshRspackPlugin } = require('@rspack/plugin-preact-refresh'); + * ``` + */ + +const { PreactRefreshRspackPlugin } = require('./dist/index.cjs'); + +module.exports = PreactRefreshRspackPlugin; diff --git a/jest.config.js b/jest.config.cjs similarity index 100% rename from jest.config.js rename to jest.config.cjs diff --git a/package.json b/package.json index f5dbf23..5855be5 100644 --- a/package.json +++ b/package.json @@ -3,20 +3,23 @@ "version": "1.1.1", "repository": "https://github.com/rspack-contrib/rspack-plugin-preact-refresh", "license": "MIT", - "description": "Preact refresh plugin for rspack", - "main": "dist/index.js", + "description": "Preact refresh plugin for Rspack", + "type": "module", + "main": "index.cjs", "types": "dist/index.d.ts", "exports": { ".": { - "default": "./dist/index.js" + "types": "./dist/index.d.ts", + "import": "./dist/index.js", + "require": "./index.cjs" }, "./intercept": "./client/reactRefresh.js", "./prefresh": "./client/prefresh.js", "./package.json": "./package.json" }, "scripts": { - "build": "tsc -b ./tsconfig.build.json", - "dev": "tsc -b -w", + "build": "rslib build", + "dev": "rslib build --watch", "lint": "biome check .", "lint:write": "biome check . --write", "prepare": "simple-git-hooks && npm run build", @@ -24,10 +27,7 @@ "testu": "cross-env RSPACK_HOT_TEST=true jest --colors --updateSnapshot", "release": "node ./scripts/release.mjs" }, - "files": [ - "client", - "dist" - ], + "files": ["client", "dist", "index.cjs"], "simple-git-hooks": { "pre-commit": "npx nano-staged" }, @@ -40,12 +40,13 @@ "@biomejs/biome": "^1.9.4", "@prefresh/core": "^1.5.2", "@prefresh/utils": "^1.2.0", + "@rslib/core": "^0.0.16", "@rspack/core": "1.0.14", "@rspack/test-tools": "1.0.14", - "@swc/plugin-prefresh": "^2.0.10", "@swc/helpers": "0.5.13", - "@types/node": "^22.8.6", + "@swc/plugin-prefresh": "^2.0.10", "@types/jest": "29.5.14", + "@types/node": "^22.8.6", "cross-env": "^7.0.3", "execa": "9.5.1", "fs-extra": "11.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5d5d3e..7273fbe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@prefresh/utils': specifier: ^1.2.0 version: 1.2.0 + '@rslib/core': + specifier: ^0.0.16 + version: 0.0.16(typescript@5.6.3) '@rspack/core': specifier: 1.0.14 version: 1.0.14(@swc/helpers@0.5.13) @@ -446,6 +449,24 @@ packages: '@prefresh/utils@1.2.0': resolution: {integrity: sha512-KtC/fZw+oqtwOLUFM9UtiitB0JsVX0zLKNyRTA332sqREqSALIIQQxdUCS1P3xR/jT1e2e8/5rwH6gdcMLEmsQ==} + '@rsbuild/core@1.0.19': + resolution: {integrity: sha512-63DAPvYfRBoUrb51BUPb4Xoqx48MHQ0yLcmnCiqZGpMeKYtTWzD+lyx5va4cr9qvdnIFTAX2BMuYC/j5iSrtTA==} + engines: {node: '>=16.7.0'} + hasBin: true + + '@rslib/core@0.0.16': + resolution: {integrity: sha512-g6IFUZW0K7DDKK/27kF7DOKhP5JIqZoIVAU0c9DZffRE3Pw9sWTyEyVN/kaEnp5k3gaeQTBpcV9nSW+41B2slQ==} + engines: {node: '>=16.0.0'} + hasBin: true + peerDependencies: + '@microsoft/api-extractor': ^7 + typescript: ^5 + peerDependenciesMeta: + '@microsoft/api-extractor': + optional: true + typescript: + optional: true + '@rspack/binding-darwin-arm64@1.0.14': resolution: {integrity: sha512-dHvlF6T6ctThGDIdvkSdacroA1xlCxfteuppBj8BX/UxzLPr4xsaEtNilfJmFfd2/J02UQyTQauN/9EBuA+YkA==} cpu: [arm64] @@ -828,6 +849,9 @@ packages: convert-source-map@2.0.0: resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==} + core-js@3.38.1: + resolution: {integrity: sha512-OP35aUorbU3Zvlx7pjsFdu1rGNnD4pgw/CWoYzRY3t2EzoVT7shKHY1dlAy3f41cGIO7ZDPQimhGFTlEYkG/Hw==} + create-jest@29.7.0: resolution: {integrity: sha512-Adz2bdH0Vq3F53KEMJOoftQFutWCukm6J24wbPWRO4k1kMY7gS7ds/uoJkNuV8wDCtWWnuwGcJwpWcih+zEW1Q==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -987,6 +1011,14 @@ packages: fb-watchman@2.0.2: resolution: {integrity: sha512-p5161BqbuCaSnB8jIbzQHOlpgsPmK5rJVDfDKO91Axs5NC1uu3HRQm6wt9cd9/+GtQQIO53JdGXXoyDpTAsgYA==} + fdir@6.4.2: + resolution: {integrity: sha512-KnhMXsKSPZlAhp7+IjUkRZKPb4fUyccpDrdFXbi4QL1qkmFh9kVY09Yox+n4MaOb3lHZ1Tv829C3oaaXoMYPDQ==} + peerDependencies: + picomatch: ^3 || ^4 + peerDependenciesMeta: + picomatch: + optional: true + figures@6.1.0: resolution: {integrity: sha512-d+l3qxjSesT4V7v2fh+QnmFnUWv9lSpjarhShNTgBOfA0ttejbQUAlHLitbjkoRiDulW0OPoQPYIGhIC8ohejg==} engines: {node: '>=18'} @@ -1417,6 +1449,9 @@ packages: lru-cache@5.1.1: resolution: {integrity: sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==} + magic-string@0.30.12: + resolution: {integrity: sha512-Ea8I3sQMVXr8JhN4z+H/d8zwo+tYDgHE9+5G4Wnrwhs0gaK9fXTKx0Tw5Xwsd/bCPTTZNRAdpyzvoeORe9LYpw==} + make-dir@4.0.0: resolution: {integrity: sha512-hXdUTZYIVOt1Ex//jAQi+wTZZpUpwBj/0QsOzqegb3rGMMeJiSEu5xLHnYfBrRV4RH2+OCSOO95Is/7x1WJ4bw==} engines: {node: '>=10'} @@ -1573,10 +1608,17 @@ packages: picocolors@1.0.1: resolution: {integrity: sha512-anP1Z8qwhkbmu7MFP5iTt+wQKXgwzf7zTyGlcdzabySa9vd0Xt392U0rVmz9poOaBj0uHJKyyo9/upk0HrEQew==} + picocolors@1.1.1: + resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} + picomatch@2.3.1: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} engines: {node: '>=8.6'} + picomatch@4.0.2: + resolution: {integrity: sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==} + engines: {node: '>=12'} + pirates@4.0.6: resolution: {integrity: sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==} engines: {node: '>= 6'} @@ -1641,6 +1683,19 @@ packages: rrweb-cssom@0.7.1: resolution: {integrity: sha512-TrEMa7JGdVm0UThDJSx7ddw5nVm3UJS9o9CCIZ72B1vSyEZoziDqBYP3XIoi/12lKrJR8rE3jeFHMok2F/Mnsg==} + rsbuild-plugin-dts@0.0.16: + resolution: {integrity: sha512-Yfc7h1cTTp55baLgS6DVa3mlLaFlNfoUVmcX7hCH1BT1fNbMabnngvXDtfqJpP62QyXcfrXNmcW0fznTYKgKmA==} + engines: {node: '>=16.0.0'} + peerDependencies: + '@microsoft/api-extractor': ^7 + '@rsbuild/core': 1.x + typescript: ^5 + peerDependenciesMeta: + '@microsoft/api-extractor': + optional: true + typescript: + optional: true + safe-buffer@5.2.1: resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==} @@ -1806,6 +1861,10 @@ packages: resolution: {integrity: sha512-cAGWPIyOHU6zlmg88jwm7VRyXnMN7iV68OGAbYDk/Mh/xC/pzVPlQtY6ngoIH/5/tciuhGfvESU8GrHrcxD56w==} engines: {node: '>=8'} + tinyglobby@0.2.10: + resolution: {integrity: sha512-Zc+8eJlFMvgatPZTl6A9L/yht8QqdmUNtURHaKZLmKBE12hNPSrqNkUp2cs3M/UKmNVVAMFQYSjYIVHDjW5zew==} + engines: {node: '>=12.0.0'} + tldts-core@6.1.54: resolution: {integrity: sha512-5cc42+0G0EjYRDfIJHKraaT3I5kPm7j6or3Zh1T9sF+Ftj1T+isT4thicUyQQ1bwN7/xjHQIuY2fXCoXP8Haqg==} @@ -2529,6 +2588,23 @@ snapshots: '@prefresh/utils@1.2.0': {} + '@rsbuild/core@1.0.19': + dependencies: + '@rspack/core': 1.0.14(@swc/helpers@0.5.13) + '@rspack/lite-tapable': 1.0.1 + '@swc/helpers': 0.5.13 + core-js: 3.38.1 + optionalDependencies: + fsevents: 2.3.3 + + '@rslib/core@0.0.16(typescript@5.6.3)': + dependencies: + '@rsbuild/core': 1.0.19 + rsbuild-plugin-dts: 0.0.16(@rsbuild/core@1.0.19)(typescript@5.6.3) + tinyglobby: 0.2.10 + optionalDependencies: + typescript: 5.6.3 + '@rspack/binding-darwin-arm64@1.0.14': optional: true @@ -2981,6 +3057,8 @@ snapshots: convert-source-map@2.0.0: {} + core-js@3.38.1: {} + create-jest@29.7.0(@types/node@22.8.6): dependencies: '@jest/types': 29.6.3 @@ -3128,6 +3206,10 @@ snapshots: dependencies: bser: 2.1.1 + fdir@6.4.2(picomatch@4.0.2): + optionalDependencies: + picomatch: 4.0.2 + figures@6.1.0: dependencies: is-unicode-supported: 2.0.0 @@ -3743,6 +3825,10 @@ snapshots: dependencies: yallist: 3.1.1 + magic-string@0.30.12: + dependencies: + '@jridgewell/sourcemap-codec': 1.5.0 + make-dir@4.0.0: dependencies: semver: 7.6.3 @@ -3875,8 +3961,12 @@ snapshots: picocolors@1.0.1: {} + picocolors@1.1.1: {} + picomatch@2.3.1: {} + picomatch@4.0.2: {} + pirates@4.0.6: {} pkg-dir@4.2.0: @@ -3932,6 +4022,15 @@ snapshots: rrweb-cssom@0.7.1: {} + rsbuild-plugin-dts@0.0.16(@rsbuild/core@1.0.19)(typescript@5.6.3): + dependencies: + '@rsbuild/core': 1.0.19 + magic-string: 0.30.12 + picocolors: 1.1.1 + tinyglobby: 0.2.10 + optionalDependencies: + typescript: 5.6.3 + safe-buffer@5.2.1: {} safer-buffer@2.1.2: {} @@ -4071,6 +4170,11 @@ snapshots: glob: 7.2.3 minimatch: 3.1.2 + tinyglobby@0.2.10: + dependencies: + fdir: 6.4.2(picomatch@4.0.2) + picomatch: 4.0.2 + tldts-core@6.1.54: {} tldts@6.1.54: diff --git a/rslib.config.ts b/rslib.config.ts new file mode 100644 index 0000000..c451f7e --- /dev/null +++ b/rslib.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from '@rslib/core'; + +export default defineConfig({ + lib: [ + { format: 'esm', syntax: 'es2021', dts: true }, + { format: 'cjs', syntax: 'es2021' }, + ], + output: { + target: 'node', + }, +}); diff --git a/src/index.ts b/src/index.ts index 6eb5476..b09fb07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import { createRequire } from 'node:module'; import { dirname } from 'node:path'; import type { Compiler, @@ -6,6 +7,8 @@ import type { RuleSetCondition, } from '@rspack/core'; +const require = createRequire(import.meta.url); + export interface IPreactRefreshRspackPluginOptions { /** * Include files to be processed by the plugin. @@ -133,5 +136,5 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance { } } -// @ts-expect-error output module.exports -export = PreactRefreshRspackPlugin; +export { PreactRefreshRspackPlugin }; +export default PreactRefreshRspackPlugin; diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/test/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/tsconfig.build.json b/tsconfig.build.json deleted file mode 100644 index 7973b10..0000000 --- a/tsconfig.build.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "sourceMap": false, - "declarationMap": false - } -} diff --git a/tsconfig.json b/tsconfig.json index 17ef029..6254129 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,22 +1,17 @@ { "compilerOptions": { - "module": "NodeNext", - "moduleResolution": "NodeNext", + "outDir": "./dist", + "baseUrl": "./", "target": "ES2021", - "esModuleInterop": true, + "lib": ["ESNext"], + "module": "Node16", + "strict": true, "declaration": true, "isolatedModules": true, - "sourceMap": true, - "declarationMap": true, - "composite": true, - "forceConsistentCasingInFileNames": true, - "allowJs": true, - "checkJs": true, - "strict": true, + "esModuleInterop": true, "skipLibCheck": true, - "noUnusedLocals": true, - "outDir": "dist", - "rootDir": "src" + "resolveJsonModule": true, + "moduleResolution": "Node16" }, "include": ["src"] }