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

feat: make it easier to remove the built-in alias #3715

Merged
merged 2 commits into from
Oct 14, 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
12 changes: 12 additions & 0 deletions packages/compat/webpack/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"extensions": [
".ts",
".tsx",
Expand Down Expand Up @@ -692,6 +695,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"extensions": [
".ts",
".tsx",
Expand Down Expand Up @@ -1003,6 +1009,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"extensions": [
".ts",
".tsx",
Expand Down Expand Up @@ -1297,6 +1306,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"extensions": [
".ts",
".tsx",
Expand Down
30 changes: 20 additions & 10 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs';
import { isAbsolute, join } from 'node:path';
import { dirname, isAbsolute, join } from 'node:path';
import type { WatchOptions } from 'chokidar';
import color from 'picocolors';
import RspackChain from 'rspack-chain';
Expand Down Expand Up @@ -80,15 +80,25 @@ const getDefaultServerConfig = (): NormalizedServerConfig => ({
strictPort: false,
});

const getDefaultSourceConfig = (): NormalizedSourceConfig => ({
alias: {},
define: {},
aliasStrategy: 'prefer-tsconfig',
preEntry: [],
decorators: {
version: '2022-03',
},
});
let swcHelpersPath: string;

const getDefaultSourceConfig = (): NormalizedSourceConfig => {
if (!swcHelpersPath) {
swcHelpersPath = dirname(require.resolve('@swc/helpers/package.json'));
}

return {
alias: {
'@swc/helpers': swcHelpersPath,
},
define: {},
aliasStrategy: 'prefer-tsconfig',
preEntry: [],
decorators: {
version: '2022-03',
},
};
};

const getDefaultHtmlConfig = (): NormalizedHtmlConfig => ({
meta: {
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ export const pluginSwc = (): RsbuildPlugin => ({
applyTransformImport(swcConfig, config.source.transformImport);
applySwcDecoratorConfig(swcConfig, config);

if (swcConfig.jsc?.externalHelpers) {
chain.resolve.alias.set(
'@swc/helpers',
path.dirname(require.resolve('@swc/helpers/package.json')),
);
}

// apply polyfill
if (isWebTarget(target)) {
const polyfillMode = config.output.polyfill;
Expand Down
11 changes: 11 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ exports[`environment config > should normalize environment config correctly 1`]
"source": {
"alias": {
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -234,6 +235,7 @@ exports[`environment config > should normalize environment config correctly 2`]
"source": {
"alias": {
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -397,6 +399,7 @@ exports[`environment config > should print environment config when inspect confi
"source": {
"alias": {
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -556,6 +559,7 @@ exports[`environment config > should print environment config when inspect confi
"source": {
"alias": {
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand All @@ -582,10 +586,12 @@ exports[`environment config > should print environment config when inspect confi
exports[`environment config > should support add single environment plugin 1`] = `
{
"ssr": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"global": "ssr",
"ssr": "ssr",
},
"web": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
"global": "web",
"web": "web",
},
Expand Down Expand Up @@ -734,6 +740,7 @@ exports[`environment config > should support modify environment config by api.mo
"source": {
"alias": {
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -895,6 +902,7 @@ exports[`environment config > should support modify environment config by api.mo
"alias": {
"@common": "./src/common",
"@common1": "./src/common1",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -1056,6 +1064,7 @@ exports[`environment config > should support modify environment config by api.mo
"alias": {
"@common": "./src/common",
"@common1": "./src/common1",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -1219,6 +1228,7 @@ exports[`environment config > should support modify single environment config by
"source": {
"alias": {
"@common": "./src/common",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down Expand Up @@ -1380,6 +1390,7 @@ exports[`environment config > should support modify single environment config by
"alias": {
"@common": "./src/common",
"@common1": "./src/common1",
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"aliasStrategy": "prefer-tsconfig",
"decorators": {
Expand Down
50 changes: 0 additions & 50 deletions packages/core/tests/__snapshots__/swc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ exports[`plugin-swc > should add browserslist 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -227,11 +222,6 @@ exports[`plugin-swc > should add pluginImport 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -735,11 +725,6 @@ exports[`plugin-swc > should apply pluginImport correctly when ConfigChain 1`] =
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -849,11 +834,6 @@ exports[`plugin-swc > should disable pluginImport when return undefined 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -955,11 +935,6 @@ exports[`plugin-swc > should disable preset_env in target other than web 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -1069,11 +1044,6 @@ exports[`plugin-swc > should disable preset_env mode 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -1194,11 +1164,6 @@ exports[`plugin-swc > should enable entry mode preset_env 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -1320,11 +1285,6 @@ exports[`plugin-swc > should enable usage mode preset_env 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -1445,11 +1405,6 @@ exports[`plugin-swc > should has correct core-js 1`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
Expand Down Expand Up @@ -1551,11 +1506,6 @@ exports[`plugin-swc > should has correct core-js 2`] = `
"plugins": [
RsbuildCorePlugin {},
],
"resolve": {
"alias": {
"@swc/helpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
},
},
]
`;
4 changes: 1 addition & 3 deletions packages/core/tests/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ describe('plugin-resolve', () => {
});
const bundlerConfigs = await rsbuild.initConfigs();

expect(bundlerConfigs[0].resolve?.alias).toEqual({
foo: 'bar',
});
expect(bundlerConfigs[0].resolve?.alias?.foo).toEqual('bar');
});

it('should support source.alias to be a function', async () => {
Expand Down
22 changes: 20 additions & 2 deletions website/docs/en/config/source/alias.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
type Alias = Record<string, string | false | (string | false)[]> | Function;
```

- **Default:** `undefined`
- **Default:**

```ts
const defaultAlias = {
'@swc/helpers': path.dirname(require.resolve('@swc/helpers/package.json')),
};
```

Create aliases to import or require certain modules, same as the [resolve.alias](https://rspack.dev/config/resolve#resolvealias) config of Rspack.

Expand Down Expand Up @@ -44,7 +50,19 @@ export default {
};
```

You can also return a new object as the final result in the function, which will replace the previous alias object.
If you need to remove the built-in `@swc/helpers` alias, you can delete it in the function:

```js
export default {
source: {
alias: (alias) => {
delete alias['@swc/helpers'];
},
},
};
```

You can also return a new object as the final result in the function, which will replace the preset alias object.

```js
export default {
Expand Down
22 changes: 20 additions & 2 deletions website/docs/zh/config/source/alias.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
type Alias = Record<string, string | false | (string | false)[]> | Function;
```

- **默认值:** `undefined`
- **默认值:**

```ts
const defaultAlias = {
'@swc/helpers': path.dirname(require.resolve('@swc/helpers/package.json')),
};
```

设置文件引用的别名,对应 Rspack 的 [resolve.alias](https://rspack.dev/zh/config/resolve#resolvealias) 配置。

Expand Down Expand Up @@ -44,7 +50,19 @@ export default {
};
```

也可以在函数中返回一个新对象作为最终结果,新对象会覆盖预设的 alias 对象。
如果你需要移除 Rsbuild 内置的 `@swc/helpers` 别名,可以在函数中删除它:

```js
export default {
source: {
alias: (alias) => {
delete alias['@swc/helpers'];
},
},
};
```

你也可以在函数中返回一个新对象作为最终结果,新对象会覆盖预设的 alias 对象。

```js
export default {
Expand Down
Loading