Skip to content

Commit 26ecde7

Browse files
changLiuUNSWMRHarrison
authored andcommitted
feat(build): add publicPath support via command and angular-cli.json (angular#3285)
Add publicPath option for webpack. User can specify publicPath via `--deploy-url` / `-d` from command line or add `deployUrl` to `angular-cli.json`. It can solve following issues: Change the public URL address of the output files (different from baseUrl). Manipulate the request url for chunk js files. It is very helpful to solve resources url and route lazying load issues for those applications which have different static files paths such as ASP.NET MVC. Fixes angular#3136 Fixes angular#2960 Fixes angular#2276 Fixes angular#2241 Fixes angular#3344
1 parent 4352a22 commit 26ecde7

File tree

8 files changed

+35
-5
lines changed

8 files changed

+35
-5
lines changed

packages/angular-cli/commands/build.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface BuildOptions {
1919
i18nFile?: string;
2020
i18nFormat?: string;
2121
locale?: string;
22+
deployUrl?: string;
2223
}
2324

2425
const BuildCommand = Command.extend({
@@ -46,7 +47,8 @@ const BuildCommand = Command.extend({
4647
{ name: 'progress', type: Boolean, default: true },
4748
{ name: 'i18n-file', type: String, default: null },
4849
{ name: 'i18n-format', type: String, default: null },
49-
{ name: 'locale', type: String, default: null }
50+
{ name: 'locale', type: String, default: null },
51+
{ name: 'deploy-url', type: String, default: null, aliases: ['d'] }
5052
],
5153

5254
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+
deployUrl?: 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
@@ -45,6 +45,9 @@
4545
],
4646
"default": []
4747
},
48+
"deployUrl": {
49+
"type": "string"
50+
},
4851
"index": {
4952
"type": "string",
5053
"default": "index.html"

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ export function getWebpackCommonConfig(
118118
context: projectRoot,
119119
entry: entryPoints,
120120
output: {
121-
path: path.resolve(projectRoot, appConfig.outDir)
121+
path: path.resolve(projectRoot, appConfig.outDir),
122+
publicPath: appConfig.deployUrl
122123
},
123124
module: {
124125
rules: [

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ export class NgCliWebpackConfig {
3131
sourcemap = true,
3232
vendorChunk = false,
3333
verbose = false,
34-
progress = true
34+
progress = true,
35+
deployUrl?: string
3536
) {
3637
const config: CliConfig = CliConfig.fromProject();
3738
const appConfig = config.config.apps[0];
3839

3940
appConfig.outDir = outputDir || appConfig.outDir;
41+
appConfig.deployUrl = deployUrl || appConfig.deployUrl;
4042

4143
let baseConfig = getWebpackCommonConfig(
4244
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 deployUrl = runTaskOptions.deployUrl ||
19+
CliConfig.fromProject().config.apps[0].deployUrl;
1820
rimraf.sync(path.resolve(project.root, outputDir));
1921

2022
const config = new NgCliWebpackConfig(
@@ -30,7 +32,8 @@ export default Task.extend({
3032
runTaskOptions.sourcemap,
3133
runTaskOptions.vendorChunk,
3234
runTaskOptions.verbose,
33-
runTaskOptions.progress
35+
runTaskOptions.progress,
36+
deployUrl
3437
).config;
3538
const webpackCompiler: any = webpack(config);
3639

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export default <any>Task.extend({
1717
const project = this.cliProject;
1818

1919
const outputDir = runTaskOptions.outputPath || CliConfig.fromProject().config.apps[0].outDir;
20+
const deployUrl = runTaskOptions.deployUrl ||
21+
CliConfig.fromProject().config.apps[0].deployUrl;
2022
rimraf.sync(path.resolve(project.root, outputDir));
2123
const config = new NgCliWebpackConfig(
2224
project,
@@ -31,7 +33,8 @@ export default <any>Task.extend({
3133
runTaskOptions.sourcemap,
3234
runTaskOptions.vendorChunk,
3335
runTaskOptions.verbose,
34-
runTaskOptions.progress
36+
runTaskOptions.progress,
37+
deployUrl
3538
).config;
3639

3740
const webpackCompiler: any = webpack(config);

tests/e2e/tests/build/deploy-url.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', '-d', 'deployUrl/')
8+
.then(() => expectFileToMatch('dist/index.html', 'deployUrl/main.bundle.js'))
9+
.then(() => updateJsonFile('angular-cli.json', configJson => {
10+
const app = configJson['apps'][0];
11+
app['deployUrl'] = 'config-deployUrl/';
12+
}))
13+
.then(() => ng('build'))
14+
.then(() => expectFileToMatch('dist/index.html', 'config-deployUrl/main.bundle.js'));
15+
}

0 commit comments

Comments
 (0)