From 392caf0f59ea86de66a5196c5905e6afd2c19460 Mon Sep 17 00:00:00 2001 From: Chang Liu <3dslayer@gmail.com> Date: Fri, 25 Nov 2016 23:47:36 +1100 Subject: [PATCH] feat(build): add publicPath support via command and angular-cli.json --- packages/angular-cli/commands/build.ts | 4 +++- packages/angular-cli/lib/config/schema.d.ts | 1 + packages/angular-cli/lib/config/schema.json | 3 +++ .../angular-cli/models/webpack-build-common.ts | 1 + packages/angular-cli/models/webpack-config.ts | 4 +++- packages/angular-cli/tasks/build-webpack-watch.ts | 5 ++++- packages/angular-cli/tasks/build-webpack.ts | 5 ++++- tests/e2e/tests/build/public-path.ts | 15 +++++++++++++++ 8 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 tests/e2e/tests/build/public-path.ts diff --git a/packages/angular-cli/commands/build.ts b/packages/angular-cli/commands/build.ts index 97ba333527cf..0a8d5ac9b812 100644 --- a/packages/angular-cli/commands/build.ts +++ b/packages/angular-cli/commands/build.ts @@ -15,6 +15,7 @@ export interface BuildOptions { vendorChunk?: boolean; verbose?: boolean; progress?: boolean; + publicPath?: string; } const BuildCommand = Command.extend({ @@ -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) { diff --git a/packages/angular-cli/lib/config/schema.d.ts b/packages/angular-cli/lib/config/schema.d.ts index 1f5ea0d0d526..4d645576dcb4 100644 --- a/packages/angular-cli/lib/config/schema.d.ts +++ b/packages/angular-cli/lib/config/schema.d.ts @@ -13,6 +13,7 @@ export interface CliConfig { root?: string; outDir?: string; assets?: string; + publicPath?: string; index?: string; main?: string; test?: string; diff --git a/packages/angular-cli/lib/config/schema.json b/packages/angular-cli/lib/config/schema.json index 37668441c0b0..34b86e9a5052 100644 --- a/packages/angular-cli/lib/config/schema.json +++ b/packages/angular-cli/lib/config/schema.json @@ -39,6 +39,9 @@ }, "default": [] }, + "publicPath": { + "type": "string" + }, "index": { "type": "string", "default": "index.html" diff --git a/packages/angular-cli/models/webpack-build-common.ts b/packages/angular-cli/models/webpack-build-common.ts index 27b85cfad977..bc5f2de8efe7 100644 --- a/packages/angular-cli/models/webpack-build-common.ts +++ b/packages/angular-cli/models/webpack-build-common.ts @@ -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' diff --git a/packages/angular-cli/models/webpack-config.ts b/packages/angular-cli/models/webpack-config.ts index d605305bc96c..dd8d0d0e732a 100644 --- a/packages/angular-cli/models/webpack-config.ts +++ b/packages/angular-cli/models/webpack-config.ts @@ -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, diff --git a/packages/angular-cli/tasks/build-webpack-watch.ts b/packages/angular-cli/tasks/build-webpack-watch.ts index 7e6958a90fbe..b3d66db7fcd1 100644 --- a/packages/angular-cli/tasks/build-webpack-watch.ts +++ b/packages/angular-cli/tasks/build-webpack-watch.ts @@ -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( @@ -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); diff --git a/packages/angular-cli/tasks/build-webpack.ts b/packages/angular-cli/tasks/build-webpack.ts index 770a9befe6e0..7ed69a34b8b6 100644 --- a/packages/angular-cli/tasks/build-webpack.ts +++ b/packages/angular-cli/tasks/build-webpack.ts @@ -16,6 +16,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( project, @@ -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); diff --git a/tests/e2e/tests/build/public-path.ts b/tests/e2e/tests/build/public-path.ts new file mode 100644 index 000000000000..29929bdba4cd --- /dev/null +++ b/tests/e2e/tests/build/public-path.ts @@ -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')); +}