Skip to content

Commit 9d8e702

Browse files
committed
fix: support custom rootURL in base page writer
1 parent 400ab5f commit 9d8e702

File tree

2 files changed

+131
-51
lines changed

2 files changed

+131
-51
lines changed

packages/ember-cli-fastboot/lib/broccoli/base-page-writer.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ module.exports = class BasePageWriter extends Filter {
8080
return _manifest;
8181
};
8282

83-
function stripLeadingSlash(filePath) {
84-
return filePath.replace(/^\//, '');
85-
}
86-
8783
let appFilePath = stripLeadingSlash(this._appJsPath);
8884
let appFastbootFilePath = appFilePath.replace(/\.js$/, '') + '-fastboot.js';
8985
let vendorFilePath = stripLeadingSlash(this._vendorJsPath);
@@ -127,7 +123,7 @@ module.exports = class BasePageWriter extends Filter {
127123
this._ignoreUnexpectedScripts(scriptTags);
128124

129125
let fastbootScripts = this._findFastbootScriptToInsert(scriptTags);
130-
let appJsTag = findAppJsTag(scriptTags, this._appJsPath, this._rootURL);
126+
let appJsTag = findAppJsTag(scriptTags, this._appJsPath);
131127
if (!appJsTag) {
132128
throw new Error(
133129
'ember-cli-fastboot cannot find own app script tag, please check your html file'
@@ -165,10 +161,6 @@ module.exports = class BasePageWriter extends Filter {
165161
};
166162

167163
function expectedFiles(appJsPath, vendorJsPath) {
168-
function stripLeadingSlash(filePath) {
169-
return filePath.replace(/^\//, '');
170-
}
171-
172164
let appFilePath = stripLeadingSlash(appJsPath);
173165
let appFastbootFilePath = appFilePath.replace(/\.js$/, '') + '-fastboot.js';
174166
let vendorFilePath = stripLeadingSlash(vendorJsPath);
@@ -195,10 +187,11 @@ function urlWithin(candidate, root) {
195187
}
196188
}
197189

198-
function findAppJsTag(scriptTags, appJsPath, rootURL) {
199-
appJsPath = urlWithin(appJsPath, rootURL);
190+
function findAppJsTag(scriptTags, appJsPath) {
191+
appJsPath = stripLeadingSlash(appJsPath);
200192
for (let e of scriptTags) {
201-
if (urlWithin(e.getAttribute('src'), rootURL) === appJsPath) {
193+
let src = e.getAttribute('src');
194+
if (src.includes(appJsPath)) {
202195
return e;
203196
}
204197
}
@@ -242,3 +235,7 @@ class NodeRange {
242235
this.end.parentElement.insertBefore(node, this.end);
243236
}
244237
}
238+
239+
function stripLeadingSlash(filePath) {
240+
return filePath.replace(/^\//, '');
241+
}

packages/ember-cli-fastboot/test/base-page-writer-test.js

Lines changed: 122 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ describe('BasePageWriter', function () {
1212
let output;
1313
let subject;
1414
let project;
15-
16-
beforeEach(async function () {
17-
input = await createTempDir();
18-
project = {
19-
addons: [],
20-
pkg: {},
21-
};
22-
subject = new BasePageWriter(input.path(), {
15+
function getOptions(overrides = {}) {
16+
return {
2317
project,
2418
appConfig: {
2519
modulePrefix: 'basic-app',
2620
environment: 'development',
27-
rootURL: '/',
21+
rootURL: overrides.rootURL || '/',
2822
locationType: 'auto',
2923
EmberENV: {
3024
FEATURES: {},
@@ -73,37 +67,15 @@ describe('BasePageWriter', function () {
7367
},
7468
},
7569
},
76-
});
77-
output = createBuilder(subject);
78-
input.write({
79-
'index.html': `<!DOCTYPE html>
80-
<html>
81-
<head>
82-
<meta charset="utf-8">
83-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
84-
<title>BasicApp</title>
85-
<meta name="description" content="">
86-
<meta name="viewport" content="width=device-width, initial-scale=1">
87-
88-
89-
<meta name="basic-app/config/environment" content="%7B%22modulePrefix%22%3A%22basic-app%22%2C%22environment%22%3A%22development%22%2C%22rootURL%22%3A%22%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_APPLICATION_TEMPLATE_WRAPPER%22%3Afalse%2C%22_DEFAULT_ASYNC_OBSERVERS%22%3Atrue%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22basic-app%22%2C%22version%22%3A%220.1.0%2Baec2e45f%22%7D%2C%22fastboot%22%3A%7B%22hostWhitelist%22%3A%5B%22example.com%22%2C%22subdomain.example.com%22%2C%22%2Flocalhost%3A%5C%5Cd%2B%2F%22%5D%7D%2C%22exportApplicationGlobal%22%3Atrue%7D" />
90-
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
91-
92-
<link integrity="" rel="stylesheet" href="/assets/vendor.css">
93-
<link integrity="" rel="stylesheet" href="/assets/basic-app.css">
94-
95-
96-
</head>
97-
<body>
98-
<!-- EMBER_CLI_FASTBOOT_BODY -->
99-
100-
<script src="/assets/vendor.js"></script>
101-
<script src="/assets/basic-app.js"></script>
102-
70+
};
71+
}
10372

104-
</body>
105-
</html>`,
106-
});
73+
beforeEach(async function () {
74+
input = await createTempDir();
75+
project = {
76+
addons: [],
77+
pkg: {},
78+
};
10779
});
10880

10981
afterEach(async function () {
@@ -112,6 +84,11 @@ describe('BasePageWriter', function () {
11284
});
11385

11486
it('it writes correct base page HTML', async function () {
87+
input.write({
88+
'index.html': INPUT_HTML,
89+
});
90+
subject = new BasePageWriter(input.path(), getOptions());
91+
output = createBuilder(subject);
11592
await output.build();
11693

11794
expect(output.read()['index.html'].replace(/(^[ \t]*\n)/gm, '')).to.equal(
@@ -145,6 +122,11 @@ describe('BasePageWriter', function () {
145122
});
146123

147124
it('it writes addon fastboot config to base page', async function () {
125+
input.write({
126+
'index.html': INPUT_HTML,
127+
});
128+
subject = new BasePageWriter(input.path(), getOptions());
129+
output = createBuilder(subject);
148130
project.addons.push({
149131
name: 'example-addon',
150132
updateFastBootManifest: function (manifest) {
@@ -195,7 +177,108 @@ describe('BasePageWriter', function () {
195177
<fastboot-script src="example-addon/bar.js"></fastboot-script>
196178
<script src="/assets/basic-app.js"></script>
197179
180+
</body></html>`.replace(/(^[ \t]*\n)/gm, '')
181+
);
182+
});
183+
184+
it('it works with custom root url', async function () {
185+
input.write({
186+
'index.html': INPUT_CUSTOM_ROOT_HTML,
187+
});
188+
subject = new BasePageWriter(
189+
input.path(),
190+
getOptions({ rootURL: '/root-url/' })
191+
);
192+
output = createBuilder(subject);
193+
194+
await output.build();
195+
196+
expect(
197+
output.read()['index.html'].replace(/(^[ \t]*\n)/gm, ''),
198+
'example-addon config and manifest updates are written to base page'
199+
).to.equal(
200+
`<!DOCTYPE html><html><head>
201+
<meta charset="utf-8">
202+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
203+
<title>BasicApp</title>
204+
<meta name="description" content="">
205+
<meta name="viewport" content="width=device-width, initial-scale=1">
206+
207+
208+
<meta name="basic-app/config/environment" content="%7B%22modulePrefix%22%3A%22basic-app%22%2C%22environment%22%3A%22development%22%2C%22rootURL%22%3A%22%2Froot-url%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_APPLICATION_TEMPLATE_WRAPPER%22%3Afalse%2C%22_DEFAULT_ASYNC_OBSERVERS%22%3Atrue%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22basic-app%22%2C%22version%22%3A%220.1.0%2Baec2e45f%22%7D%2C%22fastboot%22%3A%7B%22hostWhitelist%22%3A%5B%22example.com%22%2C%22subdomain.example.com%22%2C%22%2Flocalhost%3A%5C%5Cd%2B%2F%22%5D%7D%2C%22exportApplicationGlobal%22%3Atrue%7D">
209+
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
210+
211+
<link integrity="" rel="stylesheet" href="/root-url/assets/vendor.css">
212+
<link integrity="" rel="stylesheet" href="/root-url/assets/basic-app.css">
213+
214+
215+
<meta name="basic-app/config/fastboot-environment" content="%7B%22modulePrefix%22%3A%22basic-app%22%2C%22environment%22%3A%22development%22%2C%22rootURL%22%3A%22%2Froot-url%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_APPLICATION_TEMPLATE_WRAPPER%22%3Afalse%2C%22_DEFAULT_ASYNC_OBSERVERS%22%3Atrue%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22basic-app%22%2C%22version%22%3A%220.1.0%2Baec2e45f%22%2C%22autoboot%22%3Afalse%7D%2C%22fastboot%22%3A%7B%22hostWhitelist%22%3A%5B%22example.com%22%2C%22subdomain.example.com%22%2C%22%2Flocalhost%3A%5C%5Cd%2B%2F%22%5D%7D%2C%22exportApplicationGlobal%22%3Atrue%7D">
216+
217+
</head>
218+
<body>
219+
<!-- EMBER_CLI_FASTBOOT_BODY -->
220+
221+
<script src="/root-url/assets/vendor.js"></script>
222+
<fastboot-script src="assets/basic-app-fastboot.js"></fastboot-script>
223+
<script src="/root-url/assets/basic-app.js"></script>
224+
198225
</body></html>`.replace(/(^[ \t]*\n)/gm, '')
199226
);
200227
});
201228
});
229+
230+
const INPUT_HTML = `<!DOCTYPE html>
231+
<html>
232+
<head>
233+
<meta charset="utf-8">
234+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
235+
<title>BasicApp</title>
236+
<meta name="description" content="">
237+
<meta name="viewport" content="width=device-width, initial-scale=1">
238+
239+
240+
<meta name="basic-app/config/environment" content="%7B%22modulePrefix%22%3A%22basic-app%22%2C%22environment%22%3A%22development%22%2C%22rootURL%22%3A%22%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_APPLICATION_TEMPLATE_WRAPPER%22%3Afalse%2C%22_DEFAULT_ASYNC_OBSERVERS%22%3Atrue%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22basic-app%22%2C%22version%22%3A%220.1.0%2Baec2e45f%22%7D%2C%22fastboot%22%3A%7B%22hostWhitelist%22%3A%5B%22example.com%22%2C%22subdomain.example.com%22%2C%22%2Flocalhost%3A%5C%5Cd%2B%2F%22%5D%7D%2C%22exportApplicationGlobal%22%3Atrue%7D" />
241+
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
242+
243+
<link integrity="" rel="stylesheet" href="/assets/vendor.css">
244+
<link integrity="" rel="stylesheet" href="/assets/basic-app.css">
245+
246+
247+
</head>
248+
<body>
249+
<!-- EMBER_CLI_FASTBOOT_BODY -->
250+
251+
<script src="/assets/vendor.js"></script>
252+
<script src="/assets/basic-app.js"></script>
253+
254+
255+
</body>
256+
</html>`;
257+
258+
const INPUT_CUSTOM_ROOT_HTML = `<!DOCTYPE html>
259+
<html>
260+
<head>
261+
<meta charset="utf-8">
262+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
263+
<title>BasicApp</title>
264+
<meta name="description" content="">
265+
<meta name="viewport" content="width=device-width, initial-scale=1">
266+
267+
268+
<meta name="basic-app/config/environment" content="%7B%22modulePrefix%22%3A%22basic-app%22%2C%22environment%22%3A%22development%22%2C%22rootURL%22%3A%22%2Froot-url%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_APPLICATION_TEMPLATE_WRAPPER%22%3Afalse%2C%22_DEFAULT_ASYNC_OBSERVERS%22%3Atrue%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22basic-app%22%2C%22version%22%3A%220.1.0%2Baec2e45f%22%7D%2C%22fastboot%22%3A%7B%22hostWhitelist%22%3A%5B%22example.com%22%2C%22subdomain.example.com%22%2C%22%2Flocalhost%3A%5C%5Cd%2B%2F%22%5D%7D%2C%22exportApplicationGlobal%22%3Atrue%7D" />
269+
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
270+
271+
<link integrity="" rel="stylesheet" href="/root-url/assets/vendor.css">
272+
<link integrity="" rel="stylesheet" href="/root-url/assets/basic-app.css">
273+
274+
275+
</head>
276+
<body>
277+
<!-- EMBER_CLI_FASTBOOT_BODY -->
278+
279+
<script src="/root-url/assets/vendor.js"></script>
280+
<script src="/root-url/assets/basic-app.js"></script>
281+
282+
283+
</body>
284+
</html>`;

0 commit comments

Comments
 (0)