Skip to content

Commit

Permalink
refactor: use static as the asset dir name (#1586)
Browse files Browse the repository at this point in the history
* refactor: use static as the asset dir name

* chore: update unleash-frontend

* refactor: use the real index.html in tests
  • Loading branch information
olav committed May 10, 2022
1 parent 3b46bfb commit 3bea8a9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"stoppable": "^1.1.0",
"type-is": "^1.6.18",
"@unleash/express-openapi": "^0.2.0",
"unleash-frontend": "4.11.0-beta.0",
"unleash-frontend": "4.11.0-beta.1",
"uuid": "^8.3.2",
"semver": "^7.3.5"
},
Expand Down
37 changes: 7 additions & 30 deletions src/lib/util/rewriteHTML.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import fs from 'fs';
import path from 'path';
import { rewriteHTML } from './rewriteHTML';
import { publicFolder } from 'unleash-frontend';

const input = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="::faviconPrefix::/favicon.ico" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="baseUriPath" content="::baseUriPath::" />
<meta name="cdnPrefix" content="::cdnPrefix::" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="unleash" />
<title>Unleash</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Sen:wght@400;700;800&display=swap"
rel="stylesheet"
/>
<script type="module" crossorigin src="/assets/index.556ac563.js"></script>
<link rel="stylesheet" href="/assets/index.4b6b260a.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
`;
const input = fs.readFileSync(path.join(publicFolder, 'index.html')).toString();

test('rewriteHTML substitutes meta tag with existing rewrite value', () => {
const result = rewriteHTML(input, '/hosted');
Expand All @@ -46,23 +23,23 @@ test('rewriteHTML substitutes asset paths correctly with baseUriPath', () => {
const result = rewriteHTML(input, '/hosted');
expect(
result.includes(
'<script type="module" crossorigin src="/hosted/assets/index',
'<script type="module" crossorigin src="/hosted/static/index',
),
).toBe(true);
});

test('rewriteHTML substitutes asset paths correctly without baseUriPath', () => {
const result = rewriteHTML(input, '');
expect(
result.includes('<script type="module" crossorigin src="/assets/index'),
result.includes('<script type="module" crossorigin src="/static/index'),
).toBe(true);
});

test('rewriteHTML substitutes asset paths correctly with cdnPrefix', () => {
const result = rewriteHTML(input, '', 'https://cdn.getunleash.io/v4.1.0');
expect(
result.includes(
'<script type="module" crossorigin src="https://cdn.getunleash.io/v4.1.0/assets/index',
'<script type="module" crossorigin src="https://cdn.getunleash.io/v4.1.0/static/index',
),
).toBe(true);
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/util/rewriteHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const rewriteHTML = (
result = result.replace(/::faviconPrefix::/gi, faviconPrefix);

result = result.replace(
/\/assets/gi,
`${cdnPrefix || rewriteValue}/assets`,
/\/static/gi,
`${cdnPrefix || rewriteValue}/static`,
);

return result;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7466,10 +7466,10 @@ universalify@^2.0.0:
resolved "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz"
integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==

unleash-frontend@4.11.0-beta.0:
version "4.11.0-beta.0"
resolved "https://registry.yarnpkg.com/unleash-frontend/-/unleash-frontend-4.11.0-beta.0.tgz#1e48bf66f15a4999c8efab19d214a975858ede7d"
integrity sha512-yNKzjGXUXPZgp+ihGdYphiDgeOzxN2mAslJnvFnGh15c7B2MBYeflf1DaHihcdwJTxhJsEucxE6cRucH/2DQOA==
unleash-frontend@4.11.0-beta.1:
version "4.11.0-beta.1"
resolved "https://registry.yarnpkg.com/unleash-frontend/-/unleash-frontend-4.11.0-beta.1.tgz#b93245488b5d8a143c8e17728e27b6005b917754"
integrity sha512-CSTG+mJyxxlNEBlGPzRxP8ExwbtFjE9IyB1JGV3X7YpsWXQDOxspB2/PZLUL0yAZhUrtbcg2I2hHhbp8ehVdCQ==

unpipe@1.0.0, unpipe@~1.0.0:
version "1.0.0"
Expand Down

2 comments on commit 3bea8a9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 91.28% 5205/5702
🟡 Branches 79.74% 811/1017
🟢 Functions 86.29% 1221/1415
🟢 Lines 91.21% 5083/5573

Test suite run success

866 tests passing in 124 suites.

Report generated by 🧪jest coverage report action from d88ad20

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 91.28% 5205/5702
🟡 Branches 79.74% 811/1017
🟢 Functions 86.29% 1221/1415
🟢 Lines 91.21% 5083/5573

Test suite run success

866 tests passing in 124 suites.

Report generated by 🧪jest coverage report action from 2f698fb

Please sign in to comment.