webpack.config.js 4.41 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1 2 3 4 5 6 7 8
const path = require('path');
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');

const tailPkgs = readdirSync(path.join(__dirname, 'packages')).filter(
邓晓峰's avatar
邓晓峰 committed
9
  (pkg) => pkg.charAt(0) !== '.',
dengxiaofeng's avatar
dengxiaofeng committed
10 11
);

邓晓峰's avatar
邓晓峰 committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
const isCI = process.env.PRO_COMPONENTS_CI === 'CI';

const externals = isCI
  ? tailPkgs.reduce((pre, value) => {
      return {
        ...pre,
        [`@ant-design/pro-${value}`]: `Pro${value
          .toLowerCase()
          .replace(/( |^)[a-z]/g, (L) => L.toUpperCase())}`,
      };
    }, {})
  : {};

console.log(externals);

dengxiaofeng's avatar
dengxiaofeng committed
27 28 29
const webPackConfigList = [];

tailPkgs.forEach((pkg) => {
李纪文's avatar
李纪文 committed
30
  if (pkg == 'ParseForm') return false;
邓晓峰's avatar
邓晓峰 committed
31 32 33
  const entry = {};
  entry[`${pkg}`] = `./packages/${pkg}/src/index.js`;
  entry[`${pkg}.min`] = `./packages/${pkg}/src/index.js`;
dengxiaofeng's avatar
dengxiaofeng committed
34

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

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