diff --git a/package-lock.json b/package-lock.json index 59dc7e2e625..26786d5e084 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7491,6 +7491,12 @@ } } }, + "magic-string": { + "version": "0.21.3", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.21.3.tgz", + "integrity": "sha1-h+IBAJ6/3m9G3FdXMFpwr3HjFiQ=", + "dev": true + }, "make-dir": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.0.0.tgz", diff --git a/package.json b/package.json index 3c7a524385b..1eff947ba7d 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,7 @@ "karma-sauce-launcher": "^1.1.0", "karma-sourcemap-loader": "^0.3.7", "madge": "^1.6.0", + "magic-string": "^0.21.3", "merge2": "^1.0.3", "minimist": "^1.2.0", "node-sass": "^4.5.3", diff --git a/src/cdk/tsconfig-build.json b/src/cdk/tsconfig-build.json index 9df97ab71c9..b8439f5ce46 100644 --- a/src/cdk/tsconfig-build.json +++ b/src/cdk/tsconfig-build.json @@ -8,6 +8,7 @@ "experimentalDecorators": true, "noUnusedParameters": true, "importHelpers": true, + "newLine": "lf", "module": "es2015", "moduleResolution": "node", "outDir": "../../dist/packages/cdk", diff --git a/src/lib/tsconfig-build.json b/src/lib/tsconfig-build.json index 020a9b3aee7..434881accd7 100644 --- a/src/lib/tsconfig-build.json +++ b/src/lib/tsconfig-build.json @@ -7,6 +7,7 @@ "experimentalDecorators": true, "noUnusedParameters": true, "importHelpers": true, + "newLine": "lf", "module": "es2015", "moduleResolution": "node", "outDir": "../../dist/packages/material", diff --git a/tools/package-tools/build-bundles.ts b/tools/package-tools/build-bundles.ts index dcb42588938..8d3f1b93454 100644 --- a/tools/package-tools/build-bundles.ts +++ b/tools/package-tools/build-bundles.ts @@ -1,5 +1,5 @@ import {join} from 'path'; -import {ScriptTarget, ModuleKind} from 'typescript'; +import {ScriptTarget, ModuleKind, NewLineKind} from 'typescript'; import {uglifyJsFile} from './minify-sources'; import {createRollupBundle} from './rollup-helpers'; import {remapSourcemap} from './sourcemap-remap'; @@ -38,7 +38,8 @@ export async function buildPackageBundles(entryFile: string, packageName: string importHelpers: true, target: ScriptTarget.ES5, module: ModuleKind.ES2015, - allowJs: true + allowJs: true, + newLine: NewLineKind.LineFeed }); await remapSourcemap(fesm2014File); diff --git a/tools/package-tools/rollup-helpers.ts b/tools/package-tools/rollup-helpers.ts index 9b923188cba..fabc11f494f 100644 --- a/tools/package-tools/rollup-helpers.ts +++ b/tools/package-tools/rollup-helpers.ts @@ -1,4 +1,5 @@ import {buildConfig} from './build-config'; +import {rollupRemoveLicensesPlugin} from './rollup-remove-licenses'; // There are no type definitions available for these imports. const rollup = require('rollup'); @@ -59,10 +60,11 @@ export type BundleConfig = { /** Creates a rollup bundle of a specified JavaScript file.*/ export function createRollupBundle(config: BundleConfig): Promise { - const bundleOptions: any = { + const bundleOptions = { context: 'this', external: Object.keys(ROLLUP_GLOBALS), entry: config.entry, + plugins: [rollupRemoveLicensesPlugin] }; const writeOptions = { @@ -79,7 +81,7 @@ export function createRollupBundle(config: BundleConfig): Promise { // When creating a UMD, we want to exclude tslib from the `external` bundle option so that it // is inlined into the bundle. if (config.format === 'umd') { - bundleOptions.plugins = [rollupNodeResolutionPlugin()]; + bundleOptions.plugins.push(rollupNodeResolutionPlugin()); const external = Object.keys(ROLLUP_GLOBALS); external.splice(external.indexOf('tslib'), 1); diff --git a/tools/package-tools/rollup-remove-licenses.ts b/tools/package-tools/rollup-remove-licenses.ts new file mode 100644 index 00000000000..4ee720b4336 --- /dev/null +++ b/tools/package-tools/rollup-remove-licenses.ts @@ -0,0 +1,26 @@ +import {buildConfig} from './build-config'; +import MagicString from 'magic-string'; + +/** License banner from the build config that will be removed in all source files. */ +const licenseBanner = buildConfig.licenseBanner; + +/** + * Rollup plugin that removes all license banners of source files. + * This is necessary to avoid having the license comment repeated in the output. + */ +export const rollupRemoveLicensesPlugin = { + name: 'rollup-clean-duplicate-licenses', + transform: (code: string) => { + const newContent = new MagicString(code); + + // Walks through every occurrence of a license comment and overwrites it with an empty string. + for (let pos = -1; (pos = code.indexOf(licenseBanner, pos + 1)) !== -1; null) { + newContent.overwrite(pos, pos + licenseBanner.length, ''); + } + + return { + code: newContent.toString(), + map: newContent.generateMap({ hires: true }) + }; + } +};