Skip to content

Commit c4e36c0

Browse files
committed
fix(polyfills): move polyfills to scripts array
Polyfills found in polyfills.ts would not be available for scripts due to being loaded in the main bundle only. Fix angular#2752 Fix angular#3309
1 parent f4b5773 commit c4e36c0

File tree

12 files changed

+49
-12
lines changed

12 files changed

+49
-12
lines changed

packages/angular-cli/blueprints/ng2/files/__path__/main.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import './polyfills.ts';
2-
31
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
42
import { enableProdMode } from '@angular/core';
53
import { environment } from './environments/environment';

packages/angular-cli/blueprints/ng2/files/__path__/test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
22

3-
import './polyfills.ts';
4-
53
import 'zone.js/dist/long-stack-trace-zone';
64
import 'zone.js/dist/proxy.js';
75
import 'zone.js/dist/sync-test';

packages/angular-cli/blueprints/ng2/files/angular-cli.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
"index": "index.html",
1515
"main": "main.ts",
16+
"polyfills": "polyfills.ts",
1617
"test": "test.ts",
1718
"tsconfig": "tsconfig.json",
1819
"prefix": "<%= prefix %>",

packages/angular-cli/lib/config/schema.json

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"main": {
5656
"type": "string"
5757
},
58+
"polyfills": {
59+
"type": "string"
60+
},
5861
"test": {
5962
"type": "string"
6063
},

packages/angular-cli/models/webpack-build-common.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export function getWebpackCommonConfig(
5252
entryPoints['main'] = [path.resolve(appRoot, appConfig.main)];
5353
}
5454

55+
if (appConfig.polyfills) {
56+
entryPoints['polyfills'] = [path.resolve(appRoot, appConfig.polyfills)];
57+
}
58+
5559
// determine hashing format
5660
const hashFormat = getOutputHashFormat(outputHashing);
5761

@@ -143,7 +147,9 @@ export function getWebpackCommonConfig(
143147
new HtmlWebpackPlugin({
144148
template: path.resolve(appRoot, appConfig.index),
145149
filename: path.resolve(appConfig.outDir, appConfig.index),
146-
chunksSortMode: packageChunkSort(['inline', 'styles', 'scripts', 'vendor', 'main']),
150+
chunksSortMode: packageChunkSort([
151+
'inline', 'polyfills', 'styles', 'scripts', 'vendor', 'main'
152+
]),
147153
excludeChunks: lazyChunks,
148154
xhtml: true
149155
}),

packages/angular-cli/models/webpack-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class NgCliWebpackConfig {
6969

7070
let config = webpackMerge(baseConfig, targetConfigPartial);
7171

72-
if (appConfig.main) {
72+
if (appConfig.main || appConfig.polyfills) {
7373
const typescriptConfigPartial = isAoT
7474
? getWebpackAotConfigPartial(projectRoot, appConfig, i18nFile, i18nFormat, locale)
7575
: getWebpackNonAotConfigPartial(projectRoot, appConfig);

packages/angular-cli/plugins/karma.js

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ const init = (config) => {
6868
.map((file) => config.preprocessors[file])
6969
.map((arr) => arr.splice(arr.indexOf('angular-cli'), 1, 'webpack', 'sourcemap'));
7070

71+
// Add polyfills file
72+
if (appConfig.polyfills) {
73+
const polyfillsFile = path.resolve(appRoot, appConfig.polyfills);
74+
Array.prototype.unshift.apply(config.files, [polyfillsFile]);
75+
config.preprocessors[polyfillsFile] = ['webpack', 'sourcemap'];
76+
}
77+
7178
// Add global scripts
7279
if (appConfig.scripts && appConfig.scripts.length > 0) {
7380
const globalScriptPatterns = appConfig.scripts

tests/e2e/tests/build/polyfills.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expectFileToMatch } from '../../utils/fs';
2+
import { ng } from '../../utils/process';
3+
import { oneLineTrim } from 'common-tags';
4+
5+
export default function () {
6+
return Promise.resolve()
7+
.then(() => ng('build'))
8+
// files were created successfully
9+
.then(() => expectFileToMatch('dist/polyfills.bundle.js', 'core-js'))
10+
.then(() => expectFileToMatch('dist/polyfills.bundle.js', 'zone`-js'))
11+
// index.html lists the right bundles
12+
.then(() => expectFileToMatch('dist/index.html', oneLineTrim`
13+
<script type="text/javascript" src="inline.bundle.js"></script>
14+
<script type="text/javascript" src="polyfills.bundle.js"></script>
15+
<script type="text/javascript" src="vendor.bundle.js"></script>
16+
<script type="text/javascript" src="main.bundle.js"></script>
17+
`));
18+
}

tests/e2e/tests/build/scripts-array.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export default function () {
1818
})
1919
.then(() => updateJsonFile('angular-cli.json', configJson => {
2020
const app = configJson['apps'][0];
21-
app['scripts'] = [
21+
app['scripts'] = app['scripts'].concat([
2222
'string-script.js',
2323
{ input: 'input-script.js' },
2424
{ input: 'lazy-script.js', lazy: true },
2525
{ input: 'pre-rename-script.js', output: 'renamed-script' },
2626
{ input: 'pre-rename-lazy-script.js', output: 'renamed-lazy-script', lazy: true },
2727
{ input: 'common-entry-script.js', output: 'common-entry' }
28-
];
28+
]);
2929
app['styles'] = [{ input: 'common-entry-style.css', output: 'common-entry' }];
3030
}))
3131
.then(() => ng('build'))
@@ -43,6 +43,7 @@ export default function () {
4343
`))
4444
.then(() => expectFileToMatch('dist/index.html', oneLineTrim`
4545
<script type="text/javascript" src="inline.bundle.js"></script>
46+
<script type="text/javascript" src="polyfills.bundle.js"></script>
4647
<script type="text/javascript" src="renamed-script.bundle.js"></script>
4748
<script type="text/javascript" src="common-entry.bundle.js"></script>
4849
<script type="text/javascript" src="scripts.bundle.js"></script>

tests/e2e/tests/build/styles/styles-array.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export default function () {
2828
{ input: 'pre-rename-lazy-style.css', output: 'renamed-lazy-style', lazy: true },
2929
{ input: 'common-entry-style.css', output: 'common-entry' }
3030
];
31-
app['scripts'] = [{ input: 'common-entry-script.js', output: 'common-entry' }];
31+
app['scripts'] = app['scripts'].concat(
32+
[{ input: 'common-entry-script.js', output: 'common-entry' }]
33+
);
3234
}))
3335
.then(() => ng('build'))
3436
// files were created successfully
@@ -52,8 +54,10 @@ export default function () {
5254
`))
5355
.then(() => expectFileToMatch('dist/index.html', oneLineTrim`
5456
<script type="text/javascript" src="inline.bundle.js"></script>
55-
<script type="text/javascript" src="vendor.bundle.js"></script>
57+
<script type="text/javascript" src="polyfills.bundle.js"></script>
5658
<script type="text/javascript" src="common-entry.bundle.js"></script>
59+
<script type="text/javascript" src="scripts.bundle.js"></script>
60+
<script type="text/javascript" src="vendor.bundle.js"></script>
5761
<script type="text/javascript" src="main.bundle.js"></script>
5862
`))
5963
.then(() => ng('build', '--no-extract-css'))

tests/e2e/tests/test/test-scripts.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export default function () {
6060
.then(() => expectToFail(() => ng('test', '--single-run')))
6161
.then(() => updateJsonFile('angular-cli.json', configJson => {
6262
const app = configJson['apps'][0];
63-
app['scripts'] = [
63+
app['scripts'] = app['scripts'].concat([
6464
'string-script.js',
6565
{ input: 'input-script.js' }
66-
];
66+
]);
6767
}))
6868
// should pass now
6969
.then(() => ng('test', '--single-run'));

tests/e2e/tests/third-party/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function() {
2323
.then(() => expectFileToMatch('dist/styles.bundle.css', '* Bootstrap'))
2424
.then(() => expectFileToMatch('dist/index.html', oneLineTrim`
2525
<script type="text/javascript" src="inline.bundle.js"></script>
26+
<script type="text/javascript" src="polyfills.bundle.js"></script>
2627
<script type="text/javascript" src="scripts.bundle.js"></script>
2728
<script type="text/javascript" src="vendor.bundle.js"></script>
2829
<script type="text/javascript" src="main.bundle.js"></script>

0 commit comments

Comments
 (0)