-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Revert "feat(builder): use builtin:swc-loader (#4626)" This reverts commit a94260d. * Revert "chore(doc): update rspress version to adapt rspack builtin:swc-loader (#4734)" This reverts commit baed8bf. * fix: lock
- Loading branch information
Showing
48 changed files
with
623 additions
and
2,367 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
88 changes: 12 additions & 76 deletions
88
packages/builder/builder-rspack-provider/src/plugins/react.ts
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,92 +1,28 @@ | ||
import type { BuilderPlugin } from '../types'; | ||
import { isUsingHMR } from '@modern-js/builder-shared'; | ||
import path from 'path'; | ||
import { setConfig, isUsingHMR } from '@modern-js/builder-shared'; | ||
|
||
export const builderPluginReact = (): BuilderPlugin => ({ | ||
name: 'builder-plugin-react', | ||
|
||
pre: ['builder-plugin-swc'], | ||
|
||
setup(api) { | ||
api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd, target }) => { | ||
api.modifyRspackConfig(async (rspackConfig, utils) => { | ||
const config = api.getNormalizedConfig(); | ||
const usingHMR = isUsingHMR(config, { isProd, target }); | ||
const rule = chain.module.rule(CHAIN_ID.RULE.JS); | ||
|
||
const reactOptions = { | ||
development: !isProd, | ||
const usingHMR = isUsingHMR(config, utils); | ||
|
||
setConfig(rspackConfig, 'builtins.react', { | ||
development: !utils.isProd, | ||
refresh: usingHMR, | ||
// https://swc.rs/docs/configuration/compilation#jsctransformreactruntime | ||
runtime: 'automatic', | ||
}; | ||
|
||
rule.use(CHAIN_ID.USE.SWC).tap(options => { | ||
options.jsc.transform.react = { | ||
...reactOptions, | ||
}; | ||
return options; | ||
}); | ||
|
||
if (chain.module.rules.has(CHAIN_ID.RULE.JS_DATA_URI)) { | ||
chain.module | ||
.rule(CHAIN_ID.RULE.JS_DATA_URI) | ||
.use(CHAIN_ID.USE.SWC) | ||
.tap(options => { | ||
options.jsc.transform.react = { | ||
...reactOptions, | ||
}; | ||
return options; | ||
}); | ||
} | ||
|
||
if (!usingHMR) { | ||
return; | ||
} | ||
|
||
const { default: ReactRefreshRspackPlugin } = await import( | ||
// @ts-expect-error | ||
'@rspack/plugin-react-refresh' | ||
); | ||
|
||
const reactRefreshPath = path.dirname( | ||
require.resolve('@rspack/plugin-react-refresh/react-refresh'), | ||
); | ||
|
||
const refreshUtilsPath = require.resolve( | ||
'@pmmmwh/react-refresh-webpack-plugin/lib/runtime/RefreshUtils', | ||
{ | ||
paths: [reactRefreshPath], | ||
}, | ||
); | ||
const refreshRuntimeDirPath = path.dirname( | ||
require.resolve('react-refresh', { | ||
paths: [reactRefreshPath], | ||
}), | ||
); | ||
const runtimePaths = [ | ||
reactRefreshPath, | ||
refreshUtilsPath, | ||
refreshRuntimeDirPath, | ||
]; | ||
|
||
// TODO: not used when rspack react-refresh align with community | ||
runtimePaths.forEach((condition: string) => { | ||
rule.exclude.add(condition); | ||
setConfig(rspackConfig, 'builtins.provide', { | ||
...(rspackConfig.builtins?.provide || {}), | ||
$ReactRefreshRuntime$: [ | ||
require.resolve('@rspack/dev-client/react-refresh'), | ||
], | ||
}); | ||
|
||
chain | ||
.plugin(CHAIN_ID.PLUGIN.REACT_FAST_REFRESH) | ||
.use(ReactRefreshRspackPlugin, [ | ||
{ | ||
// consistent with swc-loader rules | ||
// include: [ | ||
// { | ||
// and: [api.context.rootPath, { not: NODE_MODULES_REGEX }], | ||
// }, | ||
// ...(config.source.include || []), | ||
// ], | ||
exclude: config.source.exclude || null, | ||
}, | ||
]); | ||
}); | ||
}, | ||
}); |
Oops, something went wrong.