Skip to content

Commit 1e1ef93

Browse files
committed
refactor(github-pages-deploy): reuse webpack build --base-href logic
Reuse basehref handling logic from webpack build task
1 parent e6dc4c9 commit 1e1ef93

File tree

8 files changed

+59
-61
lines changed

8 files changed

+59
-61
lines changed

addon/ng2/commands/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Command from 'ember-cli/lib/models/command';
22
import * as WebpackBuild from '../tasks/build-webpack';
33
import * as WebpackBuildWatch from '../tasks/build-webpack-watch';
44

5-
interface BuildOptions {
5+
export interface BuildOptions {
66
target?: string;
77
environment?: string;
88
outputPath?: string;

addon/ng2/commands/github-pages-deploy.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import * as CreateGithubRepo from '../tasks/create-github-repo';
1111
import { CliConfig } from '../models/config';
1212
import { oneLine } from 'common-tags';
1313

14-
const fsReadFile = Promise.denodeify(fs.readFile);
15-
const fsWriteFile = Promise.denodeify(fs.writeFile);
1614
const fsReadDir = Promise.denodeify(fs.readdir);
1715
const fsCopy = Promise.denodeify(fse.copy);
1816

@@ -115,11 +113,19 @@ module.exports = Command.extend({
115113
outputPath: outDir
116114
});
117115

116+
/**
117+
* BaseHref tag setting logic:
118+
* First, use --base-href flag value if provided.
119+
* Else if --user-page is true, then keep baseHref default as declared in index.html.
120+
* Otherwise auto-replace with `/${projectName}/`.
121+
*/
122+
const baseHref = options.baseHref || (options.userPage ? null : `/${projectName}/`);
123+
118124
const buildOptions = {
119125
target: options.target,
120126
environment: options.environment,
121127
outputPath: outDir,
122-
baseHref: options.baseHref,
128+
baseHref: baseHref,
123129
};
124130

125131
const createGithubRepoTask = new CreateGithubRepo({
@@ -140,7 +146,7 @@ module.exports = Command.extend({
140146
.then(createGitHubRepoIfNeeded)
141147
.then(checkoutGhPages)
142148
.then(copyFiles)
143-
.then(updateBaseHref)
149+
.then(createNotFoundPage)
144150
.then(addAndCommit)
145151
.then(returnStartingBranch)
146152
.then(pushToGitRepo)
@@ -208,15 +214,10 @@ module.exports = Command.extend({
208214
})));
209215
}
210216

211-
function updateBaseHref() {
212-
if (options.userPage) { return Promise.resolve(); }
213-
let indexHtml = path.join(root, 'index.html');
214-
return fsReadFile(indexHtml, 'utf8')
215-
.then((data) => data.replace(/<base href="\/">/g, `<base href="/${projectName}/">`))
216-
.then((data) => {
217-
fsWriteFile(indexHtml, data, 'utf8');
218-
fsWriteFile(path.join(root, '404.html'), data, 'utf8');
219-
});
217+
function createNotFoundPage() {
218+
const indexHtml = path.join(root, 'index.html');
219+
const notFoundPage = path.join(root, '404.html');
220+
return fsCopy(indexHtml, notFoundPage);
220221
}
221222

222223
function addAndCommit() {

addon/ng2/models/webpack-build-common.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import * as atl from 'awesome-typescript-loader';
77
import { findLazyModules } from './find-lazy-modules';
88
import { BaseHrefWebpackPlugin } from '../utilities/base-href-webpack-plugin';
99

10-
export function getWebpackCommonConfig(projectRoot: string, environment: string, appConfig: any, baseHref: string) {
10+
export function getWebpackCommonConfig(
11+
projectRoot: string,
12+
environment: string,
13+
appConfig: any,
14+
baseHref: string
15+
) {
1116

1217
const appRoot = path.resolve(projectRoot, appConfig.root);
1318
const appMain = path.resolve(appRoot, appConfig.main);

addon/ng2/models/webpack-config.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export class NgCliWebpackConfig {
2828

2929
appConfig.outDir = outputDir || appConfig.outDir;
3030

31-
this.baseConfig = getWebpackCommonConfig(this.ngCliProject.root, environment, appConfig, baseHref);
31+
this.baseConfig = getWebpackCommonConfig(
32+
this.ngCliProject.root,
33+
environment,
34+
appConfig,
35+
baseHref
36+
);
3237
this.devConfigPartial = getWebpackDevConfigPartial(this.ngCliProject.root, appConfig);
3338
this.prodConfigPartial = getWebpackProdConfigPartial(this.ngCliProject.root, appConfig);
3439

addon/ng2/tasks/build-webpack-watch.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import * as webpack from 'webpack';
55
import * as ProgressPlugin from 'webpack/lib/ProgressPlugin';
66
import { NgCliWebpackConfig } from '../models/webpack-config';
77
import { webpackOutputOptions } from '../models/';
8-
import { ServeTaskOptions } from '../commands/serve';
8+
import { BuildOptions } from '../commands/build';
99

1010
let lastHash: any = null;
1111

1212
module.exports = Task.extend({
13-
run: function(runTaskOptions: ServeTaskOptions) {
13+
run: function(runTaskOptions: BuildOptions) {
1414

1515
const project = this.cliProject;
1616

addon/ng2/tasks/build-webpack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as rimraf from 'rimraf';
22
import * as path from 'path';
33
import * as Task from 'ember-cli/lib/models/task';
44
import * as webpack from 'webpack';
5-
import { ServeTaskOptions } from '../commands/serve';
5+
import { BuildOptions } from '../commands/build';
66
import { NgCliWebpackConfig } from '../models/webpack-config';
77
import { webpackOutputOptions } from '../models/';
88

@@ -11,7 +11,7 @@ let lastHash: any = null;
1111

1212
module.exports = Task.extend({
1313
// Options: String outputPath
14-
run: function (runTaskOptions: ServeTaskOptions) {
14+
run: function (runTaskOptions: BuildOptions) {
1515

1616
const project = this.cliProject;
1717

addon/ng2/utilities/base-href-webpack-plugin.ts

+24-15
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,30 @@ export class BaseHrefWebpackPlugin {
1212
}
1313

1414
compiler.plugin('compilation', (compilation) => {
15-
compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, callback) => {
16-
// Check if base tag already exists
17-
const baseTagRegex = /<base.*?>/i;
18-
const baseTagMatches = htmlPluginData.html.match(baseTagRegex);
19-
if (!baseTagMatches) {
20-
// Insert it in top of the head if not exist
21-
htmlPluginData.html = htmlPluginData.html.replace(/<head>/i, '$&' + `<base href="${this.options.baseHref}">`);
22-
} else {
23-
// Replace only href attribute if exists
24-
const modifiedBaseTag = baseTagMatches[0].replace(/href="\S+"/i, `href="${this.options.baseHref}"`);
25-
htmlPluginData.html = htmlPluginData.html.replace(baseTagRegex, modifiedBaseTag);
26-
}
15+
compilation.plugin(
16+
'html-webpack-plugin-before-html-processing',
17+
(htmlPluginData, callback) => {
18+
// Check if base tag already exists
19+
const baseTagRegex = /<base.*?>/i;
20+
const baseTagMatches = htmlPluginData.html.match(baseTagRegex);
21+
if (!baseTagMatches) {
22+
// Insert it in top of the head if not exist
23+
htmlPluginData.html = htmlPluginData.html.replace(
24+
/<head>/i, '$&' + `<base href="${this.options.baseHref}">`
25+
);
26+
} else {
27+
// Replace only href attribute if exists
28+
const modifiedBaseTag = baseTagMatches[0].replace(
29+
/href="\S+"/i, `href="${this.options.baseHref}"`
30+
);
31+
htmlPluginData.html = htmlPluginData.html.replace(baseTagRegex, modifiedBaseTag);
32+
}
2733

28-
callback(null, htmlPluginData);
29-
});
34+
callback(null, htmlPluginData);
35+
}
36+
);
3037
});
38+
39+
3140
}
32-
}
41+
}

tests/acceptance/github-pages-deploy.spec.js

+4-26
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ var https = require('https');
1414
var SilentError = require('silent-error');
1515

1616
const expect = chai.expect;
17-
const fsReadFile = Promise.denodeify(fs.readFile);
1817
const fsWriteFile = Promise.denodeify(fs.writeFile);
1918
const fsMkdir = Promise.denodeify(fs.mkdir);
2019

@@ -67,7 +66,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
6766
});
6867
});
6968

70-
it('should deploy with defaults to existing remote', function() {
69+
it('should deploy with defaults to existing remote', function () {
7170
execStub.addExecSuccess('git status --porcelain')
7271
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
7372
.addExecSuccess('git remote -v', remote)
@@ -78,12 +77,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
7877
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
7978
.addExecSuccess('git remote -v', remote);
8079

81-
return ng(['github-pages:deploy', '--skip-build'])
82-
.then(() => {
83-
let indexHtml = path.join(process.cwd(), 'index.html');
84-
return fsReadFile(indexHtml, 'utf8');
85-
})
86-
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
80+
return ng(['github-pages:deploy', '--skip-build']);
8781
});
8882

8983
it('should deploy with changed defaults', function() {
@@ -100,13 +94,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
10094
.addExecSuccess(`git push origin ${ghPagesBranch}:${userPageBranch}`)
10195
.addExecSuccess('git remote -v', remote);
10296

103-
return ng(['github-pages:deploy', '--skip-build', `--message=${message}`,
104-
'--user-page'])
105-
.then(() => {
106-
let indexHtml = path.join(process.cwd(), 'index.html');
107-
return fsReadFile(indexHtml, 'utf8');
108-
})
109-
.then((data) => expect(data.search('<base href="/">')).to.not.equal(-1));
97+
return ng(['github-pages:deploy', '--skip-build', `--message=${message}`, '--user-page']);
11098
});
11199

112100
it('should create branch if needed', function() {
@@ -125,12 +113,7 @@ describe('Acceptance: ng github-pages:deploy', function() {
125113
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
126114
.addExecSuccess('git remote -v', remote);
127115

128-
return ng(['github-pages:deploy', '--skip-build'])
129-
.then(() => {
130-
let indexHtml = path.join(process.cwd(), 'index.html');
131-
return fsReadFile(indexHtml, 'utf8');
132-
})
133-
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1));
116+
return ng(['github-pages:deploy', '--skip-build']);
134117
});
135118

136119
it('should create repo if needed', function() {
@@ -183,11 +166,6 @@ describe('Acceptance: ng github-pages:deploy', function() {
183166

184167
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`,
185168
`--gh-username=${username}`])
186-
.then(() => {
187-
let indexHtml = path.join(process.cwd(), 'index.html');
188-
return fsReadFile(indexHtml, 'utf8');
189-
})
190-
.then((data) => expect(data.search(`<base href="/${project}/">`)).to.not.equal(-1))
191169
.then(() => httpsStub.restore());
192170
});
193171

0 commit comments

Comments
 (0)