Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { parse } from 'svelte/compiler';
import type { Ast } from 'svelte/types/compiler/interfaces.d';
import type { PluginOptions, PreprocessorOptions, PreprocessorResult } from './types';
import { nativeProcessor, mixedProcessor, scopedProcessor } from './processors';
import { getLocalIdent, isFileIncluded, hasModuleImports, hasModuleAttribute } from './lib';
import {
getLocalIdent,
isFileIncluded,
hasModuleImports,
hasModuleAttribute,
normalizeIncludePaths,
} from './lib';

const defaultOptions = (): PluginOptions => {
return {
Expand All @@ -21,7 +27,7 @@ const defaultOptions = (): PluginOptions => {
let pluginOptions: PluginOptions;

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

if (!isIncluded || (!pluginOptions.parseStyleTag && !pluginOptions.parseExternalStylesheet)) {
return { code: content };
Expand Down Expand Up @@ -84,6 +90,11 @@ export default module.exports = (options: Partial<PluginOptions>) => {
...defaultOptions(),
...options,
};

if (pluginOptions.includePaths) {
pluginOptions.includePaths = normalizeIncludePaths(pluginOptions.includePaths);
}

return {
markup,
};
Expand Down
34 changes: 18 additions & 16 deletions src/lib/requirement.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import path from 'path';
import type { Ast } from 'svelte/types/compiler/interfaces.d';

/**
* Normalize path by replacing potential backslashes to slashes
* @param filepath The file path to normalize
* @returns a path using forward slashes
*/
const normalizePath = (filepath: string): string =>
path.sep === '\\' ? filepath.replace(/\\/g, '/') : filepath;

/**
* Normalize all included paths
* @param paths all paths to be normalized
* @returns list of path using forward slashes
*/
export const normalizeIncludePaths = (paths: string[]): string[] =>
paths.map((includePath) => normalizePath(path.resolve(includePath)));

/**
* Check if a file requires processing
* @param includePaths List of allowd paths
* @param filename the current filename to compare with the paths
* @returns The permission status
*/
export const isFileIncluded = async (
includePaths: string[],
filename: string
): Promise<boolean> => {
export const isFileIncluded = (includePaths: string[], filename: string): boolean => {
if (includePaths.length < 1) {
return true;
}

const isIncluded: boolean = await new Promise((resolve): void => {
includePaths.forEach((includePath, index): void => {
if (filename.indexOf(path.resolve(includePath)) !== -1) {
resolve(true);
}
if (index === includePaths.length - 1) {
resolve(false);
}
});
});

return isIncluded;
return includePaths.some((includePath) => filename.startsWith(includePath));
};

/**
Expand Down