UNPKG

5.46 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = _default;
5
6var _path = _interopRequireDefault(require("path"));
7
8var _castArray = _interopRequireDefault(require("lodash/castArray"));
9
10var _webpack = _interopRequireDefault(require("webpack"));
11
12var _terserWebpackPlugin = _interopRequireDefault(require("terser-webpack-plugin"));
13
14var _miniHtmlWebpackPlugin = require("mini-html-webpack-plugin");
15
16var _miniHtmlWebpackTemplate = _interopRequireDefault(require("@vxna/mini-html-webpack-template"));
17
18var _cleanWebpackPlugin = require("clean-webpack-plugin");
19
20var _copyWebpackPlugin = _interopRequireDefault(require("copy-webpack-plugin"));
21
22var _webpackMerge = _interopRequireDefault(require("webpack-merge"));
23
24var _forEach = _interopRequireDefault(require("lodash/forEach"));
25
26var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
27
28var _StyleguidistOptionsPlugin = _interopRequireDefault(require("./utils/StyleguidistOptionsPlugin"));
29
30var _mergeWebpackConfig = _interopRequireDefault(require("./utils/mergeWebpackConfig"));
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34const RENDERER_REGEXP = /Renderer$/;
35
36const sourceDir = _path.default.resolve(__dirname, '../client');
37
38function _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 * Disable reduce_funcs to keep Terser from inlining
85 * Preact's VNode. If enabled, the 'new VNode()' is replaced
86 * with a anonymous 'function(){}', which is problematic for
87 * preact-compat, since it extends the VNode prototype to
88 * accomodate React's API.
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( // FIXME: Since we don't have the type of copy-webpack-plugin@6.0
119 // we cast the config as any to make it work. Once the new types are
120 // released we must remove the cast.
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 } // Custom aliases
134 // NOTE: in a sanitized config, moduleAliases are always an object (never null or undefined)
135
136
137 const aliasedWebpackConfig = (0, _webpackMerge.default)(webpackConfig, {
138 resolve: {
139 alias: config.moduleAliases
140 }
141 });
142 const alias = aliasedWebpackConfig.resolve.alias; // Custom style guide components
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 } // Add components folder alias at the end, so users can override our components
150 // to customize the style guide (their aliases should be before this one)
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