Skip to content

Sync preprocess #5770

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
all transforms
  • Loading branch information
ehrencrona committed Dec 11, 2020
commit d8f02cab2495c032da63bc27baffc55f47b7f78c
4 changes: 2 additions & 2 deletions src/compiler/compile/nodes/shared/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ export default class Expression {
// rename #ctx -> child_ctx;
walk(func_expression, {
enter(node) {
if (node.type === 'Identifier' && node.name === '#ctx') {
node.name = 'child_ctx';
if (node.type === 'Identifier' && (node as Identifier).name === '#ctx') {
(node as Identifier).name = 'child_ctx';
}
}
});
Expand Down
117 changes: 52 additions & 65 deletions src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,51 +160,49 @@ function to_source_update(tags: TagInstance[], processed: Processed[], tag_name:
return {...perform_replacements(replacements, source), dependencies};
}

/**
* Calculate the updates required to process all instances of the specified tag.
*/
async function process_tag(
function get_processed_for_tag(
tag_name: 'style' | 'script',
preprocessor: Preprocessor | SyncPreprocessor,
source: Source
): Promise<SourceUpdate> {
) {
const { filename } = 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) {
return preprocessor({
content,
attributes: parse_tag_attributes(attributes || ''),
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(
tag_name: 'style' | 'script',
preprocessor: SyncPreprocessor,
source: Source
): SourceUpdate {
const { filename } = 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
});
}
});
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, processed as Processed[], tag_name, source);
}

function decode_preprocessor_params(
Expand All @@ -217,61 +215,59 @@ function decode_preprocessor_params(
const preprocessors = preprocessor ? (Array.isArray(preprocessor) ? preprocessor : [preprocessor]) : [];

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_sync = preprocessors.map(p => p.script_sync).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(
source: string,
preprocessor: PreprocessorGroup | PreprocessorGroup[],
options?: { filename?: string }
): 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);

// TODO keep track: what preprocessor generated what sourcemap?
// to make debugging easier = detect low-resolution sourcemaps in fn combine_mappings

// for (const process of markup) {
// if (is_sync(process)) {
// const processed = process({
// content: result.source,
// filename,
// attributes: null
// });

// if (!processed) continue;

// 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 markup_sync) {
const processed = process({
content: result.source,
filename,
attributes: null
});

if (!processed) continue;

result.update_source(processed_markup_to_source_update(processed));
}

for (const process of script_sync) {
result.update_source(process_tag_sync('script', process, result));
}

// for (const process of style) {
// if (is_sync(process)) {
// result.update_source(process_tag_sync('style', process, result));
// } else {
// console.error(`Preprocessor is not synchronous: ${process}.`);
// }
// }
for (const process of style_sync) {
result.update_source(process_tag_sync('style', process, result));
}

return result.to_processed();
}
Expand All @@ -297,16 +293,7 @@ export default async function preprocess(

if (!processed) continue;

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
});
result.update_source(processed_markup_to_source_update(processed));
}

for (const process of script) {
Expand Down