[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Yuta Hiroto @hiroppy
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const Parser = require("../Parser");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("../../declarations/WebpackOptions").AssetParserOptions} AssetParserOptions */
|
---|
| 11 | /** @typedef {import("../Parser").ParserState} ParserState */
|
---|
| 12 | /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
|
---|
| 13 |
|
---|
| 14 | class AssetParser extends Parser {
|
---|
| 15 | /**
|
---|
| 16 | * @param {AssetParserOptions["dataUrlCondition"] | boolean} dataUrlCondition condition for inlining as DataUrl
|
---|
| 17 | */
|
---|
| 18 | constructor(dataUrlCondition) {
|
---|
| 19 | super();
|
---|
| 20 | this.dataUrlCondition = dataUrlCondition;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @param {string | Buffer | PreparsedAst} source the source to parse
|
---|
| 25 | * @param {ParserState} state the parser state
|
---|
| 26 | * @returns {ParserState} the parser state
|
---|
| 27 | */
|
---|
| 28 | parse(source, state) {
|
---|
| 29 | if (typeof source === "object" && !Buffer.isBuffer(source)) {
|
---|
| 30 | throw new Error("AssetParser doesn't accept preparsed AST");
|
---|
| 31 | }
|
---|
| 32 | state.module.buildInfo.strict = true;
|
---|
| 33 | state.module.buildMeta.exportsType = "default";
|
---|
| 34 |
|
---|
| 35 | if (typeof this.dataUrlCondition === "function") {
|
---|
| 36 | state.module.buildInfo.dataUrl = this.dataUrlCondition(source, {
|
---|
| 37 | filename: state.module.matchResource || state.module.resource,
|
---|
| 38 | module: state.module
|
---|
| 39 | });
|
---|
| 40 | } else if (typeof this.dataUrlCondition === "boolean") {
|
---|
| 41 | state.module.buildInfo.dataUrl = this.dataUrlCondition;
|
---|
| 42 | } else if (
|
---|
| 43 | this.dataUrlCondition &&
|
---|
| 44 | typeof this.dataUrlCondition === "object"
|
---|
| 45 | ) {
|
---|
| 46 | state.module.buildInfo.dataUrl =
|
---|
| 47 | Buffer.byteLength(source) <= this.dataUrlCondition.maxSize;
|
---|
| 48 | } else {
|
---|
| 49 | throw new Error("Unexpected dataUrlCondition type");
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | return state;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | module.exports = AssetParser;
|
---|