Skip to content

Commit 2bf8b79

Browse files
committed
dll support
1 parent 69745fc commit 2bf8b79

File tree

9 files changed

+106
-34
lines changed

9 files changed

+106
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/node_modules
22
**/*.log
33
/packages/more/.cache
4+
/packages/more/dll

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Modus React Application Scaffolding
2+
3+
Monorepo containing setup for a best-practice React application scaffolding
4+
5+
### DLLs
6+
7+
DLLs precompile libraries to save time for development builds and re-builds.
8+
9+
React and React Router are included in DLLs by default. You can add other libraries by including them in package.json as an array in the `dll` property.
10+
11+
```json
12+
"dll": [
13+
"foo"
14+
]
15+
```

packages/more/.env.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
SOURCE_DIR=src
22
BUILD_DIR=build
3+
SOURCEMAPS=false

packages/more/bin/more.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
// ignoring them. In the future, promise rejections that are not handled will
55
// terminate the Node.js process with a non-zero exit code.
66
process.on('unhandledRejection', err => {
7-
throw err
8-
})
7+
throw err;
8+
});
99

10-
const spawn = require('cross-spawn')
11-
const args = process.argv.slice(2)
10+
const spawn = require('cross-spawn');
11+
const args = process.argv.slice(2);
1212

13-
const commands = ['build', 'start', 'pre-commit']
13+
const commands = ['build', 'start', 'pre-commit', 'dll'];
1414

15-
const scriptIndex = args.findIndex(x => commands.includes(x))
15+
const scriptIndex = args.findIndex(x => commands.includes(x));
1616

17-
const script = scriptIndex === -1 ? args[0] : args[scriptIndex]
18-
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : []
17+
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
18+
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
1919

2020
switch (script) {
2121
case 'pre-commit':
22+
case 'dll':
2223
case 'build':
2324
case 'start': {
2425
const result = spawn.sync(
@@ -27,27 +28,27 @@ switch (script) {
2728
.concat(require.resolve('../scripts/' + script))
2829
.concat(args.slice(scriptIndex + 1)),
2930
{ stdio: 'inherit' }
30-
)
31+
);
3132
if (result.signal) {
3233
if (result.signal === 'SIGKILL') {
3334
console.log(
3435
'The build failed because the process exited too early. ' +
3536
'This probably means the system ran out of memory or someone called ' +
3637
'`kill -9` on the process.'
37-
)
38+
);
3839
} else if (result.signal === 'SIGTERM') {
3940
console.log(
4041
'The build failed because the process exited too early. ' +
4142
'Someone might have called `kill` or `killall`, or the system could ' +
4243
'be shutting down.'
43-
)
44+
);
4445
}
45-
process.exit(1)
46+
process.exit(1);
4647
}
47-
process.exit(result.status)
48-
break
48+
process.exit(result.status);
49+
break;
4950
}
5051
default:
51-
console.log('Unknown script "' + script + '".')
52-
break
52+
console.log('Unknown script "' + script + '".');
53+
break;
5354
}

packages/more/scripts/dll.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const path = require('path');
2+
const webpack = require('webpack');
3+
const webpackConfig = path.resolve(__dirname, '../webpack.dll.config');
4+
5+
const defaultWebpackConfig = require(webpackConfig);
6+
const compiler = webpack(defaultWebpackConfig);
7+
compiler.run();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
require('lint-staged')
1+
require('lint-staged');

packages/more/webpack.config.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ const path = require('path');
33
const webpack = require('webpack');
44
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
55
const HtmlWebpackPlugin = require('html-webpack-plugin');
6+
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
67

78
const projectRoot = process.env.INIT_CWD;
89

910
const sourceDir = path.join(projectRoot, process.env.SOURCE_DIR);
1011
const buildDir = path.join(projectRoot, process.env.BUILD_DIR);
11-
const shouldUseSourceMap = false;
12+
const dllDir = path.join(__dirname, 'dll');
13+
const dllManifest = require(path.join(dllDir, 'libs-manifest.json'));
14+
15+
const shouldUseSourceMap =
16+
process.env.SOURCEMAPS.toLowerCase() === 'false' || false;
1217

1318
const { NODE_ENV = 'production' } = process.env;
1419

@@ -67,13 +72,8 @@ const getStyleLoaders = (cssOptions, preProcessor) => {
6772
options: cssOptions,
6873
},
6974
{
70-
// Options for PostCSS as we reference these options twice
71-
// Adds vendor prefixing based on your specified browser support in
72-
// package.json
7375
loader: require.resolve('postcss-loader'),
7476
options: {
75-
// Necessary for external CSS imports to work
76-
// https://github.com/facebook/create-react-app/issues/2677
7777
ident: 'postcss',
7878
plugins: () => [
7979
require('postcss-flexbugs-fixes'),
@@ -134,10 +134,11 @@ module.exports = {
134134
}),
135135

136136
// import dll manifest
137-
// new webpack.DllReferencePlugin({
138-
// context: path.resolve(__dirname, '.'),
139-
// manifest: dllManifest,
140-
// }),
137+
isDev &&
138+
new webpack.DllReferencePlugin({
139+
context: path.resolve(__dirname), //path.resolve(projectRoot), //path.resolve(__dirname, '.'),
140+
manifest: dllManifest,
141+
}),
141142

142143
new HtmlWebpackPlugin(
143144
Object.assign(
@@ -165,12 +166,13 @@ module.exports = {
165166
),
166167

167168
// make DLL assets available for the app to download
168-
// new AddAssetHtmlPlugin([
169-
// {
170-
// filepath: require.resolve('./dll/libs.dll.js'),
171-
// includeSourcemap: false,
172-
// },
173-
// ]),
169+
isDev &&
170+
new AddAssetHtmlPlugin([
171+
{
172+
filepath: path.join(dllDir, 'libs.dll.js'),
173+
includeSourcemap: false,
174+
},
175+
]),
174176

175177
isDev && new webpack.HotModuleReplacementPlugin(),
176178
].filter(Boolean),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const webpack = require('webpack');
2+
const path = require('path');
3+
4+
const outputPath = path.join(__dirname, 'dll');
5+
const pkgJson = path.resolve(process.env.INIT_CWD, 'package.json');
6+
7+
const config = require(pkgJson);
8+
const customLibs = Array.isArray(config && config.dll) ? config.dll : [];
9+
10+
module.exports = {
11+
mode: 'development',
12+
entry: {
13+
libs: [
14+
'react',
15+
'react-dom',
16+
'react-router',
17+
'react-router-dom',
18+
...customLibs,
19+
],
20+
},
21+
22+
output: {
23+
filename: '[name].dll.js',
24+
path: outputPath,
25+
26+
// The name of the global variable which the library's
27+
// require() function will be assigned to
28+
library: '[name]_dll',
29+
},
30+
31+
plugins: [
32+
new webpack.DllPlugin({
33+
// The path to the manifest file which maps between
34+
// modules included in a bundle and the internal IDs
35+
// within that bundle
36+
path: path.join(outputPath, '[name]-manifest.json'),
37+
// The name of the global variable which the library's
38+
// require function has been assigned to. This must match the
39+
// output.library option above
40+
name: '[name]_dll',
41+
}),
42+
],
43+
};

packages/sample/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
},
1313
"scripts": {
1414
"start": "more start",
15-
"build": "more build"
15+
"build": "more build",
16+
"dll": "more dll",
17+
"postinstall": "more dll"
1618
}
1719
}

0 commit comments

Comments
 (0)