Skip to content

Commit f5789f4

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
fix(@angular-devkit/build-angular): ensure live-reload shim workaround isolation
This change reduces the workaround to a single file location as well as ensuring that only the shims of interest from the two necessary live reload files are affected. This makes sure that build and serve behavior is the same in this regard.
1 parent 276be8a commit f5789f4

File tree

3 files changed

+56
-19
lines changed
  • packages/angular_devkit/build_angular/src
  • tests/legacy-cli/e2e/tests/commands/serve

3 files changed

+56
-19
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

-8
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
523523
test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
524524
parser: { system: true },
525525
},
526-
{
527-
test: /[\/\\]hot[\/\\]emitter\.js$/,
528-
parser: { node: { events: true } },
529-
},
530-
{
531-
test: /[\/\\]webpack-dev-server[\/\\]client[\/\\]utils[\/\\]createSocketUrl\.js$/,
532-
parser: { node: { querystring: true } },
533-
},
534526
{
535527
test: /\.js$/,
536528
// Factory files are processed by BO in the rules added in typescript.ts.

packages/angular_devkit/build_angular/src/dev-server/index.ts

+31-11
Original file line numberDiff line numberDiff line change
@@ -551,19 +551,39 @@ function _addLiveReload(
551551
webpackConfig.plugins = [];
552552
}
553553

554-
// Enable the internal node plugins but no individual shims
555-
// This is needed to allow module specific rules to include node shims
554+
// Workaround node shim hoisting issues with live reload client
556555
// Only needed in dev server mode to support live reload capabilities in all package managers
557-
if (webpackConfig.node === false) {
558-
webpackConfig.node = {
559-
global: false,
560-
process: false,
561-
__filename: false,
562-
__dirname: false,
563-
Buffer: false,
564-
setImmediate: false,
565-
};
556+
const webpackPath = path.dirname(require.resolve('webpack/package.json'));
557+
const nodeLibsBrowserPath = require.resolve('node-libs-browser', { paths: [webpackPath] });
558+
const nodeLibsBrowser = require(nodeLibsBrowserPath);
559+
webpackConfig.plugins.push(
560+
new webpack.NormalModuleReplacementPlugin(
561+
/^events|url|querystring$/,
562+
(resource: { issuer?: string; request: string }) => {
563+
if (!resource.issuer) {
564+
return;
565+
}
566+
if (/[\/\\]hot[\/\\]emitter\.js$/.test(resource.issuer)) {
567+
if (resource.request === 'events') {
568+
resource.request = nodeLibsBrowser.events;
569+
}
570+
} else if (
571+
/[\/\\]webpack-dev-server[\/\\]client[\/\\]utils[\/\\]createSocketUrl\.js$/.test(
572+
resource.issuer,
573+
)
574+
) {
575+
switch (resource.request) {
576+
case 'url':
577+
resource.request = nodeLibsBrowser.url;
578+
break;
579+
case 'querystring':
580+
resource.request = nodeLibsBrowser.querystring;
581+
break;
582+
}
566583
}
584+
},
585+
),
586+
);
567587

568588
// This allows for live reload of page when changes are made to repo.
569589
// https://webpack.js.org/configuration/dev-server/#devserver-inline
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { prependToFile, writeFile } from '../../../utils/fs';
2+
import { execAndWaitForOutputToMatch, killAllProcesses } from '../../../utils/process';
3+
4+
export default async function() {
5+
// Simulate a JS library using a Node.js specific module
6+
await writeFile('src/node-usage.js', `const path = require('path');\n`);
7+
await prependToFile('src/main.ts', `import './node-usage';\n`);
8+
9+
try {
10+
// Make sure serve is consistent with build
11+
await execAndWaitForOutputToMatch(
12+
'ng',
13+
['build'],
14+
/Module not found: Error: Can't resolve 'path'/,
15+
);
16+
// The Node.js specific module should not be found
17+
await execAndWaitForOutputToMatch(
18+
'ng',
19+
['serve', '--port=0'],
20+
/Module not found: Error: Can't resolve 'path'/,
21+
);
22+
} finally {
23+
killAllProcesses();
24+
}
25+
}

0 commit comments

Comments
 (0)