Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync preprocess #291

Closed
wants to merge 2 commits into from
Closed
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
334 changes: 222 additions & 112 deletions src/autoProcess.ts

Large diffs are not rendered by default.

44 changes: 28 additions & 16 deletions src/modules/markup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import pipe, { EventuallyProcessed } from '../pipe';
import { Transformer, Preprocessor } from '../types';

export async function transformMarkup(
export function transformMarkup<S>(
sync: S,
{ content, filename }: { content: string; filename: string },
transformer: Preprocessor | Transformer<unknown>,
options: Record<string, any> = {},
) {
): EventuallyProcessed<S> {
let { markupTagName = 'template' } = options;

markupTagName = markupTagName.toLocaleLowerCase();
Expand All @@ -17,7 +19,12 @@ export async function transformMarkup(

/** If no <template> was found, run the transformer over the whole thing */
if (!templateMatch) {
return transformer({ content, attributes: {}, filename, options });
return transformer({
content,
attributes: {},
filename,
options,
}) as EventuallyProcessed<S>;
}

const [fullMatch, attributesStr, templateCode] = templateMatch;
Expand All @@ -36,17 +43,22 @@ export async function transformMarkup(
}, {});

/** Transform the found template code */
let { code, map, dependencies } = await transformer({
content: templateCode,
attributes,
filename,
options,
});

code =
content.slice(0, templateMatch.index) +
code +
content.slice(templateMatch.index + fullMatch.length);

return { code, map, dependencies };
return pipe<S>(
sync,
() =>
transformer({
content: templateCode,
attributes,
filename,
options,
}) as EventuallyProcessed<S>,
({ code, map, dependencies }) => {
code =
content.slice(0, templateMatch.index) +
code +
content.slice(templateMatch.index + fullMatch.length);

return { code, map, dependencies } as EventuallyProcessed<S>;
},
);
}
60 changes: 59 additions & 1 deletion src/modules/tagInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile, access } from 'fs';
import { readFile, access, accessSync, readFileSync } from 'fs';
import { resolve, dirname } from 'path';

import { PreprocessorArgs } from '../types';
Expand All @@ -18,10 +18,68 @@ const getSrcContent = (file: string): Promise<string> => {
});
};

const getSrcContentSync = (file: string) => {
const data = readFileSync(file);

return data.toString();
};

async function doesFileExist(file: string) {
return new Promise((resolve) => access(file, 0, (err) => resolve(!err)));
}

function doesFileExistSync(file: string) {
try {
accessSync(file, 0);

return true;
} catch (e) {
return false;
}
}

export const getTagInfoSync = ({
attributes,
filename,
content,
}: PreprocessorArgs) => {
const dependencies = [];
// catches empty content and self-closing tags
const isEmptyContent = content == null || content.trim().length === 0;

/** only include src file if content of tag is empty */
if (attributes.src && isEmptyContent) {
// istanbul ignore if
if (typeof attributes.src !== 'string') {
throw new Error('src attribute must be string');
}

let path = attributes.src;

/** Only try to get local files (path starts with ./ or ../) */
if (isValidLocalPath(path)) {
path = resolveSrc(filename, path);
if (doesFileExistSync(path)) {
content = getSrcContentSync(path);
dependencies.push(path);
} else {
console.warn(`[svelte-preprocess] The file "${path}" was not found.`);
}
}
}

const { lang, alias } = getLanguage(attributes);

return {
filename,
attributes,
content,
lang,
alias,
dependencies,
};
};

export const getTagInfo = async ({
attributes,
filename,
Expand Down
4 changes: 2 additions & 2 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ const cachedResult: Record<string, boolean> = {};
* @param {string} dep
* @returns boolean
*/
export async function hasDepInstalled(dep: string) {
export function hasDepInstalled(dep: string) {
if (cachedResult[dep] != null) {
return cachedResult[dep];
}

let result = false;

try {
await import(dep);
require(dep);

result = true;
} catch (e) {
Expand Down
42 changes: 42 additions & 0 deletions src/pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Processed } from './types';

export type EventuallyProcessed<Sync> = Sync extends true
? Processed
: Processed | Promise<Processed>;

const pipe: <Sync>(
sync: Sync,
start: () => EventuallyProcessed<Sync>,
...then: Array<(processed: Processed) => EventuallyProcessed<Sync>>
) => EventuallyProcessed<Sync> = (sync, start, ...then) =>
((sync ? pipe_sync : pipe_async) as any)(start, ...then);

async function pipe_async(
start: () => Promise<Processed>,
...then: Array<(processed: Processed) => Promise<Processed>>
) {
let processed = await start();

for (let i = 0; i < then.length; i++) {
// eslint-disable-next-line no-await-in-loop
processed = await then[i](processed);
}

return processed;
}

function pipe_sync(
start: () => Processed,
...then: Array<(processed: Processed) => Processed>
) {
let processed = start();

for (let i = 0; i < then.length; i++) {
processed = then[i](processed);
}

return processed;
}

export default pipe;
21 changes: 20 additions & 1 deletion src/processors/pug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ export default (options?: Options.Pug): PreprocessorGroup => ({
content,
});

return transformMarkup({ content, filename }, transformer, options);
return transformMarkup(false, { content, filename }, transformer, options);
},

markup_sync({ content, filename }) {
const { transformer } = require('../transformers/pug');

content = prepareContent({
options: {
...options,
stripIndent: true,
},
content,
});

return transformMarkup<true>(
true,
{ content, filename },
transformer,
options,
);
},
});
29 changes: 15 additions & 14 deletions src/processors/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { Options, PreprocessorGroup } from '../types';
import { getTagInfo } from '../modules/tagInfo';
import { getTagInfoSync } from '../modules/tagInfo';
import { concat } from '../modules/utils';
import { prepareContent } from '../modules/prepareContent';

export default (options?: Options.Typescript): PreprocessorGroup => ({
async script(svelteFile) {
const { transformer } = await import('../transformers/typescript');
let {
content,
filename,
attributes,
lang,
dependencies,
} = await getTagInfo(svelteFile);
export default (options?: Options.Typescript): PreprocessorGroup => {
const script = (svelteFile) => {
const { transformer } = require('../transformers/typescript');
let { content, filename, attributes, lang, dependencies } = getTagInfoSync(
svelteFile,
);

content = prepareContent({ options, content });

if (lang !== 'typescript') {
return { code: content };
}

const transformed = await transformer({
const transformed = transformer({
content,
filename,
attributes,
Expand All @@ -31,5 +27,10 @@ export default (options?: Options.Typescript): PreprocessorGroup => ({
...transformed,
dependencies: concat(dependencies, transformed.dependencies),
};
},
});
};

return {
script,
script_sync: script,
};
};
10 changes: 5 additions & 5 deletions src/transformers/replace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Transformer, Options } from '../types';

const transformer: Transformer<Options.Replace> = async ({
content,
options,
}) => {
const transformer: Transformer<Options.Replace> = ({ content, options }) => {
let newContent = content;

for (const [regex, replacer] of options) {
Expand All @@ -15,4 +13,6 @@ const transformer: Transformer<Options.Replace> = async ({
};
};

export { transformer };
const is_sync = true;

export { transformer, is_sync };
5 changes: 4 additions & 1 deletion src/transformers/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { dirname } from 'path';

import ts from 'typescript';
Expand Down Expand Up @@ -151,4 +152,6 @@ const transformer: Transformer<Options.Typescript> = ({
};
};

export { transformer };
const is_sync = true;

export { transformer, is_sync };
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export type Transformer<T> = (
args: TransformerArgs<T>,
) => Processed | Promise<Processed>;

export type SyncTransformer<T> = (args: TransformerArgs<T>) => Processed;

export type TransformerOptions<T = any> = boolean | T | Transformer<T>;

export interface Transformers {
Expand Down
2 changes: 2 additions & 0 deletions test/autoProcess/externalFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('external files', () => {
<style src="./fixtures/style.css"></style>
<script src="./fixtures/script.js"></script>`,
filename: resolve(__dirname, '..', 'App.svelte'),
attributes: {},
}),
await scriptProcessor({
content: ``,
Expand All @@ -57,6 +58,7 @@ describe('external files', () => {
const markup = await markupProcessor({
content: `<template src="./fixtures/template.html"/>`,
filename: resolve(__dirname, '..', 'App.svelte'),
attributes: {},
});

expect(markup.code).toContain(getFixtureContent('template.html'));
Expand Down