Skip to content

Commit e71157c

Browse files
committed
fix: πŸ› prevent trying to resolve interpolated src values
βœ… Closes: #226
1 parent e913dd8 commit e71157c

File tree

7 files changed

+36
-19
lines changed

7 files changed

+36
-19
lines changed

β€Ž.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
### Tests
88

9-
- [ ] Run the tests tests with `npm test` or `yarn test`
9+
- [ ] Run the tests with `npm test` or `yarn test`

β€Žsrc/autoProcess.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type AutoPreprocessOptions = {
5555
[languageName: string]: TransformerOptions;
5656
};
5757

58-
export const runTransformer = async (
58+
export const transform = async (
5959
name: string,
6060
options: TransformerOptions,
6161
{ content, map, filename, attributes }: TransformerArgs<any>,
@@ -68,6 +68,7 @@ export const runTransformer = async (
6868
return options({ content, map, filename, attributes });
6969
}
7070

71+
// todo: maybe add a try-catch here looking for module-not-found errors
7172
const { transformer } = await import(`./transformers/${name}`);
7273

7374
return transformer({
@@ -166,7 +167,7 @@ export function sveltePreprocess(
166167
return { code: content, dependencies };
167168
}
168169

169-
const transformed = await runTransformer(lang, transformerOptions, {
170+
const transformed = await transform(lang, transformerOptions, {
170171
content,
171172
filename,
172173
attributes,
@@ -184,11 +185,10 @@ export function sveltePreprocess(
184185

185186
const markup: PreprocessorGroup['markup'] = async ({ content, filename }) => {
186187
if (transformers.replace) {
187-
const transformed = await runTransformer(
188-
'replace',
189-
transformers.replace,
190-
{ content, filename },
191-
);
188+
const transformed = await transform('replace', transformers.replace, {
189+
content,
190+
filename,
191+
});
192192

193193
content = transformed.code;
194194
}
@@ -214,7 +214,7 @@ export function sveltePreprocess(
214214
let { code, map, dependencies, diagnostics } = transformResult;
215215

216216
if (transformers.babel) {
217-
const transformed = await runTransformer(
217+
const transformed = await transform(
218218
'babel',
219219
getTransformerOptions('babel'),
220220
{
@@ -250,7 +250,7 @@ export function sveltePreprocess(
250250
// istanbul ignore else
251251
if (await hasDepInstalled('postcss')) {
252252
if (transformers.postcss) {
253-
const transformed = await runTransformer(
253+
const transformed = await transform(
254254
'postcss',
255255
getTransformerOptions('postcss'),
256256
{ content: code, map, filename, attributes },
@@ -261,7 +261,7 @@ export function sveltePreprocess(
261261
dependencies = concat(dependencies, transformed.dependencies);
262262
}
263263

264-
const transformed = await runTransformer(
264+
const transformed = await transform(
265265
'globalStyle',
266266
getTransformerOptions('globalStyle'),
267267
{ content: code, map, filename, attributes },

β€Žsrc/modules/language.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { basename } from 'path';
22

33
import { PreprocessorArgs } from '../types';
4+
import { isValidLocalPath } from './utils';
45

56
export const LANG_SPECIFIC_OPTIONS: Record<string, any> = {
67
sass: {
@@ -63,12 +64,10 @@ export const getLanguage = (attributes: PreprocessorArgs['attributes']) => {
6364
}
6465

6566
alias = attributes.type.replace(/^(text|application)\/(.*)$/, '$2');
66-
} else if (attributes.src) {
67-
// istanbul ignore if
68-
if (typeof attributes.src !== 'string') {
69-
throw new Error('src attribute must be string');
70-
}
71-
67+
} else if (
68+
typeof attributes.src === 'string' &&
69+
isValidLocalPath(attributes.src)
70+
) {
7271
const parts = basename(attributes.src).split('.');
7372

7473
if (parts.length > 1) {

β€Žsrc/modules/tagInfo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { resolve, dirname } from 'path';
33

44
import { PreprocessorArgs } from '../types';
55
import { getLanguage } from './language';
6+
import { isValidLocalPath } from './utils';
67

78
const resolveSrc = (importerFile: string, srcPath: string) =>
89
resolve(dirname(importerFile), srcPath);
@@ -40,7 +41,7 @@ export const getTagInfo = async ({
4041
let path = attributes.src;
4142

4243
/** Only try to get local files (path starts with ./ or ../) */
43-
if (path.match(/^(https?:)?\/\//) == null) {
44+
if (isValidLocalPath(path)) {
4445
path = resolveSrc(filename, path);
4546
if (await doesFileExist(path)) {
4647
content = await getSrcContent(path);

β€Žsrc/modules/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ export async function hasDepInstalled(dep: string) {
5656

5757
return (cachedResult[dep] = result);
5858
}
59+
60+
const REMOTE_SRC_PATTERN = /^(https?:)?\/\//;
61+
62+
export function isValidLocalPath(path: string) {
63+
return (
64+
path.match(REMOTE_SRC_PATTERN) == null &&
65+
// only literal strings allowed
66+
!path.startsWith('{') &&
67+
!path.endsWith('}')
68+
);
69+
}

β€Žtest/autoProcess/autoProcess.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ describe('detect - mimetype', () => {
1818
lang: 'customLanguage',
1919
targetLanguage: 'customLanguage',
2020
},
21+
{
22+
src: '{_potato("foo")}',
23+
targetLanguage: null,
24+
},
2125
];
2226

2327
MIMETYPES.forEach(({ type, lang, src, targetLanguage }) => {

β€Žtest/autoProcess/externalFiles.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
spyConsole,
99
} from '../utils';
1010

11-
const { warnSpy } = spyConsole();
11+
const { warnSpy } = spyConsole({ silent: true });
1212

1313
const {
1414
markup: markupProcessor,
@@ -23,6 +23,8 @@ const REMOTE_JS = [
2323
];
2424

2525
describe('external files', () => {
26+
afterEach(warnSpy.mockClear);
27+
2628
it('should insert external file content and add as deps', async () => {
2729
const [markup, script, style] = [
2830
await markupProcessor({

0 commit comments

Comments
Β (0)