Extract tag preprocessor method

Tidy code for consistency
pull/5584/head
halfnelson 5 years ago
parent 713827126a
commit 21f73389ff

@ -83,7 +83,9 @@ async function replace_async(
return out.concat(final_content); return out.concat(final_content);
} }
// Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap /**
* Convert a preprocessor output and its leading prefix and trailing suffix into StringWithSourceMap
*/
function get_replacement( function get_replacement(
filename: string, filename: string,
offset: number, offset: number,
@ -124,7 +126,9 @@ export default async function preprocess(
const filename = (options && options.filename) || preprocessor.filename; // legacy const filename = (options && options.filename) || preprocessor.filename; // legacy
const dependencies = []; const dependencies = [];
const preprocessors = 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 script = preprocessors.map(p => p.script).filter(Boolean); const script = preprocessors.map(p => p.script).filter(Boolean);
@ -145,24 +149,30 @@ export default async function preprocess(
filename filename
}); });
if (processed && processed.dependencies) dependencies.push(...processed.dependencies); if (processed) {
source = processed ? processed.code : source; if (processed.dependencies) dependencies.push(...processed.dependencies);
if (processed && processed.map) { source = processed.code;
sourcemap_list.unshift( if (processed.map) {
typeof(processed.map) === 'string' sourcemap_list.unshift(
? JSON.parse(processed.map) typeof(processed.map) === 'string'
: processed.map ? JSON.parse(processed.map)
); : processed.map
);
}
} }
} }
for (const fn of script) { async function preprocess_tag_content(tag_name: 'style' | 'script', preprocessor: Preprocessor) {
const get_location = getLocator(source); const get_location = getLocator(source);
const tag_regex = tag_name == 'style'
? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi
: /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
const res = await replace_async( const res = await replace_async(
filename, filename,
source, source,
get_location, get_location,
/<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi, tag_regex,
async (match, attributes = '', content = '', offset) => { async (match, attributes = '', content = '', offset) => {
const no_change = () => StringWithSourcemap.from_source( const no_change = () => StringWithSourcemap.from_source(
filename, match, get_location(offset)); filename, match, get_location(offset));
@ -173,51 +183,27 @@ export default async function preprocess(
content = content || ''; content = content || '';
// run script preprocessor // run script preprocessor
const processed = await fn({ const processed = await preprocessor({
content, content,
attributes: parse_attributes(attributes), attributes: parse_attributes(attributes),
filename filename
}); });
if (processed && processed.dependencies) dependencies.push(...processed.dependencies);
return processed if (!processed) return no_change();
? get_replacement(filename, offset, get_location, content, processed, `<script${attributes}>`, '</script>') if (processed.dependencies) dependencies.push(...processed.dependencies);
: no_change(); return get_replacement(filename, offset, get_location, content, processed, `<${tag_name}${attributes}>`, `</${tag_name}>`);
} }
); );
source = res.string; source = res.string;
sourcemap_list.unshift(res.map); sourcemap_list.unshift(res.map);
} }
for (const fn of style) { for (const fn of script) {
const get_location = getLocator(source); await preprocess_tag_content('script', fn);
const res = await replace_async( }
filename,
source,
get_location,
/<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi,
async (match, attributes = '', content = '', offset) => {
const no_change = () => StringWithSourcemap.from_source(
filename, match, get_location(offset));
if (!attributes && !content) {
return no_change();
}
attributes = attributes || '';
content = content || '';
// run style preprocessor for (const fn of style) {
const processed: Processed = await fn({ await preprocess_tag_content('style', fn);
content,
attributes: parse_attributes(attributes),
filename
});
if (processed && processed.dependencies) dependencies.push(...processed.dependencies);
return processed
? get_replacement(filename, offset, get_location, content, processed, `<style${attributes}>`, '</style>')
: no_change();
}
);
source = res.string;
sourcemap_list.unshift(res.map);
} }
// Combine all the source maps for each preprocessor function into one // Combine all the source maps for each preprocessor function into one

@ -32,7 +32,7 @@ export function sourcemap_add_offset(
} }
} }
function merge_tables<T>(this_table: T[], other_table): [T[], number[], boolean, boolean] { function merge_tables<T>(this_table: T[], other_table: T[]): [T[], number[], boolean, boolean] {
const new_table = this_table.slice(); const new_table = this_table.slice();
const idx_map = []; const idx_map = [];
other_table = other_table || []; other_table = other_table || [];
@ -68,7 +68,7 @@ export class StringWithSourcemap {
string: string; string: string;
map: DecodedSourceMap; map: DecodedSourceMap;
constructor(string = '', map = null) { constructor(string = '', map: DecodedSourceMap = null) {
this.string = string; this.string = string;
if (map) { if (map) {
this.map = map as DecodedSourceMap; this.map = map as DecodedSourceMap;
@ -82,8 +82,10 @@ export class StringWithSourcemap {
} }
} }
// concat in-place (mutable), return this (chainable) /**
// will also mutate the `other` object * concat in-place (mutable), return this (chainable)
* will also mutate the `other` object
*/
concat(other: StringWithSourcemap): StringWithSourcemap { concat(other: StringWithSourcemap): StringWithSourcemap {
// noop: if one is empty, return the other // noop: if one is empty, return the other
if (other.string == '') return this; if (other.string == '') return this;
@ -250,8 +252,8 @@ export function apply_preprocessor_sourcemap(filename: string, svelte_map: Sourc
] ]
) as RawSourceMap; ) as RawSourceMap;
//Svelte expects a SourceMap which includes toUrl and toString. Instead of using the magic-string constructor that takes a decoded map // Svelte expects a SourceMap which includes toUrl and toString. Instead of wrapping our output in a class,
//we just tack on the extra properties. // we just tack on the extra properties.
Object.defineProperties(result_map, { Object.defineProperties(result_map, {
toString: { toString: {
enumerable: false, enumerable: false,

@ -12,4 +12,4 @@
div { div {
--keep-me: blue; --keep-me: blue;
} }
</style> </style>

Loading…
Cancel
Save