webpack
Version:
Packs ECMAScript/CommonJs/AMD modules for the browser. Allows you to split your codebase into multiple bundles, which can be loaded on demand. Supports loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.
69 lines (61 loc) • 2.48 kB
JavaScript
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Raj Aryan (based on SWC parser by Alexander Akait)
*/
;
const SourceProcessor = require("../util/SourceProcessor");
/** @typedef {typeof import("./syntax-parser")} SyntaxParser */
/** @typedef {typeof import("./syntax-printer")} SyntaxPrinter */
/** @typedef {import("./syntax-parser").HtmlParts} HtmlParts */
/**
* @template [S=unknown]
* @typedef {import("./syntax-parser").HtmlVisitorMap<S>} HtmlVisitorMap
*/
/** @typedef {import("./syntax-parser").HtmlNodeRef} HtmlNodeRef */
/** @typedef {import("./syntax-parser").HtmlPath} HtmlPath */
/** @typedef {import("./syntax-printer").HtmlProcessOptions} HtmlProcessOptions */
/** @typedef {import("./syntax-printer").HtmlPrintOptions} HtmlPrintOptions */
/** @type {SyntaxParser | undefined} */
let _parser;
/** @type {SyntaxPrinter | undefined} */
let _printer;
// HTML's two halves, as `javascript` names theirs. Both are reached through
// a getter and remembered after the first read, so parsing never loads the
// printer and printing never loads the parser.
/**
* @returns {SyntaxParser} the parser half
*/
const parser = () => _parser || (_parser = require("./syntax-parser"));
/**
* @returns {SyntaxPrinter} the printer half
*/
const printer = () => _printer || (_printer = require("./syntax-printer"));
/**
* The generic visitor coordinator (`util/SourceProcessor`) bound to the HTML
* `grammar`. Usage:
*
* ```
* new HtmlSourceProcessor().use({ [NodeType.Element]: (path) => {}, [NodeType.Comment]: { enter, exit } }).process(source, { skip });
* ```
* @experimental exposed as `webpack.html.syntax.SourceProcessor`; unstable API
* @template [TState=unknown] what `process` hands every visitor as `path.state`
* @extends {SourceProcessor<HtmlPath, HtmlNodeRef, HtmlProcessOptions, HtmlPrintOptions, HtmlParts, TState, HtmlVisitorMap<TState>>}
*/
class HtmlSourceProcessor extends SourceProcessor {
constructor() {
// The printer is not named here but asked for on the first print, so a
// walk that only fires visitors never loads it.
super(parser().grammar, undefined, () => printer().printer, parser().A);
}
}
module.exports = {
/** @returns {SyntaxParser} the parser: what reads a source into nodes */
get parser() {
return parser();
},
/** @returns {SyntaxPrinter} the printer: what writes nodes back out */
get printer() {
return printer();
},
SourceProcessor: HtmlSourceProcessor
};