| 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").AssetParserDataUrlOptions} AssetParserDataUrlOptions */
|
|---|
| 11 | /** @typedef {import("../../declarations/WebpackOptions").AssetParserOptions} AssetParserOptions */
|
|---|
| 12 | /** @typedef {import("../Module")} Module */
|
|---|
| 13 | /** @typedef {import("../Module").BuildInfo} BuildInfo */
|
|---|
| 14 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
|---|
| 15 | /** @typedef {import("../Parser").ParserState} ParserState */
|
|---|
| 16 | /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {((source: string | Buffer, context: { filename: string, module: Module }) => boolean)} AssetParserDataUrlFunction */
|
|---|
| 19 |
|
|---|
| 20 | class AssetParser extends Parser {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of AssetParser.
|
|---|
| 23 | * @param {AssetParserOptions["dataUrlCondition"] | boolean} dataUrlCondition condition for inlining as DataUrl
|
|---|
| 24 | */
|
|---|
| 25 | constructor(dataUrlCondition) {
|
|---|
| 26 | super();
|
|---|
| 27 | /** @type {AssetParserOptions["dataUrlCondition"] | boolean} */
|
|---|
| 28 | this.dataUrlCondition = dataUrlCondition;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Parses the provided source and updates the parser state.
|
|---|
| 33 | * @param {string | Buffer | PreparsedAst} source the source to parse
|
|---|
| 34 | * @param {ParserState} state the parser state
|
|---|
| 35 | * @returns {ParserState} the parser state
|
|---|
| 36 | */
|
|---|
| 37 | parse(source, state) {
|
|---|
| 38 | if (typeof source === "object" && !Buffer.isBuffer(source)) {
|
|---|
| 39 | throw new Error("AssetParser doesn't accept preparsed AST");
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | const buildInfo =
|
|---|
| 43 | /** @type {BuildInfo} */
|
|---|
| 44 | (state.module.buildInfo);
|
|---|
| 45 | buildInfo.strict = true;
|
|---|
| 46 | const buildMeta =
|
|---|
| 47 | /** @type {BuildMeta} */
|
|---|
| 48 | (state.module.buildMeta);
|
|---|
| 49 | buildMeta.exportsType = "default";
|
|---|
| 50 | buildMeta.defaultObject = false;
|
|---|
| 51 |
|
|---|
| 52 | if (typeof this.dataUrlCondition === "function") {
|
|---|
| 53 | buildInfo.dataUrl = this.dataUrlCondition(source, {
|
|---|
| 54 | filename: /** @type {string} */ (state.module.getResource()),
|
|---|
| 55 | module: state.module
|
|---|
| 56 | });
|
|---|
| 57 | } else if (typeof this.dataUrlCondition === "boolean") {
|
|---|
| 58 | buildInfo.dataUrl = this.dataUrlCondition;
|
|---|
| 59 | } else if (
|
|---|
| 60 | this.dataUrlCondition &&
|
|---|
| 61 | typeof this.dataUrlCondition === "object"
|
|---|
| 62 | ) {
|
|---|
| 63 | buildInfo.dataUrl =
|
|---|
| 64 | Buffer.byteLength(source) <=
|
|---|
| 65 | /** @type {NonNullable<AssetParserDataUrlOptions["maxSize"]>} */
|
|---|
| 66 | (this.dataUrlCondition.maxSize);
|
|---|
| 67 | } else {
|
|---|
| 68 | throw new Error("Unexpected dataUrlCondition type");
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return state;
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | module.exports = AssetParser;
|
|---|