Skip to content

Commit

Permalink
Exclude node builtins from build
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeSheard committed Apr 28, 2022
1 parent 591acf5 commit 327118a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-ants-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"modular-scripts": major
---

Exclude node builtins from build
15 changes: 5 additions & 10 deletions packages/modular-scripts/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const resolve = require('resolve');
const builtinModules = require('builtin-modules');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
Expand Down Expand Up @@ -632,16 +633,10 @@ module.exports = function (webpackEnv) {
].filter(Boolean),
// Some libraries import Node modules but don't use them in the browser.
// Tell webpack to provide empty mocks for them so importing them works.
node: {
module: 'empty',
dgram: 'empty',
dns: 'mock',
fs: 'empty',
http2: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty',
},
node: builtinModules.reduce((acc, next) => {
acc[next] = false;
return acc;
}, {}),
// Turn off performance processing because we utilize
// our own hints via the FileSizeReporter
performance: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import isCi from 'is-ci';
import * as path from 'path';
import * as esbuild from 'esbuild';
import builtinModules from 'builtin-modules';
import type { Paths } from '../../utils/createPaths';
import getClientEnvironment from './getClientEnvironment';
import createEsbuildBrowserslistTarget from '../../utils/createEsbuildBrowserslistTarget';
Expand Down Expand Up @@ -34,6 +35,11 @@ export default function createEsbuildConfig(

logger.debug(`Using target: ${target.join(', ')}`);

// merge and de-duplicate node builtins with external in the parameters
const external = [
...new Set((partialConfig.external ?? []).concat(builtinModules)),
];

return {
entryPoints: [paths.appIndexJs],
plugins,
Expand Down Expand Up @@ -72,5 +78,7 @@ export default function createEsbuildConfig(
publicPath: paths.publicUrlOrPath,
nodePaths: (process.env.NODE_PATH || '').split(path.delimiter),
...partialConfig,
// this was merged previously; do not override.
external,
};
}

0 comments on commit 327118a

Please sign in to comment.