Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 0922de7

Browse files
authored
fix(express-engine): add webpack option to schematics (#1081)
* Enable webpack option by default * Bump karma to version 3 to fix audit issues Fixes #1080
1 parent c16860c commit 0922de7

7 files changed

Lines changed: 1063 additions & 733 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Work around for https://github.com/angular/angular-cli/issues/7200
2+
3+
const path = require('path');
4+
const webpack = require('webpack');
5+
6+
module.exports = {
7+
mode: 'none',
8+
entry: {
9+
// This is our Express server for Dynamic universal
10+
server: './<%= stripTsExtension(serverFileName) %>.ts'
11+
},
12+
target: 'node',
13+
resolve: { extensions: ['.ts', '.js'] },
14+
optimization: {
15+
minimize: false
16+
},
17+
output: {
18+
// Puts the output at the root of the dist folder
19+
path: path.join(__dirname, 'dist'),
20+
filename: '[name].js'
21+
},
22+
module: {
23+
rules: [
24+
{ test: /\.ts$/, loader: 'ts-loader' },
25+
{
26+
// Mark files inside `@angular/core` as using SystemJS style dynamic imports.
27+
// Removing this will cause deprecation warnings to appear.
28+
test: /(\\|\/)@angular(\\|\/)core(\\|\/).+\.js$/,
29+
parser: { system: true },
30+
},
31+
]
32+
},
33+
plugins: [
34+
new webpack.ContextReplacementPlugin(
35+
// fixes WARNING Critical dependency: the request of a dependency is an expression
36+
/(.+)?angular(\\|\/)core(.+)?/,
37+
path.join(__dirname, 'src'), // location of your src
38+
{} // a map of your routes
39+
),
40+
new webpack.ContextReplacementPlugin(
41+
// fixes WARNING Critical dependency: the request of a dependency is an expression
42+
/(.+)?express(\\|\/)(.+)?/,
43+
path.join(__dirname, 'src'),
44+
{}
45+
)
46+
]
47+
};

modules/express-engine/schematics/install/index.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,38 @@ describe('Universal Schematic', () => {
4444
expect(contents).toMatch(/\"express\": \"/);
4545
});
4646

47+
it('should add dependency: ts-loader', () => {
48+
const tree = schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
49+
const filePath = '/package.json';
50+
const contents = tree.readContent(filePath);
51+
expect(contents).toMatch(/\"ts-loader\": \"/);
52+
});
53+
54+
it('should add dependency: webpack-cli', () => {
55+
const tree = schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
56+
const filePath = '/package.json';
57+
const contents = tree.readContent(filePath);
58+
expect(contents).toMatch(/\"webpack-cli\": \"/);
59+
});
60+
61+
it('should not add dependency: ts-loader when webpack is false', () => {
62+
const noWebpack = Object.assign({}, defaultOptions);
63+
noWebpack.webpack = false;
64+
const tree = schematicRunner.runSchematic('ng-add', noWebpack, appTree);
65+
const filePath = '/package.json';
66+
const contents = tree.readContent(filePath);
67+
expect(contents).not.toContain('ts-loader');
68+
});
69+
70+
it('should not add dependency: webpack-cli when webpack is false', () => {
71+
const noWebpack = Object.assign({}, defaultOptions);
72+
noWebpack.webpack = false;
73+
const tree = schematicRunner.runSchematic('ng-add', noWebpack, appTree);
74+
const filePath = '/package.json';
75+
const contents = tree.readContent(filePath);
76+
expect(contents).not.toContain('webpack-cli');
77+
});
78+
4779
it('should install npm dependencies', () => {
4880
schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
4981
expect(schematicRunner.tasks.length).toBe(2);

modules/express-engine/schematics/install/index.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@
77
*/
88
import {experimental, JsonObject, strings} from '@angular-devkit/core';
99
import {
10-
Rule,
11-
SchematicContext,
12-
SchematicsException,
13-
Tree,
1410
apply,
1511
chain,
12+
externalSchematic,
13+
filter,
1614
mergeWith,
15+
noop,
16+
Rule,
17+
SchematicContext,
18+
SchematicsException,
1719
template,
20+
Tree,
1821
url,
19-
noop,
20-
filter,
21-
externalSchematic,
2222
} from '@angular-devkit/schematics';
2323
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
2424
import {getWorkspace, getWorkspacePath} from '@schematics/angular/utility/config';
2525
import {Schema as UniversalOptions} from './schema';
26+
import {
27+
addPackageJsonDependency,
28+
NodeDependencyType,
29+
} from '@schematics/angular/utility/dependencies';
2630

2731

2832
function getClientProject(
@@ -39,7 +43,36 @@ function getClientProject(
3943

4044
function addDependenciesAndScripts(options: UniversalOptions): Rule {
4145
return (host: Tree) => {
46+
addPackageJsonDependency(host, {
47+
type: NodeDependencyType.Default,
48+
name: '@nguniversal/express-engine',
49+
version: '0.0.0-PLACEHOLDER',
50+
});
51+
addPackageJsonDependency(host, {
52+
type: NodeDependencyType.Default,
53+
name: '@nguniversal/module-map-ngfactory-loader',
54+
version: '0.0.0-PLACEHOLDER',
55+
});
56+
addPackageJsonDependency(host, {
57+
type: NodeDependencyType.Default,
58+
name: 'express',
59+
version: 'EXPRESS_VERSION',
60+
});
61+
62+
if (options.webpack) {
63+
addPackageJsonDependency(host, {
64+
type: NodeDependencyType.Dev,
65+
name: 'ts-loader',
66+
version: '^5.2.0',
67+
});
68+
addPackageJsonDependency(host, {
69+
type: NodeDependencyType.Dev,
70+
name: 'webpack-cli',
71+
version: '^3.1.0',
72+
});
73+
}
4274

75+
const serverFileName = options.serverFileName.replace('.ts', '');
4376
const pkgPath = '/package.json';
4477
const buffer = host.read(pkgPath);
4578
if (buffer === null) {
@@ -48,16 +81,13 @@ function addDependenciesAndScripts(options: UniversalOptions): Rule {
4881

4982
const pkg = JSON.parse(buffer.toString());
5083

51-
pkg.dependencies['@nguniversal/express-engine'] = '0.0.0-PLACEHOLDER';
52-
pkg.dependencies['@nguniversal/module-map-ngfactory-loader'] = '0.0.0-PLACEHOLDER';
53-
pkg.dependencies['express'] = 'EXPRESS_VERSION';
54-
55-
pkg.scripts['serve:ssr'] = 'node dist/server';
84+
pkg.scripts['compile:server'] = options.webpack ?
85+
'webpack --config webpack.server.config.js --progress --colors' :
86+
`tsc -p ${serverFileName}.tsconfig.json`;
87+
pkg.scripts['serve:ssr'] = `node dist/${serverFileName}`;
5688
pkg.scripts['build:ssr'] = 'npm run build:client-and-server-bundles && npm run compile:server';
5789
pkg.scripts['build:client-and-server-bundles'] =
5890
`ng build --prod && ng run ${options.clientProject}:server:production`;
59-
pkg.scripts['compile:server'] =
60-
`tsc -p ${options.serverFileName.replace(/\.ts$/, '')}.tsconfig.json`;
6191

6292
host.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
6393

@@ -118,6 +148,8 @@ export default function (options: UniversalOptions): Rule {
118148

119149
const rootSource = apply(url('./files/root'), [
120150
options.skipServer ? filter(path => !path.startsWith('__serverFileName')) : noop(),
151+
options.webpack ?
152+
filter(path => !path.includes('tsconfig')) : filter(path => !path.startsWith('webpack')),
121153
template({
122154
...strings,
123155
...options as object,

modules/express-engine/schematics/install/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@
7777
"description": "Skip the Angular Universal schematic",
7878
"type": "boolean",
7979
"default": false
80+
},
81+
"webpack": {
82+
"description": "Whether to add webpack configuration files",
83+
"type": "boolean",
84+
"default": true
8085
}
8186
},
8287
"required": [

modules/express-engine/schematics/install/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ export interface Schema {
6363
* Skip the Angular Universal schematic
6464
*/
6565
skipUniversal?: boolean;
66+
/**
67+
* Whether to add webpack configuration files
68+
*/
69+
webpack?: boolean;
6670
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"glob": "^7.1.2",
100100
"hapi": "^17.5.1",
101101
"jasmine-core": "^2.8.0",
102-
"karma": "^2.0.0",
102+
"karma": "^3.0.0",
103103
"karma-chrome-launcher": "^2.2.0",
104104
"karma-jasmine": "^1.1.1",
105105
"karma-sourcemap-loader": "^0.3.7",

0 commit comments

Comments
 (0)