Skip to content

Commit

Permalink
fix: mf-dev should apply correct config (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Dec 25, 2024
1 parent fcbdfa5 commit 76f8713
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default defineConfig({
},
{
format: 'mf',
dts: true,
output: {
distPath: {
root: './dist/mf',
Expand Down Expand Up @@ -52,6 +51,9 @@ export default defineConfig({
],
},
],
source: {
tsconfigPath: './tsconfig.build.json',
},
// just for dev
server: {
port: 3001,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src"]
}
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"tinyglobby": "^0.2.10"
},
"devDependencies": {
"@module-federation/rsbuild-plugin": "^0.8.5",
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "^11.0.4",
"chokidar": "^4.0.3",
Expand Down
9 changes: 1 addition & 8 deletions packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,10 @@ async function initMFRsbuild(
plugins: config.plugins,
dev: {
...(config.dev ?? {}),
// TODO: remove this after Rsbuild fix that the environment config of writeToDisk not takes effect
writeToDisk: true,
},
server: config.server,
tools: {
rspack: {
optimization: {
nodeEnv: 'development',
moduleIds: 'named',
},
},
},
environments: selectedEnvironments,
},
});
Expand Down
24 changes: 15 additions & 9 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,17 +616,23 @@ const composeFormatConfig = ({
}
case 'mf':
return {
dev: {
writeToDisk: true,
},
tools: {
rspack: {
output: {
rspack: (config, { env }) => {
config.output = {
...config.output,
uniqueName: pkgJson.name as string,
},
// can not set nodeEnv to false, because mf format should build shared module.
// If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced
optimization: {
nodeEnv: 'production',
moduleIds: 'deterministic',
},
};

config.optimization = {
...config.optimization,
// can not set nodeEnv to false, because mf format should build shared module.
// If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced
nodeEnv: env === 'development' ? 'development' : 'production',
moduleIds: env === 'development' ? 'named' : 'deterministic',
};
},
},
output: {
Expand Down
156 changes: 155 additions & 1 deletion packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1`] = `
exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config in each format 1`] = `
[
{
"config": {
Expand Down Expand Up @@ -696,5 +696,159 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
"format": "umd",
},
{
"config": {
"dev": {
"progressBar": false,
"writeToDisk": true,
},
"output": {
"distPath": {
"css": "./",
"cssAsync": "./",
"js": "./",
"jsAsync": "./",
},
"filename": {
"js": "[name].js",
},
"filenameHash": false,
"minify": {
"css": false,
"js": true,
"jsOptions": {
"minimizerOptions": {
"compress": {
"dead_code": true,
"defaults": false,
"toplevel": false,
"unused": true,
},
"format": {
"comments": "some",
"preserve_annotations": true,
},
"mangle": false,
"minify": true,
},
},
},
"overrideBrowserslist": [
"last 1 Chrome versions",
"last 1 Firefox versions",
"last 1 Edge versions",
"last 1 Safari versions",
"last 1 ios_saf versions",
"not dead",
],
"target": "web",
},
"performance": {
"chunkSplit": {
"strategy": "custom",
},
},
"plugins": [
{
"name": "rsbuild:lib-entry-chunk",
"setup": [Function],
},
{
"name": "rsbuild:module-federation-enhanced",
"setup": [Function],
},
],
"resolve": {
"alias": {
"bar": "bar",
"foo": "foo",
},
},
"source": {
"entry": {},
"preEntry": "./a.js",
},
"tools": {
"htmlPlugin": false,
"rspack": [
{
"experiments": {
"rspackFuture": {
"bundlerInfo": {
"force": false,
},
},
},
"optimization": {
"moduleIds": "named",
"nodeEnv": false,
"splitChunks": {
"chunks": "async",
},
},
"resolve": {
"extensionAlias": {
".cjs": [
".cts",
".cjs",
],
".js": [
".ts",
".tsx",
".js",
".jsx",
],
".jsx": [
".tsx",
".jsx",
],
".mjs": [
".mts",
".mjs",
],
},
},
},
[Function],
[Function],
{
"target": [
"web",
],
},
{
"externalsType": "global",
},
{
"plugins": [
EntryChunkPlugin {
"enabledImportMetaUrlShim": false,
"reactDirectives": {},
"shebangChmod": 493,
"shebangEntries": {},
"shebangInjectedAssets": Set {},
},
],
},
{
"resolve": {
"extensionAlias": {
".js": [
".ts",
".tsx",
],
},
},
},
],
"swc": {
"jsc": {
"externalHelpers": false,
},
},
},
},
"format": "mf",
},
]
`;
7 changes: 6 additions & 1 deletion packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'node:path';
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import { describe, expect, test, vi } from 'vitest';
import {
composeCreateRsbuildConfig,
Expand Down Expand Up @@ -148,7 +149,7 @@ describe('Should load config file correctly', () => {
});

describe('Should compose create Rsbuild config correctly', () => {
test('Merge Rsbuild config', async () => {
test('Merge Rsbuild config in each format', async () => {
const rslibConfig: RslibConfig = {
lib: [
{
Expand Down Expand Up @@ -176,6 +177,10 @@ describe('Should compose create Rsbuild config correctly', () => {
{
format: 'umd',
},
{
format: 'mf',
plugins: [pluginModuleFederation({})],
},
],
source: {
preEntry: './a.js',
Expand Down
7 changes: 6 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 0 additions & 46 deletions tests/integration/cli/mf-dev/dev.test.ts

This file was deleted.

40 changes: 0 additions & 40 deletions tests/integration/cli/mf-dev/rslib.config.ts

This file was deleted.

6 changes: 6 additions & 0 deletions tests/integration/cli/mf/build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "cli-mf-build-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
6 changes: 6 additions & 0 deletions tests/integration/cli/mf/build/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rslib/core';
import { generateBundleMFConfig } from 'test-helper';

export default defineConfig({
lib: [generateBundleMFConfig({ name: 'test-build' })],
});
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 76f8713

Please sign in to comment.