2019-08-10

const path = require('path');
const CSSSplitWebpackPlugin = require('css-split-webpack-plugin').default;
const replaceLib = require('antd-tools/lib/replaceLib');

const isDev = process.env.NODE_ENV === 'development';
const usePreact = process.env.REACT_ENV === 'preact';

const ThemeColorReplacer = require('webpack-theme-color-replacer');
const { generate } = require('@ant-design/colors/lib');

function alertBabelConfig(rules) {
rules.forEach(rule => {
if (rule.loader && rule.loader === 'babel-loader') {
if (rule.options.plugins.indexOf(replaceLib) === -1) {
rule.options.plugins.push(replaceLib);
}
// eslint-disable-next-line
rule.options.plugins = rule.options.plugins.filter(
plugin => !plugin.indexOf || plugin.indexOf('babel-plugin-add-module-exports') === -1,
);
// Add babel-plugin-add-react-displayname
rule.options.plugins.push(require.resolve('babel-plugin-add-react-displayname'));
} else if (rule.use) {
alertBabelConfig(rule.use);
}
});
}

const getAntdSerials = color => {
const lightNum = 9;
const devide10 = 10; // 淡化(即less的tint)

const lightens = new Array(lightNum).fill(undefined).map((_, i) => {
return ThemeColorReplacer.varyColor.lighten(color, i / devide10);
});
const colorPalettes = generate(color);
return lightens.concat(colorPalettes);
};

module.exports = {
port: 8001,
hash: true,
source: {
components: './components',
docs: './docs',
changelog: ['CHANGELOG.zh-CN.md', 'CHANGELOG.en-US.md'],
},
theme: './site/theme',
htmlTemplate: './site/theme/static/template.html',
themeConfig: {
categoryOrder: {
'Ant Design': 0,
原则: 1,
Principles: 1,
视觉: 2,
Visual: 2,
模式: 3,
Patterns: 3,
其他: 6,
Other: 6,
Components: 100,
},
typeOrder: {
General: 0,
Layout: 1,
Navigation: 2,
'Data Entry': 3,
'Data Display': 4,
Feedback: 5,
Other: 6,
Deprecated: 7,
通用: 0,
布局: 1,
导航: 2,
数据录入: 3,
数据展示: 4,
反馈: 5,
其他: 6,
废弃: 7,
},
docVersions: {
'0.9.x': 'http://09x.ant.design',
'0.10.x': 'http://010x.ant.design',
'0.11.x': 'http://011x.ant.design',
'0.12.x': 'http://012x.ant.design',
'1.x': 'http://1x.ant.design',
'2.x': 'http://2x.ant.design',
},
},
filePathMapper(filePath) {
if (filePath === '/index.html') {
return ['/index.html', '/index-cn.html'];
}
if (filePath.endsWith('/index.html')) {
return [filePath, filePath.replace(//index.html/, '-cn/index.html')]; } if (filePath !== '/404.html' && filePath !== '/index-cn.html') { return [filePath, filePath.replace(/\.html/, '-cn.html')];
}
return filePath;
},
doraConfig: {
verbose: true,
},
lessConfig: {
javascriptEnabled: true,
},
webpackConfig(config) {
// eslint-disable-next-line
config.resolve.alias = {
'antd/lib': path.join(process.cwd(), 'components'),
'antd/es': path.join(process.cwd(), 'components'),
antd: path.join(process.cwd(), 'index'),
site: path.join(process.cwd(), 'site'),
'react-router': 'react-router/umd/ReactRouter',
};

// eslint-disable-next-line
config.externals = {
  'react-router-dom': 'ReactRouterDOM',
};

if (usePreact) {
  // eslint-disable-next-line
  config.resolve.alias = Object.assign({}, config.resolve.alias, {
    react: 'preact-compat',
    'react-dom': 'preact-compat',
    'create-react-class': 'preact-compat/lib/create-react-class',
    'react-router': 'react-router',
  });
}

if (isDev) {
  // eslint-disable-next-line
  config.devtool = 'source-map';
}

alertBabelConfig(config.module.rules);

config.plugins.push(new CSSSplitWebpackPlugin({ size: 4000 }));
config.plugins.push(new ThemeColorReplacer({
  fileName: 'css/theme-colors-[contenthash:8].css',
  matchColors: getAntdSerials('#1890ff'),

  // 主色系列
  // 改变样式选择器,解决样式覆盖问题
  changeSelector(selector) {
    switch (selector) {
      case '.ant-calendar-today .ant-calendar-date':
        return ':not(.ant-calendar-selected-date)' + selector;

      case '.ant-btn:focus,.ant-btn:hover':
        return '.ant-btn:focus:not(.ant-btn-primary),.ant-btn:hover:not(.ant-btn-primary)';

      case '.ant-btn.active,.ant-btn:active':
        return '.ant-btn.active:not(.ant-btn-primary),.ant-btn:active:not(.ant-btn-primary)';

      default:
        return selector;
    }
  },
}));
return config;

},

devServerConfig: {
public: process.env.DEV_HOST || 'localhost',
disableHostCheck: !!process.env.DEV_HOST,
},

htmlTemplateExtraData: {
isDev,
usePreact,
},
};

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • // eslint-disable-next-line eslint-comments/disable-enabl...
    gitblong阅读 2,915评论 0 0
  • Day 1 Python的特性 @(第一张Python基础)[MeiCheng|帮助|Markdown] Pyth...
    Masom阅读 962评论 0 0
  • 我惧怕思想的无味、语言的空洞,我站在楼顶往下望,并没有看到人流的涌动,只有我的影子落在地上。 它在看我,像是注视着...
    五色浮元子_阅读 1,319评论 0 0
  • 最近参加的一些活动中,不乏行业大咖和知名人士,当寒暄一阵觉得气场相符,对方一般会加个微信然后优雅恰当地说句“下次聊...
    木子小阳阅读 42,347评论 132 1,169
  • 听说过偷钱,第一次知道竟然还有偷路的! 最近,江苏宿迁经开区的一条路上近800米的水泥路面竟然“不翼而飞”,好好的...
    写作的技巧阅读 1,319评论 0 0