Skip to content

Commit 8fc4f4f

Browse files
committed
use webpack-node-externals for library build
1 parent 9ba15c5 commit 8fc4f4f

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

packages/react-scripts/config/webpack.config.library.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const webpack = require('webpack');
1313
const ExtractTextPlugin = require('extract-text-webpack-plugin');
1414
const eslintFormatter = require('react-dev-utils/eslintFormatter');
1515
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
16+
const nodeExternals = require('webpack-node-externals');
1617
const paths = require('./paths');
1718
const getClientEnvironment = require('./env');
1819

@@ -82,7 +83,8 @@ module.exports = {
8283
// You can exclude the *.map files from the build during deployment.
8384
devtool: shouldUseSourceMap ? 'source-map' : false,
8485
// In production, we only want to load the polyfills and the app code.
85-
entry: [require.resolve('./polyfills'), paths.appIndexJs],
86+
entry: [paths.appIndexJs],
87+
target: 'node',
8688
output: {
8789
// The build folder.
8890
path: paths.appBuild,
@@ -299,6 +301,7 @@ module.exports = {
299301
// You can remove this if you don't use Moment.js:
300302
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
301303
],
304+
externals: [nodeExternals()],
302305
// Some libraries import Node modules but don't use them in the browser.
303306
// Tell Webpack to provide empty mocks for them so importing them works.
304307
node: {

packages/react-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"webpack": "3.8.1",
6262
"webpack-dev-server": "2.9.4",
6363
"webpack-manifest-plugin": "1.3.2",
64+
"webpack-node-externals": "^1.6.0",
6465
"whatwg-fetch": "2.0.3"
6566
},
6667
"devDependencies": {

packages/react-scripts/scripts/init.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,18 @@ module.exports = function(
9090
);
9191

9292
let command;
93-
let depArgs;
93+
let peerDepArgs;
9494

9595
if (useYarn) {
9696
command = 'yarnpkg';
97-
depArgs = ['add'];
97+
peerDepArgs = ['add', '--save-dev'];
9898
} else {
9999
command = 'npm';
100-
depArgs = ['install', '--save', verbose && '--verbose'].filter(e => e);
100+
peerDepArgs = ['install', '--save-dev', verbose && '--verbose'].filter(
101+
e => e
102+
);
101103
}
102-
depArgs.push('react', 'react-dom');
104+
peerDepArgs.push('react', 'react-dom');
103105

104106
// Install additional template dependencies, if present
105107
const templateDependenciesPath = path.join(
@@ -108,7 +110,7 @@ module.exports = function(
108110
);
109111
if (fs.existsSync(templateDependenciesPath)) {
110112
const templateDependencies = require(templateDependenciesPath).dependencies;
111-
depArgs = depArgs.concat(
113+
peerDepArgs = peerDepArgs.concat(
112114
Object.keys(templateDependencies).map(key => {
113115
return `${key}@${templateDependencies[key]}`;
114116
})
@@ -123,9 +125,9 @@ module.exports = function(
123125
console.log(`Installing react and react-dom using ${command}...`);
124126
console.log();
125127

126-
const proc = spawn.sync(command, depArgs, { stdio: 'inherit' });
128+
const proc = spawn.sync(command, peerDepArgs, { stdio: 'inherit' });
127129
if (proc.status !== 0) {
128-
console.error(`\`${command} ${depArgs.join(' ')}\` failed`);
130+
console.error(`\`${command} ${peerDepArgs.join(' ')}\` failed`);
129131
return;
130132
}
131133
}

0 commit comments

Comments
 (0)