162 lines
4.8 KiB
JavaScript
Raw Normal View History

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');
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');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const WriteJsonPlugin = require('write-json-webpack-plugin');
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, '');
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);
2017-11-10 16:04:57 +01:00
// Necessary configuration file to ensure that plugins will be loaded.
const pluginsToInitialize = (() => {
try {
return require(path.resolve(process.cwd(), 'admin', 'src', 'config', 'plugins.json'));
} catch (e) {
return [];
}
})();
const plugins = [
new webpack.DllReferencePlugin({
manifest: require(path.join(__dirname, 'manifest.json')),
}),
// Minify and optimize the JavaScript
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
},
}),
new webpack.LoaderOptionsPlugin({
minimize: true
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// new BundleAnalyzerPlugin(),
];
2017-11-14 15:27:56 +01:00
// Default configurations.
const settings = {
path: 'admin',
folder: 'plugins',
2017-11-14 15:27:56 +01:00
host: 'http://localhost:1337'
};
2017-11-10 16:04:57 +01:00
2017-11-14 15:27:56 +01:00
if (!isSetup) {
// Load server configurations.
const serverConfig = isAdmin ?
path.resolve(process.env.PWD, '..', 'config', 'environments', _.lowerCase(process.env.NODE_ENV), 'server.json'):
path.resolve(process.env.PWD, '..', '..', 'config', 'environments', _.lowerCase(process.env.NODE_ENV), 'server.json');
2017-11-10 16:04:57 +01:00
2017-11-14 15:27:56 +01:00
const server = require(serverConfig);
const pathAccess = _.get(server, 'admin.path', 'admin');
Object.assign(settings, {
path: pathAccess[0] === '/' ? pathAccess.substring(1) : pathAccess,
folder: _.get(server, 'admin.build.plugins.folder', 'plugins'),
host: _.get(server, 'admin.build.host', 'http://localhost:1337')
});
}
2017-11-10 16:04:57 +01:00
2017-11-14 15:27:56 +01:00
// Build the `index.html file`
if (isAdmin) {
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,
},
chunksSortMode: 'manual',
chunks: ['main'],
inject: true,
}));
plugins.push(new ExtractTextPlugin('[name].[contenthash].css'));
plugins.push(new AddAssetHtmlPlugin({
filepath: path.resolve(__dirname, 'dist/*.dll.js')
}));
plugins.push(new WriteJsonPlugin({
object: pluginsToInitialize,
path: 'config',
// default output is timestamp.json
filename: 'plugins.json',
}));
}
const appPath = isAdmin
? path.join(process.cwd(), 'admin', 'src', 'app.js')
: path.join(process.cwd(), 'node_modules', 'strapi-helper-plugin', 'lib', 'src', 'app.js');
2017-01-17 13:40:59 +01:00
module.exports = require('./webpack.base.babel')({
// In production, we skip all hot-reloading stuff
entry: {
main: appPath,
},
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',
chunkFilename: '[name].[chunkhash].chunk.js'
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,
}),
],
// Plugins
plugins,
2017-06-17 11:45:07 +02:00
// Babel presets configuration
babelPresets: [
[
require.resolve('babel-preset-latest'),
{
es2015: {
modules: false,
2017-06-17 11:45:07 +02:00
},
},
],
require.resolve('babel-preset-react'),
require.resolve('babel-preset-stage-0'),
],
alias: {
moment: 'moment/moment.js',
'lodash': path.resolve(__dirname, '..', '..', '..', 'node_modules', 'lodash'),
'immutable': path.resolve(__dirname, '..', '..', '..', 'node_modules', 'immutable'),
'react-intl': path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react-intl'),
'react': path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react'),
'react-dom': path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react-dom'),
'react-transition-group': path.resolve(__dirname, '..', '..', '..', 'node_modules', 'react-transition-group')
},
devtool: 'cheap-module-source-map',
2017-01-17 13:40:59 +01:00
});