Skip to content

Commit 83854a2

Browse files
committed
feat: add ability to output bundles in separate directory
1 parent a2e819a commit 83854a2

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
lines changed

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

+1
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

+4
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

+27-8
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+
getBundleOutputPath,
12+
} from './utils';
813

914
const autoprefixer = require('autoprefixer');
1015
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
@@ -96,18 +101,32 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
96101
output: {
97102
path: path.resolve(projectRoot, buildOptions.outputPath),
98103
publicPath: buildOptions.deployUrl,
99-
filename: `[name]${hashFormat.chunk}.bundle.js`,
100-
chunkFilename: `[id]${hashFormat.chunk}.chunk.js`
104+
filename: `${appConfig.bundlesOutDir}/[name]${hashFormat.chunk}.bundle.js`,
105+
chunkFilename: `${appConfig.bundlesOutDir}/[id]${hashFormat.chunk}.chunk.js`
101106
},
102107
module: {
103108
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]` },
109+
{
110+
enforce: 'pre',
111+
test: /\.js$/,
112+
loader: `source-map-loader?name=${getBundleOutputPath(appConfig)}`,
113+
exclude: [nodeModules]
114+
},
115+
{
116+
test: /\.json$/,
117+
loader: `json-loader?name=${getBundleOutputPath(appConfig)}`
118+
},
119+
{
120+
test: /\.html$/,
121+
loader: `raw-loader?name=${getBundleOutputPath(appConfig)}`
122+
},
123+
{
124+
test: /\.(eot|svg)$/,
125+
loader: `file-loader?name=${getBundleOutputPath(appConfig, hashFormat)}`
126+
},
108127
{
109128
test: /\.(jpg|png|gif|otf|ttf|woff|woff2|cur|ani)$/,
110-
loader: `url-loader?name=[name]${hashFormat.file}.[ext]&limit=10000`
129+
loader: `url-loader?name=${getBundleOutputPath(appConfig, hashFormat)}&limit=10000`
111130
}
112131
].concat(extraRules)
113132
},

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
161161
plugins: [
162162
// extract global css from js files into own css file
163163
new ExtractTextPlugin({
164-
filename: `[name]${hashFormat.extract}.bundle.css`,
164+
filename: `${appConfig.bundlesOutDir}/[name]${hashFormat.extract}.bundle.css`,
165165
disable: !buildOptions.extractCss
166166
}),
167167
new webpack.LoaderOptionsPlugin({

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

+16
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,19 @@ export function getOutputHashFormat(option: string, length = 20): HashFormat {
8686
/* tslint:enable:max-line-length */
8787
return hashFormats[option] || hashFormats['none'];
8888
}
89+
90+
export function getBundleOutputPath(
91+
appConfig: any,
92+
hashFormat?: HashFormat,
93+
): string {
94+
let result = '';
95+
if (appConfig.bundlesOutDir !== '.') {
96+
result += `${appConfig.bundlesOutDir}/`;
97+
if (hashFormat) {
98+
result += `[name]${hashFormat.file}.[ext]`;
99+
} else {
100+
result += `[name].[ext]`;
101+
}
102+
}
103+
return result;
104+
}
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)