Skip to content

Commit 709fd48

Browse files
committed
refactor(@angular-devkit/build-angular): cleanup index processing
1 parent aae5e42 commit 709fd48

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
132132
baseHref: buildOptions.baseHref,
133133
entrypoints: generateEntryPoints(buildOptions),
134134
deployUrl: buildOptions.deployUrl,
135+
sri: buildOptions.subresourceIntegrity,
135136
}),
136137
]),
137138
node: false,

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/index-html-webpack-plugin.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
// tslint:disable
2-
// TODO: cleanup this file, it's copied as is from Angular CLI.
3-
41
/**
52
* @license
63
* Copyright Google Inc. All Rights Reserved.
74
*
85
* Use of this source code is governed by an MIT-style license that can be
96
* found in the LICENSE file at https://angular.io/license
107
*/
11-
8+
import { createHash } from 'crypto';
9+
import { Compiler, compilation } from 'webpack';
1210
import { RawSource } from 'webpack-sources';
1311

1412
const parse5 = require('parse5');
@@ -19,13 +17,15 @@ export interface IndexHtmlWebpackPluginOptions {
1917
baseHref?: string;
2018
entrypoints: string[];
2119
deployUrl?: string;
20+
sri: boolean;
2221
}
2322

24-
function readFile(filename: string, compilation: any): Promise<string> {
23+
function readFile(filename: string, compilation: compilation.Compilation): Promise<string> {
2524
return new Promise<string>((resolve, reject) => {
2625
compilation.inputFileSystem.readFile(filename, (err: Error, data: Buffer) => {
2726
if (err) {
2827
reject(err);
28+
2929
return;
3030
}
3131

@@ -53,23 +53,25 @@ export class IndexHtmlWebpackPlugin {
5353
input: 'index.html',
5454
output: 'index.html',
5555
entrypoints: ['polyfills', 'main'],
56-
...options
56+
sri: false,
57+
...options,
5758
};
5859
}
5960

60-
apply(compiler: any) {
61-
compiler.hooks.emit.tapPromise('index-html-webpack-plugin', async (compilation: any) => {
61+
apply(compiler: Compiler) {
62+
compiler.hooks.emit.tapPromise('index-html-webpack-plugin', async compilation => {
6263
// Get input html file
6364
const inputContent = await readFile(this._options.input, compilation);
64-
compilation.fileDependencies.add(this._options.input);
65+
(compilation as compilation.Compilation & { fileDependencies: Set<string> })
66+
.fileDependencies.add(this._options.input);
6567

6668

6769
// Get all files for selected entrypoints
68-
const unfilteredSortedFiles: string[] = [];
70+
let unfilteredSortedFiles: string[] = [];
6971
for (const entryName of this._options.entrypoints) {
7072
const entrypoint = compilation.entrypoints.get(entryName);
71-
if (entrypoint) {
72-
unfilteredSortedFiles.push(...entrypoint.getFiles());
73+
if (entrypoint && entrypoint.getFiles) {
74+
unfilteredSortedFiles = unfilteredSortedFiles.concat(entrypoint.getFiles() || []);
7375
}
7476
}
7577

@@ -116,13 +118,25 @@ export class IndexHtmlWebpackPlugin {
116118
}
117119

118120
for (const script of scripts) {
121+
const attrs = [
122+
{ name: 'type', value: 'text/javascript' },
123+
{ name: 'src', value: (this._options.deployUrl || '') + script },
124+
];
125+
if (this._options.sri) {
126+
const algo = 'sha384';
127+
const hash = createHash(algo)
128+
.update(compilation.assets[script].source(), 'utf8')
129+
.digest('base64');
130+
attrs.push(
131+
{ name: 'integrity', value: `${algo}-${hash}` },
132+
{ name: 'crossorigin', value: 'anonymous' },
133+
);
134+
}
135+
119136
const element = treeAdapter.createElement(
120137
'script',
121138
undefined,
122-
[
123-
{ name: 'type', value: 'text/javascript' },
124-
{ name: 'src', value: (this._options.deployUrl || '') + script },
125-
]
139+
attrs,
126140
);
127141
treeAdapter.appendChild(bodyElement, element);
128142
}
@@ -143,7 +157,7 @@ export class IndexHtmlWebpackPlugin {
143157
undefined,
144158
[
145159
{ name: 'href', value: this._options.baseHref },
146-
]
160+
],
147161
);
148162
treeAdapter.appendChild(headElement, element);
149163
} else {
@@ -168,7 +182,7 @@ export class IndexHtmlWebpackPlugin {
168182
[
169183
{ name: 'rel', value: 'stylesheet' },
170184
{ name: 'href', value: (this._options.deployUrl || '') + stylesheet },
171-
]
185+
],
172186
);
173187
treeAdapter.appendChild(headElement, element);
174188
}

packages/angular_devkit/build_angular/test/browser/subresource-integrity_spec_large.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ describe('Browser Builder subresource integrity', () => {
1717
beforeEach(done => host.initialize().subscribe(undefined, done.fail, done));
1818
afterEach(done => host.restore().subscribe(undefined, done.fail, done));
1919

20-
// TODO: WEBPACK4_DISABLED - disabled pending a webpack 4 version
21-
xit('works', (done) => {
20+
it('works', (done) => {
2221
host.writeMultipleFiles({
2322
'src/my-js-file.js': `console.log(1); export const a = 2;`,
2423
'src/main.ts': `import { a } from './my-js-file'; console.log(a);`,

0 commit comments

Comments
 (0)