2019-03-31 22:12:19 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Yuta Hiroto @hiroppy
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-11-26 20:56:27 +08:00
|
|
|
/** @typedef {import("../../declarations/plugins/AssetModulesPluginParser").AssetModulesPluginParserOptions} AssetModulesPluginParserOptions */
|
|
|
|
|
2019-07-16 19:16:27 +08:00
|
|
|
class AssetParser {
|
2019-11-26 20:56:27 +08:00
|
|
|
/**
|
|
|
|
* @param {AssetModulesPluginParserOptions["dataUrlCondition"] | boolean} dataUrlCondition condition for inlining as DataUrl
|
|
|
|
*/
|
|
|
|
constructor(dataUrlCondition) {
|
|
|
|
this.dataUrlCondition = dataUrlCondition;
|
|
|
|
}
|
|
|
|
|
2019-03-31 22:12:19 +08:00
|
|
|
parse(source, state) {
|
|
|
|
state.module.buildInfo.strict = true;
|
2019-11-19 21:48:46 +08:00
|
|
|
state.module.buildMeta.exportsType = "default";
|
2019-03-31 22:12:19 +08:00
|
|
|
|
2019-11-26 20:56:27 +08:00
|
|
|
if (typeof this.dataUrlCondition === "function") {
|
|
|
|
state.module.buildInfo.dataUrl = this.dataUrlCondition(source, {
|
|
|
|
filename: state.module.nameForCondition(),
|
|
|
|
module: state.module
|
|
|
|
});
|
|
|
|
} else if (typeof this.dataUrlCondition === "boolean") {
|
|
|
|
state.module.buildInfo.dataUrl = this.dataUrlCondition;
|
|
|
|
} else if (
|
|
|
|
this.dataUrlCondition &&
|
|
|
|
typeof this.dataUrlCondition === "object"
|
|
|
|
) {
|
|
|
|
state.module.buildInfo.dataUrl =
|
|
|
|
Buffer.byteLength(source) <= this.dataUrlCondition.maxSize;
|
|
|
|
} else {
|
|
|
|
throw new Error("Unexpected dataUrlCondition type");
|
|
|
|
}
|
|
|
|
|
2019-03-31 22:12:19 +08:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:16:27 +08:00
|
|
|
module.exports = AssetParser;
|