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() {
|
|
|
|
if (fs.existsSync(path.join(__dirname, './lib'))) {
|
|
|
|
// Build a entry less file to dist/antd.less
|
|
|
|
const componentsPath = path.join(process.cwd(), 'components');
|
|
|
|
let componentsLessContent = '';
|
|
|
|
// Build components in one file: lib/style/components.less
|
|
|
|
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'))) {
|
|
|
|
componentsLessContent += `@import "../${path.join(file, 'style', 'index.less')}";\n`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'lib', 'style', 'components.less'),
|
|
|
|
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`),
|
|
|
|
`@import "../lib/style/${theme}.less";\n@import "../lib/style/components.less";`,
|
|
|
|
);
|
|
|
|
// 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'),
|
|
|
|
'@import "../lib/style/index.less";\n@import "../lib/style/components.less";',
|
|
|
|
);
|
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);
|
2020-04-13 00:05:48 +08:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(process.cwd(), 'dist', `theme.js`),
|
|
|
|
`
|
|
|
|
function getThemeVariables(options = {}) {
|
|
|
|
let themeVar = {
|
|
|
|
'hack': \`true;@import "\${require.resolve('antd/lib/style/color/colorPalette.less')}";\`,
|
|
|
|
...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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
compile: {
|
|
|
|
finalize: finalizeCompile,
|
|
|
|
},
|
|
|
|
dist: {
|
|
|
|
finalize: finalizeDist,
|
|
|
|
},
|
2020-04-15 11:37:29 +08:00
|
|
|
generateThemeFileContent,
|
2019-04-02 19:04:38 +08:00
|
|
|
};
|