|
|
@ -1,4 +1,5 @@
|
|
|
|
import { SourceMap } from 'magic-string';
|
|
|
|
import { SourceMap } from 'magic-string';
|
|
|
|
|
|
|
|
import replaceAsync from '../utils/replaceAsync';
|
|
|
|
|
|
|
|
|
|
|
|
export interface PreprocessOptions {
|
|
|
|
export interface PreprocessOptions {
|
|
|
|
markup?: (options: {
|
|
|
|
markup?: (options: {
|
|
|
@ -16,6 +17,11 @@ export type Preprocessor = (options: {
|
|
|
|
filename?: string
|
|
|
|
filename?: string
|
|
|
|
}) => { code: string, map?: SourceMap | string };
|
|
|
|
}) => { code: string, map?: SourceMap | string };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface Processed {
|
|
|
|
|
|
|
|
code: string;
|
|
|
|
|
|
|
|
map?: SourceMap | string;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function parseAttributeValue(value: string) {
|
|
|
|
function parseAttributeValue(value: string) {
|
|
|
|
return /^['"]/.test(value) ?
|
|
|
|
return /^['"]/.test(value) ?
|
|
|
|
value.slice(1, -1) :
|
|
|
|
value.slice(1, -1) :
|
|
|
@ -31,60 +37,32 @@ function parseAttributes(str: string) {
|
|
|
|
return attrs;
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function replaceTagContents(
|
|
|
|
|
|
|
|
source,
|
|
|
|
|
|
|
|
type: 'script' | 'style',
|
|
|
|
|
|
|
|
preprocessor: Preprocessor,
|
|
|
|
|
|
|
|
options: PreprocessOptions
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
const exp = new RegExp(`<${type}([\\S\\s]*?)>([\\S\\s]*?)<\\/${type}>`, 'ig');
|
|
|
|
|
|
|
|
const match = exp.exec(source);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
|
|
|
|
const attributes: Record<string, string | boolean> = parseAttributes(match[1]);
|
|
|
|
|
|
|
|
const content: string = match[2];
|
|
|
|
|
|
|
|
const processed: { code: string, map?: SourceMap | string } = await preprocessor({
|
|
|
|
|
|
|
|
content,
|
|
|
|
|
|
|
|
attributes,
|
|
|
|
|
|
|
|
filename : options.filename
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (processed && processed.code) {
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
source.slice(0, match.index) +
|
|
|
|
|
|
|
|
`<${type}>${processed.code}</${type}>` +
|
|
|
|
|
|
|
|
source.slice(match.index + match[0].length)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default async function preprocess(
|
|
|
|
export default async function preprocess(
|
|
|
|
source: string,
|
|
|
|
source: string,
|
|
|
|
options: PreprocessOptions
|
|
|
|
options: PreprocessOptions
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
const { markup, style, script } = options;
|
|
|
|
if (options.markup) {
|
|
|
|
|
|
|
|
const processed: Processed = await options.markup({
|
|
|
|
if (!!markup) {
|
|
|
|
|
|
|
|
const processed: {
|
|
|
|
|
|
|
|
code: string,
|
|
|
|
|
|
|
|
map?: SourceMap | string
|
|
|
|
|
|
|
|
} = await markup({
|
|
|
|
|
|
|
|
content: source,
|
|
|
|
content: source,
|
|
|
|
filename: options.filename
|
|
|
|
filename: options.filename,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
source = processed ? processed.code : source;
|
|
|
|
source = processed.code;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.style || options.script) {
|
|
|
|
if (!!style) {
|
|
|
|
source = await replaceAsync(
|
|
|
|
source = await replaceTagContents(source, 'style', style, options);
|
|
|
|
source,
|
|
|
|
|
|
|
|
/<(script|style)([^]*?)>([^]*?)<\/\1>/gi,
|
|
|
|
|
|
|
|
async (match, type, attributes, content) => {
|
|
|
|
|
|
|
|
const processed: Processed =
|
|
|
|
|
|
|
|
type in options &&
|
|
|
|
|
|
|
|
(await options[type]({
|
|
|
|
|
|
|
|
content,
|
|
|
|
|
|
|
|
attributes: parseAttributes(attributes),
|
|
|
|
|
|
|
|
filename: options.filename,
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
return processed ? `<${type}>${processed.code}</${type}>` : match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
if (!!script) {
|
|
|
|
|
|
|
|
source = await replaceTagContents(source, 'script', script, options);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|