2018-01-30 11:53:12 -05:00
|
|
|
const autoprefixer = require('autoprefixer');
|
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
|
|
|
const defaults = require('lodash.defaults');
|
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
|
|
const gitsha = require('git-bundle-sha');
|
|
|
|
const path = require('path');
|
|
|
|
const webpack = require('webpack');
|
2015-10-13 15:40:51 -04:00
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
let routes = require('./src/routes.json');
|
|
|
|
const templateConfig = require('./src/template-config.js'); // eslint-disable-line global-require
|
2016-04-12 08:23:43 -04:00
|
|
|
|
2016-05-11 18:44:54 -04:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2018-01-30 11:53:12 -05:00
|
|
|
routes = routes.concat(require('./src/routes-dev.json')); // eslint-disable-line global-require
|
2016-05-11 18:44:54 -04:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:16:46 -05:00
|
|
|
routes = routes.filter(route => !process.env.VIEW || process.env.VIEW === route.view);
|
|
|
|
|
2018-01-30 11:53:12 -05:00
|
|
|
let VersionPlugin = function (options) {
|
2016-04-28 15:40:50 -04:00
|
|
|
this.options = options || {};
|
|
|
|
return this;
|
|
|
|
};
|
2018-01-30 11:53:12 -05:00
|
|
|
|
2016-04-28 15:40:50 -04:00
|
|
|
VersionPlugin.prototype.apply = function (compiler) {
|
2018-01-30 11:53:12 -05:00
|
|
|
const addVersion = function (compilation, versionId, callback) {
|
2016-04-28 15:40:50 -04:00
|
|
|
compilation.assets['version.txt'] = {
|
2018-01-30 11:53:12 -05:00
|
|
|
source: function () {
|
|
|
|
return versionId;
|
|
|
|
},
|
|
|
|
size: function () {
|
|
|
|
return versionId.length;
|
|
|
|
}
|
2016-04-28 15:40:50 -04:00
|
|
|
};
|
|
|
|
callback();
|
|
|
|
};
|
2018-01-30 11:53:12 -05:00
|
|
|
const options = this.options;
|
|
|
|
|
2016-04-28 15:40:50 -04:00
|
|
|
compiler.plugin('emit', function (compilation, callback) {
|
2018-01-30 11:53:12 -05:00
|
|
|
const sha = process.env.WWW_VERSION;
|
|
|
|
if (!sha) { // eslint-disable-line no-negated-condition
|
|
|
|
gitsha(options, function (err, _sha) {
|
2016-04-28 15:40:50 -04:00
|
|
|
if (err) return callback(err);
|
2018-01-30 11:53:12 -05:00
|
|
|
return addVersion(compilation, _sha, callback);
|
2016-04-28 15:40:50 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return addVersion(compilation, sha, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-04 01:26:56 -04:00
|
|
|
// Prepare all entry points
|
2018-01-30 11:53:12 -05:00
|
|
|
let entry = {
|
2016-05-16 12:36:28 -04:00
|
|
|
common: [
|
|
|
|
// Vendor
|
|
|
|
'raven-js',
|
|
|
|
'react',
|
|
|
|
'react-dom',
|
|
|
|
'react-intl',
|
|
|
|
'redux',
|
|
|
|
// Init
|
|
|
|
'./src/init.js'
|
|
|
|
]
|
2015-09-04 01:26:56 -04:00
|
|
|
};
|
|
|
|
routes.forEach(function (route) {
|
2016-04-09 13:07:21 -04:00
|
|
|
if (!route.redirect) {
|
2018-01-30 11:53:12 -05:00
|
|
|
entry[route.name] = `./src/views/${route.view}.jsx`;
|
2016-04-09 13:07:21 -04:00
|
|
|
}
|
2015-09-04 01:26:56 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Config
|
2015-09-02 15:08:58 -04:00
|
|
|
module.exports = {
|
2015-09-04 01:26:56 -04:00
|
|
|
entry: entry,
|
2017-11-27 14:59:45 -05:00
|
|
|
devtool: process.env.NODE_ENV === 'production' ? 'none' : 'eval',
|
2015-09-02 15:08:58 -04:00
|
|
|
output: {
|
2015-11-10 13:38:28 -05:00
|
|
|
path: path.resolve(__dirname, 'build'),
|
|
|
|
filename: 'js/[name].bundle.js'
|
2015-09-02 15:08:58 -04:00
|
|
|
},
|
|
|
|
module: {
|
2018-01-30 11:53:12 -05:00
|
|
|
rules: [
|
2015-09-02 15:08:58 -04:00
|
|
|
{
|
2018-01-30 11:53:12 -05:00
|
|
|
test: /\.jsx?$/,
|
|
|
|
loader: 'babel-loader',
|
|
|
|
include: path.resolve(__dirname, 'src'),
|
|
|
|
options: {
|
|
|
|
presets: ['es2015', 'react']
|
|
|
|
}
|
2015-09-02 15:08:58 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.scss$/,
|
2018-01-30 11:53:12 -05:00
|
|
|
use: [
|
|
|
|
'style-loader',
|
|
|
|
'css-loader',
|
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
|
|
|
plugins: function () {
|
|
|
|
return [autoprefixer({browsers: ['last 3 versions', 'Safari >= 8', 'iOS >= 8']})];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'sass-loader'
|
|
|
|
]
|
2015-09-08 14:56:28 -04:00
|
|
|
},
|
2016-06-02 15:25:02 -04:00
|
|
|
{
|
|
|
|
test: /\.css$/,
|
2018-01-30 11:53:12 -05:00
|
|
|
use: [
|
|
|
|
'style-loader',
|
|
|
|
'css-loader',
|
|
|
|
{
|
|
|
|
loader: 'postcss-loader',
|
|
|
|
options: {
|
|
|
|
plugins: function () {
|
|
|
|
return [autoprefixer({browsers: ['last 3 versions', 'Safari >= 8', 'iOS >= 8']})];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2016-06-02 15:25:02 -04:00
|
|
|
},
|
2015-09-08 14:56:28 -04:00
|
|
|
{
|
|
|
|
test: /\.(png|jpg|gif|eot|svg|ttf|woff)$/,
|
|
|
|
loader: 'url-loader'
|
2015-09-02 15:08:58 -04:00
|
|
|
}
|
2016-06-02 15:25:02 -04:00
|
|
|
],
|
|
|
|
noParse: /node_modules\/google-libphonenumber\/dist/
|
2015-09-02 16:33:31 -04:00
|
|
|
},
|
2015-10-07 11:29:17 -04:00
|
|
|
node: {
|
|
|
|
fs: 'empty'
|
|
|
|
},
|
2015-09-02 16:33:31 -04:00
|
|
|
plugins: [
|
2016-09-07 19:34:46 -04:00
|
|
|
new VersionPlugin({length: 5})
|
2018-03-19 11:58:03 -04:00
|
|
|
].concat(routes
|
2018-01-30 11:53:12 -05:00
|
|
|
.filter(function (route) {
|
|
|
|
return !route.redirect;
|
|
|
|
})
|
2016-09-09 09:34:36 -04:00
|
|
|
.map(function (route) {
|
2017-06-19 04:22:55 -04:00
|
|
|
return new HtmlWebpackPlugin(defaults({}, {
|
2016-09-09 09:34:36 -04:00
|
|
|
title: route.title,
|
|
|
|
filename: route.name + '.html',
|
|
|
|
route: route
|
2018-01-30 11:53:12 -05:00
|
|
|
}, templateConfig));
|
2016-09-09 09:34:36 -04:00
|
|
|
})
|
2018-03-19 11:58:03 -04:00
|
|
|
).concat([
|
2015-11-10 13:38:28 -05:00
|
|
|
new CopyWebpackPlugin([
|
2016-01-08 13:50:58 -05:00
|
|
|
{from: 'static'},
|
2016-01-14 08:16:02 -05:00
|
|
|
{from: 'intl', to: 'js'}
|
2015-11-10 13:38:28 -05:00
|
|
|
]),
|
2016-05-16 12:36:28 -04:00
|
|
|
new webpack.DefinePlugin({
|
2016-05-17 15:57:23 -04:00
|
|
|
'process.env.NODE_ENV': '"' + (process.env.NODE_ENV || 'development') + '"',
|
2016-06-06 10:10:27 -04:00
|
|
|
'process.env.SENTRY_DSN': '"' + (process.env.SENTRY_DSN || '') + '"',
|
|
|
|
'process.env.API_HOST': '"' + (process.env.API_HOST || 'https://api.scratch.mit.edu') + '"',
|
2018-01-30 11:53:12 -05:00
|
|
|
'process.env.SCRATCH_ENV': '"' + (process.env.SCRATCH_ENV || 'development') + '"'
|
2016-05-16 12:36:28 -04:00
|
|
|
}),
|
2018-01-30 11:53:12 -05:00
|
|
|
new webpack.optimize.CommonsChunkPlugin({
|
|
|
|
name: 'common',
|
|
|
|
filename: 'js/common.bundle.js'
|
|
|
|
})
|
2018-02-08 12:01:07 -05:00
|
|
|
])
|
2018-03-19 11:58:03 -04:00
|
|
|
.concat(process.env.NODE_ENV === 'production' ? [
|
|
|
|
new webpack.optimize.UglifyJsPlugin({
|
|
|
|
compress: {
|
|
|
|
warnings: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
] : [])
|
2015-09-02 15:08:58 -04:00
|
|
|
};
|