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

fix: copy configuration should only works in main(web) environment #6340

Merged
merged 2 commits into from
Oct 10, 2024
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
5 changes: 5 additions & 0 deletions .changeset/nine-suns-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-js/app-tools': patch
---

fix: copy configuration should only works in main(web) environment
1 change: 1 addition & 0 deletions packages/cli/uni-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export type OverridesUniBuilderInstance = {
plugins: Array<UniBuilderPlugin | LooseRsbuildPlugin>,
options?: {
before?: string;
environment?: string;
},
) => void;
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { generateBuilder } from '../generator';
import type { BuilderOptions } from '../shared';
import { builderPluginAdapterModern } from './adapterModern';

export async function createWebpackBuilderForModern(
options: BuilderOptions<'webpack'>,
Expand All @@ -16,7 +15,5 @@ export async function createWebpackBuilderForModern(
builder.addPlugins([pluginEsbuild(esbuildOptions)]);
}

builder.addPlugins([builderPluginAdapterModern(options)]);

return builder;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AppNormalizedConfig, Bundler, IAppContext } from '../../types';
import { createUploadPattern } from '../builder-webpack/createCopyPattern';
import { createUploadPattern } from './createCopyPattern';

function modifyOutputConfig<B extends Bundler>(
config: AppNormalizedConfig<B>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<B extends Bundler>(
normalizedConfig: AppNormalizedConfig<'shared'>,
appContext: IAppContext,
tempBuilderConfig: Omit<AppNormalizedConfig<B>, 'plugins'>,
) {
// create entries
type Entries = Record<string, string[]>;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -77,5 +85,8 @@ export function getBuilderEnvironments(
};
}

return environments;
return {
environments,
builderConfig: tempBuilderConfig,
};
}
13 changes: 11 additions & 2 deletions packages/solutions/app-tools/src/builder/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -21,12 +22,16 @@ export async function generateBuilder<B extends Bundler>(
const { normalizedConfig, appContext } = options;

// create provider
const builderConfig = createBuilderProviderConfig<B>(
const tempBuilderConfig = createBuilderProviderConfig<B>(
normalizedConfig,
appContext,
);

const environments = getBuilderEnvironments(normalizedConfig, appContext);
const { environments, builderConfig } = getBuilderEnvironments(
normalizedConfig,
appContext,
tempBuilderConfig,
);

builderConfig.environments = builderConfig.environments
? mergeRsbuildConfig(environments, builderConfig.environments)
Expand Down Expand Up @@ -60,6 +65,10 @@ async function applyBuilderPlugins<B extends Bundler>(
builderPluginAdapterHtml(options),
]);

builder.addPlugins([builderPluginAdapterCopy(options)], {
environment: 'web',
});

const { normalizedConfig } = options;
if (!normalizedConfig.output.disableNodePolyfill) {
const { pluginNodePolyfill } = await import(
Expand Down
Loading
Loading