Skip to content

Commit

Permalink
feat: add resolve.alias and deprecate source.alias (#4098)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Dec 1, 2024
1 parent 406eec6 commit 995b8da
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 37 deletions.
4 changes: 2 additions & 2 deletions e2e/cases/alias/alias-by-target/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
},
environments: {
web: {
source: {
resolve: {
alias: {
'@common': './src/common',
},
Expand All @@ -16,7 +16,7 @@ export default defineConfig({
},
},
node: {
source: {
resolve: {
alias: {
'@common': './src/common2',
},
Expand Down
10 changes: 7 additions & 3 deletions e2e/cases/alias/jsconfig-paths/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ rspackOnlyTest(
cwd: __dirname,
page,
rsbuildConfig: {
source: {
resolve: {
alias: {
'@common': './src/common2',
},
},
source: {
tsconfigPath: './jsconfig.json',
},
},
Expand All @@ -31,18 +33,20 @@ rspackOnlyTest(
cwd: __dirname,
page,
rsbuildConfig: {
source: {
resolve: {
alias: {
'@/common': './src/common2',
},
},
source: {
aliasStrategy: 'prefer-alias',
tsconfigPath: './jsconfig.json',
},
},
});

const foo = page.locator('#foo');
await expect(foo).toHaveText('source.alias worked');
await expect(foo).toHaveText('resolve.alias worked');

await rsbuild.close();
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/alias/jsconfig-paths/src/common2/test.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const content = 'source.alias worked';
export const content = 'resolve.alias worked';
20 changes: 20 additions & 0 deletions e2e/cases/alias/legacy-alias/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should allow to use the legacy `source.alias` config', async () => {
const rsbuild = await build({
cwd: __dirname,
});

const files = await rsbuild.unwrapOutputJSON();
const fileNames = Object.keys(files);
const webIndex = fileNames.find(
(file) => file.includes('static/js') && file.endsWith('index.js'),
);
const nodeIndex = fileNames.find(
(file) => file.includes('server/index') && file.endsWith('index.js'),
);

expect(files[webIndex!]).toContain('for web target');
expect(files[nodeIndex!]).toContain('for node target');
});
32 changes: 32 additions & 0 deletions e2e/cases/alias/legacy-alias/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
output: {
filenameHash: false,
},
environments: {
web: {
source: {
alias: {
'@common': './src/common',
},
},
output: {
target: 'web',
},
},
node: {
source: {
alias: {
'@common': './src/common2',
},
},
output: {
target: 'node',
distPath: {
root: 'dist/server',
},
},
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/alias/legacy-alias/src/common/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const content = 'for web target';
1 change: 1 addition & 0 deletions e2e/cases/alias/legacy-alias/src/common2/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const content = 'for node target';
3 changes: 3 additions & 0 deletions e2e/cases/alias/legacy-alias/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { content } from '@common/test';

console.log(content);
2 changes: 1 addition & 1 deletion e2e/cases/alias/tsconfig-paths-references/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rspackOnlyTest(
cwd: __dirname,
page,
rsbuildConfig: {
source: {
resolve: {
alias: {
'@common': './src/common2',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const content = 'source.alias worked';
export const content = 'resolve.alias worked';
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
source: {
resolve: {
alias: {
'@common': './src/common2',
},
},
source: {
tsconfigPath: 'tsconfig.custom.json',
},
});
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const content = 'source.alias worked';
export const content = 'resolve.alias worked';
8 changes: 5 additions & 3 deletions e2e/cases/alias/tsconfig-paths/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test('tsconfig paths should work and override the alias config', async ({
cwd: __dirname,
page,
rsbuildConfig: {
source: {
resolve: {
alias: {
'@common': './src/common2',
},
Expand All @@ -29,17 +29,19 @@ test('tsconfig paths should not work when aliasStrategy is "prefer-alias"', asyn
cwd: __dirname,
page,
rsbuildConfig: {
source: {
resolve: {
alias: {
'@/common': './src/common2',
},
},
source: {
aliasStrategy: 'prefer-alias',
},
},
});

const foo = page.locator('#foo');
await expect(foo).toHaveText('source.alias worked');
await expect(foo).toHaveText('resolve.alias worked');

await rsbuild.close();
});
2 changes: 1 addition & 1 deletion e2e/cases/alias/tsconfig-paths/src/common2/test.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const content = 'source.alias worked';
export const content = 'resolve.alias worked';
2 changes: 1 addition & 1 deletion e2e/cases/css/import-common-css/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rspackOnlyTest('should compile common CSS import correctly', async () => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
source: {
resolve: {
alias: {
'@': './src',
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/css/resolve-alias/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pluginSass } from '@rsbuild/plugin-sass';

export default {
plugins: [pluginLess(), pluginSass()],
source: {
resolve: {
alias: {
'@common': path.resolve(__dirname, 'src/common'),
},
Expand Down
4 changes: 3 additions & 1 deletion e2e/cases/source/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ test.describe('source configure multi', () => {
entry: {
index: join(fixtures, 'basic/src/index.js'),
},
preEntry: ['./src/pre.js'],
},
resolve: {
alias: {
'@common': './src/common',
},
preEntry: ['./src/pre.js'],
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const updateConfigForTest = async (
dev: {
progressBar: false,
},
source: {
resolve: {
alias: {
'@assets': join(__dirname, '../assets'),
},
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type {
NormalizedHtmlConfig,
NormalizedOutputConfig,
NormalizedPerformanceConfig,
NormalizedResolveConfig,
NormalizedSecurityConfig,
NormalizedServerConfig,
NormalizedSourceConfig,
Expand Down Expand Up @@ -192,11 +193,15 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
emitAssets: true,
});

const getDefaultResolveConfig = (): NormalizedResolveConfig => ({
alias: {},
});

const createDefaultConfig = (): RsbuildConfig => ({
dev: getDefaultDevConfig(),
server: getDefaultServerConfig(),
html: getDefaultHtmlConfig(),
resolve: {},
resolve: getDefaultResolveConfig(),
source: getDefaultSourceConfig(),
output: getDefaultOutputConfig(),
tools: getDefaultToolsConfig(),
Expand Down
25 changes: 19 additions & 6 deletions packages/core/src/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,25 @@ function applyAlias({
config: NormalizedEnvironmentConfig;
rootPath: string;
}) {
const { alias } = config.source;

const mergedAlias = reduceConfigs({
initial: {},
config: alias,
});
// TODO: remove `source.alias` in the next major version
const mergedSourceAlias = config.source.alias
? reduceConfigs({
initial: {},
config: config.source.alias,
})
: {};

const mergedResolveAlias = config.resolve.alias
? reduceConfigs({
initial: {},
config: config.resolve.alias,
})
: {};

const mergedAlias = {
...mergedSourceAlias,
...mergedResolveAlias,
};

if (config.resolve.dedupe) {
for (const pkgName of config.resolve.dedupe) {
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export type Decorators = {

export interface SourceConfig {
/**
* Create aliases to import or require certain modules,
* same as the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack.
* @deprecated Use `resolve.alias` instead.
* `source.alias` will be removed in v2.0.0.
*/
alias?: ConfigChain<Alias>;
/**
Expand Down Expand Up @@ -248,6 +248,10 @@ type TransformImportFn = (

export interface NormalizedSourceConfig extends SourceConfig {
define: Define;
/**
* @deprecated Use `resolve.alias` instead.
* `source.alias` will be removed in v2.0.0.
*/
alias: ConfigChain<Alias>;
aliasStrategy: AliasStrategy;
preEntry: string[];
Expand Down Expand Up @@ -1401,9 +1405,15 @@ export interface ResolveConfig {
* which is useful for deduplicating packages and reducing the bundle size.
*/
dedupe?: string[];
/**
* Create aliases to import or require certain modules,
* same as the [resolve.alias](https://rspack.dev/config/resolve) config of Rspack.
*/
alias?: ConfigChain<Alias>;
}

export type NormalizedResolveConfig = ResolveConfig;
export type NormalizedResolveConfig = ResolveConfig &
Pick<Required<ResolveConfig>, 'alias'>;

export type ModuleFederationConfig = {
options: ModuleFederationPluginOptions;
Expand Down
Loading

0 comments on commit 995b8da

Please sign in to comment.