2019-04-02 19:04:38 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2020-04-05 20:38:56 +08:00
|
|
|
const defaultVars = require('./scripts/default-vars');
|
2019-12-18 22:42:09 +08:00
|
|
|
const darkVars = require('./scripts/dark-vars');
|
2020-03-29 10:39:46 +08:00
|
|
|
const compactVars = require('./scripts/compact-vars');
|
2019-04-02 19:04:38 +08:00
|
|
|
|
2020-04-15 11:37:29 +08:00
|
|
|
function generateThemeFileContent(theme) {
|
|
|
|
return `const { ${theme}ThemeSingle } = require('./theme');\nconst defaultTheme = require('./default-theme');\n
|
|
|
|
module.exports = {
|
|
|
|
...defaultTheme,
|
|
|
|
...${theme}ThemeSingle
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:04:38 +08:00
|
|
|
// We need compile additional content for antd user
|
|
|
|
function finalizeCompile() {
|
2022-07-04 22:08:24 +08:00
|
|
|
if (fs.existsSync(path.join(__dirname, './es'))) {
|
2019-04-02 19:04:38 +08:00
|
|
|
// Build a entry less file to dist/antd.less
|
|
|
|
const componentsPath = path.join(process.cwd(), 'components');
|
|
|
|
let componentsLessContent = '';
|
2022-07-04 22:08:24 +08:00
|
|
|
// Build components in one file: es/style/components.less
|
2019-04-02 19:04:38 +08:00
|
|
|
fs.readdir(componentsPath, (err, files) => {
|
2019-05-07 14:57:32 +08:00
|
|
|
files.forEach(file => {
|
2019-04-02 19:04:38 +08:00
|
|
|
if (fs.existsSync(path.join(componentsPath, file, 'style', 'index.less'))) {
|
2021-08-05 15:30:03 +08:00
|
|
|
componentsLessContent += `@import "../${path.posix.join(
|
|
|
|
file,
|
|
|
|
'style',
|
2021-09-07 16:14:20 +08:00
|
|
|
'index-pure.less',
|
2021-08-05 15:30:03 +08:00
|
|
|
)}";\n`;
|
2019-04-02 19:04:38 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
fs.writeFileSync(
|
2022-07-04 22:08:24 +08:00
|
|
|
path.join(process.cwd(), 'es', 'style', 'components.less'),
|
2019-04-02 19:04:38 +08:00
|
|
|
componentsLessContent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 10:39:46 +08:00
|
|
|
function buildThemeFile(theme, vars) {
|
|
|
|
// Build less entry file: dist/antd.${theme}.less
|
2020-04-28 14:11:43 +08:00
|
|
|
if (theme !== 'default') {
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', `antd.${theme}.less`),
|
2022-07-04 22:08:24 +08:00
|
|
|
`@import "../es/style/${theme}.less";\n@import "../es/style/components.less";`,
|
2020-04-28 14:11:43 +08:00
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(`Built a entry less file to dist/antd.${theme}.less`);
|
|
|
|
} else {
|
2020-04-13 00:05:48 +08:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', `default-theme.js`),
|
|
|
|
`module.exports = ${JSON.stringify(vars, null, 2)};\n`,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-29 10:39:46 +08:00
|
|
|
// Build ${theme}.js: dist/${theme}-theme.js, for less-loader
|
|
|
|
|
2020-04-13 00:05:48 +08:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', `theme.js`),
|
|
|
|
`const ${theme}ThemeSingle = ${JSON.stringify(vars, null, 2)};\n`,
|
|
|
|
{
|
|
|
|
flag: 'a',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-03-29 10:39:46 +08:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', `${theme}-theme.js`),
|
2020-04-15 11:37:29 +08:00
|
|
|
generateThemeFileContent(theme),
|
2020-03-29 10:39:46 +08:00
|
|
|
);
|
|
|
|
|
2020-04-03 15:02:11 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2020-03-29 10:39:46 +08:00
|
|
|
console.log(`Built a ${theme} theme js file to dist/${theme}-theme.js`);
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:04:38 +08:00
|
|
|
function finalizeDist() {
|
|
|
|
if (fs.existsSync(path.join(__dirname, './dist'))) {
|
|
|
|
// Build less entry file: dist/antd.less
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', 'antd.less'),
|
2022-07-04 22:08:24 +08:00
|
|
|
'@import "../es/style/default.less";\n@import "../es/style/components.less";',
|
2019-04-02 19:04:38 +08:00
|
|
|
);
|
2020-04-03 15:02:11 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2020-04-13 00:05:48 +08:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', 'theme.js'),
|
|
|
|
`const defaultTheme = require('./default-theme.js');\n`,
|
|
|
|
);
|
2020-04-14 10:30:14 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2019-04-02 19:04:38 +08:00
|
|
|
console.log('Built a entry less file to dist/antd.less');
|
2020-04-05 20:38:56 +08:00
|
|
|
buildThemeFile('default', defaultVars);
|
2020-03-29 10:39:46 +08:00
|
|
|
buildThemeFile('dark', darkVars);
|
|
|
|
buildThemeFile('compact', compactVars);
|
2021-09-01 10:56:50 +08:00
|
|
|
buildThemeFile('variable', {});
|
2020-04-13 00:05:48 +08:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', `theme.js`),
|
|
|
|
`
|
|
|
|
function getThemeVariables(options = {}) {
|
|
|
|
let themeVar = {
|
2022-07-04 22:08:24 +08:00
|
|
|
'hack': \`true;@import "\${require.resolve('antd/es/style/color/colorPalette.less')}";\`,
|
2020-04-13 00:05:48 +08:00
|
|
|
...defaultTheme
|
|
|
|
};
|
|
|
|
if(options.dark) {
|
|
|
|
themeVar = {
|
|
|
|
...themeVar,
|
|
|
|
...darkThemeSingle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(options.compact){
|
|
|
|
themeVar = {
|
|
|
|
...themeVar,
|
|
|
|
...compactThemeSingle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return themeVar;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
darkThemeSingle,
|
|
|
|
compactThemeSingle,
|
|
|
|
getThemeVariables
|
|
|
|
}`,
|
|
|
|
{
|
|
|
|
flag: 'a',
|
|
|
|
},
|
|
|
|
);
|
2019-04-02 19:04:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:14:20 +08:00
|
|
|
function isComponentStyleEntry(file) {
|
2021-09-01 10:56:50 +08:00
|
|
|
return file.path.match(/style(\/|\\)index\.tsx/);
|
|
|
|
}
|
|
|
|
|
|
|
|
function needTransformStyle(content) {
|
2021-09-07 16:14:20 +08:00
|
|
|
return content.includes('../../style/index.less') || content.includes('./index.less');
|
2021-09-01 10:56:50 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 19:04:38 +08:00
|
|
|
module.exports = {
|
|
|
|
compile: {
|
2021-09-11 13:28:25 +08:00
|
|
|
includeLessFile: [/(\/|\\)components(\/|\\)style(\/|\\)default.less$/],
|
2021-09-01 10:56:50 +08:00
|
|
|
transformTSFile(file) {
|
2021-09-07 16:14:20 +08:00
|
|
|
if (isComponentStyleEntry(file)) {
|
2021-09-01 10:56:50 +08:00
|
|
|
let content = file.contents.toString();
|
|
|
|
|
|
|
|
if (needTransformStyle(content)) {
|
|
|
|
const cloneFile = file.clone();
|
|
|
|
|
|
|
|
// Origin
|
2021-09-11 13:28:25 +08:00
|
|
|
content = content.replace('../../style/index.less', '../../style/default.less');
|
2021-09-01 10:56:50 +08:00
|
|
|
cloneFile.contents = Buffer.from(content);
|
|
|
|
|
|
|
|
return cloneFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
transformFile(file) {
|
2021-09-07 16:14:20 +08:00
|
|
|
if (isComponentStyleEntry(file)) {
|
|
|
|
const indexLessFilePath = file.path.replace('index.tsx', 'index.less');
|
|
|
|
|
|
|
|
if (fs.existsSync(indexLessFilePath)) {
|
|
|
|
// We put origin `index.less` file to `index-pure.less`
|
|
|
|
const pureFile = file.clone();
|
|
|
|
pureFile.contents = Buffer.from(fs.readFileSync(indexLessFilePath, 'utf8'));
|
|
|
|
pureFile.path = pureFile.path.replace('index.tsx', 'index-pure.less');
|
|
|
|
|
|
|
|
// Rewrite `index.less` file with `root-entry-name`
|
|
|
|
const indexLessFile = file.clone();
|
|
|
|
indexLessFile.contents = Buffer.from(
|
2021-09-01 10:56:50 +08:00
|
|
|
[
|
|
|
|
// Inject variable
|
2021-09-06 20:04:25 +08:00
|
|
|
'@root-entry-name: default;',
|
2021-09-01 10:56:50 +08:00
|
|
|
// Point to origin file
|
2021-09-07 16:14:20 +08:00
|
|
|
"@import './index-pure.less';",
|
2021-09-01 10:56:50 +08:00
|
|
|
].join('\n\n'),
|
|
|
|
);
|
2021-09-07 16:14:20 +08:00
|
|
|
indexLessFile.path = indexLessFile.path.replace('index.tsx', 'index.less');
|
|
|
|
|
|
|
|
return [indexLessFile, pureFile];
|
2021-09-01 10:56:50 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-07 16:14:20 +08:00
|
|
|
|
2021-09-01 10:56:50 +08:00
|
|
|
return [];
|
|
|
|
},
|
|
|
|
lessConfig: {
|
|
|
|
modifyVars: {
|
|
|
|
'root-entry-name': 'default',
|
|
|
|
},
|
|
|
|
},
|
2019-04-02 19:04:38 +08:00
|
|
|
finalize: finalizeCompile,
|
|
|
|
},
|
|
|
|
dist: {
|
|
|
|
finalize: finalizeDist,
|
|
|
|
},
|
2020-04-15 11:37:29 +08:00
|
|
|
generateThemeFileContent,
|
2021-08-05 15:30:03 +08:00
|
|
|
bail: true,
|
2019-04-02 19:04:38 +08:00
|
|
|
};
|