works with typescript

pull/5770/head
Andreas Ehrencrona 6 years ago
parent d17807a13a
commit e5c71eeb29

@ -1,6 +1,6 @@
export { default as compile } from './compile/index'; export { default as compile } from './compile/index';
export { default as parse } from './parse/index'; export { default as parse } from './parse/index';
export { default as preprocess } from './preprocess/index'; export { default as preprocess, preprocess_sync } from './preprocess/index';
export { walk } from 'estree-walker'; export { walk } from 'estree-walker';
export const VERSION = '__VERSION__'; export const VERSION = '__VERSION__';

@ -218,13 +218,10 @@ function decode_preprocessor_params(
const markup = preprocessors.map(p => p.markup).filter(Boolean); const markup = preprocessors.map(p => p.markup).filter(Boolean);
const script = preprocessors.map(p => p.script).filter(Boolean); const script = preprocessors.map(p => p.script).filter(Boolean);
const script_sync = preprocessors.map(p => p.script_sync).filter(Boolean);
const style = preprocessors.map(p => p.style).filter(Boolean); const style = preprocessors.map(p => p.style).filter(Boolean);
return { markup, script, style, filename }; return { markup, script, script_sync, style, filename };
}
function is_sync(processor: Preprocessor | SyncPreprocessor): processor is SyncPreprocessor {
return processor['is_sync'];
} }
export function preprocess_sync( export function preprocess_sync(
@ -232,47 +229,49 @@ export function preprocess_sync(
preprocessor: PreprocessorGroup | PreprocessorGroup[], preprocessor: PreprocessorGroup | PreprocessorGroup[],
options?: { filename?: string } options?: { filename?: string }
): Processed { ): Processed {
const { markup, script, style, filename } = decode_preprocessor_params(preprocessor, options); const { script_sync, filename } = decode_preprocessor_params(preprocessor, options);
const result = new PreprocessResult(source, filename); const result = new PreprocessResult(source, filename);
// TODO keep track: what preprocessor generated what sourcemap? // TODO keep track: what preprocessor generated what sourcemap?
// to make debugging easier = detect low-resolution sourcemaps in fn combine_mappings // to make debugging easier = detect low-resolution sourcemaps in fn combine_mappings
for (const process of markup) { // for (const process of markup) {
if (is_sync(process)) { // if (is_sync(process)) {
const processed = process({ // const processed = process({
content: result.source, // content: result.source,
filename, // filename,
attributes: null // attributes: null
}); // });
if (!processed) continue; // if (!processed) continue;
result.update_source({ // result.update_source({
string: processed.code, // string: processed.code,
map: processed.map // map: processed.map
? // TODO: can we use decode_sourcemap? // ? // TODO: can we use decode_sourcemap?
typeof processed.map === 'string' // typeof processed.map === 'string'
? JSON.parse(processed.map) // ? JSON.parse(processed.map)
: processed.map // : processed.map
: undefined, // : undefined,
dependencies: processed.dependencies // dependencies: processed.dependencies
}); // });
} // } else {
// console.error(`Preprocessor is not synchronous: ${process}.`);
// }
// }
for (const process of script_sync) {
result.update_source(process_tag_sync('script', process, result));
} }
for (const process of script) { // for (const process of style) {
if (is_sync(process)) { // if (is_sync(process)) {
result.update_source(process_tag_sync('script', process, result)); // result.update_source(process_tag_sync('style', process, result));
} // } else {
} // console.error(`Preprocessor is not synchronous: ${process}.`);
// }
for (const process of style) { // }
if (is_sync(process)) {
result.update_source(process_tag_sync('style', process, result));
}
}
return result.to_processed(); return result.to_processed();
} }

@ -6,9 +6,12 @@ export interface Processed {
} }
export interface PreprocessorGroup { export interface PreprocessorGroup {
markup?: Preprocessor | SyncPreprocessor; markup?: Preprocessor;
style?: Preprocessor | SyncPreprocessor; style?: Preprocessor;
script?: Preprocessor | SyncPreprocessor; script?: Preprocessor;
markup_sync?: SyncPreprocessor;
script_sync?: SyncPreprocessor;
style_sync?: SyncPreprocessor;
} }
interface PreprocessorOptions { interface PreprocessorOptions {
@ -19,4 +22,4 @@ interface PreprocessorOptions {
export declare type Preprocessor = (options: PreprocessorOptions) => Promise<Processed>; export declare type Preprocessor = (options: PreprocessorOptions) => Promise<Processed>;
export declare type SyncPreprocessor = ((options: PreprocessorOptions) => Processed) & { is_sync: true }; export declare type SyncPreprocessor = (options: PreprocessorOptions) => Processed;

Loading…
Cancel
Save