1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = _default;
|
5 |
|
6 | var _path = _interopRequireDefault(require("path"));
|
7 |
|
8 | var _castArray = _interopRequireDefault(require("lodash/castArray"));
|
9 |
|
10 | var _webpack = _interopRequireDefault(require("webpack"));
|
11 |
|
12 | var _terserWebpackPlugin = _interopRequireDefault(require("terser-webpack-plugin"));
|
13 |
|
14 | var _miniHtmlWebpackPlugin = require("mini-html-webpack-plugin");
|
15 |
|
16 | var _miniHtmlWebpackTemplate = _interopRequireDefault(require("@vxna/mini-html-webpack-template"));
|
17 |
|
18 | var _cleanWebpackPlugin = require("clean-webpack-plugin");
|
19 |
|
20 | var _copyWebpackPlugin = _interopRequireDefault(require("copy-webpack-plugin"));
|
21 |
|
22 | var _webpackMerge = _interopRequireDefault(require("webpack-merge"));
|
23 |
|
24 | var _forEach = _interopRequireDefault(require("lodash/forEach"));
|
25 |
|
26 | var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
|
27 |
|
28 | var _StyleguidistOptionsPlugin = _interopRequireDefault(require("./utils/StyleguidistOptionsPlugin"));
|
29 |
|
30 | var _mergeWebpackConfig = _interopRequireDefault(require("./utils/mergeWebpackConfig"));
|
31 |
|
32 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33 |
|
34 | const RENDERER_REGEXP = /Renderer$/;
|
35 |
|
36 | const sourceDir = _path.default.resolve(__dirname, '../client');
|
37 |
|
38 | function _default(config, env) {
|
39 | process.env.NODE_ENV = process.env.NODE_ENV || env;
|
40 | const isProd = env === 'production';
|
41 | const template = (0, _isFunction.default)(config.template) ? config.template : _miniHtmlWebpackTemplate.default;
|
42 | const templateContext = (0, _isFunction.default)(config.template) ? {} : config.template;
|
43 | const htmlPluginOptions = {
|
44 | context: {
|
45 | lang: 'en',
|
46 | ...templateContext,
|
47 | title: config.title,
|
48 | container: config.mountPointId
|
49 | },
|
50 | template
|
51 | };
|
52 | let webpackConfig = {
|
53 | entry: config.require.concat([_path.default.resolve(sourceDir, 'index')]),
|
54 | mode: env,
|
55 | output: {
|
56 | path: config.styleguideDir,
|
57 | filename: 'build/[name].bundle.js',
|
58 | chunkFilename: 'build/[name].js',
|
59 | publicPath: ''
|
60 | },
|
61 | resolve: {
|
62 | extensions: ['.wasm', '.mjs', '.js', '.jsx', '.ts', '.tsx', '.json'],
|
63 | alias: {}
|
64 | },
|
65 | plugins: [new _StyleguidistOptionsPlugin.default(config), new _miniHtmlWebpackPlugin.MiniHtmlWebpackPlugin(htmlPluginOptions), new _webpack.default.DefinePlugin({
|
66 | 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
|
67 | 'process.env.STYLEGUIDIST_ENV': JSON.stringify(env)
|
68 | })],
|
69 | performance: {
|
70 | hints: false
|
71 | }
|
72 | };
|
73 |
|
74 | if (isProd) {
|
75 | const minimizer = new _terserWebpackPlugin.default({
|
76 | terserOptions: {
|
77 | ie8: false,
|
78 | ecma: 5,
|
79 | compress: {
|
80 | keep_fnames: true,
|
81 | warnings: false,
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | reduce_funcs: false
|
91 | },
|
92 | mangle: {
|
93 | keep_fnames: true
|
94 | }
|
95 | }
|
96 | });
|
97 | webpackConfig = (0, _webpackMerge.default)(webpackConfig, {
|
98 | output: {
|
99 | filename: 'build/bundle.[chunkhash:8].js',
|
100 | chunkFilename: 'build/[name].[chunkhash:8].js'
|
101 | },
|
102 | plugins: [new _cleanWebpackPlugin.CleanWebpackPlugin({
|
103 | cleanOnceBeforeBuildPatterns: [`${config.styleguideDir}/build/**/*`],
|
104 | verbose: config.verbose === true
|
105 | })],
|
106 | optimization: {
|
107 | minimize: config.minimize === true,
|
108 | minimizer: [minimizer]
|
109 | }
|
110 | });
|
111 |
|
112 | if (config.assetsDir && webpackConfig.plugins) {
|
113 | const copyPatterns = {
|
114 | patterns: (0, _castArray.default)(config.assetsDir).map(dir => ({
|
115 | from: dir
|
116 | }))
|
117 | };
|
118 | webpackConfig.plugins.push(
|
119 |
|
120 |
|
121 | new _copyWebpackPlugin.default(copyPatterns));
|
122 | }
|
123 | } else {
|
124 | webpackConfig = (0, _webpackMerge.default)(webpackConfig, {
|
125 | devServer: {
|
126 | webSocketServer: 'ws'
|
127 | }
|
128 | });
|
129 | }
|
130 |
|
131 | if (config.webpackConfig) {
|
132 | webpackConfig = (0, _mergeWebpackConfig.default)(webpackConfig, config.webpackConfig, env);
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 | const aliasedWebpackConfig = (0, _webpackMerge.default)(webpackConfig, {
|
138 | resolve: {
|
139 | alias: config.moduleAliases
|
140 | }
|
141 | });
|
142 | const alias = aliasedWebpackConfig.resolve.alias;
|
143 |
|
144 | if (config.styleguideComponents) {
|
145 | (0, _forEach.default)(config.styleguideComponents, (filepath, name) => {
|
146 | const fullName = name.match(RENDERER_REGEXP) ? `${name.replace(RENDERER_REGEXP, '')}/${name}` : name;
|
147 | alias[`rsg-components/${fullName}`] = filepath;
|
148 | });
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 | alias['rsg-components'] = _path.default.resolve(sourceDir, 'rsg-components');
|
154 | webpackConfig = config.dangerouslyUpdateWebpackConfig ? config.dangerouslyUpdateWebpackConfig(aliasedWebpackConfig, env) : aliasedWebpackConfig;
|
155 | return webpackConfig;
|
156 | } |
\ | No newline at end of file |