|
1 | 1 | const fs = require('fs');
|
2 | 2 | const path = require('path');
|
3 |
| -const defaultVars = require('./scripts/default-vars'); |
4 |
| -const darkVars = require('./scripts/dark-vars'); |
5 |
| -const compactVars = require('./scripts/compact-vars'); |
6 | 3 |
|
7 |
| -function generateThemeFileContent(theme) { |
8 |
| - return `const { ${theme}ThemeSingle } = require('./theme');\nconst defaultTheme = require('./default-theme');\n |
9 |
| -module.exports = { |
10 |
| - ...defaultTheme, |
11 |
| - ...${theme}ThemeSingle |
12 |
| -}`; |
13 |
| -} |
| 4 | +const restCssPath = path.join(process.cwd(), 'components', 'style', 'reset.css'); |
| 5 | +const tokenStatisticPath = path.join(process.cwd(), 'components', 'version', 'token.json'); |
| 6 | +const tokenMetaPath = path.join(process.cwd(), 'components', 'version', 'token-meta.json'); |
14 | 7 |
|
15 |
| -// We need compile additional content for antd user |
16 | 8 | function finalizeCompile() {
|
17 |
| - if (fs.existsSync(path.join(__dirname, './lib'))) { |
18 |
| - // Build a entry less file to dist/antd.less |
19 |
| - const componentsPath = path.join(process.cwd(), 'components'); |
20 |
| - let componentsLessContent = ''; |
21 |
| - // Build components in one file: lib/style/components.less |
22 |
| - fs.readdir(componentsPath, (err, files) => { |
23 |
| - files.forEach(file => { |
24 |
| - if (fs.existsSync(path.join(componentsPath, file, 'style', 'index.less'))) { |
25 |
| - componentsLessContent += `@import "../${path.posix.join( |
26 |
| - file, |
27 |
| - 'style', |
28 |
| - 'index-pure.less', |
29 |
| - )}";\n`; |
30 |
| - } |
31 |
| - }); |
32 |
| - fs.writeFileSync( |
33 |
| - path.join(process.cwd(), 'lib', 'style', 'components.less'), |
34 |
| - componentsLessContent, |
35 |
| - ); |
36 |
| - }); |
| 9 | + if (fs.existsSync(path.join(__dirname, './es'))) { |
| 10 | + fs.copyFileSync(restCssPath, path.join(process.cwd(), 'es', 'style', 'reset.css')); |
| 11 | + fs.copyFileSync(tokenStatisticPath, path.join(process.cwd(), 'es', 'version', 'token.json')); |
| 12 | + fs.copyFileSync(tokenMetaPath, path.join(process.cwd(), 'es', 'version', 'token-meta.json')); |
37 | 13 | }
|
38 |
| -} |
39 | 14 |
|
40 |
| -function buildThemeFile(theme, vars) { |
41 |
| - // Build less entry file: dist/antd.${theme}.less |
42 |
| - if (theme !== 'default') { |
43 |
| - fs.writeFileSync( |
44 |
| - path.join(process.cwd(), 'dist', `antd.${theme}.less`), |
45 |
| - `@import "../lib/style/${theme}.less";\n@import "../lib/style/components.less";`, |
46 |
| - ); |
47 |
| - // eslint-disable-next-line no-console |
48 |
| - console.log(`Built a entry less file to dist/antd.${theme}.less`); |
49 |
| - } else { |
50 |
| - fs.writeFileSync( |
51 |
| - path.join(process.cwd(), 'dist', `default-theme.js`), |
52 |
| - `module.exports = ${JSON.stringify(vars, null, 2)};\n`, |
53 |
| - ); |
54 |
| - return; |
| 15 | + if (fs.existsSync(path.join(__dirname, './lib'))) { |
| 16 | + fs.copyFileSync(restCssPath, path.join(process.cwd(), 'lib', 'style', 'reset.css')); |
| 17 | + fs.copyFileSync(tokenStatisticPath, path.join(process.cwd(), 'lib', 'version', 'token.json')); |
| 18 | + fs.copyFileSync(tokenMetaPath, path.join(process.cwd(), 'lib', 'version', 'token-meta.json')); |
55 | 19 | }
|
56 |
| - |
57 |
| - // Build ${theme}.js: dist/${theme}-theme.js, for less-loader |
58 |
| - |
59 |
| - fs.writeFileSync( |
60 |
| - path.join(process.cwd(), 'dist', `theme.js`), |
61 |
| - `const ${theme}ThemeSingle = ${JSON.stringify(vars, null, 2)};\n`, |
62 |
| - { |
63 |
| - flag: 'a', |
64 |
| - }, |
65 |
| - ); |
66 |
| - |
67 |
| - fs.writeFileSync( |
68 |
| - path.join(process.cwd(), 'dist', `${theme}-theme.js`), |
69 |
| - generateThemeFileContent(theme), |
70 |
| - ); |
71 |
| - |
72 |
| - // eslint-disable-next-line no-console |
73 |
| - console.log(`Built a ${theme} theme js file to dist/${theme}-theme.js`); |
74 | 20 | }
|
75 | 21 |
|
76 | 22 | function finalizeDist() {
|
77 | 23 | if (fs.existsSync(path.join(__dirname, './dist'))) {
|
78 |
| - // Build less entry file: dist/antd.less |
79 |
| - fs.writeFileSync( |
80 |
| - path.join(process.cwd(), 'dist', 'antd.less'), |
81 |
| - '@import "../lib/style/default.less";\n@import "../lib/style/components.less";', |
82 |
| - ); |
83 |
| - // eslint-disable-next-line no-console |
84 |
| - fs.writeFileSync( |
85 |
| - path.join(process.cwd(), 'dist', 'theme.js'), |
86 |
| - `const defaultTheme = require('./default-theme.js');\n`, |
87 |
| - ); |
88 |
| - // eslint-disable-next-line no-console |
89 |
| - console.log('Built a entry less file to dist/antd.less'); |
90 |
| - buildThemeFile('default', defaultVars); |
91 |
| - buildThemeFile('dark', darkVars); |
92 |
| - buildThemeFile('compact', compactVars); |
93 |
| - buildThemeFile('variable', {}); |
94 |
| - fs.writeFileSync( |
95 |
| - path.join(process.cwd(), 'dist', `theme.js`), |
96 |
| - ` |
97 |
| -function getThemeVariables(options = {}) { |
98 |
| - let themeVar = { |
99 |
| - 'hack': \`true;@import "\${require.resolve('ant-design-vue/lib/style/color/colorPalette.less')}";\`, |
100 |
| - ...defaultTheme |
101 |
| - }; |
102 |
| - if(options.dark) { |
103 |
| - themeVar = { |
104 |
| - ...themeVar, |
105 |
| - ...darkThemeSingle |
106 |
| - } |
107 |
| - } |
108 |
| - if(options.compact){ |
109 |
| - themeVar = { |
110 |
| - ...themeVar, |
111 |
| - ...compactThemeSingle |
112 |
| - } |
| 24 | + fs.copyFileSync(restCssPath, path.join(process.cwd(), 'dist', 'reset.css')); |
113 | 25 | }
|
114 |
| - return themeVar; |
115 |
| -} |
116 |
| -
|
117 |
| -module.exports = { |
118 |
| - darkThemeSingle, |
119 |
| - compactThemeSingle, |
120 |
| - getThemeVariables |
121 |
| -}`, |
122 |
| - { |
123 |
| - flag: 'a', |
124 |
| - }, |
125 |
| - ); |
126 |
| - } |
127 |
| -} |
128 |
| - |
129 |
| -function isComponentStyleEntry(file) { |
130 |
| - return file.path.match(/style(\/|\\)index\.tsx/); |
131 |
| -} |
132 |
| - |
133 |
| -function needTransformStyle(content) { |
134 |
| - return content.includes('../../style/index.less') || content.includes('./index.less'); |
135 | 26 | }
|
136 | 27 |
|
137 | 28 | module.exports = {
|
138 | 29 | compile: {
|
139 |
| - includeLessFile: [/(\/|\\)components(\/|\\)style(\/|\\)default.less$/], |
140 |
| - transformTSFile(file) { |
141 |
| - if (isComponentStyleEntry(file)) { |
142 |
| - let content = file.contents.toString(); |
143 |
| - |
144 |
| - if (needTransformStyle(content)) { |
145 |
| - const cloneFile = file.clone(); |
146 |
| - |
147 |
| - // Origin |
148 |
| - content = content.replace('../../style/index.less', '../../style/default.less'); |
149 |
| - cloneFile.contents = Buffer.from(content); |
150 |
| - |
151 |
| - return cloneFile; |
152 |
| - } |
153 |
| - } |
154 |
| - }, |
155 |
| - transformFile(file) { |
156 |
| - if (isComponentStyleEntry(file)) { |
157 |
| - const indexLessFilePath = file.path.replace('index.tsx', 'index.less'); |
158 |
| - |
159 |
| - if (fs.existsSync(indexLessFilePath)) { |
160 |
| - // We put origin `index.less` file to `index-pure.less` |
161 |
| - const pureFile = file.clone(); |
162 |
| - pureFile.contents = Buffer.from(fs.readFileSync(indexLessFilePath, 'utf8')); |
163 |
| - pureFile.path = pureFile.path.replace('index.tsx', 'index-pure.less'); |
164 |
| - |
165 |
| - // Rewrite `index.less` file with `root-entry-name` |
166 |
| - const indexLessFile = file.clone(); |
167 |
| - indexLessFile.contents = Buffer.from( |
168 |
| - [ |
169 |
| - // Inject variable |
170 |
| - '@root-entry-name: default;', |
171 |
| - // Point to origin file |
172 |
| - "@import './index-pure.less';", |
173 |
| - ].join('\n\n'), |
174 |
| - ); |
175 |
| - indexLessFile.path = indexLessFile.path.replace('index.tsx', 'index.less'); |
176 |
| - |
177 |
| - return [indexLessFile, pureFile]; |
178 |
| - } |
179 |
| - } |
180 |
| - |
181 |
| - return []; |
182 |
| - }, |
183 |
| - lessConfig: { |
184 |
| - modifyVars: { |
185 |
| - 'root-entry-name': 'default', |
186 |
| - }, |
187 |
| - }, |
188 | 30 | finalize: finalizeCompile,
|
189 | 31 | },
|
190 | 32 | dist: {
|
191 | 33 | finalize: finalizeDist,
|
192 | 34 | },
|
193 |
| - generateThemeFileContent, |
194 | 35 | bail: true,
|
195 | 36 | };
|
0 commit comments