Skip to content

Commit

Permalink
fix!: remove transformImport false option (#3289)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Aug 27, 2024
1 parent c8cb3eb commit bee8d4c
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 279 deletions.
1 change: 1 addition & 0 deletions e2e/cases/source/plugin-import/foo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'test succeed';
18 changes: 18 additions & 0 deletions e2e/cases/source/plugin-import/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ test('should import with template config', async () => {
expect(files[entry]).toContain('transformImport test succeed');
});

test('should not transformImport by default', async () => {
copyPkgToNodeModules();

const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
performance: {
chunkSplit: {
strategy: 'all-in-one',
},
},
},
});
const files = await rsbuild.unwrapOutputJSON(false);
const entry = findEntry(files);
expect(files[entry]).toContain('test succeed');
});

for (const c of cases) {
const [name, entry, config] = c;
shareTest(`${name}-rspack`, entry, config);
Expand Down
8 changes: 4 additions & 4 deletions packages/compat/plugin-swc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ const getCoreJsVersion = (corejsPkgPath: string) => {

const reduceTransformImportConfig = (
options: NormalizedSourceConfig['transformImport'],
): TransformImport[] | false => {
if (options === false || !options) {
return false;
): TransformImport[] => {
if (!options) {
return [];
}

let imports: TransformImport[] = [];
Expand Down Expand Up @@ -279,7 +279,7 @@ export async function applyPluginConfig(
rsbuildConfig.source?.transformImport,
);

if (finalPluginImport !== false && finalPluginImport?.length) {
if (finalPluginImport?.length) {
extensions.pluginImport ??= [];
extensions.pluginImport.push(...finalPluginImport);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ async function applyCoreJs(

const reduceTransformImportConfig = (
options: NormalizedSourceConfig['transformImport'],
): TransformImport[] | false => {
if (options === false || !options) {
return false;
): TransformImport[] => {
if (!options) {
return [];
}

let imports: TransformImport[] = [];
Expand All @@ -240,7 +240,7 @@ function applyTransformImport(
) {
const finalPluginImport = reduceTransformImportConfig(pluginImport);

if (finalPluginImport !== false && finalPluginImport?.length) {
if (finalPluginImport?.length) {
swcConfig.rspackExperiments ??= {};
swcConfig.rspackExperiments.import ??= [];
swcConfig.rspackExperiments.import.push(...finalPluginImport);
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/types/config/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export interface SourceConfig {
*/
transformImport?:
| TransformImportFn
| Array<TransformImport | TransformImportFn>
| false;
| Array<TransformImport | TransformImportFn>;
/**
* Configure a custom tsconfig.json file path to use, can be a relative or absolute path.
* @default 'tsconfig.json'
Expand Down
228 changes: 0 additions & 228 deletions packages/core/tests/__snapshots__/swc.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,119 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`plugin-swc > should add antd pluginImport 1`] = `
{
"entry": {
"main": [
"./src/index.js",
],
},
"module": {
"rules": [
{
"include": [
{
"and": [
"<ROOT>/packages/core/tests",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"env": {
"mode": undefined,
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
},
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"preserveAllComments": true,
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
},
},
},
},
],
},
{
"mimetype": {
"or": [
"text/javascript",
"application/javascript",
],
},
"resolve": {
"fullySpecified": false,
},
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"env": {
"mode": undefined,
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
},
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"preserveAllComments": true,
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
},
},
},
},
],
},
],
},
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
}
`;

exports[`plugin-swc > should add browserslist 1`] = `
[
{
Expand Down Expand Up @@ -870,120 +756,6 @@ exports[`plugin-swc > should apply pluginImport correctly when ConfigChain 1`] =
]
`;

exports[`plugin-swc > should disable all pluginImport 1`] = `
{
"entry": {
"main": [
"./src/index.js",
],
},
"module": {
"rules": [
{
"include": [
{
"and": [
"<ROOT>/packages/core/tests",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"type": "javascript/auto",
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"env": {
"mode": undefined,
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
},
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"preserveAllComments": true,
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
},
},
},
},
],
},
{
"mimetype": {
"or": [
"text/javascript",
"application/javascript",
],
},
"resolve": {
"fullySpecified": false,
},
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"env": {
"mode": undefined,
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/packages/core/tests/node_modules/.cache/.swc",
},
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"preserveAllComments": true,
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
},
},
},
},
],
},
],
},
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
}
`;

exports[`plugin-swc > should disable pluginImport when return undefined 1`] = `
[
{
Expand Down
39 changes: 0 additions & 39 deletions packages/core/tests/swc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,45 +105,6 @@ describe('plugin-swc', () => {
});
});

it('should disable all pluginImport', async () => {
const rsbuild = await createStubRsbuild({
plugins: [pluginSwc(), pluginEntry()],
rsbuildConfig: {
source: {
entry: {
main: './src/index.js',
},
transformImport: false,
},
},
});

const bundlerConfigs = await rsbuild.initConfigs();

for (const bundlerConfig of bundlerConfigs) {
expect(bundlerConfig).toMatchSnapshot();
}
});

it('should add antd pluginImport', async () => {
const rsbuild = await createStubRsbuild({
rsbuildConfig: {
source: {
entry: {
main: './src/index.js',
},
},
},
plugins: [pluginSwc(), pluginEntry()],
});

const bundlerConfigs = await rsbuild.initConfigs();

for (const bundlerConfig of bundlerConfigs) {
expect(bundlerConfig).toMatchSnapshot();
}
});

it('should allow to use `tools.swc` to configure swc-loader options', async () => {
const rsbuild = await createStubRsbuild({
rsbuildConfig: {
Expand Down
1 change: 0 additions & 1 deletion website/docs/en/config/source/transform-import.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

```ts
type TransformImport =
| false
| Array<{
libraryName: string;
libraryDirectory?: string;
Expand Down
1 change: 0 additions & 1 deletion website/docs/zh/config/source/transform-import.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

```ts
type TransformImport =
| false
| Array<{
libraryName: string;
libraryDirectory?: string;
Expand Down

0 comments on commit bee8d4c

Please sign in to comment.