Minko Gechev a355e7d693 feat(@schematics/angular): drop es6 from modern polyfills
1. Remove imports of es6 polyfills introduced by the CLI.
2. Refactor the migrations for version 8 by moving the codelyzer and
polyfill transforms into different files.

The PR drops all `core-js/es6` polyfills that we've introduced with the
CLI, except the commented ones. We do not remove commented imports,
since they are not part of the internal es6 polyfills.

The migration automatically drops the
associated comments with the removed imports since they are part of the node - under its
`jsDoc` property.
2019-03-29 18:06:01 -07:00

78 lines
2.5 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Rule, Tree } from '@angular-devkit/schematics';
import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
const toDrop: {[importName: string]: true} = {
'core-js/es6/symbol': true,
'core-js/es6/object': true,
'core-js/es6/function': true,
'core-js/es6/parse-int': true,
'core-js/es6/parse-float': true,
'core-js/es6/number': true,
'core-js/es6/math': true,
'core-js/es6/string': true,
'core-js/es6/date': true,
'core-js/es6/array': true,
'core-js/es6/regexp': true,
'core-js/es6/map': true,
'zone.js/dist/zone': true,
'core-js/es6/set': true,
};
const header = `/**
*/
/***************************************************************************************************
* BROWSER POLYFILLS
*/
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
// import 'core-js/es6/weak-map';`;
const applicationPolyfillsHeader = 'APPLICATION IMPORTS';
export const dropES2015Polyfills = (): Rule => {
return (tree: Tree) => {
const path = '/polyfills.ts';
const source = tree.read(path);
if (!source) {
return;
}
// Start the update of the file.
const recorder = tree.beginUpdate(path);
const sourceFile = ts.createSourceFile(path, source.toString(), ts.ScriptTarget.Latest, true);
const imports = sourceFile.statements
.filter(s => s.kind === ts.SyntaxKind.ImportDeclaration) as ts.ImportDeclaration[];
const applicationPolyfillsStart = sourceFile.getText().indexOf(applicationPolyfillsHeader);
if (imports.length === 0) { return; }
for (const i of imports) {
const module = ts.isStringLiteral(i.moduleSpecifier) && i.moduleSpecifier.text;
// We do not want to remove imports which are after the "APPLICATION IMPORTS" header.
if (module && toDrop[module] && applicationPolyfillsStart > i.getFullStart()) {
recorder.remove(i.getFullStart(), i.getFullWidth());
}
}
// We've removed the header since it's part of the JSDoc of the nodes we dropped
// As part of the header, we also add the comment for importing WeakMap since
// it's not part of the internal ES2015 polyfills we provide.
if (sourceFile.getText().indexOf(header) < 0) {
recorder.insertLeft(0, header);
}
tree.commitUpdate(recorder);
};
};