Skip to content

Webpack v4 Update #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,30 @@
"lint": "eslint --ext .js,.jsx ."
},
"dependencies": {
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"actioncable": "5.0.x",
"autoprefixer": "^7.1.5",
"axios": "^0.17.1",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-loader": "^7.1.5",
"babel-plugin-transform-es2015-modules-amd": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-runtime": "^6.26.0",
"bootstrap-loader": "^2.2.0",
"bootstrap-sass": "^3.3.7",
"bootstrap-loader": "^3.0.2",
"bootstrap-sass": "^3.4.1",
"classnames": "^2.2.5",
"compression-webpack-plugin": "^1.0.1",
"compression-webpack-plugin": "^2.0.0",
"css-loader": "^0.28.7",
"es5-shim": "^4.5.9",
"estraverse": "^4.2.0",
"expose-loader": "^0.7.4",
"expose-loader": "^0.7.5",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.5",
"file-loader": "^3.0.1",
"glob": "^7.1.2",
"immutable": "^3.8.2",
"imports-loader": "^0.7.1",
Expand All @@ -71,8 +73,9 @@
"js-yaml": "^3.10.0",
"loader-utils": "^1.1.0",
"lodash": "^4.17.4",
"marked": "^0.3.6",
"node-sass": "^4.7.2",
"marked": "^0.6.1",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.11.0",
"node-uuid": "^1.4.8",
"postcss-loader": "^2.0.8",
"prop-types": "^15.6.0",
Expand All @@ -88,14 +91,15 @@
"react-router-redux": "^4.0.8",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"resolve-url-loader": "^2.2.0",
"resolve-url-loader": "^3.0.1",
"sass-loader": "^6.0.6",
"sass-resources-loader": "^1.3.1",
"style-loader": "^0.19.0",
"sass-resources-loader": "^1.3.3",
"style-loader": "^0.23.1",
"turbolinks": "^5.0.3",
"url-loader": "^0.6.2",
"webpack": "^3.8.1",
"webpack-manifest-plugin": "^1.3.2",
"url-loader": "^1.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-manifest-plugin": "^2.0.4",
"webpack-merge": "^4.1.1"
},
"devDependencies": {
Expand All @@ -113,11 +117,11 @@
"express": "^4.16.2",
"identity-obj-proxy": "^3.0.0",
"jest": "^23.5.0",
"pug": "^2.0.0-rc.4",
"pug": "^2.0.3",
"react-transform-hmr": "^1.0.4",
"regenerator-runtime": "^0.12.1",
"sleep": "^5.1.1",
"webpack-dev-server": "^2.9.4"
"webpack-dev-server": "^3.2.1"
},
"jest": {
"moduleNameMapper": {
Expand Down
68 changes: 48 additions & 20 deletions client/webpack.client.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

// Common client-side webpack configuration used by webpack.hot.config and webpack.rails.config.
const webpack = require('webpack');

const urlFileSizeCutover = 10 * 1024;
const ManifestPlugin = require('webpack-manifest-plugin');
const { resolve } = require('path');
const webpackConfigLoader = require('react-on-rails/webpackConfigLoader');
Expand Down Expand Up @@ -45,28 +47,30 @@ module.exports = {
'client/node_modules',
],
},

optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
name: 'vendor-bundle',
test: /[\\/]node_modules[\\/]/,
priority: -10,
chunks: 'all',
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
}
}
},
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: 'development', // use 'development' unless process.env.NODE_ENV is defined
DEBUG: false,
TRACE_TURBOLINKS: devBuild,
}),

// https://webpack.github.io/docs/list-of-plugins.html#2-explicit-vendor-chunk
new webpack.optimize.CommonsChunkPlugin({

// This name 'vendor-bundle' ties into the entry definition
name: 'vendor-bundle',

// We don't want the default vendor.js name
filename: 'vendor-bundle-[hash].js',

minChunks(module) {
// this assumes your vendor imports exist in the node_modules directory
return module.context && module.context.indexOf('node_modules') !== -1;
},
}),
new ManifestPlugin({
publicPath: output.publicPath,
writeToFileEmit: true
Expand All @@ -75,19 +79,42 @@ module.exports = {

module: {
rules: [
{
test: /\.svg$/,
loader: 'svg-url-loader',
options: {
limit: urlFileSizeCutover,
// Remove quotes around the encoded URL –
// they’re rarely useful
noquotes: true,
name: '[name].[hash].[ext]',
publicPath: output.publicPath,
},
},
{
test: /\.(ttf|eot)$/,
use: 'file-loader',
},
{
test: /\.(woff2?|jpe?g|png|gif|svg|ico)$/,
test: /\.(woff2|woff|jpe?g|png|gif|ico)?$/,
use: {
loader: 'resource-url-loader',
Copy link
Collaborator

@Judahmeek Judahmeek Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loader: 'url-loader',

options: {
name: '[name].[hash].[ext]',
limit: urlFileSizeCutover,
publicPath: output.publicPath,
},
},
},
{
test: /\.jsx?$/,
use: {
loader: 'url-loader',
loader: 'babel-loader',
options: {
name: '[name]-[hash].[ext]',
limit: 10000,
cacheDirectory: true,
},
},
exclude: /node_modules/,
},
{
test: require.resolve('jquery'),
Expand Down Expand Up @@ -135,3 +162,4 @@ module.exports = {
],
},
};

102 changes: 58 additions & 44 deletions client/webpack.client.rails.build.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
// cd client && yarn run build:client
// Note that Foreman (Procfile.dev) has also been configured to take care of this.

const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const merge = require('webpack-merge');
const config = require('./webpack.client.base.config');
const { resolve } = require('path');
const webpackConfigLoader = require('react-on-rails/webpackConfigLoader');

const configPath = resolve('..', 'config');
const { output } = webpackConfigLoader(configPath);

const sassResources = [
'./app/assets/styles/app-variables.scss',
'./app/assets/styles/bootstrap-pre-customizations.scss'
];
const devBuild = process.env.NODE_ENV !== 'production';

if (devBuild) {
Expand All @@ -32,7 +35,7 @@ module.exports = merge(config, {
'bootstrap-loader/extractStyles'
],
},

mode: process.env.NODE_ENV,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and add

mode: process.env.NODE_ENV === 'development' ? 'development' : 'production',

to client/webpack.client.base.config.js & client/webpack.server.rails.build.config.js

output: {
filename: '[name]-[chunkhash].js',
chunkFilename: '[name]-[chunkhash].chunk.js',
Expand All @@ -51,46 +54,57 @@ module.exports = merge(config, {
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true,
modules: true,
importLoaders: 1,
localIdentName: '[name]__[local]__[hash:base64:5]',
},
use: [
'style-loader', // For client bundle no MiniCssExtractPlugin
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: '[name]__[local]__[hash:base64:5]',
},
},
'postcss-loader',
],
},
{
test: /^((?!\.global).)*\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 2,
localIdentName: '[name]__[local]__[hash:base64:5]',
},
'postcss-loader',
],
}),
},
'postcss-loader',
'sass-loader',
{
loader: 'sass-resources-loader',
options: { resources: sassResources },
},
],
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true,
modules: true,
importLoaders: 3,
localIdentName: '[name]__[local]__[hash:base64:5]',
},
test: /\.global\.scss$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: false,
importLoaders: 2,
},
'postcss-loader',
'sass-loader',
{
loader: 'sass-resources-loader',
options: {
resources: './app/assets/styles/app-variables.scss'
},
}
],
}),
},
'postcss-loader',
'sass-loader',
{
loader: 'sass-resources-loader',
options: { resources: sassResources },
},
],
},
{
test: require.resolve('react'),
Expand All @@ -110,14 +124,14 @@ module.exports = merge(config, {
jQuery: 'jquery',
}
}
}
},
],
},

plugins: [
new ExtractTextPlugin({
filename: '[name]-[hash].css',
allChunks: true
}),
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
})
],
});
Loading