diff --git a/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs b/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs index 8f98c61550f..91b744f5023 100644 --- a/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs +++ b/crates/rspack_binding_options/src/plugins/js_loader/resolver.rs @@ -43,7 +43,7 @@ pub fn get_builtin_loader(builtin: &str, options: Option<&str>) -> BoxLoader { }); // TODO: builtin-loader supports function return Arc::new(rspack_loader_lightningcss::LightningCssLoader::new( - None, config, + None, config, builtin, )); } diff --git a/crates/rspack_loader_lightningcss/src/lib.rs b/crates/rspack_loader_lightningcss/src/lib.rs index e19294feab0..cc1f5c8caa5 100644 --- a/crates/rspack_loader_lightningcss/src/lib.rs +++ b/crates/rspack_loader_lightningcss/src/lib.rs @@ -29,9 +29,13 @@ pub struct LightningCssLoader { } impl LightningCssLoader { - pub fn new(visitors: Option>, config: Config) -> Self { + pub fn new( + visitors: Option>, + config: Config, + ident: &str, + ) -> Self { Self { - id: LIGHTNINGCSS_LOADER_IDENTIFIER.into(), + id: ident.into(), visitors: visitors.map(|v| Mutex::new(v)), config, } diff --git a/crates/rspack_loader_react_refresh/src/lib.rs b/crates/rspack_loader_react_refresh/src/lib.rs index c4196a088ab..ba85db67918 100644 --- a/crates/rspack_loader_react_refresh/src/lib.rs +++ b/crates/rspack_loader_react_refresh/src/lib.rs @@ -18,7 +18,7 @@ impl ReactRefreshLoader { /// Panics: /// Panics if `identifier` passed in is not starting with `builtin:react-refresh-loader`. pub fn with_identifier(mut self, identifier: Identifier) -> Self { - assert!(identifier.starts_with(REACT_REFRESH_LOADER_IDENTIFIER)); + debug_assert!(identifier.starts_with(REACT_REFRESH_LOADER_IDENTIFIER)); self.identifier = identifier; self } diff --git a/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/index.css b/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/index.css new file mode 100644 index 00000000000..5828000205c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/index.css @@ -0,0 +1,3 @@ +.foo { + user-select: none; +} diff --git a/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/index.js b/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/index.js new file mode 100644 index 00000000000..27dd41bb7eb --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/index.js @@ -0,0 +1,14 @@ +import './index.css' + +const fs = require("node:fs"); +const path = require("node:path"); + +it("should transform CSS and add prefixes correctly", () => { + const css = fs.readFileSync( + path.resolve(__dirname, "./bundle0.css"), + "utf-8" + ); + + expect(css.includes('-ms-user-select: none;')).toBeTruthy(); + expect(css.includes('user-select: none;')).toBeTruthy(); +}); diff --git a/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/rspack.config.js b/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/rspack.config.js new file mode 100644 index 00000000000..4c19842decb --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/builtin-lightningcss-loader/with-css-extract/rspack.config.js @@ -0,0 +1,34 @@ +const rspack = require("@rspack/core"); + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + module: { + rules: [ + { + test: /\.css$/, + use: [ + rspack.CssExtractRspackPlugin.loader, + "css-loader", + { + loader: "builtin:lightningcss-loader", + /** @type {import("@rspack/core").LightningcssLoaderOptions} */ + options: { + targets: [ + 'Edge >= 12' + ] + } + } + ], + type: "javascript/auto" + } + ] + }, + plugins: [ + new rspack.CssExtractRspackPlugin({ + filename: 'bundle0.css' + }) + ], + experiments: { + css: true + } +}; diff --git a/packages/rspack/src/config/adapterRuleUse.ts b/packages/rspack/src/config/adapterRuleUse.ts index 4412e41f7f7..08909d9e8a2 100644 --- a/packages/rspack/src/config/adapterRuleUse.ts +++ b/packages/rspack/src/config/adapterRuleUse.ts @@ -225,14 +225,15 @@ const getLightningcssLoaderOptions: GetLoaderOptions = (o, _) => { o.targets = browserslistToTargets(browserslist(o.targets)); } - if (o.include) { + if (o.include && typeof o.include === "object") { o.include = toFeatures(o.include as unknown as FeatureOptions); } - if (o.exclude) { + if (o.exclude && typeof o.exclude === "object") { o.exclude = toFeatures(o.exclude as unknown as FeatureOptions); } } + return o; };