From 309b4838c35524a1e902e6bef2015cf868464e95 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 6 Mar 2019 07:22:39 +0100 Subject: [PATCH] fix(@angular-devkit/build-angular): don't bundle linked modules when `bundleDependencies` in `none` in server builder Fixes #13817 --- .../models/webpack-configs/server.ts | 27 ++------ .../misc/universal-bundle-dependencies.ts | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 tests/legacy-cli/e2e/tests/misc/universal-bundle-dependencies.ts diff --git a/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/server.ts b/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/server.ts index b1e323c69aa6..eb592750d2fe 100644 --- a/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/server.ts +++ b/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/server.ts @@ -9,30 +9,21 @@ import { Configuration } from 'webpack'; import { WebpackConfigOptions } from '../build-options'; import { getSourceMapDevTool } from './utils'; - /** * Returns a partial specific to creating a bundle for node * @param wco Options which are include the build options and app config */ export function getServerConfig(wco: WebpackConfigOptions) { - const extraPlugins = []; if (wco.buildOptions.sourceMap) { const { scripts, styles, hidden } = wco.buildOptions.sourceMap; - extraPlugins.push(getSourceMapDevTool( - scripts || false, - styles || false, - hidden || false, - )); + extraPlugins.push(getSourceMapDevTool(scripts || false, styles || false, hidden || false)); } const config: Configuration = { resolve: { - mainFields: [ - ...(wco.supportES2015 ? ['es2015'] : []), - 'main', 'module', - ], + mainFields: [...(wco.supportES2015 ? ['es2015'] : []), 'main', 'module'], }, target: 'node', output: { @@ -45,23 +36,15 @@ export function getServerConfig(wco: WebpackConfigOptions) { if (wco.buildOptions.bundleDependencies == 'none') { config.externals = [ /^@angular/, - // tslint:disable-next-line:no-any - (_: any, request: any, callback: (error?: any, result?: any) => void) => { + (context: string, request: string, callback: (error?: null, result?: string) => void) => { // Absolute & Relative paths are not externals if (request.match(/^\.{0,2}\//)) { return callback(); } try { - // Attempt to resolve the module via Node - const e = require.resolve(request); - if (/node_modules/.test(e)) { - // It's a node_module - callback(null, request); - } else { - // It's a system thing (.ie util, fs...) - callback(); - } + require.resolve(request); + callback(null, request); } catch { // Node couldn't find it, so it must be user-aliased callback(); diff --git a/tests/legacy-cli/e2e/tests/misc/universal-bundle-dependencies.ts b/tests/legacy-cli/e2e/tests/misc/universal-bundle-dependencies.ts new file mode 100644 index 000000000000..5c34a295cc27 --- /dev/null +++ b/tests/legacy-cli/e2e/tests/misc/universal-bundle-dependencies.ts @@ -0,0 +1,66 @@ +import * as path from 'path'; +import { + createDir, + expectFileToMatch, + rimraf, + symlinkFile, + writeMultipleFiles, +} from '../../utils/fs'; +import { ng } from '../../utils/process'; +import { updateJsonFile } from '../../utils/project'; + +export default async function() { + await updateJsonFile('angular.json', workspaceJson => { + const appArchitect = workspaceJson.projects['test-project'].architect; + appArchitect['server'] = { + builder: '@angular-devkit/build-angular:server', + options: { + bundleDependencies: 'none', + outputPath: 'dist/test-project-server', + main: 'src/main.server.ts', + tsConfig: 'tsconfig.server.json', + }, + }; + }); + + await createDir('./dummy-lib'); + + await writeMultipleFiles({ + './tsconfig.server.json': ` + { + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../dist-server", + "baseUrl": "./", + "module": "commonjs", + "types": [] + }, + "include": [ + "src/main.server.ts" + ] + } + `, + './src/main.server.ts': ` + import { dummyVersion } from 'dummy-lib'; + console.log(dummyVersion); + `, + // create a dummy library + './dummy-lib/package.json': `{ + "name": "dummy-lib", + "version": "0.0.0", + "typings": "./main.d.ts", + "main": "./main.js" + }`, + './dummy-lib/main.js': 'export const dummyVersion = 1', + './dummy-lib/main.d.ts': 'export declare const dummyVersion = 1', + }); + + await symlinkFile(path.resolve('./dummy-lib'), path.resolve('./node_modules/dummy-lib'), 'dir'); + + await ng('run', 'test-project:server'); + // when preserve symlinks is true, it should not included node_modules in the bundle + await expectFileToMatch('dist/test-project-server/main.js', 'require("dummy-lib")'); + + // cleanup the package + await rimraf('node_modules/dummy-lib'); +}