From c5a552332373cf34aa82c33126e056448115cd12 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 | 2 ++ .../angular-cli/tasks/build-webpack-watch.ts | 5 ++++- packages/angular-cli/tasks/build-webpack.ts | 6 +++++- tests/e2e/tests/build/public-path.ts | 16 ++++++++++++++++ 8 files changed, 35 insertions(+), 3 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 843fa466f79d..aee19dc66407 100644 --- a/packages/angular-cli/commands/build.ts +++ b/packages/angular-cli/commands/build.ts @@ -12,6 +12,7 @@ export interface BuildOptions { baseHref?: string; aot?: boolean; sourcemap?: boolean; + publicPath?: string; } const BuildCommand = Command.extend({ @@ -33,7 +34,8 @@ const BuildCommand = Command.extend({ { name: 'suppress-sizes', type: Boolean, default: false }, { name: 'base-href', type: String, default: null, aliases: ['bh'] }, { name: 'aot', type: Boolean, default: false }, - { name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] } + { name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] }, + { 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 d81e3c44ebb1..51c95346e44c 100644 --- a/packages/angular-cli/models/webpack-build-common.ts +++ b/packages/angular-cli/models/webpack-build-common.ts @@ -42,6 +42,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 a6c42a8ba407..e9c5e4d80c12 100644 --- a/packages/angular-cli/models/webpack-config.ts +++ b/packages/angular-cli/models/webpack-config.ts @@ -25,11 +25,13 @@ export class NgCliWebpackConfig { baseHref?: string, isAoT = false, sourcemap = 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 ab7498b3dc00..aae5d9f2158a 100644 --- a/packages/angular-cli/tasks/build-webpack-watch.ts +++ b/packages/angular-cli/tasks/build-webpack-watch.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( @@ -25,7 +27,8 @@ export default Task.extend({ outputDir, runTaskOptions.baseHref, runTaskOptions.aot, - runTaskOptions.sourcemap + runTaskOptions.sourcemap, + 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 a2bd60c5c608..bd6df8875948 100644 --- a/packages/angular-cli/tasks/build-webpack.ts +++ b/packages/angular-cli/tasks/build-webpack.ts @@ -16,6 +16,9 @@ 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, @@ -24,7 +27,8 @@ export default Task.extend({ outputDir, runTaskOptions.baseHref, runTaskOptions.aot, - runTaskOptions.sourcemap + runTaskOptions.sourcemap, + 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..94bd04d6161e --- /dev/null +++ b/tests/e2e/tests/build/public-path.ts @@ -0,0 +1,16 @@ +import {ng} from '../../utils/process'; +import {expectFileToMatch} from '../../utils/fs'; +import {expectGitToBeClean} from '../../utils/git'; +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')); +}