Skip to content

Commit 4eefd83

Browse files
committed
feat(build): add publicPath support via command and angular-cli.json
Conflicts: packages/angular-cli/commands/build.ts packages/angular-cli/models/webpack-config.ts packages/angular-cli/tasks/build-webpack-watch.ts packages/angular-cli/tasks/build-webpack.ts
1 parent ed305a2 commit 4eefd83

File tree

8 files changed

+34
-4
lines changed

8 files changed

+34
-4
lines changed

packages/angular-cli/commands/build.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface BuildOptions {
1515
vendorChunk?: boolean;
1616
verbose?: boolean;
1717
progress?: boolean;
18+
publicPath?: string;
1819
}
1920

2021
const BuildCommand = Command.extend({
@@ -39,7 +40,8 @@ const BuildCommand = Command.extend({
3940
{ name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] },
4041
{ name: 'vendor-chunk', type: Boolean, default: true },
4142
{ name: 'verbose', type: Boolean, default: false },
42-
{ name: 'progress', type: Boolean, default: true }
43+
{ name: 'progress', type: Boolean, default: true },
44+
{ name: 'public-path', type: String, default: null, aliases: ['p'] }
4345
],
4446

4547
run: function (commandOptions: BuildOptions) {

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

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function getWebpackCommonConfig(
6464
entry: entry,
6565
output: {
6666
path: path.resolve(projectRoot, appConfig.outDir),
67+
publicPath: appConfig.publicPath,
6768
filename: '[name].bundle.js',
6869
sourceMapFilename: '[name].bundle.map',
6970
chunkFilename: '[id].chunk.js'

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ export class NgCliWebpackConfig {
2727
sourcemap = true,
2828
vendorChunk = false,
2929
verbose = false,
30-
progress = true
30+
progress = true,
31+
publicPath?: string
3132
) {
3233
const config: CliConfig = CliConfig.fromProject();
3334
const appConfig = config.config.apps[0];
3435

3536
appConfig.outDir = outputDir || appConfig.outDir;
37+
appConfig.publicPath = publicPath || appConfig.publicPath;
3638

3739
let baseConfig = getWebpackCommonConfig(
3840
this.ngCliProject.root,

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default Task.extend({
1515
const project = this.cliProject;
1616

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

2022
const config = new NgCliWebpackConfig(
@@ -27,7 +29,8 @@ export default Task.extend({
2729
runTaskOptions.sourcemap,
2830
runTaskOptions.vendorChunk,
2931
runTaskOptions.verbose,
30-
runTaskOptions.progress
32+
runTaskOptions.progress,
33+
publicPath
3134
).config;
3235
const webpackCompiler: any = webpack(config);
3336

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

+4-1
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,
@@ -27,7 +29,8 @@ export default <any>Task.extend({
2729
runTaskOptions.sourcemap,
2830
runTaskOptions.vendorChunk,
2931
runTaskOptions.verbose,
30-
runTaskOptions.progress
32+
runTaskOptions.progress,
33+
publicPath
3134
).config;
3235

3336
const webpackCompiler: any = webpack(config);

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

+15
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)