Skip to content

Commit 80d87ed

Browse files
authoredFeb 23, 2022
fix: only try to load external files with relative paths (#487)
* fix: only try to load external files with relative paths * refactor: remove redundant check
1 parent 484b26e commit 80d87ed

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ _Vue-like_ support for defining your markup between a specific tag. The default
7474
<style src="./style.css"></style>
7575
```
7676

77+
> Note: using a relative path starting with `.` is important. Otherwise `svelte-preprocess` will ignore the `src` attribute.
78+
7779
### Global style
7880

7981
#### `global` attribute

‎src/modules/utils.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,8 @@ export async function hasDepInstalled(dep: string) {
6060
return (depCheckCache[dep] = result);
6161
}
6262

63-
const REMOTE_SRC_PATTERN = /^(https?:)?\/\//;
64-
6563
export function isValidLocalPath(path: string) {
66-
return (
67-
path.match(REMOTE_SRC_PATTERN) == null &&
68-
// only literal strings allowed
69-
!path.startsWith('{') &&
70-
!path.endsWith('}')
71-
);
64+
return path.startsWith('.');
7265
}
7366

7467
// finds a existing path up the tree

‎test/autoProcess/externalFiles.test.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,40 @@ describe('external files', () => {
6868
expect(markup.code).toContain(getFixtureContent('template.html'));
6969
});
7070

71+
it("warns if local file don't exist", async () => {
72+
const input = `<style src="./missing-potato"></style>`;
73+
74+
await preprocess(input, sveltePreprocess());
75+
76+
expect(warnSpy).toHaveBeenCalledWith(
77+
expect.stringContaining('was not found'),
78+
);
79+
});
80+
7181
REMOTE_JS.forEach((url) => {
72-
it(`should not attempt to locally resolve ${url}`, async () => {
82+
it(`ignores remote path "${url}"`, async () => {
7383
const input = `<div></div><script src="${url}"></script>`;
7484

7585
const preprocessed = await preprocess(input, sveltePreprocess());
7686

7787
expect(preprocessed.toString?.()).toContain(input);
7888
expect(preprocessed.dependencies).toHaveLength(0);
89+
expect(warnSpy).not.toHaveBeenCalledWith(
90+
expect.stringContaining('was not found'),
91+
);
7992
});
8093
});
8194

82-
it("should warn if local file don't exist", async () => {
83-
const input = `<style src="./missing-potato"></style>`;
95+
it('ignores external source if path is not relative', async () => {
96+
const input = `<style src="/root-potato"></style>`;
8497

8598
await preprocess(input, sveltePreprocess());
8699

87-
expect(warnSpy).toHaveBeenCalledWith(
100+
const preprocessed = await preprocess(input, sveltePreprocess());
101+
102+
expect(preprocessed.toString?.()).toContain(input);
103+
expect(preprocessed.dependencies).toHaveLength(0);
104+
expect(warnSpy).not.toHaveBeenCalledWith(
88105
expect.stringContaining('was not found'),
89106
);
90107
});

0 commit comments

Comments
 (0)
Please sign in to comment.