UNPKG

6.79 kBJavaScriptView Raw
1const webpack = require('webpack');
2const path = require('path');
3const fs = require('fs');
4const TerserPlugin = require('terser-webpack-plugin');
5const MiniCssExtractPlugin = require('mini-css-extract-plugin');
6const LiveReloadPlugin = require('webpack-livereload-plugin');
7const env = require('./environment');
8//
9// Prevent from failing, when NEOS_BUILD_ROOT env variable isn't set
10// (e.g. when extending this config from storybook)
11//
12const rootPath = env.rootPath || __dirname;
13
14const liveReloadOptionsFileName = '.webpack.livereload.local.js';
15const liveReloadOptionsFile = path.join(rootPath, liveReloadOptionsFileName);
16
17const mandatoryLiveReloadOptions = {appendScriptTag: true};
18
19let finalLiveReloadOptions = Object.assign({}, mandatoryLiveReloadOptions);
20
21if (fs.existsSync(liveReloadOptionsFile) && fs.lstatSync(liveReloadOptionsFile).isFile()) {
22 const liveReloadOptions = require(liveReloadOptionsFile);
23 finalLiveReloadOptions = Object.assign({}, finalLiveReloadOptions, liveReloadOptions);
24}
25
26const webpackConfig = {
27 // https://github.com/webpack/docs/wiki/build-performance#sourcemaps
28 devtool: 'source-map',
29 mode: env.isProduction ? 'production' : 'development',
30 module: {
31 rules: [
32 {
33 test: /\.js$/,
34 exclude: /node_modules\/(?!@ckeditor)(?!@neos-project).*$/,
35 use: [{
36 loader: 'babel-loader'
37 }]
38 },
39 {
40 test: /\.js$/,
41 include: [
42 /node_modules\/debug/,
43 /node_modules\/d3-scale/,
44 /node_modules\/d3-array/
45 ],
46 use: [{
47 loader: 'babel-loader'
48 }]
49 },
50 {
51 test: /\.tsx?$/,
52 exclude: /node_modules/,
53 use: [{
54 loader: 'ts-loader'
55 }]
56 },
57 {
58 type: 'javascript/auto',
59 test: /\.json$/,
60 exclude: /node_modules\/(?!@neos-project).*$/,
61 use: [{
62 loader: 'json-loader'
63 }]
64 },
65 {
66 test: /node_modules\/@ckeditor\/.*\.svg$/,
67 use: ['raw-loader']
68 },
69 {
70 test: /node_modules\/@ckeditor\/.*\.css$/,
71 use: ['null-loader']
72 },
73 {
74 test: /\.(woff|woff2|eot|ttf|svg)$/,
75 exclude: /node_modules\/@ckeditor.*$/,
76 use: [{
77 loader: 'url-loader',
78 options: {
79 limit: 10000,
80 name: './Styles/Font-[hash].[ext]',
81 publicPath: './../'
82 }
83 }]
84 },
85 {
86 test: /node_modules\/@fortawesome\/fontawesome-svg-core\/styles\.css$/,
87 use: [
88 {
89 loader: MiniCssExtractPlugin.loader,
90 options: {
91 publicPath: './../'
92 }
93 },
94 {
95 loader: 'css-loader'
96 },
97 {
98 loader: 'string-replace-loader',
99 options: {
100 search: 'svg-inline--fa',
101 replace: 'neos-svg-inline--fa',
102 flags: 'g'
103 }
104 }
105 ]
106 },
107 {
108 test: /\.css$/,
109 exclude: [
110 /node_modules\/@fortawesome\/fontawesome-svg-core\/styles\.css$/,
111 /node_modules\/@ckeditor.*$/
112 ],
113 use: [
114 {
115 loader: MiniCssExtractPlugin.loader,
116 options: {
117 publicPath: './../'
118 }
119 },
120 {
121 loader: 'css-loader',
122 options: {
123 modules: {
124 localIdentName: '[name]__[local]___[hash:base64:5]'
125 },
126 importLoaders: 1
127 }
128 },
129 {
130 loader: 'postcss-loader',
131 options: {
132 config: {
133 path: path.join(__dirname, 'postcss.config.js')
134 }
135 }
136 }
137 ]
138 },
139 {
140 test: /\.vanilla-css$/,
141 use: [
142 {
143 loader: MiniCssExtractPlugin.loader,
144 options: {
145 publicPath: './../'
146 }
147 },
148 {
149 loader: 'css-loader'
150 }
151 ]
152 }
153 ]
154 },
155
156 plugins: [
157 new webpack.DefinePlugin({
158 'process.env': {
159 NODE_ENV: JSON.stringify(process.env.NODE_ENV)
160 }
161 }),
162 new webpack.optimize.OccurrenceOrderPlugin(),
163 new MiniCssExtractPlugin({filename: './Styles/[name].css'})
164 ],
165
166 optimization: {
167 splitChunks: {
168 name: 'Vendor'
169 },
170 minimizer: []
171 },
172
173 resolve: {
174 modules: [
175 path.resolve(rootPath, './node_modules')
176 ],
177 extensions: ['.ts', '.tsx', '.js']
178 },
179
180 output: {
181 filename: 'JavaScript/[name].js',
182 path: path.resolve(__dirname, '../../../Resources/Public/')
183 },
184 stats: {
185 assets: false,
186 children: false
187 },
188 performance: {
189 hints: env.isProduction ? 'warning' : false
190 }
191};
192
193//
194// Adjust the config depending on the env.
195//
196if (!env.isCi && !env.isTesting && !env.isStorybook && !env.isProduction) {
197 // TODO: LIVE RELOADING DOES NOT WORK WITH CODE SPLITTING
198 webpackConfig.plugins.push(new LiveReloadPlugin(finalLiveReloadOptions));
199}
200
201/* eslint camelcase: ["error", {properties: "never"}] */
202if (env.isProduction) {
203 webpackConfig.optimization.minimizer.push(
204 new TerserPlugin({
205 terserOptions: {
206 sourceMap: true,
207 warnings: false,
208 parse: {},
209 compress: {},
210 mangle: true,
211 keep_fnames: true
212 }
213 }),
214 );
215}
216
217module.exports = webpackConfig;