Skip to content

Commit

Permalink
fix: support custom rootURL in base page writer
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Aug 22, 2021
1 parent 400ab5f commit 9d8e702
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 51 deletions.
21 changes: 9 additions & 12 deletions packages/ember-cli-fastboot/lib/broccoli/base-page-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ module.exports = class BasePageWriter extends Filter {
return _manifest;
};

function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}

let appFilePath = stripLeadingSlash(this._appJsPath);
let appFastbootFilePath = appFilePath.replace(/\.js$/, '') + '-fastboot.js';
let vendorFilePath = stripLeadingSlash(this._vendorJsPath);
Expand Down Expand Up @@ -127,7 +123,7 @@ module.exports = class BasePageWriter extends Filter {
this._ignoreUnexpectedScripts(scriptTags);

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

function expectedFiles(appJsPath, vendorJsPath) {
function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}

let appFilePath = stripLeadingSlash(appJsPath);
let appFastbootFilePath = appFilePath.replace(/\.js$/, '') + '-fastboot.js';
let vendorFilePath = stripLeadingSlash(vendorJsPath);
Expand All @@ -195,10 +187,11 @@ function urlWithin(candidate, root) {
}
}

function findAppJsTag(scriptTags, appJsPath, rootURL) {
appJsPath = urlWithin(appJsPath, rootURL);
function findAppJsTag(scriptTags, appJsPath) {
appJsPath = stripLeadingSlash(appJsPath);
for (let e of scriptTags) {
if (urlWithin(e.getAttribute('src'), rootURL) === appJsPath) {
let src = e.getAttribute('src');
if (src.includes(appJsPath)) {
return e;
}
}
Expand Down Expand Up @@ -242,3 +235,7 @@ class NodeRange {
this.end.parentElement.insertBefore(node, this.end);
}
}

function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}
161 changes: 122 additions & 39 deletions packages/ember-cli-fastboot/test/base-page-writer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ describe('BasePageWriter', function () {
let output;
let subject;
let project;

beforeEach(async function () {
input = await createTempDir();
project = {
addons: [],
pkg: {},
};
subject = new BasePageWriter(input.path(), {
function getOptions(overrides = {}) {
return {
project,
appConfig: {
modulePrefix: 'basic-app',
environment: 'development',
rootURL: '/',
rootURL: overrides.rootURL || '/',
locationType: 'auto',
EmberENV: {
FEATURES: {},
Expand Down Expand Up @@ -73,37 +67,15 @@ describe('BasePageWriter', function () {
},
},
},
});
output = createBuilder(subject);
input.write({
'index.html': `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>BasicApp</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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" />
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
<link integrity="" rel="stylesheet" href="/assets/vendor.css">
<link integrity="" rel="stylesheet" href="/assets/basic-app.css">
</head>
<body>
<!-- EMBER_CLI_FASTBOOT_BODY -->
<script src="/assets/vendor.js"></script>
<script src="/assets/basic-app.js"></script>
};
}

</body>
</html>`,
});
beforeEach(async function () {
input = await createTempDir();
project = {
addons: [],
pkg: {},
};
});

afterEach(async function () {
Expand All @@ -112,6 +84,11 @@ describe('BasePageWriter', function () {
});

it('it writes correct base page HTML', async function () {
input.write({
'index.html': INPUT_HTML,
});
subject = new BasePageWriter(input.path(), getOptions());
output = createBuilder(subject);
await output.build();

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

it('it writes addon fastboot config to base page', async function () {
input.write({
'index.html': INPUT_HTML,
});
subject = new BasePageWriter(input.path(), getOptions());
output = createBuilder(subject);
project.addons.push({
name: 'example-addon',
updateFastBootManifest: function (manifest) {
Expand Down Expand Up @@ -195,7 +177,108 @@ describe('BasePageWriter', function () {
<fastboot-script src="example-addon/bar.js"></fastboot-script>
<script src="/assets/basic-app.js"></script>
</body></html>`.replace(/(^[ \t]*\n)/gm, '')
);
});

it('it works with custom root url', async function () {
input.write({
'index.html': INPUT_CUSTOM_ROOT_HTML,
});
subject = new BasePageWriter(
input.path(),
getOptions({ rootURL: '/root-url/' })
);
output = createBuilder(subject);

await output.build();

expect(
output.read()['index.html'].replace(/(^[ \t]*\n)/gm, ''),
'example-addon config and manifest updates are written to base page'
).to.equal(
`<!DOCTYPE html><html><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>BasicApp</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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">
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
<link integrity="" rel="stylesheet" href="/root-url/assets/vendor.css">
<link integrity="" rel="stylesheet" href="/root-url/assets/basic-app.css">
<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">
</head>
<body>
<!-- EMBER_CLI_FASTBOOT_BODY -->
<script src="/root-url/assets/vendor.js"></script>
<fastboot-script src="assets/basic-app-fastboot.js"></fastboot-script>
<script src="/root-url/assets/basic-app.js"></script>
</body></html>`.replace(/(^[ \t]*\n)/gm, '')
);
});
});

const INPUT_HTML = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>BasicApp</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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" />
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
<link integrity="" rel="stylesheet" href="/assets/vendor.css">
<link integrity="" rel="stylesheet" href="/assets/basic-app.css">
</head>
<body>
<!-- EMBER_CLI_FASTBOOT_BODY -->
<script src="/assets/vendor.js"></script>
<script src="/assets/basic-app.js"></script>
</body>
</html>`;

const INPUT_CUSTOM_ROOT_HTML = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>BasicApp</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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" />
<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->
<link integrity="" rel="stylesheet" href="/root-url/assets/vendor.css">
<link integrity="" rel="stylesheet" href="/root-url/assets/basic-app.css">
</head>
<body>
<!-- EMBER_CLI_FASTBOOT_BODY -->
<script src="/root-url/assets/vendor.js"></script>
<script src="/root-url/assets/basic-app.js"></script>
</body>
</html>`;

0 comments on commit 9d8e702

Please sign in to comment.