Skip to content

Commit

Permalink
feat(build): add publicPath support via command and angular-cli.json
Browse files Browse the repository at this point in the history
  • Loading branch information
changLiuUNSW committed Dec 4, 2016
1 parent 457a6e0 commit 392caf0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/angular-cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BuildOptions {
vendorChunk?: boolean;
verbose?: boolean;
progress?: boolean;
publicPath?: string;
}

const BuildCommand = Command.extend({
Expand All @@ -39,7 +40,8 @@ const BuildCommand = Command.extend({
{ name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] },
{ name: 'vendor-chunk', type: Boolean, default: true },
{ name: 'verbose', type: Boolean, default: false },
{ name: 'progress', type: Boolean, default: true }
{ name: 'progress', type: Boolean, default: true },
{ name: 'public-path', type: String, default: null, aliases: ['p'] }
],

run: function (commandOptions: BuildOptions) {
Expand Down
1 change: 1 addition & 0 deletions packages/angular-cli/lib/config/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface CliConfig {
root?: string;
outDir?: string;
assets?: string;
publicPath?: string;
index?: string;
main?: string;
test?: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/angular-cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
},
"default": []
},
"publicPath": {
"type": "string"
},
"index": {
"type": "string",
"default": "index.html"
Expand Down
1 change: 1 addition & 0 deletions packages/angular-cli/models/webpack-build-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function getWebpackCommonConfig(
entry: entry,
output: {
path: path.resolve(projectRoot, appConfig.outDir),
publicPath: appConfig.publicPath,
filename: '[name].bundle.js',
sourceMapFilename: '[name].bundle.map',
chunkFilename: '[id].chunk.js'
Expand Down
4 changes: 3 additions & 1 deletion packages/angular-cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export class NgCliWebpackConfig {
sourcemap = true,
vendorChunk = false,
verbose = false,
progress = true
progress = true,
publicPath?: string
) {
const config: CliConfig = CliConfig.fromProject();
const appConfig = config.config.apps[0];

appConfig.outDir = outputDir || appConfig.outDir;
appConfig.publicPath = publicPath || appConfig.publicPath;

let baseConfig = getWebpackCommonConfig(
this.ngCliProject.root,
Expand Down
5 changes: 4 additions & 1 deletion packages/angular-cli/tasks/build-webpack-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default Task.extend({
const project = this.cliProject;

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

const config = new NgCliWebpackConfig(
Expand All @@ -27,7 +29,8 @@ export default Task.extend({
runTaskOptions.sourcemap,
runTaskOptions.vendorChunk,
runTaskOptions.verbose,
runTaskOptions.progress
runTaskOptions.progress,
publicPath
).config;
const webpackCompiler: any = webpack(config);

Expand Down
5 changes: 4 additions & 1 deletion packages/angular-cli/tasks/build-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default <any>Task.extend({
const project = this.cliProject;

const outputDir = runTaskOptions.outputPath || CliConfig.fromProject().config.apps[0].outDir;
const publicPath = runTaskOptions.publicPath ||
CliConfig.fromProject().config.apps[0].publicPath;
rimraf.sync(path.resolve(project.root, outputDir));
const config = new NgCliWebpackConfig(
project,
Expand All @@ -27,7 +29,8 @@ export default <any>Task.extend({
runTaskOptions.sourcemap,
runTaskOptions.vendorChunk,
runTaskOptions.verbose,
runTaskOptions.progress
runTaskOptions.progress,
publicPath
).config;

const webpackCompiler: any = webpack(config);
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/tests/build/public-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {ng} from '../../utils/process';
import {expectFileToMatch} from '../../utils/fs';
import {updateJsonFile} from '../../utils/project';


export default function() {
return ng('build', '-p', 'publicPath/')
.then(() => expectFileToMatch('dist/index.html', 'publicPath/main.bundle.js'))
.then(() => updateJsonFile('angular-cli.json', configJson => {
const app = configJson['apps'][0];
app['publicPath'] = 'config-publicPath/';
}))
.then(() => ng('build'))
.then(() => expectFileToMatch('dist/index.html', 'config-publicPath/main.bundle.js'));
}

0 comments on commit 392caf0

Please sign in to comment.