diff --git a/README.md b/README.md index 7ccff29a..72c0e825 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ _Vue-like_ support for defining your markup between a specific tag. The default ``` +> Note: using a relative path starting with `.` is important. Otherwise `svelte-preprocess` will ignore the `src` attribute. + ### Global style #### `global` attribute diff --git a/src/modules/utils.ts b/src/modules/utils.ts index c957f29c..c6fdd7a8 100644 --- a/src/modules/utils.ts +++ b/src/modules/utils.ts @@ -60,15 +60,8 @@ export async function hasDepInstalled(dep: string) { return (depCheckCache[dep] = result); } -const REMOTE_SRC_PATTERN = /^(https?:)?\/\//; - export function isValidLocalPath(path: string) { - return ( - path.match(REMOTE_SRC_PATTERN) == null && - // only literal strings allowed - !path.startsWith('{') && - !path.endsWith('}') - ); + return path.startsWith('.') && !path.startsWith('{') && !path.endsWith('}'); } // finds a existing path up the tree diff --git a/test/autoProcess/externalFiles.test.ts b/test/autoProcess/externalFiles.test.ts index b3a8b3c6..d656bbd0 100644 --- a/test/autoProcess/externalFiles.test.ts +++ b/test/autoProcess/externalFiles.test.ts @@ -68,23 +68,40 @@ describe('external files', () => { expect(markup.code).toContain(getFixtureContent('template.html')); }); + it("warns if local file don't exist", async () => { + const input = ``; + + await preprocess(input, sveltePreprocess()); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('was not found'), + ); + }); + REMOTE_JS.forEach((url) => { - it(`should not attempt to locally resolve ${url}`, async () => { + it(`ignores remote paths ${url}`, async () => { const input = `
`; const preprocessed = await preprocess(input, sveltePreprocess()); expect(preprocessed.toString?.()).toContain(input); expect(preprocessed.dependencies).toHaveLength(0); + expect(warnSpy).not.toHaveBeenCalledWith( + expect.stringContaining('was not found'), + ); }); }); - it("should warn if local file don't exist", async () => { - const input = ``; + it('ignores external source if path is not relative', async () => { + const input = ``; await preprocess(input, sveltePreprocess()); - expect(warnSpy).toHaveBeenCalledWith( + const preprocessed = await preprocess(input, sveltePreprocess()); + + expect(preprocessed.toString?.()).toContain(input); + expect(preprocessed.dependencies).toHaveLength(0); + expect(warnSpy).not.toHaveBeenCalledWith( expect.stringContaining('was not found'), ); });