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(builder): update rspack to 0.1.12 #3741

Merged
merged 4 commits into from
May 23, 2023
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/cold-poets-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/builder-rspack-provider': patch
---

fix(builder): output.copy not work in Rspack

fix(builder): output.copy 在 Rspack 构建时不生效
8 changes: 8 additions & 0 deletions .changeset/dull-geckos-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/builder-rspack-provider': patch
'@modern-js/builder-shared': patch
---

feat(builder): update rspack to 0.1.12

feat(builder): 升级 rspack 到 0.1.12 版本
10 changes: 5 additions & 5 deletions packages/builder/builder-rspack-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
"@modern-js/types": "workspace:*",
"@modern-js/utils": "workspace:*",
"@babel/preset-typescript": "^7.21.5",
"@rspack/core": "0.0.0-canary-22b006c-20230517164249",
"@rspack/dev-client": "0.0.0-canary-22b006c-20230517164249",
"@rspack/dev-middleware": "0.0.0-canary-22b006c-20230517164249",
"@rspack/plugin-html": "0.0.0-canary-22b006c-20230517164249",
"@rspack/postcss-loader": "0.0.0-canary-22b006c-20230517164249",
"@rspack/core": "0.1.12",
"@rspack/dev-client": "0.1.12",
"@rspack/dev-middleware": "0.1.12",
"@rspack/plugin-html": "0.1.12",
"@rspack/postcss-loader": "0.1.12",
"rspack-manifest-plugin": "5.0.0-alpha0",
"caniuse-lite": "^1.0.30001451",
"core-js": "~3.30.0",
Expand Down
11 changes: 10 additions & 1 deletion packages/builder/builder-rspack-provider/src/plugins/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ export const builderPluginOutput = (): BuilderPlugin => ({
}

if (config.output.copy) {
setConfig(rspackConfig, 'builtins.copy', config.output.copy);
const { copy } = config.output;
const options = Array.isArray(copy) ? { patterns: copy } : copy;

setConfig(rspackConfig, 'builtins.copy', {
...options,
patterns: [
...(rspackConfig?.builtins?.copy?.patterns || []),
...options.patterns,
],
});
}
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type OutputConfig = SharedOutputConfig & {
/**
* Copies the specified file or directory to the dist directory.
*/
copy?: Builtins['copy'];
copy?: Builtins['copy'] | NonNullable<Builtins['copy']>['patterns'];
/**
* Convert px to rem in CSS.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ exports[`plugins/output > should allow to use copy plugin 1`] = `
}
`;

exports[`plugins/output > should allow to use copy plugin with multiply config 1`] = `
{
"builtins": {
"copy": {
"patterns": [
{
"from": "test",
},
"src/assets/",
"tests/",
],
},
},
"module": {},
"output": {
"chunkFilename": "static/js/async/[name].js",
"cssChunkFilename": "static/css/async/[name].css",
"cssFilename": "static/css/[name].css",
"filename": "static/js/[name].js",
"hashFunction": "xxhash64",
"path": "<ROOT>/dist",
"pathinfo": false,
"publicPath": "/",
},
}
`;

exports[`plugins/output > should allow to use filename.js to modify filename 1`] = `
{
"module": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,34 @@ describe('plugins/output', () => {
} = await builder.inspectConfig();
expect(bundlerConfigs[0]).toMatchSnapshot();
});

it('should allow to use copy plugin with multiply config', async () => {
const builder = await createBuilder({
plugins: [builderPluginOutput()],
builderConfig: {
output: {
copy: [
{
from: 'test',
},
'src/assets/',
],
},
tools: {
rspack: {
builtins: {
copy: {
patterns: ['tests/'],
},
},
},
},
},
});

const {
origin: { bundlerConfigs },
} = await builder.inspectConfig();
expect(bundlerConfigs[0]).toMatchSnapshot();
});
});
3 changes: 1 addition & 2 deletions packages/builder/builder-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@
"html-webpack-plugin": "5.5.0",
"terser": "^5.14.1",
"typescript": "^5",
"webpack": "^5.82.1",
"@rspack/core": "0.0.0-canary-22b006c-20230517164249"
"webpack": "^5.82.1"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
5 changes: 1 addition & 4 deletions packages/builder/builder-shared/src/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
BuilderContext,
OnAfterStartDevServerFn,
OnBeforeStartDevServerFn,
CompilerTapFn,
} from './types';
import type { ModernDevServerOptions, Server } from '@modern-js/server';
import { merge } from '@modern-js/utils/lodash';
Expand Down Expand Up @@ -207,10 +208,6 @@ type ServerCallbacks = {
onDone: (stats: any) => void;
};

type CompilerTapFn<CallBack extends (...args: any[]) => void> = {
tap: (name: string, cb: CallBack) => void;
};

export const setupServerHooks = (
compiler: {
name?: Compiler['name'];
Expand Down
24 changes: 9 additions & 15 deletions packages/builder/builder-shared/src/patch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import path from 'path';
import { pathToFileURL } from 'url';
import type {
Compiler as WebpackCompiler,
MultiCompiler as WebpackMultiCompiler,
} from 'webpack';
import type {
Compiler as RspackCompiler,
MultiCompiler as RspackMultiCompiler,
} from '@rspack/core';
import { CompilerTapFn } from './types';

const GLOBAL_PATCHED_SYMBOL: unique symbol = Symbol('GLOBAL_PATCHED_SYMBOL');

Expand All @@ -33,13 +26,14 @@ export function unpatchGlobalLocation() {
}
}

export function patchCompilerGlobalLocation(
compiler:
| WebpackCompiler
| RspackCompiler
| WebpackMultiCompiler
| RspackMultiCompiler,
) {
export function patchCompilerGlobalLocation(compiler: {
hooks: {
run: CompilerTapFn;
watchRun: CompilerTapFn;
watchClose: CompilerTapFn;
done: CompilerTapFn;
};
}) {
// https://github.com/webpack/webpack/blob/136b723023f8f26d71eabdd16badf04c1c8554e4/lib/MultiCompiler.js#L64
compiler.hooks.run.tap('PatchGlobalLocation', patchGlobalLocation);
compiler.hooks.watchRun.tap('PatchGlobalLocation', patchGlobalLocation);
Expand Down
6 changes: 6 additions & 0 deletions packages/builder/builder-shared/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ export type SharedCompiledPkgNames =
| 'toml-loader'
| 'yaml-loader'
| 'assetsRetry.js';

export type CompilerTapFn<
CallBack extends (...args: any[]) => void = () => void,
> = {
tap: (name: string, cb: CallBack) => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const builderPluginAdpaterCopy = (
...(config.builtins || {}),
copy: {
patterns: [
...transformCopy(config.builtins?.copy?.patterns),
...(config.builtins?.copy?.patterns || []),
...createConfigBuiltinCopy(options),
],
},
Expand Down Expand Up @@ -80,22 +80,6 @@ export const builderPluginAdpaterCopy = (
},
});

function transformCopy(
patterns?: string[] | { from: string; [prop: string]: any }[],
) {
if (patterns) {
patterns.map(value => {
if (typeof value === 'string') {
return {
from: value,
};
}
return value;
});
}
return [];
}

function createConfigBuiltinCopy(options: BuilderOptions<'rspack'>) {
const { normalizedConfig, appContext } = options;
const { uploadDir, publicDir } = createCopyInfo(appContext, normalizedConfig);
Expand Down
Loading