Skip to content

Commit 2384e20

Browse files
committed
feat(vite): 支持在viet >= 5 版本中使用
1 parent 077851d commit 2384e20

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ lib
2626
es
2727
esm
2828
cjs
29+
plugins/*.js
30+
plugins/*.js.map

README-zh_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import 'tdesign-web-components/lib/style/index.css'
5656
document.querySelector('#app').innerHTML = `<t-button>Hello TDesign</t-button>`
5757
```
5858

59+
更多使用方式请点击 👉🏻 [快速开始](./site/docs/getting-started.md)
60+
5961
npm package 中提供了多种构建产物,可以阅读 [这里](https://github.com/Tencent/tdesign/blob/main/docs/develop-install.md) 了解不同目录下产物的差别。
6062

6163
# 快速体验

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import 'tdesign-web-components/lib/style/index.css'
5454
document.querySelector('#app').innerHTML = `<t-button>Hello TDesign</t-button>`
5555
```
5656

57+
More ways to use please click 👉🏻 [getting-started](./site/docs/getting-started.md)
58+
5759
The package of tdesign-web-components provides kinds of bundles, read [the documentation](https://github.com/Tencent/tdesign/blob/main/docs/develop-install.md) for the detail of differences between bundles.
5860

5961
# Quick Start

plugins/vite-plugin-less-compiler.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import fs from 'fs';
2+
import less from 'less';
3+
import MagicString from 'magic-string';
4+
5+
const lessToCss = async (id: string, lessOptions: Less.Options): Promise<string> => {
6+
const source = fs.readFileSync(id, 'utf-8');
7+
const output = await (less as any).render(source, {
8+
...lessOptions,
9+
filename: id,
10+
});
11+
return (output as Less.RenderOutput).css || '';
12+
};
13+
14+
export default function lessCompilerPlugin(
15+
options: {
16+
include?: RegExp;
17+
exclude?: RegExp;
18+
lessOptions?: Less.Options;
19+
} = {},
20+
) {
21+
const { include = /tdesign-web-components.*\.js/, exclude, lessOptions = {} } = options;
22+
23+
return {
24+
name: 'vite-plugin-less-compiler',
25+
async transform(code, id) {
26+
if (exclude && exclude.test(id)) {
27+
return null;
28+
}
29+
30+
if (!include.test(id)) {
31+
return null;
32+
}
33+
34+
const magicString = new MagicString(code);
35+
const ast = this.parse(code);
36+
37+
for (const node of ast.body) {
38+
const { type = '', source = {}, specifiers } = node;
39+
if (type !== 'ImportDeclaration' || !source.value.match(/^.*\.less$/)) {
40+
continue;
41+
}
42+
if (!specifiers && specifiers?.[0]?.type !== 'ImportDefaultSpecifier') {
43+
continue;
44+
}
45+
const [{ local = {} }] = specifiers;
46+
// 这里只能使用 for loop
47+
// eslint-disable-next-line no-await-in-loop
48+
const css = await lessToCss(source.value, lessOptions);
49+
magicString.overwrite(node.start, node.end, `const ${local.name} = \`${css}\``);
50+
}
51+
return {
52+
code: magicString.toString(),
53+
};
54+
},
55+
};
56+
}

script/rollup.config.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import pkg from '../package.json';
2020
const name = 'TDesign Web Components';
2121
const externalDeps = Object.keys(pkg.dependencies || {});
2222
const externalPeerDeps = Object.keys(pkg.peerDependencies || {});
23+
const buildPlugins = ['vite-plugin-less-compiler'];
24+
2325
const banner = `/**
2426
* ${name} v${pkg.version}
2527
* (c) ${new Date().getFullYear()} ${pkg.author}
2628
* @license ${pkg.license}
2729
*/
2830
`;
31+
2932
const input = 'src/index-lib.ts';
3033
const inputList = [
3134
'src/**/*.ts',
@@ -218,4 +221,26 @@ const umdMinConfig = {
218221
},
219222
};
220223

221-
export default [cssConfig, umdCssConfig, libConfig, cjsConfig, umdConfig, umdMinConfig, esmConfig];
224+
const pluginConfig = buildPlugins.map((plugin) => ({
225+
input: `plugin/${plugin}.ts`,
226+
external: ['less', 'fs'],
227+
plugins: [
228+
nodeResolve(),
229+
commonjs(),
230+
esbuild({
231+
include: /\.[jt]s$/,
232+
target: 'esnext',
233+
minify: false,
234+
loader: 'ts',
235+
tsconfig: resolve(__dirname, '../tsconfig.build.json'),
236+
}),
237+
],
238+
output: {
239+
banner,
240+
format: 'esm',
241+
sourcemap: false,
242+
file: `plugin/${plugin}.js`,
243+
},
244+
}));
245+
246+
export default [cssConfig, umdCssConfig, libConfig, cjsConfig, umdConfig, umdMinConfig, esmConfig, ...pluginConfig];

site/docs/getting-started.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ export default defineConfig({
4545
})
4646
```
4747

48+
> 注意:在`vite >= 5.x` 版本中,需要使用下面的vite插件,其它版本可跳过
49+
50+
```js
51+
import lessCompilerPlugin from 'tdesign-web-components/plugins/vite-plugin-less-compiler';
52+
53+
// vite.config.ts
54+
export default defineConfig({
55+
plugins: [lessCompilerPlugin({
56+
lessOptions: {} // less 相关参数
57+
})]
58+
})
59+
```
60+
4861
如果使用webpack打包工具,需要在`babel`中设置`jsx`的解析逻辑:
4962

5063
```javascript

0 commit comments

Comments
 (0)