Skip to content

Commit

Permalink
feat: add getThemeVar file to support theme config and fix 4.1.2 them… (
Browse files Browse the repository at this point in the history
#23171)

* feat: add getThemeVar file to support theme config and fix 4.1.2 theme config breaking change

* patch
  • Loading branch information
AshoneA authored and xrkffgg committed Apr 13, 2020
1 parent f386272 commit 291ebce
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
59 changes: 58 additions & 1 deletion .antd-tools.config.js
Expand Up @@ -61,11 +61,31 @@ function buildThemeFile(theme, vars) {
// eslint-disable-next-line no-console
console.log(`Built a entry less file to dist/antd.${theme}.less`);

if (theme === 'default') {
fs.writeFileSync(
path.join(process.cwd(), 'dist', `default-theme.js`),
`module.exports = ${JSON.stringify(vars, null, 2)};\n`,
);
return;
}

// Build ${theme}.js: dist/${theme}-theme.js, for less-loader

fs.writeFileSync(
path.join(process.cwd(), 'dist', `theme.js`),
`const ${theme}ThemeSingle = ${JSON.stringify(vars, null, 2)};\n`,
{
flag: 'a',
},
);

fs.writeFileSync(
path.join(process.cwd(), 'dist', `${theme}-theme.js`),
`module.exports = ${JSON.stringify(vars, null, 2)};`,
`const { ${theme}ThemeSingle } = require('./theme');\nconst defaultTheme = require('./default-theme');\n
module.exports = {
...${theme}ThemeSingle,
...defaultTheme
}`,
);

// eslint-disable-next-line no-console
Expand All @@ -80,10 +100,46 @@ function finalizeDist() {
'@import "../lib/style/index.less";\n@import "../lib/style/components.less";',
);
// eslint-disable-next-line no-console
fs.writeFileSync(
path.join(process.cwd(), 'dist', 'theme.js'),
`const defaultTheme = require('./default-theme.js');\n`,
);
console.log('Built a entry less file to dist/antd.less');
buildThemeFile('default', defaultVars);
buildThemeFile('dark', darkVars);
buildThemeFile('compact', compactVars);
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',
},
);
}
}

Expand All @@ -95,3 +151,4 @@ module.exports = {
finalize: finalizeDist,
},
};
finalizeDist();
14 changes: 5 additions & 9 deletions docs/react/customize-theme.en-US.md
Expand Up @@ -182,9 +182,7 @@ If the project does not use Less, you can import [antd.dark.css](https://unpkg.c
Method 3: using [less-loader](https://github.com/webpack-contrib/less-loader) in `webpack.config.js` to introduce as needed:

```diff
const defaultTheme = require('antd/dist/default-theme');
const darkTheme = require('antd/dist/dark-theme');
const compactTheme = require('antd/dist/compact-theme');
const { getThemeVariables } = require('antd/dist/theme');

// webpack.config.js
module.exports = {
Expand All @@ -197,12 +195,10 @@ module.exports = {
}, {
loader: 'less-loader', // compiles Less to CSS
+ options: {
+ modifyVars: {
+ 'hack': `true;@import "${require.resolve('antd/lib/style/color/colorPalette.less')}";`,
+ ...defaultTheme, // darkTheme and compactTheme depend on defaultTheme
+ ...darkTheme,
+ ...compactTheme,
+ },
+ modifyVars: getThemeVariables({
+ dark: true, // enable dark mode
+ compact: true, // enable compact mode
+ }),
+ javascriptEnabled: true,
+ },
}],
Expand Down
14 changes: 5 additions & 9 deletions docs/react/customize-theme.zh-CN.md
Expand Up @@ -160,9 +160,7 @@ module.exports = {
方式三:是用在 `webpack.config.js` 使用 [less-loader](https://github.com/webpack-contrib/less-loader) 按需引入:

```diff
const defaultTheme = require('antd/dist/default-theme');
const darkTheme = require('antd/dist/dark-theme');
const compactTheme = require('antd/dist/compact-theme');
const { getThemeVariables } = require('antd/dist/theme');

// webpack.config.js
module.exports = {
Expand All @@ -175,12 +173,10 @@ module.exports = {
}, {
loader: 'less-loader', // compiles Less to CSS
+ options: {
+ modifyVars: {
+ 'hack': `true;@import "${require.resolve('antd/lib/style/color/colorPalette.less')}";`,
+ ...defaultTheme, // darkTheme 和 compactTheme 依赖 defaultTheme
+ ...darkTheme,
+ ...compactTheme,
+ },
+ modifyVars: getThemeVariables({
+ dark: true, // 开启暗黑模式
+ compact: true, // 开启紧凑模式
+ }),
+ javascriptEnabled: true,
+ },
}],
Expand Down

0 comments on commit 291ebce

Please sign in to comment.