| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * COMMON WEBPACK CONFIGURATION | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 16:36:28 +02:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2017-05-11 14:17:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  | const webpack = require('webpack'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 15:31:42 +02:00
										 |  |  | const pkg = require(path.resolve(process.cwd(), 'package.json')); | 
					
						
							|  |  |  | const pluginId = pkg.name.replace(/^strapi-/i, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 17:18:43 +02:00
										 |  |  | let noPlugin = false; | 
					
						
							|  |  |  | let plugins = []; | 
					
						
							|  |  |  | let pluginFolders = {}; | 
					
						
							| 
									
										
										
										
											2017-09-26 16:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 17:18:43 +02:00
										 |  |  | if (process.env.npm_lifecycle_event === 'start') { | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     fs.accessSync(path.resolve(process.env.PWD, '..', 'plugins'), fs.constants.R_OK); | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       fs.accessSync(path.resolve(process.env.PWD, '..', 'api'), fs.constants.R_OK); | 
					
						
							| 
									
										
										
										
											2017-09-26 16:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 17:18:43 +02:00
										 |  |  |       // Allow app without plugins.
 | 
					
						
							|  |  |  |       noPlugin = true; | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       throw new Error(`You need to start the WebPack server from the /admin directory in a Strapi's project.`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   plugins = process.env.IS_ADMIN === 'true' && !noPlugin ? fs.readdirSync(path.resolve(process.env.PWD, '..', 'plugins')) | 
					
						
							|  |  |  |     .filter(x => x[0] !== '.') : []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   pluginFolders = plugins.reduce((acc, current) => { | 
					
						
							|  |  |  |     acc[current] = path.resolve(process.env.PWD, '..', 'plugins', current, 'node_modules', 'strapi-helper-plugin', 'lib', 'src'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return acc; | 
					
						
							|  |  |  |   }, {}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-26 16:36:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  | module.exports = (options) => ({ | 
					
						
							| 
									
										
										
										
											2017-09-27 15:09:46 +02:00
										 |  |  |   entry: options.entry, | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |   output: Object.assign({ // Compile into js/build.js
 | 
					
						
							| 
									
										
										
										
											2017-06-09 13:03:23 +01:00
										 |  |  |     path: path.resolve(process.cwd(), 'admin', 'build'), | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     publicPath: '/', | 
					
						
							|  |  |  |   }, options.output), // Merge with env dependent settings
 | 
					
						
							|  |  |  |   module: { | 
					
						
							|  |  |  |     loaders: [{ | 
					
						
							| 
									
										
										
										
											2017-09-22 16:56:09 +02:00
										 |  |  |       test: /\.js$/, // Transform all .js files required somewhere with Babel,
 | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |       use: { | 
					
						
							| 
									
										
										
										
											2017-08-18 17:51:10 +02:00
										 |  |  |         loader: 'babel-loader', | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |         options: { | 
					
						
							| 
									
										
										
										
											2017-07-21 17:18:47 +02:00
										 |  |  |           presets: options.babelPresets, | 
					
						
							|  |  |  |           env: { | 
					
						
							|  |  |  |             production: { | 
					
						
							|  |  |  |               only: [ | 
					
						
							|  |  |  |                 'src', | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |               ], | 
					
						
							| 
									
										
										
										
											2017-07-21 17:18:47 +02:00
										 |  |  |               plugins: [ | 
					
						
							| 
									
										
										
										
											2017-05-17 11:21:03 +02:00
										 |  |  |                 require.resolve('babel-plugin-transform-react-remove-prop-types'), | 
					
						
							|  |  |  |                 require.resolve('babel-plugin-transform-react-constant-elements'), | 
					
						
							|  |  |  |                 require.resolve('babel-plugin-transform-react-inline-elements'), | 
					
						
							| 
									
										
										
										
											2017-06-06 14:22:17 +02:00
										 |  |  |               ], | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-07-21 17:18:47 +02:00
										 |  |  |             test: { | 
					
						
							|  |  |  |               plugins: [ | 
					
						
							|  |  |  |                 'istanbul', | 
					
						
							| 
									
										
										
										
											2017-06-06 14:22:17 +02:00
										 |  |  |               ], | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2017-07-21 17:18:47 +02:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2017-09-26 16:36:28 +02:00
										 |  |  |       include: [path.join(process.cwd(), 'admin', 'src')] | 
					
						
							|  |  |  |         .concat(plugins.reduce((acc, current) => { | 
					
						
							|  |  |  |           acc.push(path.resolve(process.env.PWD, '..', 'plugins', current, 'admin', 'src'), pluginFolders[current]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return acc; | 
					
						
							|  |  |  |         }, [])) | 
					
						
							|  |  |  |         .concat([path.join(process.cwd(), 'node_modules', 'strapi-helper-plugin', 'lib', 'src')]) | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     }, { | 
					
						
							| 
									
										
										
										
											2017-05-17 11:21:03 +02:00
										 |  |  |       // Transform our own .scss files
 | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |       test: /\.scss$/, | 
					
						
							| 
									
										
										
										
											2017-08-22 15:31:42 +02:00
										 |  |  |       use: [{ | 
					
						
							|  |  |  |         loader: 'style-loader', | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         loader: 'css-loader', | 
					
						
							|  |  |  |         options: { | 
					
						
							|  |  |  |           localIdentName: `${pluginId}[local]__[path][name]__[hash:base64:5]`, | 
					
						
							|  |  |  |           modules: true, | 
					
						
							|  |  |  |           importLoaders: 1, | 
					
						
							|  |  |  |           sourceMap: true, | 
					
						
							| 
									
										
										
										
											2017-09-27 15:09:46 +02:00
										 |  |  |           minimize: process.env.NODE_ENV === 'production' | 
					
						
							| 
									
										
										
										
											2017-08-22 15:31:42 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         loader: 'postcss-loader', | 
					
						
							|  |  |  |         options: { | 
					
						
							|  |  |  |           config: { | 
					
						
							|  |  |  |             path: path.resolve(__dirname, '..', 'postcss', 'postcss.config.js'), | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, { | 
					
						
							|  |  |  |         loader: 'sass-loader', | 
					
						
							|  |  |  |       }], | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |       // Do not transform vendor's CSS with CSS-modules
 | 
					
						
							|  |  |  |       // The point is that they remain in global scope.
 | 
					
						
							|  |  |  |       // Since we require these CSS files in our JS or CSS files,
 | 
					
						
							|  |  |  |       // they will be a part of our compilation either way.
 | 
					
						
							|  |  |  |       // So, no need for ExtractTextPlugin here.
 | 
					
						
							|  |  |  |       test: /\.css$/, | 
					
						
							|  |  |  |       include: /node_modules/, | 
					
						
							| 
									
										
										
										
											2017-09-27 15:09:46 +02:00
										 |  |  |       loaders: ['style-loader', { | 
					
						
							|  |  |  |         loader: 'css-loader', | 
					
						
							|  |  |  |         options: { | 
					
						
							|  |  |  |           minimize: process.env.NODE_ENV === 'production', | 
					
						
							|  |  |  |           sourceMap: true, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }], | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |       test: /\.(eot|svg|ttf|woff|woff2)$/, | 
					
						
							|  |  |  |       loader: 'file-loader', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       test: /\.(jpg|png|gif)$/, | 
					
						
							|  |  |  |       loaders: [ | 
					
						
							|  |  |  |         'file-loader', | 
					
						
							| 
									
										
										
										
											2017-08-18 17:51:10 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |           loader: 'image-webpack-loader', | 
					
						
							|  |  |  |           query: { | 
					
						
							|  |  |  |             mozjpeg: { | 
					
						
							|  |  |  |               progressive: true, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             gifsicle: { | 
					
						
							|  |  |  |               interlaced: false, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             optipng: { | 
					
						
							|  |  |  |               optimizationLevel: 4, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             pngquant: { | 
					
						
							|  |  |  |               quality: '65-90', | 
					
						
							|  |  |  |               speed: 4, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |       ], | 
					
						
							| 
									
										
										
										
											2017-08-18 14:17:15 +02:00
										 |  |  |     }, { | 
					
						
							|  |  |  |       test: /\.html$/, | 
					
						
							|  |  |  |       loader: 'html-loader', | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     }, { | 
					
						
							|  |  |  |       test: /\.json$/, | 
					
						
							|  |  |  |       loader: 'json-loader', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       test: /\.(mp4|webm)$/, | 
					
						
							|  |  |  |       loader: 'url-loader?limit=10000', | 
					
						
							|  |  |  |     }], | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-09-26 17:37:40 +02:00
										 |  |  |   plugins: [ | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     new webpack.ProvidePlugin({ | 
					
						
							|  |  |  |       // make fetch available
 | 
					
						
							| 
									
										
										
										
											2017-08-18 17:51:10 +02:00
										 |  |  |       fetch: 'exports-loader?self.fetch!whatwg-fetch', | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     }), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Always expose NODE_ENV to webpack, in order to use `process.env.NODE_ENV`
 | 
					
						
							|  |  |  |     // inside your code for any environment checks; UglifyJS will automatically
 | 
					
						
							|  |  |  |     // drop any unreachable code.
 | 
					
						
							|  |  |  |     new webpack.DefinePlugin({ | 
					
						
							|  |  |  |       'process.env': { | 
					
						
							|  |  |  |         NODE_ENV: JSON.stringify(process.env.NODE_ENV), | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }), | 
					
						
							| 
									
										
										
										
											2017-09-27 15:09:46 +02:00
										 |  |  |     new webpack.NamedModulesPlugin() | 
					
						
							| 
									
										
										
										
											2017-09-26 17:37:40 +02:00
										 |  |  |   ].concat(options.plugins), | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |   resolve: { | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |     modules: [ | 
					
						
							| 
									
										
										
										
											2017-06-08 17:16:20 +01:00
										 |  |  |       'admin/src', | 
					
						
							|  |  |  |       'node_modules/strapi-helper-plugin/lib/src', | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |       'node_modules/strapi-helper-plugin/node_modules', | 
					
						
							| 
									
										
										
										
											2017-06-06 14:22:17 +02:00
										 |  |  |       'node_modules', | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |     ], | 
					
						
							| 
									
										
										
										
											2017-09-27 15:09:46 +02:00
										 |  |  |     alias: options.alias, | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |     symlinks: false, | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |     extensions: [ | 
					
						
							|  |  |  |       '.js', | 
					
						
							|  |  |  |       '.jsx', | 
					
						
							|  |  |  |       '.react.js', | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     mainFields: [ | 
					
						
							|  |  |  |       'browser', | 
					
						
							|  |  |  |       'jsnext:main', | 
					
						
							|  |  |  |       'main', | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-09-27 15:09:46 +02:00
										 |  |  |   externals: options.externals, | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |   resolveLoader: { | 
					
						
							|  |  |  |     modules: [ | 
					
						
							|  |  |  |       path.join(__dirname, '..', '..', '..', 'node_modules'), | 
					
						
							|  |  |  |       path.join(process.cwd(), 'node_modules'), | 
					
						
							| 
									
										
										
										
											2017-06-06 14:22:17 +02:00
										 |  |  |     ], | 
					
						
							| 
									
										
										
										
											2017-05-16 16:32:54 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  |   devtool: options.devtool, | 
					
						
							| 
									
										
										
										
											2017-09-26 16:36:28 +02:00
										 |  |  |   target: 'web', // Make web variables accessible to webpack, e.g. window,
 | 
					
						
							| 
									
										
										
										
											2017-01-17 13:40:59 +01:00
										 |  |  | }); |