Skip to content

Commit

Permalink
feat(@angular/cli): remove deduped licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitarora authored and filipesilva committed Jun 2, 2017
1 parent d1cac32 commit e72693a
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/documentation/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Flag | `--dev` | `--prod`
`--sourcemaps` | `true` | `false`
`--extract-css` | `false` | `true`

`--extract-licenses` Extract all licenses in a separate file, in the case of production builds only.
`--i18n-file` Localization file to use for i18n.
`--prod` also sets the following non-flaggable settings:
- Adds service worker if configured in `.angular-cli.json`.
- Replaces `process.env.NODE_ENV` in modules with the `production` value (this is needed for some libraries, like react).
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"less": "^2.7.2",
"less-loader": "^4.0.2",
"loader-utils": "^1.0.2",
"license-webpack-plugin": "^0.4.3",
"lodash": "^4.11.1",
"magic-string": "^0.19.0",
"memory-fs": "^0.4.1",
Expand Down
9 changes: 8 additions & 1 deletion packages/@angular/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export const baseBuildCommandOptions: any = [
},
{
name: 'watch',
type: Boolean, default: false,
type: Boolean,
default: false,
aliases: ['w'],
description: 'Run build when files change.'
},
Expand Down Expand Up @@ -131,6 +132,12 @@ export const baseBuildCommandOptions: any = [
type: Boolean,
default: false,
description: 'Do not use the real path when resolving modules.'
},
{
name: 'extract-licenses',
type: Boolean,
default: true,
description: 'Extract all licenses in a separate file, in the case of production builds only.'
}
];

Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/build-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface BuildOptions {
app?: string;
deleteOutputPath?: boolean;
preserveSymlinks?: boolean;
extractLicenses?: boolean;
}
11 changes: 10 additions & 1 deletion packages/@angular/cli/models/webpack-configs/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StaticAssetPlugin } from '../../plugins/static-asset';
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
import { WebpackConfigOptions } from '../webpack-config';

const licensePlugin = require('license-webpack-plugin');

export const getProdConfig = function (wco: WebpackConfigOptions) {
const { projectRoot, buildOptions, appConfig } = wco;
Expand Down Expand Up @@ -79,6 +80,13 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
entryPoints['sw-register'] = [registerPath];
}

if (buildOptions.extractLicenses) {
extraPlugins.push(new licensePlugin({
pattern: /^(MIT|ISC|BSD.*)$/,
suppressErrors: true
}));
}

return {
entry: entryPoints,
plugins: [
Expand All @@ -89,7 +97,8 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
new webpack.optimize.UglifyJsPlugin(<any>{
mangle: { screw_ie8: true },
compress: { screw_ie8: true, warnings: buildOptions.verbose },
sourceMap: buildOptions.sourcemaps
sourceMap: buildOptions.sourcemaps,
comments: false
})
].concat(extraPlugins)
};
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"json-loader": "^0.5.4",
"less": "^2.7.2",
"less-loader": "^4.0.2",
"license-webpack-plugin": "^0.4.2",
"lodash": "^4.11.1",
"memory-fs": "^0.4.1",
"minimatch": "^3.0.3",
Expand Down
12 changes: 10 additions & 2 deletions packages/@angular/cli/tasks/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const angularCliPlugins = require('../plugins/webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SilentError = require('silent-error');
const licensePlugin = require('license-webpack-plugin');
const Task = require('../ember-cli/lib/models/task');

const ProgressPlugin = require('webpack/lib/ProgressPlugin');
Expand Down Expand Up @@ -134,6 +135,12 @@ class JsonWebpackSerializer {
return plugin.defaultValues;
}

private _licenseWebpackPlugin(plugin: any) {
return {
'pattern': plugin.pattern
};
}

private _pluginsReplacer(plugins: any[]) {
return plugins.map(plugin => {
let args = plugin.options || undefined;
Expand Down Expand Up @@ -180,13 +187,14 @@ class JsonWebpackSerializer {
args = this._environmentPlugin(plugin);
this._addImport('webpack', 'EnvironmentPlugin');
break;

case licensePlugin:
args = this._licenseWebpackPlugin(plugin);
this.variableImports['license-webpack-plugin'] = 'licensePlugin';
default:
if (plugin.constructor.name == 'AngularServiceWorkerPlugin') {
this._addImport('@angular/service-worker/build/webpack', plugin.constructor.name);
}
break;

}

const argsSerialized = JSON.stringify(args, (k, v) => this._replacer(k, v), 2) || '';
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/tests/build/dev-build.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {ng} from '../../utils/process';
import {expectFileToMatch} from '../../utils/fs';
import {expectFileToMatch, expectFileToExist} from '../../utils/fs';
import {expectGitToBeClean} from '../../utils/git';
import {getGlobalVariable} from '../../utils/env';
import {expectToFail} from '../../utils/utils';


export default function() {
const ejected = getGlobalVariable('argv').eject;

return ng('build', '--env=dev')
.then(() => expectFileToMatch('dist/index.html', 'main.bundle.js'))
.then(() => expectToFail(() => expectFileToExist('dist/3rdpartylicenses.txt')))
// If this is an ejected test, the eject will create files so git will not be clean.
.then(() => !ejected && expectGitToBeClean());
}
10 changes: 10 additions & 0 deletions tests/e2e/tests/build/extract-licenses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {join} from 'path';
import {expectFileToExist} from '../../utils/fs';
import {expectToFail} from '../../utils/utils';
import {ng} from '../../utils/process';

export default function() {
return ng('build', '--prod', '--extract-licenses=false')
.then(() => expectFileToExist(join(process.cwd(), 'dist')))
.then(() => expectToFail(() => expectFileToExist('dist/3rdpartylicenses.txt')));
}
1 change: 1 addition & 0 deletions tests/e2e/tests/build/prod-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function() {
// Check for cache busting hash script src
.then(() => expectFileToMatch('dist/index.html', /main\.[0-9a-f]{20}\.bundle\.js/))
.then(() => expectFileToMatch('dist/index.html', /styles\.[0-9a-f]{20}\.bundle\.css/))
.then(() => expectFileToExist('dist/3rdpartylicenses.txt'))
// Defaults to AoT
.then(() => {
const main = readdirSync('./dist').find(name => !!name.match(/main.[a-z0-9]+\.bundle\.js/));
Expand Down

0 comments on commit e72693a

Please sign in to comment.