Filipe Silva e8f27f029a feat(@angular/cli): support sourcemaps and minification in scripts
Adds sourcemap and minification to javascript added via the `scripts` array in `.angular-cli.json`.

`script-loader` is no longer used, which should help with CSP since it used `eval`.

Scripts will no longer appear in the console output for `ng build`, as they are now assets instead of webpack entry points.

It's no longer possible to have the `output` property of both a `scripts` and a `styles` entry pointing to the same file. This wasn't officially supported or listed in the docs, but used to be possible.

Fix #2796
Fix #7226
Fix #7290

Related to #6872
2017-08-17 16:38:35 -04:00

90 lines
2.9 KiB
TypeScript

import * as path from 'path';
export const ngAppResolve = (resolvePath: string): string => {
return path.resolve(process.cwd(), resolvePath);
};
const webpackOutputOptions = {
colors: true,
hash: true,
timings: true,
chunks: true,
chunkModules: false,
children: false, // listing all children is very noisy in AOT and hides warnings/errors
modules: false,
reasons: false,
warnings: true,
assets: false, // listing all assets is very noisy when using assets directories
version: false
};
const verboseWebpackOutputOptions = {
children: true,
assets: true,
version: true,
reasons: true,
chunkModules: false // TODO: set to true when console to file output is fixed
};
export function getWebpackStatsConfig(verbose = false) {
return verbose
? Object.assign(webpackOutputOptions, verboseWebpackOutputOptions)
: webpackOutputOptions;
}
export interface ExtraEntry {
input: string;
output?: string;
lazy?: boolean;
path?: string;
entry?: string;
}
// Filter extra entries out of a arran of extraEntries
export function lazyChunksFilter(extraEntries: ExtraEntry[]) {
return extraEntries
.filter(extraEntry => extraEntry.lazy)
.map(extraEntry => extraEntry.entry);
}
// convert all extra entries into the object representation, fill in defaults
export function extraEntryParser(
extraEntries: (string | ExtraEntry)[],
appRoot: string,
defaultEntry: string
): ExtraEntry[] {
return extraEntries
.map((extraEntry: string | ExtraEntry) =>
typeof extraEntry === 'string' ? { input: extraEntry } : extraEntry)
.map((extraEntry: ExtraEntry) => {
extraEntry.path = path.resolve(appRoot, extraEntry.input);
if (extraEntry.output) {
extraEntry.entry = extraEntry.output.replace(/\.(js|css)$/i, '');
} else if (extraEntry.lazy) {
extraEntry.entry = extraEntry.input.replace(/\.(js|css|scss|sass|less|styl)$/i, '');
} else {
extraEntry.entry = defaultEntry;
}
return extraEntry;
});
}
export interface HashFormat {
chunk: string;
extract: string;
file: string;
script: string;
}
export function getOutputHashFormat(option: string, length = 20): HashFormat {
/* tslint:disable:max-line-length */
const hashFormats: { [option: string]: HashFormat } = {
none: { chunk: '', extract: '', file: '' , script: '' },
media: { chunk: '', extract: '', file: `.[hash:${length}]`, script: '' },
bundles: { chunk: `.[chunkhash:${length}]`, extract: `.[contenthash:${length}]`, file: '' , script: '.[hash]' },
all: { chunk: `.[chunkhash:${length}]`, extract: `.[contenthash:${length}]`, file: `.[hash:${length}]`, script: '.[hash]' },
};
/* tslint:enable:max-line-length */
return hashFormats[option] || hashFormats['none'];
}