Skip to content

Commit beac9b3

Browse files
committed
feat(build): add publicPath support via command and angular-cli.json
1 parent bf9c8f1 commit beac9b3

File tree

8 files changed

+33
-3
lines changed

8 files changed

+33
-3
lines changed

packages/angular-cli/commands/build.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface BuildOptions {
1313
aot?: boolean;
1414
sourcemap?: boolean;
1515
vendorChunk?: boolean;
16+
publicPath?: string;
1617
}
1718

1819
const BuildCommand = Command.extend({
@@ -35,7 +36,8 @@ const BuildCommand = Command.extend({
3536
{ name: 'base-href', type: String, default: null, aliases: ['bh'] },
3637
{ name: 'aot', type: Boolean, default: false },
3738
{ name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] },
38-
{ name: 'vendor-chunk', type: Boolean, default: true }
39+
{ name: 'vendor-chunk', type: Boolean, default: true },
40+
{ name: 'public-path', type: String, default: null, aliases: ['p'] }
3941
],
4042

4143
run: function (commandOptions: BuildOptions) {

packages/angular-cli/lib/config/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface CliConfig {
1313
root?: string;
1414
outDir?: string;
1515
assets?: string;
16+
publicPath?: string;
1617
index?: string;
1718
main?: string;
1819
test?: string;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
},
4040
"default": []
4141
},
42+
"publicPath": {
43+
"type": "string"
44+
},
4245
"index": {
4346
"type": "string",
4447
"default": "index.html"

packages/angular-cli/models/webpack-build-common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function getWebpackCommonConfig(
5454
entry: entry,
5555
output: {
5656
path: path.resolve(projectRoot, appConfig.outDir),
57+
publicPath: appConfig.publicPath,
5758
filename: '[name].bundle.js',
5859
sourceMapFilename: '[name].bundle.map',
5960
chunkFilename: '[id].chunk.js'

packages/angular-cli/models/webpack-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export class NgCliWebpackConfig {
2626
isAoT = false,
2727
sourcemap = true,
2828
vendorChunk = false,
29+
publicPath?: string
2930
) {
3031
const config: CliConfig = CliConfig.fromProject();
3132
const appConfig = config.config.apps[0];
3233

3334
appConfig.outDir = outputDir || appConfig.outDir;
35+
appConfig.publicPath = publicPath || appConfig.publicPath;
3436

3537
let baseConfig = getWebpackCommonConfig(
3638
this.ngCliProject.root,

packages/angular-cli/tasks/build-webpack-watch.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default Task.extend({
1616
const project = this.cliProject;
1717

1818
const outputDir = runTaskOptions.outputPath || CliConfig.fromProject().config.apps[0].outDir;
19+
const publicPath = runTaskOptions.publicPath ||
20+
CliConfig.fromProject().config.apps[0].publicPath;
1921
rimraf.sync(path.resolve(project.root, outputDir));
2022

2123
const config = new NgCliWebpackConfig(
@@ -26,7 +28,8 @@ export default Task.extend({
2628
runTaskOptions.baseHref,
2729
runTaskOptions.aot,
2830
runTaskOptions.sourcemap,
29-
runTaskOptions.vendorChunk
31+
runTaskOptions.vendorChunk,
32+
publicPath
3033
).config;
3134
const webpackCompiler: any = webpack(config);
3235

packages/angular-cli/tasks/build-webpack.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default <any>Task.extend({
1616
const project = this.cliProject;
1717

1818
const outputDir = runTaskOptions.outputPath || CliConfig.fromProject().config.apps[0].outDir;
19+
const publicPath = runTaskOptions.publicPath ||
20+
CliConfig.fromProject().config.apps[0].publicPath;
1921
rimraf.sync(path.resolve(project.root, outputDir));
2022
const config = new NgCliWebpackConfig(
2123
project,
@@ -25,7 +27,8 @@ export default <any>Task.extend({
2527
runTaskOptions.baseHref,
2628
runTaskOptions.aot,
2729
runTaskOptions.sourcemap,
28-
runTaskOptions.vendorChunk
30+
runTaskOptions.vendorChunk,
31+
publicPath
2932
).config;
3033

3134
const webpackCompiler: any = webpack(config);

tests/e2e/tests/build/public-path.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {ng} from '../../utils/process';
2+
import {expectFileToMatch} from '../../utils/fs';
3+
import {updateJsonFile} from '../../utils/project';
4+
5+
6+
export default function() {
7+
return ng('build', '-p', 'publicPath/')
8+
.then(() => expectFileToMatch('dist/index.html', 'publicPath/main.bundle.js'))
9+
.then(() => updateJsonFile('angular-cli.json', configJson => {
10+
const app = configJson['apps'][0];
11+
app['publicPath'] = 'config-publicPath/';
12+
}))
13+
.then(() => ng('build'))
14+
.then(() => expectFileToMatch('dist/index.html', 'config-publicPath/main.bundle.js'));
15+
}

0 commit comments

Comments
 (0)