Skip to content

Commit d3b598b

Browse files
frenzzyokendoken
authored andcommitted
Add ability to use images from node modules (#1334)
1 parent fddb41b commit d3b598b

File tree

3 files changed

+104
-62
lines changed

3 files changed

+104
-62
lines changed

tools/lib/overrideRules.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* React Starter Kit (https://www.reactstarterkit.com/)
3+
*
4+
* Copyright © 2014-present Kriasoft, LLC. All rights reserved.
5+
*
6+
* This source code is licensed under the MIT license found in the
7+
* LICENSE.txt file in the root directory of this source tree.
8+
*/
9+
10+
function overrideRules(rules, patch) {
11+
return rules.map((ruleToPatch) => {
12+
let rule = patch(ruleToPatch);
13+
if (rule.rules) {
14+
rule = { ...rule, rules: overrideRules(rule.rules, patch) };
15+
}
16+
if (rule.oneOf) {
17+
rule = { ...rule, oneOf: overrideRules(rule.oneOf, patch) };
18+
}
19+
return rule;
20+
});
21+
}
22+
23+
export default overrideRules;

tools/webpack.config.js

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ import webpack from 'webpack';
1212
import AssetsPlugin from 'assets-webpack-plugin';
1313
import nodeExternals from 'webpack-node-externals';
1414
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
15+
import overrideRules from './lib/overrideRules';
1516
import pkg from '../package.json';
1617

1718
const isDebug = !process.argv.includes('--release');
1819
const isVerbose = process.argv.includes('--verbose');
1920
const isAnalyze = process.argv.includes('--analyze') || process.argv.includes('--analyse');
2021

21-
const stylesRegExp = /\.(css|less|scss|sss)$/;
22+
const reScript = /\.jsx?$/;
23+
const reStyle = /\.(css|less|scss|sss)$/;
24+
const reImage = /\.(bmp|gif|jpe?g|png|svg)$/;
2225
const staticAssetName = isDebug ? '[path][name].[ext]?[hash:8]' : '[hash:8].[ext]';
2326

2427
//
@@ -49,8 +52,9 @@ const config = {
4952
strictExportPresence: true,
5053

5154
rules: [
55+
// Rules for JS / JSX
5256
{
53-
test: /\.jsx?$/,
57+
test: reScript,
5458
loader: 'babel-loader',
5559
include: [
5660
path.resolve(__dirname, '../src'),
@@ -94,18 +98,32 @@ const config = {
9498
},
9599
},
96100

97-
// Handle internal/project styles (from src folder)
101+
// Rules for Style Sheets
98102
{
99-
// Internal Styles
100-
test: stylesRegExp,
101-
include: [
102-
path.resolve(__dirname, '../src'),
103-
],
104-
use: [
103+
test: reStyle,
104+
rules: [
105+
// Convert CSS into JS module
105106
{
106-
loader: 'isomorphic-style-loader',
107+
issuer: { not: [reStyle] },
108+
use: 'isomorphic-style-loader',
107109
},
110+
111+
// Process external/third-party styles
108112
{
113+
exclude: path.resolve(__dirname, '../src'),
114+
loader: 'css-loader',
115+
options: {
116+
sourceMap: isDebug,
117+
minimize: !isDebug,
118+
discardComments: { removeAll: true },
119+
},
120+
},
121+
122+
// Process internal/project styles (from src folder)
123+
{
124+
include: [
125+
path.resolve(__dirname, '../src'),
126+
],
109127
loader: 'css-loader',
110128
options: {
111129
// CSS Loader https://github.com/webpack/css-loader
@@ -132,7 +150,7 @@ const config = {
132150
],
133151
},
134152
{
135-
test: stylesRegExp,
153+
test: reStyle,
136154
exclude: [/theme.scss$/],
137155
use: [
138156
'isomorphic-style-loader',
@@ -142,43 +160,50 @@ const config = {
142160
],
143161
},
144162
{
145-
test: /\.(bmp|gif|jpe?g|png)$/,
146-
issuer: stylesRegExp,
147-
loader: 'url-loader',
148-
options: {
149-
name: staticAssetName,
150-
limit: 4096, // 4kb
151-
},
152-
},
163+
test: reImage,
164+
oneOf: [
165+
// Inline lightweight images into CSS
166+
{
167+
issuer: reStyle,
168+
oneOf: [
169+
// Inline lightweight SVGs as UTF-8 encoded DataUrl string
170+
{
171+
test: /\.svg$/,
172+
loader: 'svg-url-loader',
173+
options: {
174+
name: staticAssetName,
175+
limit: 4096, // 4kb
176+
},
177+
},
153178

154-
// Inline small SVGs into CSS as UTF-8 encoded DataUrl string
155-
{
156-
test: /\.svg$/,
157-
issuer: stylesRegExp,
158-
loader: 'svg-url-loader',
159-
options: {
160-
name: staticAssetName,
161-
limit: 4096, // 4kb
162-
},
163-
},
179+
// Inline lightweight images as Base64 encoded DataUrl string
180+
{
181+
loader: 'url-loader',
182+
options: {
183+
name: staticAssetName,
184+
limit: 4096, // 4kb
185+
},
186+
},
187+
],
188+
},
164189

165-
// Return public URL to large images otherwise
166-
{
167-
test: /\.(bmp|gif|jpe?g|png|svg)$/,
168-
issuer: { not: [stylesRegExp] },
169-
loader: 'file-loader',
170-
options: {
171-
name: staticAssetName,
172-
},
190+
// Or return public URL to image resource
191+
{
192+
loader: 'file-loader',
193+
options: {
194+
name: staticAssetName,
195+
},
196+
},
197+
],
173198
},
174199

175-
// Convert plain text into module
200+
// Convert plain text into JS module
176201
{
177202
test: /\.txt$/,
178203
loader: 'raw-loader',
179204
},
180205

181-
// Convert markdown into html
206+
// Convert Markdown into HTML
182207
{
183208
test: /\.md$/,
184209
loader: path.resolve(__dirname, './lib/markdown-loader.js'),
@@ -188,10 +213,10 @@ const config = {
188213
// DO NOT FORGET to update `exclude` list when you adding a new loader
189214
{
190215
exclude: [
191-
/\.jsx?$/,
216+
reScript,
217+
reStyle,
218+
reImage,
192219
/\.json$/,
193-
stylesRegExp,
194-
/\.(bmp|gif|jpe?g|png|svg)$/,
195220
/\.txt$/,
196221
/\.md$/,
197222
],
@@ -346,8 +371,8 @@ const serverConfig = {
346371
module: {
347372
...config.module,
348373

349-
// Override babel-preset-env configuration for Node.js
350-
rules: config.module.rules.map((rule) => {
374+
rules: overrideRules(config.module.rules, (rule) => {
375+
// Override babel-preset-env configuration for Node.js
351376
if (rule.loader === 'babel-loader') {
352377
return {
353378
...rule,
@@ -365,6 +390,7 @@ const serverConfig = {
365390
};
366391
}
367392

393+
// Override paths to static assets
368394
if (rule.loader === 'file-loader' || rule.loader === 'url-loader' || rule.loader === 'svg-url-loader') {
369395
return {
370396
...rule,
@@ -384,7 +410,8 @@ const serverConfig = {
384410
'./assets.json',
385411
nodeExternals({
386412
whitelist: [
387-
stylesRegExp,
413+
reStyle,
414+
reImage,
388415
],
389416
}),
390417
],

yarn.lock

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,13 +2055,13 @@ [email protected]:
20552055
dependencies:
20562056
ms "0.7.3"
20572057

2058-
[email protected], debug@^2.1.1, debug@^2.2.0, debug@^2.6.0, debug@^2.6.3, debug@^2.6.6:
2058+
[email protected], debug@^2.1.1:
20592059
version "2.6.7"
20602060
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.7.tgz#92bad1f6d05bbb6bba22cca88bcd0ec894c2861e"
20612061
dependencies:
20622062
ms "2.0.0"
20632063

2064-
debug@^2.6.8:
2064+
debug@^2.2.0, debug@^2.6.0, debug@^2.6.3, debug@^2.6.6, debug@^2.6.8:
20652065
version "2.6.8"
20662066
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
20672067
dependencies:
@@ -4449,7 +4449,7 @@ lodash.uniq@^4.5.0:
44494449
version "4.5.0"
44504450
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
44514451

4452-
[email protected], lodash@^4.0.0, lodash@^4.2.0:
4452+
[email protected], lodash@^4.2.0:
44534453
version "4.12.0"
44544454
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.12.0.tgz#2bd6dc46a040f59e686c972ed21d93dc59053258"
44554455

@@ -4461,7 +4461,7 @@ lodash@^3.10.1:
44614461
version "3.10.1"
44624462
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
44634463

4464-
lodash@^4.1.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.6.1:
4464+
lodash@^4.0.0, lodash@^4.1.0, lodash@^4.14.0, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.6.1:
44654465
version "4.17.4"
44664466
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
44674467

@@ -4685,11 +4685,11 @@ [email protected], minimist@~0.0.1:
46854685
version "0.0.8"
46864686
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
46874687

4688-
[email protected], minimist@^1.1.3:
4688+
46894689
version "1.1.3"
46904690
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.1.3.tgz#3bedfd91a92d39016fcfaa1c681e8faa1a1efda8"
46914691

4692-
minimist@^1.1.0, minimist@^1.2.0:
4692+
minimist@^1.1.0, minimist@^1.1.3, minimist@^1.2.0:
46934693
version "1.2.0"
46944694
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
46954695

@@ -5818,15 +5818,7 @@ postcss@^5.0.0, postcss@^5.0.10, postcss@^5.0.11, postcss@^5.0.12, postcss@^5.0.
58185818
source-map "^0.5.6"
58195819
supports-color "^3.2.3"
58205820

5821-
postcss@^6.0.0, postcss@^6.0.1, postcss@^6.0.2:
5822-
version "6.0.2"
5823-
resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.2.tgz#5c4fea589f0ac3b00caa75b1cbc3a284195b7e5d"
5824-
dependencies:
5825-
chalk "^1.1.3"
5826-
source-map "^0.5.6"
5827-
supports-color "^3.2.3"
5828-
5829-
postcss@^6.0.3:
5821+
postcss@^6.0.0, postcss@^6.0.1, postcss@^6.0.2, postcss@^6.0.3:
58305822
version "6.0.3"
58315823
resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.3.tgz#b7f565b3d956fbb8565ca7c1e239d0506e427d8b"
58325824
dependencies:
@@ -7062,7 +7054,7 @@ sugarss@^0.2.0:
70627054
dependencies:
70637055
postcss "^5.2.4"
70647056

7065-
7057+
[email protected], supports-color@^3.1.0:
70667058
version "3.1.2"
70677059
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.1.2.tgz#72a262894d9d408b956ca05ff37b2ed8a6e2a2d5"
70687060
dependencies:
@@ -7072,7 +7064,7 @@ supports-color@^2.0.0:
70727064
version "2.0.0"
70737065
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
70747066

7075-
supports-color@^3.1.0, supports-color@^3.1.2, supports-color@^3.2.3:
7067+
supports-color@^3.1.2, supports-color@^3.2.3:
70767068
version "3.2.3"
70777069
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.2.3.tgz#65ac0504b3954171d8a64946b2ae3cbb8a5f54f6"
70787070
dependencies:

0 commit comments

Comments
 (0)