Skip to content

Commit

Permalink
fix: lightningcss-loader targets array not work as expected (#7331)
Browse files Browse the repository at this point in the history
* fix: lightningcss-loader targets array not work as expected

* docs: fix

* test: fix

* fix: folder name
  • Loading branch information
chenjiahan authored Jul 28, 2024
1 parent 6fb4b36 commit c507fe6
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
const rspack = require('@rspack/core')
const browserslist = require('browserslist')

/** @type {import("@rspack/core").Configuration} */
module.exports = {
module: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.foo {
transition: all .5s;
user-select: none;
background: linear-gradient(to bottom, white, black);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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('-webkit-user-select: none;')).toBeTruthy();
expect(css.includes('-ms-user-select: none;')).toBeTruthy();
expect(css.includes('user-select: none;')).toBeTruthy();
expect(css.includes('background: -webkit-linear-gradient(#fff, #000);')).toBeTruthy();
expect(css.includes('background: linear-gradient(#fff, #000);')).toBeTruthy();
expect(css.includes('-webkit-transition: all .5s;')).toBeTruthy();
expect(css.includes('transition: all .5s;')).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
module: {
generator: {
"css/auto": {
exportsOnly: false,
}
},
rules: [
{
test: /\.css$/,
use: [
{
loader: "builtin:lightningcss-loader",
/** @type {import("@rspack/core").LightningcssLoaderOptions} */
options: {
targets: [
'Edge >= 12',
'iOS >= 8',
'Android >= 4.0'
]
}
}
],
type: "css/auto"
}
]
},
experiments: {
css: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
user-select: none;
}
Original file line number Diff line number Diff line change
@@ -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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
module: {
generator: {
"css/auto": {
exportsOnly: false,
}
},
rules: [
{
test: /\.css$/,
use: [
{
loader: "builtin:lightningcss-loader",
/** @type {import("@rspack/core").LightningcssLoaderOptions} */
options: {
targets: [
'Edge >= 12'
]
}
}
],
type: "css/auto"
}
]
},
experiments: {
css: true
}
};
2 changes: 1 addition & 1 deletion packages/rspack/src/config/adapterRuleUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const getLightningcssLoaderOptions: GetLoaderOptions = (o, _) => {
}

if (o.targets && Array.isArray(o.targets)) {
o.targets = browserslistToTargets(o.targets);
o.targets = browserslistToTargets(browserslist(o.targets));
}

if (o.include) {
Expand Down
61 changes: 48 additions & 13 deletions website/docs/en/guide/features/builtin-lightningcss-loader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,61 @@ type LightningcssLoaderOptions = {
};
```

The `targets` can be directly written as a `browserslist` query string, the result of a `browserslist` query, or the `targets` of lightningcss, for example:
### targets

The `targets` option supports multiple formats, it can be set as a browserslist query string, a browserslist query result, or the native `targets` of lightningcss.

Here are some examples of setting targets.

- Setting a browserslist query string:

```js
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
const options = {
targets: '> 0.2%',
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: 'ie 10',
},
};
// or
```

- Setting an array of browserslist query strings:

```js
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: ['chrome >= 87', 'edge >= 88', '> 0.5%'],
},
};
```

- Setting the browserslist query result:

```js
const browserslist = require('browserslist');

/** @type {import('@rspack/core').LightningcssLoaderOptions} */
const options = {
targets: browserslist('> 0.2%'),
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: browserslist('> 0.2%'),
},
};
// or
const rspack = require('@rspack/core');
```

- Setting as lightningcss's native `targets`, which relies on lightningcss's internal `browserslistToTargets` method to convert a `browserslist` query result to lightningcss's `targets` format:

```js
const browserslist = require('browserslist');
const lightningcss = require('lightningcss');

/** @type {import('@rspack/core').LightningcssLoaderOptions} */
const options = {
targets: rspack.lightningcss.browserslistToTargets(browserslist('> 0.2%')),
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: lightningcss.browserslistToTargets(browserslist('> 0.2%')),
},
};
```
61 changes: 48 additions & 13 deletions website/docs/zh/guide/features/builtin-lightningcss-loader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,61 @@ type LightningcssLoaderOptions = {
};
```

其中 `targets` 可以直接写 `browserslist` 查询字符串,`browserslist` 查询结果,或是 lightningcss 的 `targets`,例如
### targets

`targets` 选项支持多种写法,它可以设置为 browserslist 查询字符串、browserslist 查询结果,或是 lightningcss 原生的 `targets`

下面是一些 targets 的用法示例。

- 设置 browserslist 查询字符串:

```js
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
const options = {
targets: '> 0.2%',
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: 'ie 10',
},
};
// or
```

- 设置 browserslist 查询字符串数组:

```js
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: ['chrome >= 87', 'edge >= 88', '> 0.5%'],
},
};
```

- 设置 browserslist 查询结果:

```js
const browserslist = require('browserslist');

/** @type {import('@rspack/core').LightningcssLoaderOptions} */
const options = {
targets: browserslist('> 0.2%'),
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: browserslist('> 0.2%'),
},
};
// or
const rspack = require('@rspack/core');
```

- 设置为 lightningcss 原生的 `targets`,这依赖 lightningcss 内部的 `browserslistToTargets` 方法,它会将 `browserslist` 查询结果转换为 lightningcss 的 `targets` 格式:

```js
const browserslist = require('browserslist');
const lightningcss = require('lightningcss');

/** @type {import('@rspack/core').LightningcssLoaderOptions} */
const options = {
targets: rspack.lightningcss.browserslistToTargets(browserslist('> 0.2%')),
const loader = {
loader: 'builtin:lightningcss-loader',
/** @type {import('@rspack/core').LightningcssLoaderOptions} */
options: {
targets: lightningcss.browserslistToTargets(browserslist('> 0.2%')),
},
};
```

2 comments on commit c507fe6

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ✅ success
examples ❌ failure

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-07-28 6fb4b36) Current Change
10000_development-mode + exec 2.19 s ± 28 ms 2.2 s ± 25 ms +0.28 %
10000_development-mode_hmr + exec 693 ms ± 9 ms 695 ms ± 4.9 ms +0.16 %
10000_production-mode + exec 2.65 s ± 26 ms 2.65 s ± 32 ms +0.05 %
arco-pro_development-mode + exec 1.91 s ± 54 ms 1.92 s ± 78 ms +0.50 %
arco-pro_development-mode_hmr + exec 433 ms ± 2.2 ms 433 ms ± 1.8 ms 0.00 %
arco-pro_production-mode + exec 3.38 s ± 77 ms 3.41 s ± 92 ms +0.72 %
threejs_development-mode_10x + exec 1.77 s ± 20 ms 1.78 s ± 28 ms +0.13 %
threejs_development-mode_10x_hmr + exec 876 ms ± 7.9 ms 880 ms ± 4.9 ms +0.45 %
threejs_production-mode_10x + exec 5.47 s ± 24 ms 5.48 s ± 34 ms +0.16 %

Please sign in to comment.