Skip to content

Commit

Permalink
feat: implement --base-href argument
Browse files Browse the repository at this point in the history
Implement --base-href argument for build and serve commands

Closes angular#1064
  • Loading branch information
dzonatan committed Jul 29, 2016
1 parent 67e70a0 commit 9ab6587
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
10 changes: 7 additions & 3 deletions addon/ng2/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface BuildOptions {
watch?: boolean;
watcher?: string;
supressSizes: boolean;
baseHref?: string;
}

module.exports = Command.extend({
Expand All @@ -22,7 +23,8 @@ module.exports = Command.extend({
{ name: 'output-path', type: 'Path', default: 'dist/', aliases: ['o'] },
{ name: 'watch', type: Boolean, default: false, aliases: ['w'] },
{ name: 'watcher', type: String },
{ name: 'suppress-sizes', type: Boolean, default: false }
{ name: 'suppress-sizes', type: Boolean, default: false },
{ name: 'base-href', type: String, default: null },
],

run: function (commandOptions: BuildOptions) {
Expand All @@ -32,7 +34,7 @@ module.exports = Command.extend({
}
if (commandOptions.target === 'production') {
commandOptions.environment = 'prod';
}
}
}

var project = this.project;
Expand All @@ -43,14 +45,16 @@ module.exports = Command.extend({
ui: ui,
outputPath: commandOptions.outputPath,
target: commandOptions.target,
environment: commandOptions.environment
environment: commandOptions.environment,
baseHref: commandOptions.baseHref
}) :
new WebpackBuild({
cliProject: project,
ui: ui,
outputPath: commandOptions.outputPath,
target: commandOptions.target,
environment: commandOptions.environment,
baseHref: commandOptions.baseHref
});

return buildTask.run(commandOptions);
Expand Down
5 changes: 4 additions & 1 deletion addon/ng2/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ServeTaskOptions {
ssl?: boolean;
sslKey?: string;
sslCert?: string;
baseHref?: string;
}

module.exports = Command.extend({
Expand All @@ -51,7 +52,8 @@ module.exports = Command.extend({
{ name: 'output-path', type: 'Path', default: 'dist/', aliases: ['op', 'out'] },
{ name: 'ssl', type: Boolean, default: false },
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' }
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' },
{ name: 'base-href', type: String, default: null },
],

run: function(commandOptions: ServeTaskOptions) {
Expand Down Expand Up @@ -85,6 +87,7 @@ module.exports = Command.extend({
ui: this.ui,
analytics: this.analytics,
project: this.project,
baseHref: commandOptions.baseHref
});

return serve.run(commandOptions);
Expand Down
6 changes: 5 additions & 1 deletion addon/ng2/models/webpack-build-common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as path from 'path';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import { BaseHrefWebpackPlugin } from 'base-href-webpack-plugin';
import * as webpack from 'webpack';
import { ForkCheckerPlugin } from 'awesome-typescript-loader';
import { CliConfig } from './config';

export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
export function getWebpackCommonConfig(projectRoot: string, sourceDir: string, baseHref: string) {
return {
devtool: 'inline-source-map',
resolve: {
Expand Down Expand Up @@ -64,6 +65,9 @@ export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
template: path.resolve(projectRoot, `./${sourceDir}/index.html`),
chunksSortMode: 'dependency'
}),
new BaseHrefWebpackPlugin({
baseHref: baseHref
}),
new webpack.optimize.CommonsChunkPlugin({
name: ['polyfills']
}),
Expand Down
4 changes: 2 additions & 2 deletions addon/ng2/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class NgCliWebpackConfig {
private webpackMobileConfigPartial: any;
private webpackMobileProdConfigPartial: any;

constructor(public ngCliProject: any, public target: string, public environment: string) {
constructor(public ngCliProject: any, public target: string, public environment: string, public baseHref: string) {
const sourceDir = CliConfig.fromProject().defaults.sourceDir;

const environmentPath = `./${sourceDir}/app/environments/environment.${environment}.ts`;

this.webpackBaseConfig = getWebpackCommonConfig(this.ngCliProject.root, sourceDir);
this.webpackBaseConfig = getWebpackCommonConfig(this.ngCliProject.root, sourceDir, baseHref);
this.webpackDevConfigPartial = getWebpackDevConfigPartial(this.ngCliProject.root, sourceDir);
this.webpackProdConfigPartial = getWebpackProdConfigPartial(this.ngCliProject.root, sourceDir);

Expand Down
2 changes: 1 addition & 1 deletion addon/ng2/tasks/build-webpack-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = Task.extend({

rimraf.sync(path.resolve(project.root, runTaskOptions.outputPath));

const config = new NgCliWebpackConfig(project, runTaskOptions.target, runTaskOptions.environment).config;
const config = new NgCliWebpackConfig(project, runTaskOptions.target, runTaskOptions.environment, runTaskOptions.baseHref).config;
const webpackCompiler = webpack(config);

webpackCompiler.apply(new ProgressPlugin({
Expand Down
4 changes: 2 additions & 2 deletions addon/ng2/tasks/build-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ let lastHash: any = null;

module.exports = Task.extend({
// Options: String outputPath
run: function(runTaskOptions: ServeTaskOptions) {
run: function (runTaskOptions: ServeTaskOptions) {

var project = this.cliProject;

rimraf.sync(path.resolve(project.root, runTaskOptions.outputPath));
var config = new NgCliWebpackConfig(project, runTaskOptions.target, runTaskOptions.environment).config;
var config = new NgCliWebpackConfig(project, runTaskOptions.target, runTaskOptions.environment, runTaskOptions.baseHref).config;
const webpackCompiler = webpack(config);

const ProgressPlugin = require('webpack/lib/ProgressPlugin');
Expand Down
2 changes: 1 addition & 1 deletion addon/ng2/tasks/serve-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = Task.extend({
let lastHash = null;
let webpackCompiler: any;

var config: NgCliWebpackConfig = new NgCliWebpackConfig(this.project, commandOptions.target, commandOptions.environment).config;
var config: NgCliWebpackConfig = new NgCliWebpackConfig(this.project, commandOptions.target, commandOptions.environment, commandOptions.baseHref).config;
// This allows for live reload of page when changes are made to repo.
// https://webpack.github.io/docs/webpack-dev-server.html#inline-mode
config.entry.main.unshift(`webpack-dev-server/client?http://${commandOptions.host}:${commandOptions.port}/`);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/webpack": "^1.12.22-alpha",
"angular2-template-loader": "^0.4.0",
"awesome-typescript-loader": "^2.1.1",
"base-href-webpack-plugin": "^1.0.0",
"chalk": "^1.1.3",
"compression-webpack-plugin": "^0.3.1",
"copy-webpack-plugin": "^3.0.1",
Expand Down

0 comments on commit 9ab6587

Please sign in to comment.