Skip to content

Commit 39f22c2

Browse files
devversionjelbourn
authored andcommitted
build: remove duplicate licenses in bundles (#5195)
* No longer includes the license comments from every source file in the different FESM and UMD bundles. * Build packages are now emitted with explicit `LF` line endings instead of platform dependent line endings.
1 parent b456eed commit 39f22c2

File tree

7 files changed

+42
-4
lines changed

7 files changed

+42
-4
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"karma-sauce-launcher": "^1.1.0",
9999
"karma-sourcemap-loader": "^0.3.7",
100100
"madge": "^1.6.0",
101+
"magic-string": "^0.21.3",
101102
"merge2": "^1.0.3",
102103
"minimist": "^1.2.0",
103104
"node-sass": "^4.5.3",

src/cdk/tsconfig-build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"experimentalDecorators": true,
99
"noUnusedParameters": true,
1010
"importHelpers": true,
11+
"newLine": "lf",
1112
"module": "es2015",
1213
"moduleResolution": "node",
1314
"outDir": "../../dist/packages/cdk",

src/lib/tsconfig-build.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"experimentalDecorators": true,
88
"noUnusedParameters": true,
99
"importHelpers": true,
10+
"newLine": "lf",
1011
"module": "es2015",
1112
"moduleResolution": "node",
1213
"outDir": "../../dist/packages/material",

tools/package-tools/build-bundles.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {join} from 'path';
2-
import {ScriptTarget, ModuleKind} from 'typescript';
2+
import {ScriptTarget, ModuleKind, NewLineKind} from 'typescript';
33
import {uglifyJsFile} from './minify-sources';
44
import {createRollupBundle} from './rollup-helpers';
55
import {remapSourcemap} from './sourcemap-remap';
@@ -38,7 +38,8 @@ export async function buildPackageBundles(entryFile: string, packageName: string
3838
importHelpers: true,
3939
target: ScriptTarget.ES5,
4040
module: ModuleKind.ES2015,
41-
allowJs: true
41+
allowJs: true,
42+
newLine: NewLineKind.LineFeed
4243
});
4344

4445
await remapSourcemap(fesm2014File);

tools/package-tools/rollup-helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {buildConfig} from './build-config';
2+
import {rollupRemoveLicensesPlugin} from './rollup-remove-licenses';
23

34
// There are no type definitions available for these imports.
45
const rollup = require('rollup');
@@ -59,10 +60,11 @@ export type BundleConfig = {
5960

6061
/** Creates a rollup bundle of a specified JavaScript file.*/
6162
export function createRollupBundle(config: BundleConfig): Promise<any> {
62-
const bundleOptions: any = {
63+
const bundleOptions = {
6364
context: 'this',
6465
external: Object.keys(ROLLUP_GLOBALS),
6566
entry: config.entry,
67+
plugins: [rollupRemoveLicensesPlugin]
6668
};
6769

6870
const writeOptions = {
@@ -79,7 +81,7 @@ export function createRollupBundle(config: BundleConfig): Promise<any> {
7981
// When creating a UMD, we want to exclude tslib from the `external` bundle option so that it
8082
// is inlined into the bundle.
8183
if (config.format === 'umd') {
82-
bundleOptions.plugins = [rollupNodeResolutionPlugin()];
84+
bundleOptions.plugins.push(rollupNodeResolutionPlugin());
8385

8486
const external = Object.keys(ROLLUP_GLOBALS);
8587
external.splice(external.indexOf('tslib'), 1);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {buildConfig} from './build-config';
2+
import MagicString from 'magic-string';
3+
4+
/** License banner from the build config that will be removed in all source files. */
5+
const licenseBanner = buildConfig.licenseBanner;
6+
7+
/**
8+
* Rollup plugin that removes all license banners of source files.
9+
* This is necessary to avoid having the license comment repeated in the output.
10+
*/
11+
export const rollupRemoveLicensesPlugin = {
12+
name: 'rollup-clean-duplicate-licenses',
13+
transform: (code: string) => {
14+
const newContent = new MagicString(code);
15+
16+
// Walks through every occurrence of a license comment and overwrites it with an empty string.
17+
for (let pos = -1; (pos = code.indexOf(licenseBanner, pos + 1)) !== -1; null) {
18+
newContent.overwrite(pos, pos + licenseBanner.length, '');
19+
}
20+
21+
return {
22+
code: newContent.toString(),
23+
map: newContent.generateMap({ hires: true })
24+
};
25+
}
26+
};

0 commit comments

Comments
 (0)