Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into feat/mf-environment…
Browse files Browse the repository at this point in the history
…-config
  • Loading branch information
9aoy committed Jun 26, 2024
2 parents 08c80c6 + 71c66e6 commit 02afcfa
Show file tree
Hide file tree
Showing 74 changed files with 996 additions and 409 deletions.
10 changes: 9 additions & 1 deletion .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
"packagePatterns": ["modern-js"],
"groupSlug": "modern-js"
},
{
"groupName": "eslint",
"packagePatterns": ["eslint"],
"groupSlug": "eslint"
},
{
"groupName": "types",
"packagePatterns": ["^@types/"],
Expand All @@ -50,7 +55,6 @@
"ignoreDeps": [
// manually update some packages
"pnpm",
"core-js",
"esbuild",
"typescript",
// related to the SWC version built into Rspack
Expand All @@ -64,6 +68,10 @@
"gzip-size",
"globby",
"open",
"strip-ansi",
"ansi-escapes",
"cli-truncate",
"patch-console",
// buffer v6 has compatibility issues as node polyfill
"buffer",
// align Node.js version minimum requirements
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<a href="https://discord.gg/XsaKEEk4mW">
<img src="https://img.shields.io/badge/chat-discord-blue?style=flat-square&logo=discord&colorA=564341&colorB=EDED91" alt="discord channel" />
</a>
<a href="https://npmjs.com/package/@rsbuild/shared?activeTab=readme">
<img src="https://img.shields.io/npm/v/@rsbuild/shared?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
<a href="https://npmjs.com/package/@rsbuild/core?activeTab=readme">
<img src="https://img.shields.io/npm/v/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
</a>
<a href="https://npmcharts.com/compare/@rsbuild/core?minimal=true">
<img src="https://img.shields.io/npm/dm/@rsbuild/core.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" />
Expand All @@ -18,7 +18,7 @@
<img src="https://img.shields.io/node/v/@rsbuild/core.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="node version">
</a>
<a href="https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE">
<img src="https://img.shields.io/npm/l/@rsbuild/shared?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
</a>
</p>

Expand Down
6 changes: 3 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<a href="https://discord.gg/XsaKEEk4mW">
<img src="https://img.shields.io/badge/chat-discord-blue?style=flat-square&logo=discord&colorA=564341&colorB=EDED91" alt="discord channel" />
</a>
<a href="https://npmjs.com/package/@rsbuild/shared?activeTab=readme">
<img src="https://img.shields.io/npm/v/@rsbuild/shared?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
<a href="https://npmjs.com/package/@rsbuild/core?activeTab=readme">
<img src="https://img.shields.io/npm/v/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
</a>
<a href="https://npmcharts.com/compare/@rsbuild/core?minimal=true">
<img src="https://img.shields.io/npm/dm/@rsbuild/core.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" />
Expand All @@ -18,7 +18,7 @@
<img src="https://img.shields.io/node/v/@rsbuild/core.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="node version">
</a>
<a href="https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE">
<img src="https://img.shields.io/npm/l/@rsbuild/shared?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
</a>
</p>

Expand Down
3 changes: 2 additions & 1 deletion e2e/cases/babel/preset-node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ rspackOnlyTest(
runServer: true,
});

const { foo } = await import('./dist/server/index.js');
// @ts-ignore .js file
const { foo } = await import('./dist/index.js');
expect(foo).toEqual(1);
},
);
14 changes: 6 additions & 8 deletions e2e/cases/node-addons/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ test('should compile Node addons correctly', async () => {
file.endsWith('test.darwin.node'),
);

expect(addonFile?.includes('server/test.darwin.node')).toBeTruthy();
expect(addonFile?.includes('/test.darwin.node')).toBeTruthy();

expect(
fs.existsSync(join(__dirname, 'dist', 'server', 'test.darwin.node')),
fs.existsSync(join(__dirname, 'dist', 'test.darwin.node')),
).toBeTruthy();

// the `test.darwin.node` is only compatible with darwin
if (process.platform === 'darwin') {
const content = await import('./dist/server/index.js');
const content = await import('./dist/index.js');
expect(typeof (content.default as any).readLength).toEqual('function');
}
});
Expand Down Expand Up @@ -61,14 +61,12 @@ test('should compile Node addons in the node_modules correctly', async () => {
file.endsWith('other.node'),
);

expect(addonFile?.includes('server/other.node')).toBeTruthy();
expect(addonFile?.includes('/other.node')).toBeTruthy();

expect(
fs.existsSync(join(__dirname, 'dist', 'server', 'other.node')),
).toBeTruthy();
expect(fs.existsSync(join(__dirname, 'dist', 'other.node'))).toBeTruthy();

if (process.platform === 'darwin') {
const content = await import('./dist/server/index.js');
const content = await import('./dist/index.js');
expect(typeof (content.default as any).readLength).toEqual('function');
}
});
2 changes: 1 addition & 1 deletion e2e/cases/output/externals/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, resolve } from 'node:path';
import { resolve } from 'node:path';
import { build, gotoPage } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { pluginReact } from '@rsbuild/plugin-react';
Expand Down
3 changes: 3 additions & 0 deletions e2e/cases/performance/print-file-size/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ test.describe('should print file size correctly', async () => {
node: {
output: {
target: 'node',
distPath: {
root: 'dist/server',
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export default {
node: {
output: {
target: 'node',
distPath: {
root: 'dist/server',
},
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions e2e/cases/source/alias-by-target/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default defineConfig({
},
output: {
target: 'node',
distPath: {
root: 'dist/server',
},
},
},
},
Expand Down
27 changes: 13 additions & 14 deletions e2e/cases/source/multiple-entry/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
source: {
entry({ target }) {
if (target === 'web') {
return {
index: './src/index.client.js',
};
}
if (target === 'node') {
return {
index: './src/index.server.js',
};
}
},
},
output: {
filenameHash: false,
},
environments: {
web: {
source: {
entry: {
index: './src/index.client.js',
},
},
output: {
target: 'web',
},
},
node: {
source: {
entry: {
index: './src/index.server.js',
},
},
output: {
target: 'node',
distPath: {
root: 'dist/server',
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@rsbuild/core": "workspace:*",
"@rsbuild/shared": "workspace:*",
"copy-webpack-plugin": "9.1.0",
"copy-webpack-plugin": "11.0.0",
"mini-css-extract-plugin": "2.9.0",
"tsconfig-paths-webpack-plugin": "4.1.0",
"webpack": "^5.92.1"
Expand Down
10 changes: 5 additions & 5 deletions packages/compat/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ export const pluginAdaptor = (): RsbuildPlugin => ({

const { copy } = config.output;
if (copy) {
const { default: CopyPlugin } = await import(
// @ts-expect-error copy-webpack-plugin does not provide types
'copy-webpack-plugin'
);
const { default: CopyPlugin } = await import('copy-webpack-plugin');

const options: CopyPluginOptions = Array.isArray(copy)
? { patterns: copy }
: copy;

chain.plugin(CHAIN_ID.PLUGIN.COPY).use(CopyPlugin, [options]);
chain.plugin(CHAIN_ID.PLUGIN.COPY).use(CopyPlugin, [
// @ts-expect-error to type mismatch
options,
]);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"library": {
"type": "commonjs2",
},
"path": "<ROOT>/packages/compat/webpack/tests/dist/server",
"path": "<ROOT>/packages/compat/webpack/tests/dist",
"pathinfo": false,
"publicPath": "/",
"webassemblyModuleFilename": "static/wasm/[hash].module.wasm",
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
JS_DIST_DIR,
MEDIA_DIST_DIR,
ROOT_DIST_DIR,
SERVER_DIST_DIR,
SVG_DIST_DIR,
TS_CONFIG_FILE,
WASM_DIST_DIR,
Expand Down Expand Up @@ -135,7 +134,6 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
wasm: WASM_DIST_DIR,
image: IMAGE_DIST_DIR,
media: MEDIA_DIST_DIR,
server: SERVER_DIST_DIR,
},
assetPrefix: DEFAULT_ASSET_PREFIX,
filename: {},
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { join } from 'node:path';
// loaders will be emitted to the same folder of the main bundle
export const ROOT_DIST_DIR = 'dist';
export const HTML_DIST_DIR = '/';
export const SERVER_DIST_DIR = 'server';
export const JS_DIST_DIR = 'static/js';
export const CSS_DIST_DIR = 'static/css';
export const SVG_DIST_DIR = 'static/svg';
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { getCommonParentPath } from './helpers/path';
import { initHooks } from './initHooks';
import { getHTMLPathByEntry } from './initPlugins';
import { logger } from './logger';
import { getEntryObject } from './plugins/entry';
import type {
CreateRsbuildOptions,
InternalContext,
Expand Down Expand Up @@ -139,19 +138,19 @@ export async function updateEnvironmentContext(
const tsconfigPath = config.source.tsconfigPath
? getAbsolutePath(context.rootPath, config.source.tsconfigPath)
: undefined;
const entry = getEntryObject(config, config.output.target);

const browserslist = await getBrowserslistByEnvironment(
context.rootPath,
config,
);

const entry = config.source.entry ?? {};
const htmlPaths = getEnvironmentHTMLPaths(entry, config);

context.environments[name] = {
target: config.output.target,
distPath: getAbsoluteDistPath(context.rootPath, config),
entry: getEntryObject(config, config.output.target),
entry,
browserslist,
htmlPaths,
tsconfigPath,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/initHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AsyncHook,
HookDescriptor,
ModifyBundlerChainFn,
ModifyEnvironmentConfigFn,
ModifyHTMLTagsFn,
ModifyRsbuildConfigFn,
ModifyRspackConfigFn,
Expand Down Expand Up @@ -80,6 +81,7 @@ export function initHooks() {
modifyWebpackChain: createAsyncHook<ModifyWebpackChainFn>(),
modifyWebpackConfig: createAsyncHook<ModifyWebpackConfigFn>(),
modifyRsbuildConfig: createAsyncHook<ModifyRsbuildConfigFn>(),
modifyEnvironmentConfig: createAsyncHook<ModifyEnvironmentConfigFn>(),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,6 @@ export function getPluginAPI({
modifyWebpackChain: hooks.modifyWebpackChain.tap,
modifyWebpackConfig: hooks.modifyWebpackConfig.tap,
modifyRsbuildConfig: hooks.modifyRsbuildConfig.tap,
modifyEnvironmentConfig: hooks.modifyEnvironmentConfig.tap,
};
}
12 changes: 9 additions & 3 deletions packages/core/src/mergeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { type RsbuildConfig, castArray } from '@rsbuild/shared';
import { isFunction, isPlainObject } from './helpers';

const OVERRIDE_PATH = [
const OVERRIDE_PATHS = [
'performance.removeConsole',
'output.inlineScripts',
'output.inlineStyles',
'output.cssModules.auto',
'output.targets',
'output.overrideBrowserslist',
'server.open',
'server.printUrls',
Expand All @@ -16,7 +15,14 @@ const OVERRIDE_PATH = [
/**
* When merging configs, some properties prefer `override` over `merge to array`
*/
const isOverridePath = (key: string) => OVERRIDE_PATH.includes(key);
const isOverridePath = (key: string) => {
// ignore environments name prefix, such as `environments.web`
if (key.startsWith('environments.')) {
const realKey = key.split('.').slice(2).join('.');
return OVERRIDE_PATHS.includes(realKey);
}
return OVERRIDE_PATHS.includes(key);
};

const merge = (x: unknown, y: unknown, path = '') => {
// force some keys to override
Expand Down
26 changes: 2 additions & 24 deletions packages/core/src/plugins/entry.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import {
type NormalizedEnvironmentConfig,
type RsbuildEntry,
type RsbuildTarget,
castArray,
color,
} from '@rsbuild/shared';
import { castArray, color } from '@rsbuild/shared';
import type { EntryDescription } from '@rspack/core';
import { createVirtualModule } from '../helpers';
import { reduceConfigsMergeContext } from '../reduceConfigs';
import type { NormalizedConfig, RsbuildConfig, RsbuildPlugin } from '../types';

export function getEntryObject(
config: RsbuildConfig | NormalizedConfig | NormalizedEnvironmentConfig,
target: RsbuildTarget,
): RsbuildEntry {
if (!config.source?.entry) {
return {};
}

return reduceConfigsMergeContext({
initial: {},
config: config.source?.entry,
ctx: { target },
});
}
import type { RsbuildPlugin } from '../types';

export const pluginEntry = (): RsbuildPlugin => ({
name: 'rsbuild:entry',
Expand Down
Loading

0 comments on commit 02afcfa

Please sign in to comment.