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: support apply more environment configs #2656

Merged
merged 3 commits into from
Jun 21, 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
12 changes: 9 additions & 3 deletions packages/compat/plugin-swc/src/minimizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type NormalizedConfig, __internalHelper, logger } from '@rsbuild/core';
import {
type NormalizedEnvironmentConfig,
__internalHelper,
logger,
} from '@rsbuild/core';
import { color, deepmerge } from '@rsbuild/shared';
import type { webpack } from '@rsbuild/webpack';
import { minify, minifyCss } from './binding';
Expand Down Expand Up @@ -33,7 +37,7 @@ export class SwcMinimizerPlugin {
constructor(options: {
jsMinify?: boolean | JsMinifyOptions;
cssMinify?: boolean | CssMinifyOptions;
rsbuildConfig: NormalizedConfig;
rsbuildConfig: NormalizedEnvironmentConfig;
}) {
this.minifyOptions = {
jsMinify: options.jsMinify
Expand All @@ -48,7 +52,9 @@ export class SwcMinimizerPlugin {
};
}

getDefaultJsMinifyOptions(rsbuildConfig: NormalizedConfig): JsMinifyOptions {
getDefaultJsMinifyOptions(
rsbuildConfig: NormalizedEnvironmentConfig,
): JsMinifyOptions {
const options = {
...__internalHelper.getSwcMinimizerOptions(rsbuildConfig),
mangle: true,
Expand Down
10 changes: 5 additions & 5 deletions packages/compat/plugin-swc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export const pluginSwc = (options: PluginSwcOptions = {}): RsbuildPlugin => ({
// loader should be applied in the pre stage for customizing
order: 'pre',
handler: async (chain, utils) => {
const { CHAIN_ID } = utils;
const rsbuildConfig = api.getNormalizedConfig();
const { CHAIN_ID, environment } = utils;
const rsbuildConfig = api.getNormalizedConfig({ environment });
const { rootPath } = api.context;

const { browserslist } = api.context.environments[utils.environment];
const { browserslist } = api.context.environments[environment];
const swcConfigs = await applyPluginConfig(
options,
utils,
Expand Down Expand Up @@ -119,8 +119,8 @@ export const pluginSwc = (options: PluginSwcOptions = {}): RsbuildPlugin => ({
},
});

api.modifyBundlerChain((chain, { CHAIN_ID, isProd }) => {
const rsbuildConfig = api.getNormalizedConfig();
api.modifyBundlerChain((chain, { CHAIN_ID, isProd, environment }) => {
const rsbuildConfig = api.getNormalizedConfig({ environment });

if (checkUseMinify(mainConfig, rsbuildConfig, isProd)) {
const { minify } = rsbuildConfig.output;
Expand Down
9 changes: 6 additions & 3 deletions packages/compat/plugin-swc/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';
import { __internalHelper } from '@rsbuild/core';
import type { ModifyChainUtils, NormalizedConfig } from '@rsbuild/core';
import type {
ModifyChainUtils,
NormalizedEnvironmentConfig,
} from '@rsbuild/core';
import { getCoreJsVersion } from '@rsbuild/shared';
import _ from 'lodash';
import semver from 'semver';
Expand Down Expand Up @@ -74,7 +77,7 @@ export async function determinePresetReact(

export function checkUseMinify(
options: ObjPluginSwcOptions,
config: NormalizedConfig,
config: NormalizedEnvironmentConfig,
isProd: boolean,
) {
return (
Expand Down Expand Up @@ -174,7 +177,7 @@ const getDefaultStyledComponentsConfig = (isProd: boolean, ssr: boolean) => {
export async function applyPluginConfig(
rawOptions: PluginSwcOptions,
utils: ModifyChainUtils,
rsbuildConfig: NormalizedConfig,
rsbuildConfig: NormalizedEnvironmentConfig,
rootPath: string,
browserslist?: string[],
): Promise<FinalizedConfig[]> {
Expand Down
255 changes: 255 additions & 0 deletions packages/compat/plugin-swc/tests/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,260 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`plugin-swc > should apply multiply environment configs correctly 1`] = `
[
{
"module": {
"rules": [
{
"include": [
{
"and": [
"<ROOT>/packages/compat/plugin-swc/tests",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"use": [
{
"loader": "<ROOT>/packages/compat/plugin-swc/src/loader.cjs",
"options": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"env": {
"coreJs": "3.36",
"mode": "usage",
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"extensions": {
"lockCorejsVersion": {
"corejs": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"swcHelpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"lodash": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"ids": [
"lodash",
"lodash-es",
],
},
},
"jsc": {
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": true,
},
"preserveAllComments": true,
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
"react": {
"refresh": true,
"runtime": "classic",
},
},
},
},
},
],
},
{
"mimetype": {
"or": [
"text/javascript",
"application/javascript",
],
},
"resolve": {
"fullySpecified": false,
},
"use": [
{
"loader": "<ROOT>/packages/compat/plugin-swc/src/loader.cjs",
"options": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"env": {
"coreJs": "3.36",
"mode": "usage",
"targets": [
"chrome >= 87",
"edge >= 88",
"firefox >= 78",
"safari >= 14",
],
},
"extensions": {
"lockCorejsVersion": {
"corejs": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"swcHelpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"lodash": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"ids": [
"lodash",
"lodash-es",
],
},
},
"jsc": {
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": true,
},
"preserveAllComments": true,
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
"react": {
"refresh": true,
"runtime": "classic",
},
},
},
},
},
],
},
],
},
},
{
"module": {
"rules": [
{
"include": [
{
"and": [
"<ROOT>/packages/compat/plugin-swc/tests",
{
"not": /\\[\\\\\\\\/\\]node_modules\\[\\\\\\\\/\\]/,
},
],
},
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
],
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"use": [
{
"loader": "<ROOT>/packages/compat/plugin-swc/src/loader.cjs",
"options": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"env": {
"coreJs": "3.36",
"mode": "usage",
"targets": [
"node >= 16",
],
},
"extensions": {
"lockCorejsVersion": {
"corejs": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"swcHelpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"lodash": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"ids": [
"lodash",
"lodash-es",
],
},
},
"jsc": {
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": true,
},
"preserveAllComments": true,
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true,
"react": {
"refresh": false,
"runtime": "classic",
},
"useDefineForClassFields": false,
},
},
},
},
],
},
{
"mimetype": {
"or": [
"text/javascript",
"application/javascript",
],
},
"resolve": {
"fullySpecified": false,
},
"use": [
{
"loader": "<ROOT>/packages/compat/plugin-swc/src/loader.cjs",
"options": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"env": {
"coreJs": "3.36",
"mode": "usage",
"targets": [
"node >= 16",
],
},
"extensions": {
"lockCorejsVersion": {
"corejs": "<ROOT>/node_modules/<PNPM_INNER>/core-js",
"swcHelpers": "<ROOT>/node_modules/<PNPM_INNER>/@swc/helpers",
},
"lodash": {
"cwd": "<ROOT>/packages/compat/plugin-swc/tests",
"ids": [
"lodash",
"lodash-es",
],
},
},
"jsc": {
"externalHelpers": true,
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": true,
},
"preserveAllComments": true,
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true,
"react": {
"refresh": false,
"runtime": "classic",
},
"useDefineForClassFields": false,
},
},
},
},
],
},
],
},
},
]
`;

exports[`plugin-swc > should apply source.include and source.exclude correctly 1`] = `
{
"module": {
Expand Down
Loading
Loading