Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uni-builder): repeatedly insert babel plugin when using tsLoader #5370

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/ninety-apricots-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/uni-builder': patch
---

fix(uni-builder): repeatedly insert babel plugin when using tsLoader in some edge case

fix(uni-builder): 修复在一些边界场景下使用 tsLoader 时会重复添加 babel plugin 的问题
7 changes: 6 additions & 1 deletion packages/builder/uni-builder/src/webpack/plugins/tsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ export const pluginTsLoader = (
.test(TS_REGEX)
.use(CHAIN_ID.USE.BABEL)
.loader(require.resolve('babel-loader'))
.options(babelLoaderOptions)
.options({
...babelLoaderOptions,
// fix repeatedly insert babel plugin in some boundary cases
plugins: [...(babelLoaderOptions.plugins || [])],
presets: [...(babelLoaderOptions.presets || [])],
})
.end()
.use(CHAIN_ID.USE.TS)
.loader(require.resolve('ts-loader'))
Expand Down
189 changes: 189 additions & 0 deletions packages/builder/uni-builder/tests/__snapshots__/tsLoader.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,194 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`plugin-ts-loader > should insert babel plugin correctly in some edge case 1`] = `
[
{
"resolve": {
"fullySpecified": false,
},
"test": /\\\\\\.m\\?js/,
},
{
"include": [
{
"and": [
"",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:js\\|mjs\\|cjs\\|jsx\\)\\$/,
"use": [
{
"loader": "<WORKSPACE>/node_modules/<PNPM_INNER>/babel-loader/lib/index.js",
"options": {
"babelrc": false,
"compact": true,
"configFile": false,
"plugins": [
[
"babel-plugin-xxx",
],
[
"babel-plugin-import",
{
"libraryDirectory": "es",
"libraryName": "xxx-components",
"style": true,
},
],
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/babel-plugin-styled-components/lib/index.js",
{
"displayName": true,
"pure": true,
"ssr": false,
"transpileTemplateLiterals": true,
},
],
],
"presets": [
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/preset-env/lib/index.js",
{
"bugfixes": true,
"corejs": {
"proposals": true,
"version": "3.32",
},
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"targets": [
"> 0.01%",
"not dead",
"not op_mini all",
],
"useBuiltIns": "entry",
},
],
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/preset-typescript/lib/index.js",
{
"allExtensions": true,
"allowDeclareFields": true,
"allowNamespaces": true,
"isTSX": true,
"optimizeConstEnums": true,
},
],
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/preset-react/lib/index.js",
{
"development": false,
"runtime": "automatic",
"useBuiltIns": true,
"useSpread": false,
},
],
],
},
},
],
},
]
`;

exports[`plugin-ts-loader > should insert babel plugin correctly in some edge case 2`] = `
[
{
"include": [
{
"and": [
"",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:ts\\|mts\\|cts\\|tsx\\)\\$/,
"use": [
{
"loader": "<WORKSPACE>/node_modules/<PNPM_INNER>/babel-loader/lib/index.js",
"options": {
"plugins": [
[
"babel-plugin-xxx",
],
[
"babel-plugin-import",
{
"libraryDirectory": "es",
"libraryName": "xxx-components",
"style": true,
},
],
],
"presets": [
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/preset-env/lib/index.js",
{
"bugfixes": true,
"corejs": {
"proposals": true,
"version": "3.32",
},
"exclude": [
"transform-typeof-symbol",
],
"modules": false,
"targets": [
"> 0.01%",
"not dead",
"not op_mini all",
],
"useBuiltIns": "entry",
},
],
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/preset-typescript/lib/index.js",
{
"allExtensions": true,
"allowDeclareFields": true,
"allowNamespaces": true,
"isTSX": true,
"optimizeConstEnums": true,
},
],
[
"<WORKSPACE>/node_modules/<PNPM_INNER>/@babel/preset-react/lib/index.js",
{
"development": false,
"runtime": "automatic",
"useBuiltIns": true,
"useSpread": false,
},
],
],
},
},
{
"loader": "<WORKSPACE>/node_modules/<PNPM_INNER>/ts-loader/index.js",
"options": {
"allowTsInNodeModules": true,
"compilerOptions": {
"module": "esnext",
"target": "esnext",
},
"transpileOnly": true,
},
},
],
},
]
`;

exports[`plugin-ts-loader > should set include/exclude 1`] = `
[
{
Expand Down
64 changes: 64 additions & 0 deletions packages/builder/uni-builder/tests/tsLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,68 @@ describe('plugin-ts-loader', () => {

expect(matchRules({ config, testFile: 'a.ts' })).toMatchSnapshot();
});

it('should insert babel plugin correctly in some edge case', async () => {
const { NODE_ENV } = process.env;
process.env.NODE_ENV = 'production';

const rsbuild = await createUniBuilder({
bundlerType: 'webpack',
config: {
tools: {
tsLoader: {},
babel: {
plugins: [
[
'babel-plugin-import',
{
libraryName: 'xxx-components',
libraryDirectory: 'es',
style: true,
},
],
],
},
webpackChain: (chain, { CHAIN_ID }) => {
const withBabelPlugin = (babelOptions: any) => {
if (typeof babelOptions !== 'object' || !babelOptions) {
return babelOptions;
}

babelOptions.plugins = babelOptions.plugins || [];
babelOptions.plugins.unshift(['babel-plugin-xxx']);
return babelOptions;
};

if (chain.module.rules.has(CHAIN_ID.RULE.JS)) {
chain.module
.rule(CHAIN_ID.RULE.JS)
.use(CHAIN_ID.USE.BABEL)
.tap(withBabelPlugin);
}
if (chain.module.rules.has(CHAIN_ID.RULE.TS)) {
chain.module
.rule(CHAIN_ID.RULE.TS)
.use(CHAIN_ID.USE.BABEL)
.tap(withBabelPlugin);
}
},
},
},
cwd: '',
});

const {
origin: { bundlerConfigs },
} = await rsbuild.inspectConfig();

expect(
matchRules({ config: bundlerConfigs[0], testFile: 'a.js' }),
).toMatchSnapshot();
expect(
matchRules({ config: bundlerConfigs[0], testFile: 'a.ts' }),
).toMatchSnapshot();

process.env.NODE_ENV = NODE_ENV;
});
});
Loading