Skip to content

Commit 0ed3d6d

Browse files
committed
fix(engine): support fragments when adjusting urls
1 parent 57ec19f commit 0ed3d6d

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

.changeset/fluffy-impalas-mix.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@rocket/engine': patch
3+
---
4+
5+
Adjust urls containing url fragments
6+
7+
```html
8+
<!-- user writes -->
9+
<a href="./about.rocket.js#some-id"></a>
10+
11+
<!-- rocket outputs -->
12+
<!-- before -->
13+
<a href="./about.rocket.js#some-id"></a>
14+
<!-- after -->
15+
<a href="/about/#some-id"></a>
16+
```

packages/engine/src/transformers/AdjustAssetUrls.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,39 @@ import { parser, SaxEventType } from '../web-menu/sax-parser.js';
1919
* @returns
2020
*/
2121
async function defaultAdjustAssetUrl({
22-
url,
22+
url: fullUrl,
2323
sourceFilePath,
2424
sourceRelativeFilePath,
2525
outputFilePath,
2626
}) {
27+
let url = fullUrl;
28+
let fragment = '';
29+
if (!url.startsWith('resolve:#') && url.includes('#')) {
30+
const urlParts = url.split('#');
31+
url = urlParts[0];
32+
fragment = `#${urlParts[1]}`;
33+
}
2734
if (url.startsWith('resolve:')) {
2835
const bareImport = url.substring(8);
2936
const requireOfSource = createRequire(sourceFilePath);
3037
const resolvedPath = requireOfSource.resolve(bareImport);
3138
const rel = path.relative(path.dirname(outputFilePath), resolvedPath);
32-
return rel;
39+
return rel + fragment;
3340
}
3441
if (url.match(/^[a-z]+:/) || url.startsWith('//')) {
35-
return url;
42+
return url + fragment;
3643
}
3744
if (isRocketPageFile(url)) {
3845
const dir = path.dirname(sourceRelativeFilePath);
39-
return sourceRelativeFilePathToUrl(path.join(dir, url));
46+
return sourceRelativeFilePathToUrl(path.join(dir, url)) + fragment;
4047
}
4148
if (url.startsWith('./') || url.startsWith('../')) {
42-
return path.relative(
43-
path.dirname(outputFilePath),
44-
path.join(path.dirname(sourceFilePath), url),
49+
return (
50+
path.relative(path.dirname(outputFilePath), path.join(path.dirname(sourceFilePath), url)) +
51+
fragment
4552
);
4653
}
47-
return url;
54+
return url + fragment;
4855
}
4956

5057
export class AdjustAssetUrls {

packages/engine/test-node/08a-AdjustAssetUrls.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import chai from 'chai';
22
import { AdjustAssetUrls } from '@rocket/engine';
3+
import { expectThrowsAsync } from './test-helpers.js';
34

45
const { expect } = chai;
56

@@ -48,7 +49,7 @@ describe('AdjustAssetUrls', () => {
4849
);
4950
});
5051

51-
it('ignores <a href="#foo"></a>', async () => {
52+
it('does not adjust <a href="#foo"></a>', async () => {
5253
const adjust = new AdjustAssetUrls();
5354
expect(await adjust.transform('<a href="#foo">go</a>', options)).to.equal(
5455
'<a href="#foo">go</a>',
@@ -104,4 +105,43 @@ describe('AdjustAssetUrls', () => {
104105
}),
105106
).to.equal('<a href="/">go</a>');
106107
});
108+
109+
it('adjust <a href="./about.rocket.js#some-id"></a>', async () => {
110+
const adjust = new AdjustAssetUrls();
111+
expect(await adjust.transform('<a href="./about.rocket.js#some-id">go</a>', options)).to.equal(
112+
'<a href="/about/#some-id">go</a>',
113+
);
114+
expect(
115+
await adjust.transform('<a href="./about.rocket.js#some-id">go</a>', {
116+
sourceRelativeFilePath: 'components/index.rocket.js',
117+
outputFilePath: '/my/path/to/__output/components/index.html',
118+
}),
119+
).to.equal('<a href="/components/about/#some-id">go</a>');
120+
121+
expect(
122+
await adjust.transform('<a href="./about.rocket.js#some-id">go</a>', {
123+
sourceRelativeFilePath: 'components.rocket.js',
124+
outputFilePath: '/my/path/to/__output/components/index.html',
125+
}),
126+
).to.equal('<a href="/about/#some-id">go</a>');
127+
128+
expect(
129+
await adjust.transform('<a href="./index.rocket.js#some-id">go</a>', {
130+
sourceRelativeFilePath: 'about.rocket.js',
131+
outputFilePath: '/my/path/to/__output/about/index.html',
132+
}),
133+
).to.equal('<a href="/#some-id">go</a>');
134+
});
135+
136+
it('still resolves private imports <img src="resolve:#src/logo.svg" />', async () => {
137+
const adjust = new AdjustAssetUrls();
138+
// we check for the resolve throw as this private import does not exists =>
139+
// which means if a not resolve related error in our code happens the test fails
140+
await expectThrowsAsync(
141+
() => adjust.transform('<img src="resolve:#src/logo.svg" />', options),
142+
{
143+
errorMatch: /Cannot find module '#src\/logo\.svg'/,
144+
},
145+
);
146+
});
107147
});

packages/engine/test-node/test-helpers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ const { expect } = chai;
1313
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1414

1515
/**
16+
* Expect a throw in an async function
17+
*
18+
* @example
19+
* await expectThrowsAsync(() => myAsyncFunction(), {
20+
* errorMatch: /exact throw message/,
21+
* });
22+
* @example
23+
* await expectThrowsAsync(() => myAsyncFunction(), {
24+
* errorMatch: /check throw message with a regex/,
25+
* });
26+
*
1627
* @param {function} method
1728
* @param {string} errorMessage
1829
*/

site/pages/30--tools/30--rollup-config/10--overview.rocket.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ You write modern JavaScript using the latest browser features. Rollup will optim
8181

8282
Our config sets you up with good defaults for most projects. Additionally you can add more plugins and adjust predefined plugins or even remove them if needed.
8383

84-
We use the [plugins-manager](./plugins-manager.md) for it.
84+
We use the [plugins-manager](../10--plugins-manager/10--overview.rocket.md) for it.
8585

8686
### Customizing the Babel Config
8787

@@ -112,7 +112,7 @@ SPA and MPA plugins:
112112
- [polyfills-loader](https://modern-web.dev/docs/building/rollup-plugin-polyfills-loader/)
113113
- [workbox](https://www.npmjs.com/package/rollup-plugin-workbox)
114114

115-
You can customize options for these plugins by using [adjustPluginOptions](./plugins-manager.md#adjusting-plugin-options).
115+
You can customize options for these plugins by using [adjustPluginOptions](../10--plugins-manager/10--overview.rocket.md#adjusting-plugin-options).
116116

117117
```js
118118
import { createSpaConfig } from '@rocket/building-rollup';

0 commit comments

Comments
 (0)