Skip to content

Commit

Permalink
fix: reduce alias and allow to use esm bundles of preact (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Oct 25, 2024
1 parent dd86958 commit 1cb6bec
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 55 deletions.
12 changes: 12 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2",
"ignorePaths": [],
"dictionaryDefinitions": [],
"dictionaries": [],
"words": [
"prefresh",
"preact"
],
"ignoreWords": [],
"import": []
}
58 changes: 19 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import { dirname } from 'node:path';
import type { Compiler, RspackPluginInstance } from '@rspack/core';

export interface IPreactRefreshRspackPluginOptions {
Expand All @@ -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';
Expand All @@ -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
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/[email protected]/node_modules/preact/compat/jsx-dev-runtime.js");
/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/preact/hooks/dist/hooks.js");
/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/[email protected]/node_modules/preact/compat/dist/compat.module.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/[email protected]/node_modules/preact/compat/jsx-dev-runtime.js");
/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/preact/hooks/dist/hooks.js");
/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/[email protected]/node_modules/preact/compat/dist/compat.module.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/[email protected]/node_modules/preact/compat/jsx-dev-runtime.js");
/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/preact/hooks/dist/hooks.js");
/* ESM import */var preact_compat__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/compat */ "../../../../node_modules/.pnpm/[email protected]/node_modules/preact/compat/dist/compat.module.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/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");

Expand Down
4 changes: 2 additions & 2 deletions test/hotCases/hook/useState#keep/__snapshots__/web/1.snap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/@[email protected]/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/[email protected]/node_modules/preact/compat/jsx-dev-runtime.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/node_modules/preact/hooks/dist/hooks.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/@[email protected]/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/[email protected]/node_modules/preact/compat/jsx-dev-runtime.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/node_modules/preact/hooks/dist/hooks.js");
/* ESM import */var preact_hooks__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! preact/hooks */ "../../../../node_modules/.pnpm/[email protected]/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");

Expand Down

0 comments on commit 1cb6bec

Please sign in to comment.