Skip to content

build: remove duplicate licenses in bundles #5195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/cdk/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"experimentalDecorators": true,
"noUnusedParameters": true,
"importHelpers": true,
"newLine": "lf",
"module": "es2015",
"moduleResolution": "node",
"outDir": "../../dist/packages/cdk",
Expand Down
1 change: 1 addition & 0 deletions src/lib/tsconfig-build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"experimentalDecorators": true,
"noUnusedParameters": true,
"importHelpers": true,
"newLine": "lf",
"module": "es2015",
"moduleResolution": "node",
"outDir": "../../dist/packages/material",
Expand Down
5 changes: 3 additions & 2 deletions tools/package-tools/build-bundles.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions tools/package-tools/rollup-helpers.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -59,10 +60,11 @@ export type BundleConfig = {

/** Creates a rollup bundle of a specified JavaScript file.*/
export function createRollupBundle(config: BundleConfig): Promise<any> {
const bundleOptions: any = {
const bundleOptions = {
context: 'this',
external: Object.keys(ROLLUP_GLOBALS),
entry: config.entry,
plugins: [rollupRemoveLicensesPlugin]
};

const writeOptions = {
Expand All @@ -79,7 +81,7 @@ export function createRollupBundle(config: BundleConfig): Promise<any> {
// 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);
Expand Down
26 changes: 26 additions & 0 deletions tools/package-tools/rollup-remove-licenses.ts
Original file line number Diff line number Diff line change
@@ -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 })
};
}
};