Skip to content

Commit

Permalink
pass options top minimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
simonxabris committed Jul 10, 2024
1 parent 399b7fc commit 3019604
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 13 deletions.
6 changes: 6 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,12 @@ export interface RawStatsOptions {
colors: boolean
}

export interface RawSwcCssMinimizerRspackPluginOptions {
test?: string | RegExp | (string | RegExp)[]
include?: string | RegExp | (string | RegExp)[]
exclude?: string | RegExp | (string | RegExp)[]
}

export interface RawSwcJsMinimizerRspackPluginOptions {
extractComments?: RawExtractComments
compress: any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ mod raw_mf;
mod raw_progress;
mod raw_runtime_chunk;
mod raw_size_limits;
mod raw_swc_css_minimizer;
mod raw_swc_js_minimizer;

use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown};
use napi_derive::napi;
use raw_lightning_css_minimizer::RawLightningCssMinimizerRspackPluginOptions;
use raw_swc_css_minimizer::RawSwcCssMinimizerRspackPluginOptions;
use rspack_core::{BoxPlugin, Plugin, PluginExt};
use rspack_error::Result;
use rspack_ids::{
Expand Down Expand Up @@ -447,7 +449,11 @@ impl BuiltinPlugin {
plugins.push(plugin);
}
BuiltinPluginName::SwcCssMinimizerRspackPlugin => {
plugins.push(SwcCssMinimizerRspackPlugin::default().boxed())
let plugin = SwcCssMinimizerRspackPlugin::new(
downcast_into::<RawSwcCssMinimizerRspackPluginOptions>(self.options)?.try_into()?,
)
.boxed();
plugins.push(plugin);
}
BuiltinPluginName::LightningCssMinimizerRspackPlugin => plugins.push(
LightningCssMinimizerRspackPlugin::new(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use napi::{bindgen_prelude::Either3, Either};
use napi_derive::napi;
use rspack_error::Result;
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_plugin_swc_css_minimizer::{
SwcCssMinimizerRspackPluginOptions, SwcCssMinimizerRule, SwcCssMinimizerRules,
};

type RawSwcCssMinimizerRule = Either<String, JsRegExp>;
type RawSwcCssMinimizerRules = Either3<String, JsRegExp, Vec<RawSwcCssMinimizerRule>>;
struct RawSwcCssMinimizerRuleWrapper(RawSwcCssMinimizerRule);
struct RawSwcCssMinimizerRulesWrapper(RawSwcCssMinimizerRules);

#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawSwcCssMinimizerRspackPluginOptions {
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub test: Option<RawSwcCssMinimizerRules>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub include: Option<RawSwcCssMinimizerRules>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub exclude: Option<RawSwcCssMinimizerRules>,
}

fn into_condition(c: Option<RawSwcCssMinimizerRules>) -> Option<SwcCssMinimizerRules> {
c.map(|test| RawSwcCssMinimizerRulesWrapper(test).into())
}

impl TryFrom<RawSwcCssMinimizerRspackPluginOptions> for SwcCssMinimizerRspackPluginOptions {
type Error = rspack_error::Error;

fn try_from(value: RawSwcCssMinimizerRspackPluginOptions) -> Result<Self> {
Ok(Self {
test: into_condition(value.test),
include: into_condition(value.include),
exclude: into_condition(value.exclude),
..Default::default()
})
}
}

impl From<RawSwcCssMinimizerRuleWrapper> for SwcCssMinimizerRule {
fn from(x: RawSwcCssMinimizerRuleWrapper) -> Self {
match x.0 {
Either::A(v) => Self::String(v),
Either::B(v) => Self::Regexp(v.to_rspack_regex()),
}
}
}

impl From<RawSwcCssMinimizerRulesWrapper> for SwcCssMinimizerRules {
fn from(value: RawSwcCssMinimizerRulesWrapper) -> Self {
match value.0 {
Either3::A(v) => Self::String(v),
Either3::B(v) => Self::Regexp(v.to_rspack_regex()),
Either3::C(v) => Self::Array(
v.into_iter()
.map(|v| RawSwcCssMinimizerRuleWrapper(v).into())
.collect(),
),
}
}
}
6 changes: 6 additions & 0 deletions crates/rspack_plugin_swc_css_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pub struct SwcCssMinimizerRspackPlugin {
options: SwcCssMinimizerRspackPluginOptions,
}

impl SwcCssMinimizerRspackPlugin {
pub fn new(options: SwcCssMinimizerRspackPluginOptions) -> Self {
Self::new_inner(options)
}
}

#[plugin_hook(CompilationProcessAssets for SwcCssMinimizerRspackPlugin, stage = Compilation::PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE)]
async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
let minify_options = &self.options;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import "./a.css";

consol.log("a");
require("./a.css");
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import "./b.css";

consol.log("b");
require("./b.css");
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ it("[minify-exclude-css]: chunk a should be minified", () => {

it("[minify-exclude-css]: chunk b should not be minified", () => {
const content = fs.readFileSync(path.resolve(__dirname, "b.css"), "utf-8");
expect(content).not.toMatch("\n");
expect(false).toBe(true);
expect(content).toMatch("\n");
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ module.exports = {
output: {
filename: "[name].js"
},
experiments: {
css: false
module: {
generator: {
"css/auto": {
exportsOnly: false
}
}
},
optimization: {
minimize: true,
Expand Down
9 changes: 9 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import type { RawOptions } from '@rspack/binding';
import { RawProgressPluginOptions } from '@rspack/binding';
import { RawRuntimeChunkOptions } from '@rspack/binding';
import { RawSourceMapDevToolPluginOptions } from '@rspack/binding';
import { RawSwcCssMinimizerRspackPluginOptions } from '@rspack/binding';
import { RawSwcJsMinimizerRspackPluginOptions } from '@rspack/binding';
import { registerGlobalTrace } from '@rspack/binding';
import { RspackOptionsNormalized as RspackOptionsNormalized_2 } from '.';
import sources = require('../compiled/webpack-sources');
Expand Down Expand Up @@ -13149,15 +13151,22 @@ const strictModuleExceptionHandling: z.ZodBoolean;
export const SwcCssMinimizerRspackPlugin: {
new (options?: SwcCssMinimizerRspackPluginOptions | undefined): {
name: BuiltinPluginName;
<<<<<<< HEAD
_args: [options?: any];
affectedHooks: "done" | "compilation" | "failed" | "environment" | "emit" | "make" | "compile" | "afterEmit" | "invalid" | "thisCompilation" | "afterDone" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "shutdown" | "watchRun" | "watchClose" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | undefined;
raw(compiler: Compiler_2): BuiltinPlugin;
=======
_options: RawSwcCssMinimizerRspackPluginOptions;
affectedHooks: "make" | "compile" | "emit" | "afterEmit" | "invalid" | "done" | "thisCompilation" | "afterDone" | "compilation" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "failed" | "shutdown" | "watchRun" | "watchClose" | "environment" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | undefined;
raw(): BuiltinPlugin;
>>>>>>> 3e341df52 (pass options top minimizer)
apply(compiler: Compiler_2): void;
};
};

// @public (undocumented)
type SwcCssMinimizerRspackPluginOptions = {
test?: MinifyConditions_2;
exclude?: MinifyConditions_2;
include?: MinifyConditions_2;
};
Expand Down
16 changes: 14 additions & 2 deletions packages/rspack/src/builtin-plugin/SwcCssMinimizerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { BuiltinPluginName } from "@rspack/binding";
import {
BuiltinPluginName,
RawSwcCssMinimizerRspackPluginOptions
} from "@rspack/binding";

import { create } from "./base";

type MinifyCondition = string | RegExp;
type MinifyConditions = MinifyCondition | MinifyCondition[];

export type SwcCssMinimizerRspackPluginOptions = {
test?: MinifyConditions;
exclude?: MinifyConditions;
include?: MinifyConditions;
};

export const SwcCssMinimizerRspackPlugin = create(
BuiltinPluginName.SwcCssMinimizerRspackPlugin,
(options?: SwcCssMinimizerRspackPluginOptions) => undefined
(
options?: SwcCssMinimizerRspackPluginOptions
): RawSwcCssMinimizerRspackPluginOptions => {
return {
test: options?.test,
include: options?.include,
exclude: options?.exclude
};
}
);

0 comments on commit 3019604

Please sign in to comment.