webpack.config.js 4.8 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1
const path = require('path');
2
const fs = require('fs');
dengxiaofeng's avatar
dengxiaofeng committed
3 4 5 6 7 8
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const { readdirSync } = require('fs');

9 10 11 12 13 14 15 16 17
// const tailPkgs = readdirSync(path.join(__dirname, 'packages')).filter(
//   (pkg) => pkg.charAt(0) !== '.',
// );
const tailPkgs = ['base-components', 'extend-components']
  .map((dir) => {
    const list = readdirSync(path.join(__dirname, 'packages', dir)).map((item) => `${item}!${dir}`);
    return list.filter((pkg) => pkg.charAt(0) !== '.');
  })
  .flat();
邓晓峰's avatar
邓晓峰 committed
18 19 20 21
const isCI = process.env.PRO_COMPONENTS_CI === 'CI';

const externals = isCI
  ? tailPkgs.reduce((pre, value) => {
22
      const [name] = value.split('!');
邓晓峰's avatar
邓晓峰 committed
23 24
      return {
        ...pre,
25
        [`@ant-design/pro-${name}`]: `Pro${name
邓晓峰's avatar
邓晓峰 committed
26 27 28 29 30 31
          .toLowerCase()
          .replace(/( |^)[a-z]/g, (L) => L.toUpperCase())}`,
      };
    }, {})
  : {};

dengxiaofeng's avatar
dengxiaofeng committed
32 33
const webPackConfigList = [];

34
// eslint-disable-next-line consistent-return
dengxiaofeng's avatar
dengxiaofeng committed
35
tailPkgs.forEach((pkg) => {
36 37
  const [name, p] = pkg.split('!');
  if (name === 'ParseForm') return false;
邓晓峰's avatar
邓晓峰 committed
38
  const entry = {};
39 40
  entry[`${name}`] = `./packages/${p}/${name}/src/index.js`;
  entry[`${name}.min`] = `./packages/${p}/${name}/src/index.js`;
dengxiaofeng's avatar
dengxiaofeng committed
41

邓晓峰's avatar
邓晓峰 committed
42 43 44 45
  const config = {
    entry,
    output: {
      filename: '[name].js',
46
      library: `WisDOM${name.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())}`,
邓晓峰's avatar
邓晓峰 committed
47
      libraryExport: 'default',
48
      path: path.resolve(__dirname, 'packages', p, name, 'dist'),
邓晓峰's avatar
邓晓峰 committed
49 50 51 52 53 54
      globalObject: 'this',
    },
    mode: 'production',
    resolve: {
      extensions: ['.json', '.css', '.js', '.less'],
    },
邓晓峰's avatar
邓晓峰 committed
55 56 57 58 59 60 61 62 63 64 65 66 67
    optimization: isCI
      ? {
          minimize: true,
          minimizer: [
            new TerserPlugin({
              include: /\.min\.js$/,
            }),
            new OptimizeCSSAssetsPlugin({
              include: /\.min\.js$/,
            }),
          ],
        }
      : undefined,
邓晓峰's avatar
邓晓峰 committed
68 69 70 71 72 73 74 75 76 77 78 79
    module: {
      rules: [
        {
          test: /\.(png|jpg|gif|svg)$/i,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 8192,
              },
            },
          ],
dengxiaofeng's avatar
dengxiaofeng committed
80
        },
邓晓峰's avatar
邓晓峰 committed
81 82 83 84 85 86 87 88 89 90 91 92 93
        {
          test: /\.jsx?$/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/typescript', '@babel/env', '@babel/react'],
              plugins: [
                ['@babel/plugin-proposal-decorators', { legacy: true }],
                ['@babel/plugin-proposal-class-properties', { loose: true }],
                '@babel/proposal-object-rest-spread',
              ],
            },
          },
dengxiaofeng's avatar
dengxiaofeng committed
94
        },
邓晓峰's avatar
邓晓峰 committed
95
        {
邓晓峰's avatar
邓晓峰 committed
96
          test: /\.jsx?$/,
邓晓峰's avatar
邓晓峰 committed
97 98 99 100 101 102 103 104
          exclude: /(node_modules|bower_components)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: [
                '@babel/typescript',
                [
                  '@babel/env',
dengxiaofeng's avatar
dengxiaofeng committed
105
                  {
邓晓峰's avatar
邓晓峰 committed
106 107
                    loose: true,
                    modules: false,
dengxiaofeng's avatar
dengxiaofeng committed
108 109
                  },
                ],
邓晓峰's avatar
邓晓峰 committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                '@babel/react',
              ],
              plugins: [
                ['@babel/plugin-proposal-decorators', { legacy: true }],
                ['@babel/plugin-proposal-class-properties', { loose: true }],
                '@babel/proposal-object-rest-spread',
              ],
            },
          },
        },
        {
          test: /\.css$/,
          use: [
            {
              loader: 'style-loader', // creates style nodes from JS strings
            },
            {
              loader: 'css-loader', // translates CSS into CommonJS
            },
          ],
        },
        {
          test: /\.less$/,
          use: [
            {
              loader: MiniCssExtractPlugin.loader,
              options: {
                publicPath: (resourcePath, context) =>
                  `${path.relative(path.dirname(resourcePath), context)}/`,
dengxiaofeng's avatar
dengxiaofeng committed
139
              },
邓晓峰's avatar
邓晓峰 committed
140 141 142 143 144 145 146 147 148
            },
            {
              loader: 'css-loader', // translates CSS into CommonJS
            },
            {
              loader: 'less-loader',
              options: {
                lessOptions: {
                  javascriptEnabled: true,
dengxiaofeng's avatar
dengxiaofeng committed
149 150
                },
              },
邓晓峰's avatar
邓晓峰 committed
151 152
            },
          ],
dengxiaofeng's avatar
dengxiaofeng committed
153
        },
邓晓峰's avatar
邓晓峰 committed
154 155 156 157 158 159 160 161
      ],
    },
    externals: [
      {
        react: 'React',
        'react-dom': 'ReactDOM',
        antd: 'antd',
        moment: 'moment',
邓晓峰's avatar
邓晓峰 committed
162
        ...externals,
邓晓峰's avatar
邓晓峰 committed
163 164 165 166 167 168 169 170 171 172 173
      },
    ],
    plugins: [
      new ProgressBarPlugin(),
      new MiniCssExtractPlugin({
        filename: '[name].css',
        chunkFilename: '[id].css',
      }),
    ],
  };
  webPackConfigList.push(config);
dengxiaofeng's avatar
dengxiaofeng committed
174 175
});

邓晓峰's avatar
邓晓峰 committed
176
module.exports = webPackConfigList;