Skip to content

Commit 374c210

Browse files
committed
feat: add ability to output bundles in separate directory
1 parent 599d659 commit 374c210

File tree

6 files changed

+56
-9
lines changed

6 files changed

+56
-9
lines changed

packages/@angular/cli/blueprints/ng2/files/angular-cli.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"assets",
1212
"favicon.ico"
1313
],
14+
"bundlesOutDir": ".",
1415
"index": "index.html",
1516
"main": "main.ts",
1617
"polyfills": "polyfills.ts",

packages/@angular/cli/lib/config/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
},
7272
"default": []
7373
},
74+
"bundlesOutDir": {
75+
"type": "string",
76+
"default": "."
77+
},
7478
"deployUrl": {
7579
"type": "string",
7680
"description": ""

packages/@angular/cli/models/webpack-configs/common.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import * as path from 'path';
33
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
44
import { packageChunkSort } from '../../utilities/package-chunk-sort';
55
import { BaseHrefWebpackPlugin } from '../../lib/base-href-webpack';
6-
import { extraEntryParser, lazyChunksFilter, getOutputHashFormat } from './utils';
76
import { WebpackConfigOptions } from '../webpack-config';
7+
import {
8+
extraEntryParser,
9+
lazyChunksFilter,
10+
getOutputHashFormat,
11+
getCommonOutputPath,
12+
} from './utils';
813

914
const autoprefixer = require('autoprefixer');
1015
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
@@ -50,6 +55,8 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
5055
// determine hashing format
5156
const hashFormat = getOutputHashFormat(buildOptions.outputHashing);
5257

58+
const commonOutputPath = getCommonOutputPath(appConfig, hashFormat);
59+
5360
// process global scripts
5461
if (appConfig.scripts.length > 0) {
5562
const globalScripts = extraEntryParser(appConfig.scripts, appRoot, 'scripts');
@@ -96,18 +103,32 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
96103
output: {
97104
path: path.resolve(projectRoot, buildOptions.outputPath),
98105
publicPath: buildOptions.deployUrl,
99-
filename: `[name]${hashFormat.chunk}.bundle.js`,
100-
chunkFilename: `[id]${hashFormat.chunk}.chunk.js`
106+
filename: `${appConfig.bundlesOutDir}/[name]${hashFormat.chunk}.bundle.js`,
107+
chunkFilename: `${appConfig.bundlesOutDir}/[id]${hashFormat.chunk}.chunk.js`
101108
},
102109
module: {
103110
rules: [
104-
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader', exclude: [nodeModules] },
105-
{ test: /\.json$/, loader: 'json-loader' },
106-
{ test: /\.html$/, loader: 'raw-loader' },
107-
{ test: /\.(eot|svg)$/, loader: `file-loader?name=[name]${hashFormat.file}.[ext]` },
111+
{
112+
enforce: 'pre',
113+
test: /\.js$/,
114+
loader: `source-map-loader?name=${commonOutputPath}`,
115+
exclude: [nodeModules]
116+
},
117+
{
118+
test: /\.json$/,
119+
loader: `json-loader?name=${commonOutputPath}`
120+
},
121+
{
122+
test: /\.html$/,
123+
loader: `raw-loader?name=${commonOutputPath}`
124+
},
125+
{
126+
test: /\.(eot|svg)$/,
127+
loader: `file-loader?name=${commonOutputPath}`
128+
},
108129
{
109130
test: /\.(jpg|png|gif|otf|ttf|woff|woff2|cur|ani)$/,
110-
loader: `url-loader?name=[name]${hashFormat.file}.[ext]&limit=10000`
131+
loader: `url-loader?name=${commonOutputPath}&limit=10000`
111132
}
112133
].concat(extraRules)
113134
},

packages/@angular/cli/models/webpack-configs/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
135135
plugins: [
136136
// extract global css from js files into own css file
137137
new ExtractTextPlugin({
138-
filename: `[name]${hashFormat.extract}.bundle.css`,
138+
filename: `${appConfig.bundlesOutDir}/[name]${hashFormat.extract}.bundle.css`,
139139
disable: !buildOptions.extractCss
140140
}),
141141
new webpack.LoaderOptionsPlugin({

packages/@angular/cli/models/webpack-configs/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,12 @@ export function getOutputHashFormat(option: string, length = 20): HashFormat {
8686
/* tslint:enable:max-line-length */
8787
return hashFormats[option] || hashFormats['none'];
8888
}
89+
90+
91+
92+
export function getCommonOutputPath(
93+
appConfig: any,
94+
hashFormat: HashFormat,
95+
): string {
96+
return `${appConfig.bundlesOutDir}/[name]${hashFormat.file}.[ext]`;
97+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { updateJsonFile } from '../../utils/project';
2+
import { expectFileToExist } from '../../../utils/fs';
3+
4+
export default function() {
5+
return Promise.resolve()
6+
.then(() => updateJsonFile('.angular-cli.json', configJson => {
7+
configJson.apps[0].bundlesOutDir = 'bundles';
8+
}))
9+
.then(() => ng('build', '--output-hashing=none'))
10+
.then(() => !expectFileToExist('dist/main.bundle.js'))
11+
.then(() => expectFileToExist('dist/bundles/main.bundle.js'));
12+
}

0 commit comments

Comments
 (0)