webpack-query-loader
Version:
Run loaders depending on their query
140 lines (139 loc) • 5.96 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.pitch = exports.raw = void 0;
const loader_utils_1 = __importDefault(require("loader-utils"));
const schema_utils_1 = require("schema-utils");
const options_json_1 = __importDefault(require("./options.json"));
exports.raw = true;
function pitch(remainingRequest, precedingRequest, data) {
const options = loader_utils_1.default.getOptions(this);
const params = this.resourceQuery
? loader_utils_1.default.parseQuery(this.resourceQuery)
: undefined;
if (!options)
throw "Options Not Found";
schema_utils_1.validate(options_json_1.default, options, {
name: "Query Loader",
baseDataPath: "options",
});
const conditionsMet = checkConditions(this.resource, this.resourceQuery, params !== null && params !== void 0 ? params : {}, options.resourceQuery);
if (!conditionsMet)
return undefined;
// code from https://github.com/webpack-contrib/url-loader
// Normalize the fallback.
const { loader: fallbackLoader, options: fallbackOptions } = normalizeUse(options === null || options === void 0 ? void 0 : options.use);
// Require the fallback.
const fallback = require(fallbackLoader).pitch;
if (!fallback || typeof fallback !== "function")
return undefined;
// Call the fallback, passing a copy of the loader context. The copy has the query replaced. This way, the fallback
// loader receives the query which was intended for it instead of the query which was intended for url-loader.
const fallbackLoaderContext = { ...this, query: fallbackOptions };
return fallback.call(fallbackLoaderContext, remainingRequest, precedingRequest, data);
}
exports.pitch = pitch;
function default_1(source, sourceMap) {
const options = loader_utils_1.default.getOptions(this);
const params = this.resourceQuery
? loader_utils_1.default.parseQuery(this.resourceQuery)
: undefined;
if (!options)
throw "Options Not Found";
schema_utils_1.validate(options_json_1.default, options, {
name: "Query Loader",
baseDataPath: "options",
});
const conditionsMet = checkConditions(this.resource, this.resourceQuery, params !== null && params !== void 0 ? params : {}, options.resourceQuery);
if (!conditionsMet)
return source;
// code from https://github.com/webpack-contrib/url-loader
// Normalize the fallback.
const { loader: fallbackLoader, options: fallbackOptions } = normalizeUse(options.use);
// Require the fallback.
const fallback = require(fallbackLoader);
if (!fallback || typeof fallback !== "function")
return source;
// Call the fallback, passing a copy of the loader context. The copy has the query replaced. This way, the fallback
// loader receives the query which was intended for it instead of the query which was intended for url-loader.
const fallbackLoaderContext = { ...this, query: fallbackOptions };
const normalizedContent = normalizeContent(source, fallback.raw);
return fallback.call(fallbackLoaderContext, normalizedContent, sourceMap);
}
exports.default = default_1;
// from https://github.com/webpack/loader-runner/blob/master/lib/LoaderRunner.js
function normalizeContent(source, raw) {
if (!raw && Buffer.isBuffer(source))
return utf8BufferToString(source);
else if (raw && typeof source === "string")
return Buffer.from(source, "utf-8");
return source;
}
// from https://github.com/webpack/loader-runner/blob/master/lib/LoaderRunner.js
function utf8BufferToString(buf) {
var str = buf.toString("utf-8");
if (str.charCodeAt(0) === 0xfeff) {
return str.substr(1);
}
else {
return str;
}
}
function checkConditions(resource, resourceQuery, query, resourceQueryConditions) {
if (resourceQueryConditions === undefined)
return true;
if (typeof resourceQueryConditions === "function") {
return resourceQueryConditions(resource, resourceQuery, query);
}
else if (typeof resourceQueryConditions === "object") {
return resourceQueryConditions.every((parameter) => checkQueryParameter(parameter, query));
}
else if (typeof resourceQueryConditions === "string") {
return checkQueryParameter(resourceQueryConditions, query);
}
throw "resourceQuery Not Found";
}
function checkQueryParameter(parameter, query) {
if (parameter.charAt(0) === "!") {
const actualParameter = parameter.substring(1);
return !Object.keys(query).includes(actualParameter);
}
else {
return Object.keys(query).includes(parameter);
}
}
function normalizeUse(use) {
let loaderString;
let options = {};
if (typeof use === "string") {
loaderString = use;
}
else if (typeof use === "object") {
loaderString = use.loader;
if (use.options)
options = use.options;
}
if (loaderString === undefined)
throw "Loader Not Found";
return normalizeLoader(loaderString, options);
}
// code from https://github.com/webpack-contrib/url-loader
function normalizeLoader(loaderString, originalOptions) {
let loaderName = loaderString;
let options = {};
if (typeof loaderString === "string") {
loaderName = loaderString;
const index = loaderString.indexOf("?");
if (index >= 0) {
loaderName = loaderString.substr(0, index);
options = loader_utils_1.default.parseQuery(loaderString.substr(index));
}
}
if (loaderString !== null && typeof loaderString === "object") {
({ loaderName, options } = loaderString);
}
options = Object.assign({}, originalOptions, options);
return { loader: loaderName, options };
}
;