all transforms

pull/5770/head
Andreas Ehrencrona 5 years ago
parent e5c71eeb29
commit d8f02cab24

@ -315,8 +315,8 @@ export default class Expression {
// rename #ctx -> child_ctx; // rename #ctx -> child_ctx;
walk(func_expression, { walk(func_expression, {
enter(node) { enter(node) {
if (node.type === 'Identifier' && node.name === '#ctx') { if (node.type === 'Identifier' && (node as Identifier).name === '#ctx') {
node.name = 'child_ctx'; (node as Identifier).name = 'child_ctx';
} }
} }
}); });

@ -160,19 +160,16 @@ function to_source_update(tags: TagInstance[], processed: Processed[], tag_name:
return {...perform_replacements(replacements, source), dependencies}; return {...perform_replacements(replacements, source), dependencies};
} }
/** function get_processed_for_tag(
* Calculate the updates required to process all instances of the specified tag.
*/
async function process_tag(
tag_name: 'style' | 'script', tag_name: 'style' | 'script',
preprocessor: Preprocessor | SyncPreprocessor, preprocessor: Preprocessor | SyncPreprocessor,
source: Source source: Source
): Promise<SourceUpdate> { ) {
const { filename } = source; const { filename } = source;
const tags = get_tag_instances(tag_name, source.source); const tags = get_tag_instances(tag_name, source.source);
const processed = await Promise.all<Processed>(tags.map(({attributes, content}) => { const processed = tags.map(({attributes, content}) => {
if (attributes || content) { if (attributes || content) {
return preprocessor({ return preprocessor({
content, content,
@ -180,9 +177,22 @@ async function process_tag(
filename filename
}); });
} }
})); });
return { tags, processed };
}
/**
* Calculate the updates required to process all instances of the specified tag.
*/
async function process_tag(
tag_name: 'style' | 'script',
preprocessor: Preprocessor | SyncPreprocessor,
source: Source
): Promise<SourceUpdate> {
const { tags, processed } = get_processed_for_tag(tag_name, preprocessor, source);
return to_source_update(tags, processed, tag_name, source); return to_source_update(tags, await Promise.all(processed), tag_name, source);
} }
function process_tag_sync( function process_tag_sync(
@ -190,21 +200,9 @@ function process_tag_sync(
preprocessor: SyncPreprocessor, preprocessor: SyncPreprocessor,
source: Source source: Source
): SourceUpdate { ): SourceUpdate {
const { filename } = source; const { tags, processed } = get_processed_for_tag(tag_name, preprocessor, source);
const tags = get_tag_instances(tag_name, source.source);
const processed: Processed[] = tags.map(({attributes, content}) => {
if (attributes || content) {
return preprocessor({
content,
attributes: parse_tag_attributes(attributes || ''),
filename
});
}
});
return to_source_update(tags, processed, tag_name, source); return to_source_update(tags, processed as Processed[], tag_name, source);
} }
function decode_preprocessor_params( function decode_preprocessor_params(
@ -217,11 +215,26 @@ function decode_preprocessor_params(
const preprocessors = preprocessor ? (Array.isArray(preprocessor) ? preprocessor : [preprocessor]) : []; const preprocessors = preprocessor ? (Array.isArray(preprocessor) ? preprocessor : [preprocessor]) : [];
const markup = preprocessors.map(p => p.markup).filter(Boolean); const markup = preprocessors.map(p => p.markup).filter(Boolean);
const markup_sync = preprocessors.map(p => p.markup_sync).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 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);
const style_sync = preprocessors.map(p => p.style_sync).filter(Boolean);
return { markup, script, script_sync, style, filename }; return { markup, markup_sync, script, script_sync, style, style_sync, filename };
}
function processed_markup_to_source_update(processed: Processed): SourceUpdate {
return {
string: processed.code,
map: processed.map
? // TODO: can we use decode_sourcemap?
typeof processed.map === 'string'
? JSON.parse(processed.map)
: processed.map
: undefined,
dependencies: processed.dependencies
};
} }
export function preprocess_sync( export function preprocess_sync(
@ -229,49 +242,32 @@ export function preprocess_sync(
preprocessor: PreprocessorGroup | PreprocessorGroup[], preprocessor: PreprocessorGroup | PreprocessorGroup[],
options?: { filename?: string } options?: { filename?: string }
): Processed { ): Processed {
const { script_sync, filename } = decode_preprocessor_params(preprocessor, options); const { script_sync, style_sync, markup_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_sync) {
// 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(processed_markup_to_source_update(processed));
// result.update_source({ }
// string: processed.code,
// map: processed.map
// ? // TODO: can we use decode_sourcemap?
// typeof processed.map === 'string'
// ? JSON.parse(processed.map)
// : processed.map
// : undefined,
// dependencies: processed.dependencies
// });
// } else {
// console.error(`Preprocessor is not synchronous: ${process}.`);
// }
// }
for (const process of script_sync) { for (const process of script_sync) {
result.update_source(process_tag_sync('script', process, result)); result.update_source(process_tag_sync('script', process, result));
} }
// for (const process of style) { for (const process of style_sync) {
// if (is_sync(process)) { result.update_source(process_tag_sync('style', process, result));
// result.update_source(process_tag_sync('style', process, result)); }
// } else {
// console.error(`Preprocessor is not synchronous: ${process}.`);
// }
// }
return result.to_processed(); return result.to_processed();
} }
@ -297,16 +293,7 @@ export default async function preprocess(
if (!processed) continue; if (!processed) continue;
result.update_source({ result.update_source(processed_markup_to_source_update(processed));
string: processed.code,
map: processed.map
? // TODO: can we use decode_sourcemap?
typeof processed.map === 'string'
? JSON.parse(processed.map)
: processed.map
: undefined,
dependencies: processed.dependencies
});
} }
for (const process of script) { for (const process of script) {

Loading…
Cancel
Save