Skip to content

Commit bdd2574

Browse files
committed
Added Loader for Static Build
1 parent d5b4ba7 commit bdd2574

File tree

5 files changed

+2509
-756
lines changed

5 files changed

+2509
-756
lines changed

client/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
"resolve-url-loader": "^3.0.1",
9595
"sass-loader": "^6.0.6",
9696
"sass-resources-loader": "^1.3.3",
97-
"style-loader": "^0.19.0",
97+
"style-loader": "^0.23.1",
9898
"turbolinks": "^5.0.3",
99-
"url-loader": "^0.6.2",
99+
"url-loader": "^1.1.2",
100100
"webpack": "^4.29.6",
101101
"webpack-cli": "^3.3.0",
102102
"webpack-manifest-plugin": "^2.0.4",

client/webpack.client.base.config.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
// Common client-side webpack configuration used by webpack.hot.config and webpack.rails.config.
55
const webpack = require('webpack');
6+
7+
const urlFileSizeCutover = 10 * 1024;
68
const ManifestPlugin = require('webpack-manifest-plugin');
79
const { resolve } = require('path');
810
const webpackConfigLoader = require('react-on-rails/webpackConfigLoader');
@@ -16,7 +18,6 @@ module.exports = {
1618

1719
// the project dir
1820
context: resolve(__dirname),
19-
mode: process.env.NODE_ENV,
2021
entry: {
2122
// This will contain the app entry points defined by
2223
// webpack.client.rails.hot.config and webpack.client.rails.build.config
@@ -51,6 +52,7 @@ module.exports = {
5152
chunks: 'all',
5253
cacheGroups: {
5354
vendors: {
55+
name: 'vendor-bundle',
5456
test: /[\\/]node_modules[\\/]/,
5557
priority: -10,
5658
chunks: 'all',
@@ -69,7 +71,6 @@ module.exports = {
6971
DEBUG: false,
7072
TRACE_TURBOLINKS: devBuild,
7173
}),
72-
// https://webpack.github.io/docs/list-of-plugins.html#2-explicit-vendor-chunk
7374
new ManifestPlugin({
7475
publicPath: output.publicPath,
7576
writeToFileEmit: true
@@ -78,20 +79,43 @@ module.exports = {
7879

7980
module: {
8081
rules: [
82+
{
83+
test: /\.svg$/,
84+
loader: 'svg-url-loader',
85+
options: {
86+
limit: urlFileSizeCutover,
87+
// Remove quotes around the encoded URL –
88+
// they’re rarely useful
89+
noquotes: true,
90+
name: '[name].[hash].[ext]',
91+
publicPath: output.publicPath,
92+
},
93+
},
8194
{
8295
test: /\.(ttf|eot)$/,
8396
use: 'file-loader',
8497
},
8598
{
86-
test: /\.(woff2?|jpe?g|png|gif|svg|ico)$/,
99+
test: /\.(woff2|woff|jpe?g|png|gif|ico)?$/,
87100
use: {
88-
loader: 'url-loader',
101+
loader: 'resource-url-loader',
89102
options: {
90-
name: '[name]-[hash].[ext]',
91-
limit: 10000,
103+
name: '[name].[hash].[ext]',
104+
limit: urlFileSizeCutover,
105+
publicPath: output.publicPath,
92106
},
93107
},
94108
},
109+
{
110+
test: /\.jsx?$/,
111+
use: {
112+
loader: 'babel-loader',
113+
options: {
114+
cacheDirectory: true,
115+
},
116+
},
117+
exclude: /node_modules/,
118+
},
95119
{
96120
test: require.resolve('jquery'),
97121
use: [
@@ -138,3 +162,4 @@ module.exports = {
138162
],
139163
},
140164
};
165+

client/webpack.client.rails.build.config.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// cd client && yarn run build:client
66
// Note that Foreman (Procfile.dev) has also been configured to take care of this.
77

8-
// const ExtractTextPlugin = require('extract-text-webpack-plugin');
98
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
109
const merge = require('webpack-merge');
1110
const config = require('./webpack.client.base.config');
@@ -14,7 +13,10 @@ const webpackConfigLoader = require('react-on-rails/webpackConfigLoader');
1413

1514
const configPath = resolve('..', 'config');
1615
const { output } = webpackConfigLoader(configPath);
17-
16+
const sassResources = [
17+
'./app/assets/styles/app-variables.scss',
18+
'./app/assets/styles/bootstrap-pre-customizations.scss'
19+
];
1820
const devBuild = process.env.NODE_ENV !== 'production';
1921

2022
if (devBuild) {
@@ -33,7 +35,7 @@ module.exports = merge(config, {
3335
'bootstrap-loader/extractStyles'
3436
],
3537
},
36-
38+
mode: process.env.NODE_ENV,
3739
output: {
3840
filename: '[name]-[chunkhash].js',
3941
chunkFilename: '[name]-[chunkhash].chunk.js',
@@ -51,46 +53,58 @@ module.exports = merge(config, {
5153
exclude: /node_modules/,
5254
},
5355
{
54-
test: /\.(scss|css)$/,
56+
test: /\.css$/,
5557
use: [
56-
MiniCssExtractPlugin.loader,
57-
'css-loader',
58-
'resolve-url-loader?keepQuery',
59-
'svg-fill-loader/encodeSharp',
60-
'sass-loader',
58+
'style-loader', // For client bundle no MiniCssExtractPlugin
59+
{
60+
loader: 'css-loader',
61+
options: {
62+
modules: true,
63+
importLoaders: 1,
64+
localIdentName: '[name]__[local]__[hash:base64:5]',
65+
},
66+
},
67+
'postcss-loader',
6168
],
6269
},
6370
{
64-
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
71+
test: /^((?!\.global).)*\.scss$/,
6572
use: [
73+
'style-loader',
6674
{
67-
loader: 'url-loader?limit=10000&mimetype=image/svg+xml',
75+
loader: 'css-loader',
76+
options: {
77+
modules: true,
78+
importLoaders: 2,
79+
localIdentName: '[name]__[local]__[hash:base64:5]',
80+
},
6881
},
82+
'postcss-loader',
83+
'sass-loader',
6984
{
70-
loader: 'svg-fill-loader?selector=svg',
85+
loader: 'sass-resources-loader',
86+
options: { resources: sassResources },
7187
},
7288
],
7389
},
7490
{
75-
test: /bootstrap-sass\/assets\/javascripts\//,
76-
loader: 'imports-loader?jQuery=jquery',
77-
},
78-
{
79-
test: /\.(scss|css)$/,
91+
test: /\.global\.scss$/,
8092
use: [
8193
'style-loader',
82-
MiniCssExtractPlugin.loader,
8394
{
8495
loader: 'css-loader',
8596
options: {
86-
minimize: true,
87-
sourceMap: true
88-
}
97+
modules: false,
98+
importLoaders: 2,
99+
},
89100
},
101+
'postcss-loader',
102+
'sass-loader',
90103
{
91-
loader: 'sass-loader'
92-
}
93-
]
104+
loader: 'sass-resources-loader',
105+
options: { resources: sassResources },
106+
},
107+
],
94108
},
95109
{
96110
test: require.resolve('react'),
@@ -110,7 +124,7 @@ module.exports = merge(config, {
110124
jQuery: 'jquery',
111125
}
112126
}
113-
}
127+
},
114128
],
115129
},
116130

0 commit comments

Comments
 (0)