diff --git a/cspell.json b/cspell.json new file mode 100644 index 0000000..286f5f5 --- /dev/null +++ b/cspell.json @@ -0,0 +1,12 @@ +{ + "version": "0.2", + "ignorePaths": [], + "dictionaryDefinitions": [], + "dictionaries": [], + "words": [ + "prefresh", + "preact" + ], + "ignoreWords": [], + "import": [] +} diff --git a/src/index.ts b/src/index.ts index 59671ed..dac1f15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import { dirname } from 'node:path'; import type { Compiler, RspackPluginInstance } from '@rspack/core'; export interface IPreactRefreshRspackPluginOptions { @@ -14,47 +15,11 @@ interface NormalizedPluginOptions extends IPreactRefreshRspackPluginOptions { exclude: NonNullable<IPreactRefreshRspackPluginOptions['exclude']>; } -const PREACT_PATHS = [ - 'preact', - 'preact/compat', - 'preact/debug', - 'preact/devtools', - 'preact/hooks', - 'preact/test-utils', - 'preact/jsx-runtime', - 'preact/jsx-dev-runtime', - 'preact/compat/client', - 'preact/compat/server', - 'preact/compat/jsx-runtime', - 'preact/compat/jsx-dev-runtime', - 'preact/compat/scheduler', - 'preact/package.json', - 'preact/compat/package.json', - 'preact/debug/package.json', - 'preact/devtools/package.json', - 'preact/hooks/package.json', - 'preact/test-utils/package.json', - 'preact/jsx-runtime/package.json', -].reduce( - (obj, i) => { - obj[i] = require.resolve(i); - return obj; - }, - {} as Record<string, string>, -); const PREFRESH_CORE_PATH = require.resolve('@prefresh/core'); const PREFRESH_UTILS_PATH = require.resolve('@prefresh/utils'); const RUNTIME_UTIL_PATH = require.resolve('../client/prefresh.cjs'); const RUNTIME_INTERCEPT_PATH = require.resolve('../client/intercept.cjs'); -const INTERNAL_PATHS = [ - ...Object.values(PREACT_PATHS), - PREFRESH_UTILS_PATH, - PREFRESH_CORE_PATH, - RUNTIME_UTIL_PATH, - RUNTIME_INTERCEPT_PATH, -]; - const runtimeSource = fs.readFileSync(RUNTIME_INTERCEPT_PATH, 'utf-8'); const NAME = 'PreactRefreshRspackPlugin'; @@ -78,6 +43,15 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance { ) return; + const PREACT_PATH = dirname(require.resolve('preact/package.json')); + + const INTERNAL_PATHS = [ + PREFRESH_UTILS_PATH, + PREFRESH_CORE_PATH, + RUNTIME_UTIL_PATH, + RUNTIME_INTERCEPT_PATH, + ]; + new compiler.webpack.ProvidePlugin({ __prefresh_utils__: RUNTIME_UTIL_PATH, ...(this.options.overlay @@ -89,23 +63,29 @@ class PreactRefreshRspackPlugin implements RspackPluginInstance { new compiler.webpack.EntryPlugin(compiler.context, '@prefresh/core', { name: undefined, }).apply(compiler); + // new compiler.webpack.DefinePlugin({ __refresh_library__ }).apply(compiler); compiler.options.resolve.alias = { + preact: PREACT_PATH, '@prefresh/core': PREFRESH_CORE_PATH, '@prefresh/utils': PREFRESH_UTILS_PATH, - ...PREACT_PATHS, ...compiler.options.resolve.alias, }; + compiler.options.module.rules.unshift({ include: this.options.include, exclude: { - or: [this.options.exclude, ...INTERNAL_PATHS].filter(Boolean), + or: [ + this.options.exclude, + ...INTERNAL_PATHS, + /node_modules[\\/]preact[\\/]/, + ].filter(Boolean), }, use: 'builtin:preact-refresh-loader', }); compiler.hooks.thisCompilation.tap(NAME, (compilation) => { - compilation.hooks.runtimeModule.tap(NAME, (runtimeModule, chunk) => { + compilation.hooks.runtimeModule.tap(NAME, (runtimeModule) => { // rspack does not have addRuntimeModule and runtimeRequirements on js side if ( runtimeModule.constructorName === 'HotModuleReplacementRuntimeModule' diff --git a/test/hotCases/hook/useContext#initial/__snapshots__/web/1.snap.txt b/test/hotCases/hook/useContext#initial/__snapshots__/web/1.snap.txt index d34a58d..f1f9dc5 100644 --- a/test/hotCases/hook/useContext#initial/__snapshots__/web/1.snap.txt +++ b/test/hotCases/hook/useContext#initial/__snapshots__/web/1.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 5266 +- Update: main.LAST_HASH.hot-update.js, size: 5127 ## Manifest @@ -38,9 +38,8 @@ __webpack_require__.d(__webpack_exports__, { App: function() { return App; } }); /* ESM import */var react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react/jsx-dev-runtime */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/jsx-dev-runtime.js"); -/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/dist/compat.js"); -/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(preact_compat__WEBPACK_IMPORTED_MODULE_1__); -/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.js"); +/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/dist/compat.module.js"); +/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.module.js"); /* module decorator */ module = __webpack_require__.hmd(module); /* provided dependency */ var __prefresh_utils__ = __webpack_require__(/*! ../../../../client/prefresh.cjs */ "../../../../client/prefresh.cjs"); diff --git a/test/hotCases/hook/useContext#keep/__snapshots__/web/1.snap.txt b/test/hotCases/hook/useContext#keep/__snapshots__/web/1.snap.txt index 2574c83..ead0886 100644 --- a/test/hotCases/hook/useContext#keep/__snapshots__/web/1.snap.txt +++ b/test/hotCases/hook/useContext#keep/__snapshots__/web/1.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 5571 +- Update: main.LAST_HASH.hot-update.js, size: 5432 ## Manifest @@ -38,9 +38,8 @@ __webpack_require__.d(__webpack_exports__, { App: function() { return App; } }); /* ESM import */var react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react/jsx-dev-runtime */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/jsx-dev-runtime.js"); -/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/dist/compat.js"); -/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(preact_compat__WEBPACK_IMPORTED_MODULE_1__); -/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.js"); +/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/dist/compat.module.js"); +/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.module.js"); /* module decorator */ module = __webpack_require__.hmd(module); /* provided dependency */ var __prefresh_utils__ = __webpack_require__(/*! ../../../../client/prefresh.cjs */ "../../../../client/prefresh.cjs"); diff --git a/test/hotCases/hook/useContext#provide/__snapshots__/web/1.snap.txt b/test/hotCases/hook/useContext#provide/__snapshots__/web/1.snap.txt index af22427..1bf1212 100644 --- a/test/hotCases/hook/useContext#provide/__snapshots__/web/1.snap.txt +++ b/test/hotCases/hook/useContext#provide/__snapshots__/web/1.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 5587 +- Update: main.LAST_HASH.hot-update.js, size: 5448 ## Manifest @@ -38,9 +38,8 @@ __webpack_require__.d(__webpack_exports__, { App: function() { return App; } }); /* ESM import */var react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react/jsx-dev-runtime */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/jsx-dev-runtime.js"); -/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/dist/compat.js"); -/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(preact_compat__WEBPACK_IMPORTED_MODULE_1__); -/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.js"); +/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/dist/compat.module.js"); +/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.module.js"); /* module decorator */ module = __webpack_require__.hmd(module); /* provided dependency */ var __prefresh_utils__ = __webpack_require__(/*! ../../../../client/prefresh.cjs */ "../../../../client/prefresh.cjs"); diff --git a/test/hotCases/hook/useState#keep/__snapshots__/web/1.snap.txt b/test/hotCases/hook/useState#keep/__snapshots__/web/1.snap.txt index cbe5f58..287cf3e 100644 --- a/test/hotCases/hook/useState#keep/__snapshots__/web/1.snap.txt +++ b/test/hotCases/hook/useState#keep/__snapshots__/web/1.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 4697 +- Update: main.LAST_HASH.hot-update.js, size: 4704 ## Manifest @@ -39,7 +39,7 @@ __webpack_require__.d(__webpack_exports__, { }); /* ESM import */var _swc_helpers_sliced_to_array__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @swc/helpers/_/_sliced_to_array */ "../../../../node_modules/.pnpm/@swc+helpers@0.5.13/node_modules/@swc/helpers/esm/_sliced_to_array.js"); /* ESM import */var react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react/jsx-dev-runtime */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/jsx-dev-runtime.js"); -/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.js"); +/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.module.js"); /* module decorator */ module = __webpack_require__.hmd(module); /* provided dependency */ var __prefresh_utils__ = __webpack_require__(/*! ../../../../client/prefresh.cjs */ "../../../../client/prefresh.cjs"); diff --git a/test/hotCases/hook/useState#reset/__snapshots__/web/1.snap.txt b/test/hotCases/hook/useState#reset/__snapshots__/web/1.snap.txt index f386601..07a6c93 100644 --- a/test/hotCases/hook/useState#reset/__snapshots__/web/1.snap.txt +++ b/test/hotCases/hook/useState#reset/__snapshots__/web/1.snap.txt @@ -6,7 +6,7 @@ ## Asset Files - Bundle: bundle.js - Manifest: main.LAST_HASH.hot-update.json, size: 28 -- Update: main.LAST_HASH.hot-update.js, size: 4589 +- Update: main.LAST_HASH.hot-update.js, size: 4596 ## Manifest @@ -39,7 +39,7 @@ __webpack_require__.d(__webpack_exports__, { }); /* ESM import */var _swc_helpers_sliced_to_array__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @swc/helpers/_/_sliced_to_array */ "../../../../node_modules/.pnpm/@swc+helpers@0.5.13/node_modules/@swc/helpers/esm/_sliced_to_array.js"); /* ESM import */var react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react/jsx-dev-runtime */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/compat/jsx-dev-runtime.js"); -/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.js"); +/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/preact@10.24.3/node_modules/preact/hooks/dist/hooks.module.js"); /* module decorator */ module = __webpack_require__.hmd(module); /* provided dependency */ var __prefresh_utils__ = __webpack_require__(/*! ../../../../client/prefresh.cjs */ "../../../../client/prefresh.cjs");