2017-01-17 13:40:59 +01:00
|
|
|
// Important modules this config uses
|
2017-11-10 16:04:57 +01:00
|
|
|
const _ = require('lodash');
|
2017-01-17 13:40:59 +01:00
|
|
|
const path = require('path');
|
|
|
|
|
2018-01-12 16:41:59 +01:00
|
|
|
const base = require('./webpack.base.babel');
|
|
|
|
|
2017-08-18 14:17:15 +02:00
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const ExtractTextPlugin = require('extract-text-webpack-plugin');
|
2017-01-17 13:40:59 +01:00
|
|
|
const cssnext = require('postcss-cssnext');
|
|
|
|
const postcssFocus = require('postcss-focus');
|
|
|
|
const postcssReporter = require('postcss-reporter');
|
2017-05-11 14:17:21 +02:00
|
|
|
const webpack = require('webpack');
|
2017-09-27 15:09:46 +02:00
|
|
|
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
|
|
|
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
|
2018-01-12 16:41:59 +01:00
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
2017-05-11 14:17:21 +02:00
|
|
|
|
|
|
|
const pkg = require(path.resolve(process.cwd(), 'package.json'));
|
2017-08-23 13:21:54 +02:00
|
|
|
const pluginId = pkg.name.replace(/^strapi-plugin-/i, '');
|
2017-09-27 15:09:46 +02:00
|
|
|
const dllPlugin = pkg.dllPlugin;
|
2017-01-17 13:40:59 +01:00
|
|
|
|
2017-11-10 16:04:57 +01:00
|
|
|
const isAdmin = process.env.IS_ADMIN === 'true';
|
2017-11-14 15:27:56 +01:00
|
|
|
const isSetup = path.resolve(process.env.PWD, '..', '..') === path.resolve(process.env.INIT_CWD);
|
2018-01-08 16:34:41 +01:00
|
|
|
const appPath = (() => {
|
|
|
|
if (process.env.APP_PATH) {
|
|
|
|
return process.env.APP_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isAdmin ? path.resolve(process.env.PWD, '..') : path.resolve(process.env.PWD, '..', '..');
|
|
|
|
})();
|
2018-01-09 17:09:54 +01:00
|
|
|
const adminPath = (() => {
|
|
|
|
if (isSetup) {
|
|
|
|
return isAdmin ? path.resolve(appPath, 'strapi-admin') : path.resolve(process.env.PWD, '..');
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.resolve(appPath, 'admin');
|
|
|
|
})();
|
2017-11-14 15:27:56 +01:00
|
|
|
|
2017-08-18 14:17:15 +02:00
|
|
|
const plugins = [
|
2017-09-27 15:09:46 +02:00
|
|
|
new webpack.DllReferencePlugin({
|
2017-11-20 15:01:49 +01:00
|
|
|
manifest: require(isSetup ?
|
|
|
|
path.join(__dirname, 'manifest.json'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'lib', 'internals', 'webpack', 'manifest.json')
|
|
|
|
),
|
2017-08-18 14:17:15 +02:00
|
|
|
}),
|
|
|
|
// Minify and optimize the JavaScript
|
|
|
|
new webpack.optimize.UglifyJsPlugin({
|
2017-09-27 15:09:46 +02:00
|
|
|
sourceMap: true,
|
2017-12-04 14:29:16 +01:00
|
|
|
parallel: true,
|
2017-08-18 14:17:15 +02:00
|
|
|
compress: {
|
2017-09-27 15:09:46 +02:00
|
|
|
warnings: false
|
2017-08-18 14:17:15 +02:00
|
|
|
},
|
2017-12-04 14:29:16 +01:00
|
|
|
uglifyOptions: {
|
|
|
|
ecma: 8,
|
|
|
|
},
|
2017-08-18 14:17:15 +02:00
|
|
|
}),
|
2017-09-27 15:09:46 +02:00
|
|
|
new webpack.LoaderOptionsPlugin({
|
|
|
|
minimize: true
|
|
|
|
}),
|
|
|
|
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
|
|
|
// new BundleAnalyzerPlugin(),
|
2017-08-18 14:17:15 +02:00
|
|
|
];
|
|
|
|
|
2018-01-12 14:46:02 +01:00
|
|
|
let publicPath;
|
|
|
|
|
2018-01-12 14:53:20 +01:00
|
|
|
if (isAdmin && !isSetup) {
|
2018-01-12 14:46:02 +01:00
|
|
|
// Load server configuration.
|
|
|
|
const serverConfig = path.resolve(appPath, 'config', 'environments', _.lowerCase(process.env.NODE_ENV), 'server.json');
|
|
|
|
|
|
|
|
try {
|
|
|
|
const server = require(serverConfig);
|
|
|
|
|
|
|
|
if (process.env.PWD.indexOf('/admin') !== -1) {
|
|
|
|
if (_.get(server, 'admin.build.host')) {
|
|
|
|
publicPath = _.get(server, 'admin.build.host', '/admin').replace(/\/$/, '') || '/';
|
|
|
|
} else {
|
|
|
|
publicPath = _.get(server, 'admin.path', '/admin');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Impossible to access to ${serverConfig}`);
|
|
|
|
}
|
2018-01-12 14:53:20 +01:00
|
|
|
}
|
2018-01-12 14:46:02 +01:00
|
|
|
|
2018-01-12 14:53:20 +01:00
|
|
|
// Build the `index.html file`
|
|
|
|
if (isAdmin) {
|
2017-08-18 14:17:15 +02:00
|
|
|
plugins.push(new HtmlWebpackPlugin({
|
|
|
|
template: 'admin/src/index.html',
|
|
|
|
minify: {
|
|
|
|
removeComments: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
removeRedundantAttributes: true,
|
|
|
|
useShortDoctype: true,
|
|
|
|
removeEmptyAttributes: true,
|
|
|
|
removeStyleLinkTypeAttributes: true,
|
|
|
|
keepClosingSlash: true,
|
|
|
|
minifyJS: true,
|
|
|
|
minifyCSS: true,
|
|
|
|
minifyURLs: true,
|
|
|
|
},
|
2017-09-27 15:09:46 +02:00
|
|
|
chunksSortMode: 'manual',
|
|
|
|
chunks: ['main'],
|
2017-08-18 14:17:15 +02:00
|
|
|
inject: true,
|
|
|
|
}));
|
|
|
|
plugins.push(new ExtractTextPlugin('[name].[contenthash].css'));
|
2017-09-27 15:09:46 +02:00
|
|
|
plugins.push(new AddAssetHtmlPlugin({
|
|
|
|
filepath: path.resolve(__dirname, 'dist/*.dll.js')
|
|
|
|
}));
|
2018-01-12 16:41:59 +01:00
|
|
|
|
|
|
|
// Necessary configuration file to ensure that plugins will be loaded.
|
|
|
|
const pluginsToInitialize = (() => {
|
|
|
|
try {
|
|
|
|
return require(path.resolve(adminPath, 'admin', 'src', 'config', 'plugins.json'));
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
plugins.push(new CopyWebpackPlugin([{
|
|
|
|
from: 'config/plugins.json',
|
|
|
|
context: path.resolve(adminPath, 'admin', 'src'),
|
|
|
|
to: 'config/plugins.json'
|
|
|
|
}]));
|
2017-08-18 14:17:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 15:01:49 +01:00
|
|
|
const main = (() => {
|
|
|
|
if (isAdmin && isSetup) {
|
|
|
|
return path.join(process.cwd(), 'admin', 'src', 'app.js');
|
|
|
|
} else if (isAdmin) {
|
|
|
|
return path.join(appPath, 'admin', 'admin', 'src', 'app.js');
|
|
|
|
}
|
|
|
|
|
2018-01-09 17:09:54 +01:00
|
|
|
return path.join(process.env.PWD, 'node_modules', 'strapi-helper-plugin', 'lib', 'src', 'app.js');
|
2017-11-20 15:01:49 +01:00
|
|
|
})();
|
2017-08-18 14:17:15 +02:00
|
|
|
|
2018-01-12 16:41:59 +01:00
|
|
|
module.exports = base({
|
2017-01-17 13:40:59 +01:00
|
|
|
// In production, we skip all hot-reloading stuff
|
2017-09-27 15:09:46 +02:00
|
|
|
entry: {
|
2017-11-17 16:05:47 +01:00
|
|
|
main
|
2017-09-27 15:09:46 +02:00
|
|
|
},
|
2017-01-17 13:40:59 +01:00
|
|
|
|
|
|
|
// Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
|
|
|
|
output: {
|
|
|
|
filename: '[name].js',
|
2018-01-12 14:46:02 +01:00
|
|
|
chunkFilename: '[name].[chunkhash].chunk.js',
|
|
|
|
publicPath,
|
2017-01-17 13:40:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// In production, we minify our CSS with cssnano
|
|
|
|
postcssPlugins: [
|
|
|
|
postcssFocus(),
|
|
|
|
cssnext({
|
|
|
|
browsers: ['last 2 versions', 'IE > 10'],
|
|
|
|
}),
|
|
|
|
postcssReporter({
|
|
|
|
clearMessages: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
|
2017-08-18 14:17:15 +02:00
|
|
|
// Plugins
|
|
|
|
plugins,
|
|
|
|
|
2017-06-17 11:45:07 +02:00
|
|
|
// Babel presets configuration
|
|
|
|
babelPresets: [
|
|
|
|
[
|
2017-11-20 17:04:48 +01:00
|
|
|
require.resolve('babel-preset-env'),
|
2017-06-17 11:45:07 +02:00
|
|
|
{
|
2017-08-18 14:17:15 +02:00
|
|
|
es2015: {
|
|
|
|
modules: false,
|
2017-06-17 11:45:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
require.resolve('babel-preset-react'),
|
|
|
|
require.resolve('babel-preset-stage-0'),
|
|
|
|
],
|
2017-09-27 15:09:46 +02:00
|
|
|
|
|
|
|
alias: {
|
|
|
|
moment: 'moment/moment.js',
|
2017-12-05 12:10:08 +01:00
|
|
|
'babel-polyfill': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'babel-polyfill'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'babel-polyfill'),
|
2017-11-20 15:01:49 +01:00
|
|
|
'lodash': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'lodash'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'lodash'),
|
|
|
|
'immutable': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'immutable'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'immutable'),
|
|
|
|
'react-intl': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react-intl'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'react-intl'),
|
|
|
|
'react': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'react'),
|
|
|
|
'react-dom': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react-dom'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'react-dom'),
|
|
|
|
'react-transition-group': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react-transition-group'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'react-transition-group'),
|
|
|
|
'reactstrap': isSetup ?
|
|
|
|
path.resolve(__dirname, '..', '..', '..', 'node_modules', 'reactstrap'):
|
|
|
|
path.resolve(appPath, 'admin', 'node_modules', 'strapi-helper-plugin', 'node_modules', 'reactstrap')
|
2017-09-27 15:09:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
devtool: 'cheap-module-source-map',
|
2017-01-17 13:40:59 +01:00
|
|
|
});
|