Skip to content

Commit 5aa1633

Browse files
committed
Normalize includePaths
Fixes micantoine#42 by normalizing includePaths to use forward slashes on Windows too. In addition, filenames are now expected to *start with* an includePath, not only contain it. includePaths are now resolved ahead of time to improve performance.
1 parent 7c96030 commit 5aa1633

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { parse } from 'svelte/compiler';
22
import type { Ast } from 'svelte/types/compiler/interfaces.d';
33
import type { PluginOptions, PreprocessorOptions, PreprocessorResult } from './types';
44
import { nativeProcessor, mixedProcessor, scopedProcessor } from './processors';
5-
import { getLocalIdent, isFileIncluded, hasModuleImports, hasModuleAttribute } from './lib';
5+
import {
6+
getLocalIdent,
7+
isFileIncluded,
8+
hasModuleImports,
9+
hasModuleAttribute,
10+
normalizeIncludePaths,
11+
} from './lib';
612

713
const defaultOptions = (): PluginOptions => {
814
return {
@@ -21,7 +27,7 @@ const defaultOptions = (): PluginOptions => {
2127
let pluginOptions: PluginOptions;
2228

2329
const markup = async ({ content, filename }: PreprocessorOptions): Promise<PreprocessorResult> => {
24-
const isIncluded = await isFileIncluded(pluginOptions.includePaths, filename);
30+
const isIncluded = isFileIncluded(pluginOptions.includePaths, filename);
2531

2632
if (!isIncluded || (!pluginOptions.parseStyleTag && !pluginOptions.parseExternalStylesheet)) {
2733
return { code: content };
@@ -84,6 +90,8 @@ export default module.exports = (options: Partial<PluginOptions>) => {
8490
...defaultOptions(),
8591
...options,
8692
};
93+
94+
pluginOptions.includePaths = normalizeIncludePaths(pluginOptions.includePaths);
8795
return {
8896
markup,
8997
};

src/lib/requirement.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
import path from 'path';
22
import type { Ast } from 'svelte/types/compiler/interfaces.d';
33

4+
const normalizePath = (filepath: string): string =>
5+
path.sep === '\\' ? filepath.replace(/\\/g, '/') : filepath;
6+
7+
export const normalizeIncludePaths = (paths: string[]): string[] =>
8+
paths.map((includePath) => normalizePath(path.resolve(includePath)));
9+
410
/**
511
* Check if a file requires processing
612
* @param includePaths List of allowd paths
713
* @param filename the current filename to compare with the paths
814
* @returns The permission status
915
*/
10-
export const isFileIncluded = async (
11-
includePaths: string[],
12-
filename: string
13-
): Promise<boolean> => {
16+
export const isFileIncluded = (includePaths: string[], filename: string): boolean => {
1417
if (includePaths.length < 1) {
1518
return true;
1619
}
1720

18-
const isIncluded: boolean = await new Promise((resolve): void => {
19-
includePaths.forEach((includePath, index): void => {
20-
if (filename.indexOf(path.resolve(includePath)) !== -1) {
21-
resolve(true);
22-
}
23-
if (index === includePaths.length - 1) {
24-
resolve(false);
25-
}
26-
});
27-
});
28-
29-
return isIncluded;
21+
return includePaths.some((includePath) => filename.startsWith(includePath));
3022
};
3123

3224
/**

0 commit comments

Comments
 (0)