Skip to content

Commit

Permalink
revert builtin:swc-loader (#4738)
Browse files Browse the repository at this point in the history
* 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
9aoy authored Sep 27, 2023
1 parent baed8bf commit 1d28a95
Show file tree
Hide file tree
Showing 48 changed files with 623 additions and 2,367 deletions.
8 changes: 0 additions & 8 deletions .changeset/new-donkeys-hide.md

This file was deleted.

14 changes: 0 additions & 14 deletions .changeset/tiny-moons-approve.md

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@
"pnpm": {
"overrides": {
"@types/react": "^18",
"@types/react-dom": "^18",
"rspress": "0.0.0-next-20230927072732",
"@rspress/shared": "0.0.0-next-20230927072732"
"@types/react-dom": "^18"
},
"allowedDeprecatedVersions": {
"@babel/plugin-proposal-class-properties": "7.18.6",
Expand Down
1 change: 0 additions & 1 deletion packages/builder/builder-rspack-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"@rspack/core": "0.3.5",
"@rspack/dev-client": "0.3.5",
"@rspack/plugin-html": "0.3.5",
"@rspack/plugin-react-refresh": "0.3.5",
"@swc/helpers": "0.5.1",
"rspack-manifest-plugin": "5.0.0-alpha0",
"caniuse-lite": "^1.0.30001520",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ import { sharedOutputConfigSchema, z } from '@modern-js/builder-shared';
import type { OutputConfig } from '../../types';

export const outputConfigSchema: z.ZodType<OutputConfig> =
sharedOutputConfigSchema;
sharedOutputConfigSchema
.extend({
polyfill: z.enum(['entry', 'ua', 'off']),
})
.partial();
29 changes: 9 additions & 20 deletions packages/builder/builder-rspack-provider/src/plugins/babel.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
mergeRegex,
applyScriptCondition,
JS_REGEX,
TS_REGEX,
NODE_MODULES_REGEX,
} from '@modern-js/builder-shared';
import { cloneDeep, isEqual } from '@modern-js/utils/lodash';
import { BuilderPlugin, NormalizedConfig } from '../types';
Expand All @@ -12,9 +12,6 @@ import { DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS } from '@modern-js/utils';

export const builderPluginBabel = (): BuilderPlugin => ({
name: 'builder-plugin-babel',

pre: ['builder-plugin-swc'],

setup(api) {
api.modifyBundlerChain(
async (chain, { CHAIN_ID, isProd, getCompiledPath }) => {
Expand Down Expand Up @@ -51,7 +48,6 @@ export const builderPluginBabel = (): BuilderPlugin => ({
const baseConfig = {
plugins: [],
presets: [
// TODO: only apply preset-typescript for ts file (isTSX & allExtensions false)
[
require.resolve('@babel/preset-typescript'),
DEFAULT_BABEL_PRESET_TYPESCRIPT_OPTIONS,
Expand Down Expand Up @@ -98,28 +94,21 @@ export const builderPluginBabel = (): BuilderPlugin => ({
return;
}

// already set source.include / exclude in plugin-swc
const rule = chain.module.rule(CHAIN_ID.RULE.JS);

const { rootPath } = api.context;
applyScriptCondition({
rule,
config,
context: api.context,
includes,
excludes,
});

rule
.test(mergeRegex(JS_REGEX, TS_REGEX))
.use(CHAIN_ID.USE.BABEL)
.after(CHAIN_ID.USE.SWC)
.loader(getCompiledPath('babel-loader'))
.options({
...babelOptions,
// TODO: should only apply babel include / exclude when apply source.include in rule
only: [
(pathName: string) =>
pathName.includes(rootPath) &&
!NODE_MODULES_REGEX.test(pathName),
...includes,
...(config.source.include || []),
],
ignore: [...excludes, ...(config.source.exclude || [])],
});
.options(babelOptions);
},
);
},
Expand Down
88 changes: 12 additions & 76 deletions packages/builder/builder-rspack-provider/src/plugins/react.ts
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,
},
]);
});
},
});
Loading

0 comments on commit 1d28a95

Please sign in to comment.