From ffd717fefb8413da335e8831d24646757a2a3e74 Mon Sep 17 00:00:00 2001 From: 9aoy Date: Thu, 10 Oct 2024 16:14:10 +0800 Subject: [PATCH] fix: copy configuration should only works in main(web) environment (#6340) --- .changeset/nine-suns-knock.md | 5 + packages/cli/uni-builder/src/types.ts | 1 + .../src/builder/builder-rspack/adapterCopy.ts | 95 ----------- .../src/builder/builder-rspack/index.ts | 2 - .../src/builder/builder-webpack/index.ts | 3 - .../adapterCopy.ts} | 11 +- .../generator/createBuilderProviderConfig.ts | 2 +- .../createCopyPattern.ts | 0 .../generator/getBuilderEnvironments.ts | 17 +- .../app-tools/src/builder/generator/index.ts | 13 +- .../builder/__snapshots__/index.test.ts.snap | 149 ++++++++++-------- .../app-tools/tests/builder/index.test.ts | 13 +- 12 files changed, 131 insertions(+), 180 deletions(-) create mode 100644 .changeset/nine-suns-knock.md delete mode 100644 packages/solutions/app-tools/src/builder/builder-rspack/adapterCopy.ts rename packages/solutions/app-tools/src/builder/{builder-webpack/adapterModern.ts => generator/adapterCopy.ts} (73%) rename packages/solutions/app-tools/src/builder/{builder-webpack => generator}/createCopyPattern.ts (100%) diff --git a/.changeset/nine-suns-knock.md b/.changeset/nine-suns-knock.md new file mode 100644 index 000000000000..f7068d0e4193 --- /dev/null +++ b/.changeset/nine-suns-knock.md @@ -0,0 +1,5 @@ +--- +'@modern-js/app-tools': patch +--- + +fix: copy configuration should only works in main(web) environment diff --git a/packages/cli/uni-builder/src/types.ts b/packages/cli/uni-builder/src/types.ts index 6c9b6f7e4a02..9f86116beb03 100644 --- a/packages/cli/uni-builder/src/types.ts +++ b/packages/cli/uni-builder/src/types.ts @@ -379,6 +379,7 @@ export type OverridesUniBuilderInstance = { plugins: Array, options?: { before?: string; + environment?: string; }, ) => void; }; diff --git a/packages/solutions/app-tools/src/builder/builder-rspack/adapterCopy.ts b/packages/solutions/app-tools/src/builder/builder-rspack/adapterCopy.ts deleted file mode 100644 index efde7a97b282..000000000000 --- a/packages/solutions/app-tools/src/builder/builder-rspack/adapterCopy.ts +++ /dev/null @@ -1,95 +0,0 @@ -import path from 'path'; -import type { RsbuildPlugin } from '@modern-js/uni-builder'; -import { logger, removeTailSlash } from '@modern-js/utils'; -import fs from '@modern-js/utils/fs-extra'; -import { type BuilderOptions, createCopyInfo } from '../shared'; - -export const builderPluginAdpaterCopy = ( - options: BuilderOptions<'rspack'>, -): RsbuildPlugin => ({ - name: 'builder-plugin-adapter-rspack-copy', - setup(api) { - let publicPath: string | undefined | ((...args: any[]) => string); - api.modifyBundlerChain((chain, { CHAIN_ID }) => { - chain.plugin(CHAIN_ID.PLUGIN.COPY).tap(args => [ - { - patterns: [ - ...(args[0]?.patterns || []), - ...createConfigBuiltinCopy(options), - ], - }, - ]); - }); - api.modifyRspackConfig(config => { - publicPath = config.output?.publicPath; - }); - api.onDevCompileDone(async () => { - await transformHtmlFiles(); - }); - - api.onAfterBuild(async () => { - await transformHtmlFiles(); - }); - - /** transform public/*.html manaully */ - async function transformHtmlFiles() { - const { normalizedConfig } = options; - const publicDir = path.resolve( - normalizedConfig.output.distPath?.root || './dist', - './public', - ); - - if (!fs.existsSync(publicDir) || !fs.statSync(publicDir).isDirectory()) { - return; - } - - const HTML_REGEXP = /\.html?$/; - - const filepaths = (await fs.readdir(publicDir)).map(file => - path.resolve(publicDir, file), - ); - - await Promise.all( - filepaths - .filter(file => HTML_REGEXP.test(file)) - .map(async file => { - const content = await fs.readFile(file, 'utf-8'); - if (publicPath && typeof publicPath === 'string') { - await fs.writeFile( - file, - content.replace( - /<%=\s*assetPrefix\s*%>/g, - removeTailSlash(publicPath), - ), - ); - } else { - logger.warn( - `Expect get a string from \`publicPath\`, but receive \`${typeof publicPath}\`.`, - ); - await fs.writeFile( - file, - content.replace( - /<%=\s*assetPrefix\s*%>/g, - removeTailSlash('/'), - ), - ); - } - }), - ); - } - }, -}); - -function createConfigBuiltinCopy(options: BuilderOptions<'rspack'>) { - const { normalizedConfig, appContext } = options; - const { publicDir } = createCopyInfo(appContext, normalizedConfig); - - return [ - { - from: '**/*', - to: 'public', - context: publicDir, - noErrorOnMissing: true, - }, - ]; -} diff --git a/packages/solutions/app-tools/src/builder/builder-rspack/index.ts b/packages/solutions/app-tools/src/builder/builder-rspack/index.ts index 4763fc5e6a0c..7ceaab5c971d 100644 --- a/packages/solutions/app-tools/src/builder/builder-rspack/index.ts +++ b/packages/solutions/app-tools/src/builder/builder-rspack/index.ts @@ -1,11 +1,9 @@ import { generateBuilder } from '../generator'; import type { BuilderOptions } from '../shared'; -import { builderPluginAdpaterCopy } from './adapterCopy'; export async function createRspackBuilderForModern( options: BuilderOptions<'rspack'>, ) { const builder = await generateBuilder(options, 'rspack'); - builder.addPlugins([builderPluginAdpaterCopy(options)]); return builder; } diff --git a/packages/solutions/app-tools/src/builder/builder-webpack/index.ts b/packages/solutions/app-tools/src/builder/builder-webpack/index.ts index 3eb778222ec2..b6f69b209825 100644 --- a/packages/solutions/app-tools/src/builder/builder-webpack/index.ts +++ b/packages/solutions/app-tools/src/builder/builder-webpack/index.ts @@ -1,6 +1,5 @@ import { generateBuilder } from '../generator'; import type { BuilderOptions } from '../shared'; -import { builderPluginAdapterModern } from './adapterModern'; export async function createWebpackBuilderForModern( options: BuilderOptions<'webpack'>, @@ -16,7 +15,5 @@ export async function createWebpackBuilderForModern( builder.addPlugins([pluginEsbuild(esbuildOptions)]); } - builder.addPlugins([builderPluginAdapterModern(options)]); - return builder; } diff --git a/packages/solutions/app-tools/src/builder/builder-webpack/adapterModern.ts b/packages/solutions/app-tools/src/builder/generator/adapterCopy.ts similarity index 73% rename from packages/solutions/app-tools/src/builder/builder-webpack/adapterModern.ts rename to packages/solutions/app-tools/src/builder/generator/adapterCopy.ts index 2b587aa4ad1f..bd0bd9b9d4f4 100644 --- a/packages/solutions/app-tools/src/builder/builder-webpack/adapterModern.ts +++ b/packages/solutions/app-tools/src/builder/generator/adapterCopy.ts @@ -2,18 +2,15 @@ import type { RsbuildPlugin } from '@modern-js/uni-builder'; import type { BuilderOptions } from '../shared'; import { createPublicPattern } from './createCopyPattern'; -/** - * Provides default configuration consistent with modern.js v1 - */ -export const builderPluginAdapterModern = ( - options: BuilderOptions<'webpack'>, +export const builderPluginAdapterCopy = ( + options: BuilderOptions<'shared'>, ): RsbuildPlugin => ({ - name: 'builder-plugin-adapter-modern', + name: 'builder-plugin-adapter-copy', setup(api) { const { normalizedConfig: modernConfig, appContext } = options; - api.modifyWebpackChain((chain, { CHAIN_ID }) => { + api.modifyBundlerChain((chain, { CHAIN_ID }) => { // apply copy plugin if (chain.plugins.has(CHAIN_ID.PLUGIN.COPY)) { const defaultCopyPattern = createPublicPattern( diff --git a/packages/solutions/app-tools/src/builder/generator/createBuilderProviderConfig.ts b/packages/solutions/app-tools/src/builder/generator/createBuilderProviderConfig.ts index d58a343aca4f..019cd40047df 100644 --- a/packages/solutions/app-tools/src/builder/generator/createBuilderProviderConfig.ts +++ b/packages/solutions/app-tools/src/builder/generator/createBuilderProviderConfig.ts @@ -1,5 +1,5 @@ import type { AppNormalizedConfig, Bundler, IAppContext } from '../../types'; -import { createUploadPattern } from '../builder-webpack/createCopyPattern'; +import { createUploadPattern } from './createCopyPattern'; function modifyOutputConfig( config: AppNormalizedConfig, diff --git a/packages/solutions/app-tools/src/builder/builder-webpack/createCopyPattern.ts b/packages/solutions/app-tools/src/builder/generator/createCopyPattern.ts similarity index 100% rename from packages/solutions/app-tools/src/builder/builder-webpack/createCopyPattern.ts rename to packages/solutions/app-tools/src/builder/generator/createCopyPattern.ts diff --git a/packages/solutions/app-tools/src/builder/generator/getBuilderEnvironments.ts b/packages/solutions/app-tools/src/builder/generator/getBuilderEnvironments.ts index a4696f7f8229..762d5b439238 100644 --- a/packages/solutions/app-tools/src/builder/generator/getBuilderEnvironments.ts +++ b/packages/solutions/app-tools/src/builder/generator/getBuilderEnvironments.ts @@ -7,11 +7,12 @@ import { isUseSSRBundle, } from '@modern-js/utils'; import type { RsbuildConfig } from '@rsbuild/core'; -import type { AppNormalizedConfig } from '../../types'; +import type { AppNormalizedConfig, Bundler } from '../../types'; -export function getBuilderEnvironments( +export function getBuilderEnvironments( normalizedConfig: AppNormalizedConfig<'shared'>, appContext: IAppContext, + tempBuilderConfig: Omit, 'plugins'>, ) { // create entries type Entries = Record; @@ -49,6 +50,13 @@ export function getBuilderEnvironments( }, }; + // copy config should only works in main (web) environment + if (tempBuilderConfig.output?.copy) { + environments.web.output!.copy = tempBuilderConfig.output.copy; + + delete tempBuilderConfig.output.copy; + } + const useNodeTarget = isProd() ? isUseSSRBundle(normalizedConfig) : isSSR(normalizedConfig); @@ -77,5 +85,8 @@ export function getBuilderEnvironments( }; } - return environments; + return { + environments, + builderConfig: tempBuilderConfig, + }; } diff --git a/packages/solutions/app-tools/src/builder/generator/index.ts b/packages/solutions/app-tools/src/builder/generator/index.ts index 0d3947343713..9d3a0ec6ddb4 100644 --- a/packages/solutions/app-tools/src/builder/generator/index.ts +++ b/packages/solutions/app-tools/src/builder/generator/index.ts @@ -6,6 +6,7 @@ import { import { mergeRsbuildConfig } from '@rsbuild/core'; import type { Bundler } from '../../types'; import type { BuilderOptions } from '../shared'; +import { builderPluginAdapterCopy } from './adapterCopy'; import { createBuilderProviderConfig } from './createBuilderProviderConfig'; import { getBuilderEnvironments } from './getBuilderEnvironments'; @@ -21,12 +22,16 @@ export async function generateBuilder( const { normalizedConfig, appContext } = options; // create provider - const builderConfig = createBuilderProviderConfig( + const tempBuilderConfig = createBuilderProviderConfig( normalizedConfig, appContext, ); - const environments = getBuilderEnvironments(normalizedConfig, appContext); + const { environments, builderConfig } = getBuilderEnvironments( + normalizedConfig, + appContext, + tempBuilderConfig, + ); builderConfig.environments = builderConfig.environments ? mergeRsbuildConfig(environments, builderConfig.environments) @@ -60,6 +65,10 @@ async function applyBuilderPlugins( builderPluginAdapterHtml(options), ]); + builder.addPlugins([builderPluginAdapterCopy(options)], { + environment: 'web', + }); + const { normalizedConfig } = options; if (!normalizedConfig.output.disableNodePolyfill) { const { pluginNodePolyfill } = await import( diff --git a/packages/solutions/app-tools/tests/builder/__snapshots__/index.test.ts.snap b/packages/solutions/app-tools/tests/builder/__snapshots__/index.test.ts.snap index bc84cafd89f8..8f90f6a4a337 100644 --- a/packages/solutions/app-tools/tests/builder/__snapshots__/index.test.ts.snap +++ b/packages/solutions/app-tools/tests/builder/__snapshots__/index.test.ts.snap @@ -2,19 +2,22 @@ exports[`create builder Options test create builder environments config 1`] = ` { - "web": { - "output": { - "target": "web", - }, - "source": { - "entry": { - "main": [ - "src/index.ts", - "src/main.ts", - ], - "next": [ - "src/next.ts", - ], + "builderConfig": {}, + "environments": { + "web": { + "output": { + "target": "web", + }, + "source": { + "entry": { + "main": [ + "src/index.ts", + "src/main.ts", + ], + "next": [ + "src/next.ts", + ], + }, }, }, }, @@ -23,35 +26,38 @@ exports[`create builder Options test create builder environments config 1`] = ` exports[`create builder Options test create builder environments config 2`] = ` { - "node": { - "output": { - "target": "node", - }, - "source": { - "entry": { - "main": [ - "src/index.ts", - "src/main.ts", - ], - "next": [ - "src/next.ts", - ], + "builderConfig": {}, + "environments": { + "node": { + "output": { + "target": "node", + }, + "source": { + "entry": { + "main": [ + "src/index.ts", + "src/main.ts", + ], + "next": [ + "src/next.ts", + ], + }, }, }, - }, - "web": { - "output": { - "target": "web", - }, - "source": { - "entry": { - "main": [ - "src/index.ts", - "src/main.ts", - ], - "next": [ - "src/next.ts", - ], + "web": { + "output": { + "target": "web", + }, + "source": { + "entry": { + "main": [ + "src/index.ts", + "src/main.ts", + ], + "next": [ + "src/next.ts", + ], + }, }, }, }, @@ -60,35 +66,46 @@ exports[`create builder Options test create builder environments config 2`] = ` exports[`create builder Options test create builder environments config 3`] = ` { - "web": { - "output": { - "target": "web", - }, - "source": { - "entry": { - "main": [ - "src/index.ts", - "src/main.ts", - ], - "next": [ - "src/next.ts", + "builderConfig": { + "output": {}, + }, + "environments": { + "web": { + "output": { + "copy": [ + { + "from": "**/*", + "to": "upload", + }, ], + "target": "web", + }, + "source": { + "entry": { + "main": [ + "src/index.ts", + "src/main.ts", + ], + "next": [ + "src/next.ts", + ], + }, }, }, - }, - "workerSSR": { - "output": { - "target": "web-worker", - }, - "source": { - "entry": { - "main": [ - "src/index.ts", - "src/main.ts", - ], - "next": [ - "src/next.ts", - ], + "workerSSR": { + "output": { + "target": "web-worker", + }, + "source": { + "entry": { + "main": [ + "src/index.ts", + "src/main.ts", + ], + "next": [ + "src/next.ts", + ], + }, }, }, }, diff --git a/packages/solutions/app-tools/tests/builder/index.test.ts b/packages/solutions/app-tools/tests/builder/index.test.ts index 81854c757473..da3efc70f34c 100644 --- a/packages/solutions/app-tools/tests/builder/index.test.ts +++ b/packages/solutions/app-tools/tests/builder/index.test.ts @@ -32,7 +32,7 @@ describe('create builder Options', () => { }; expect( - getBuilderEnvironments({} as any, appContext as any), + getBuilderEnvironments({} as any, appContext as any, {} as any), ).toMatchSnapshot(); expect( @@ -43,6 +43,7 @@ describe('create builder Options', () => { }, } as any, appContext as any, + {} as any, ), ).toMatchSnapshot(); @@ -59,6 +60,16 @@ describe('create builder Options', () => { }, } as any, appContext as any, + { + output: { + copy: [ + { + from: '**/*', + to: 'upload', + }, + ], + }, + } as any, ), ).toMatchSnapshot(); });