update
This commit is contained in:
+69
-18
@@ -6,11 +6,13 @@ Object.defineProperty(exports, "__esModule", {
|
||||
exports.default = loader;
|
||||
exports.raw = void 0;
|
||||
|
||||
var _path = _interopRequireDefault(require("path"));
|
||||
|
||||
var _loaderUtils = require("loader-utils");
|
||||
|
||||
var _schemaUtils = _interopRequireDefault(require("schema-utils"));
|
||||
var _schemaUtils = require("schema-utils");
|
||||
|
||||
var _mime = _interopRequireDefault(require("mime"));
|
||||
var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
||||
|
||||
var _normalizeFallback = _interopRequireDefault(require("./utils/normalizeFallback"));
|
||||
|
||||
@@ -18,12 +20,6 @@ var _options = _interopRequireDefault(require("./options.json"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
/* eslint-disable
|
||||
global-require,
|
||||
no-param-reassign,
|
||||
prefer-destructuring,
|
||||
import/no-dynamic-require,
|
||||
*/
|
||||
function shouldTransform(limit, size) {
|
||||
if (typeof limit === 'boolean') {
|
||||
return limit;
|
||||
@@ -40,24 +36,78 @@ function shouldTransform(limit, size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function loader(src) {
|
||||
function getMimetype(mimetype, resourcePath) {
|
||||
if (typeof mimetype === 'boolean') {
|
||||
if (mimetype) {
|
||||
const resolvedMimeType = _mimeTypes.default.contentType(_path.default.extname(resourcePath));
|
||||
|
||||
if (!resolvedMimeType) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return resolvedMimeType.replace(/;\s+charset/i, ';charset');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof mimetype === 'string') {
|
||||
return mimetype;
|
||||
}
|
||||
|
||||
const resolvedMimeType = _mimeTypes.default.contentType(_path.default.extname(resourcePath));
|
||||
|
||||
if (!resolvedMimeType) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return resolvedMimeType.replace(/;\s+charset/i, ';charset');
|
||||
}
|
||||
|
||||
function getEncoding(encoding) {
|
||||
if (typeof encoding === 'boolean') {
|
||||
return encoding ? 'base64' : '';
|
||||
}
|
||||
|
||||
if (typeof encoding === 'string') {
|
||||
return encoding;
|
||||
}
|
||||
|
||||
return 'base64';
|
||||
}
|
||||
|
||||
function getEncodedData(generator, mimetype, encoding, content, resourcePath) {
|
||||
if (generator) {
|
||||
return generator(content, mimetype, encoding, resourcePath);
|
||||
}
|
||||
|
||||
return `data:${mimetype}${encoding ? `;${encoding}` : ''},${content.toString( // eslint-disable-next-line no-undefined
|
||||
encoding || undefined)}`;
|
||||
}
|
||||
|
||||
function loader(content) {
|
||||
// Loader Options
|
||||
const options = (0, _loaderUtils.getOptions)(this) || {};
|
||||
(0, _schemaUtils.default)(_options.default, options, {
|
||||
(0, _schemaUtils.validate)(_options.default, options, {
|
||||
name: 'URL Loader',
|
||||
baseDataPath: 'options'
|
||||
}); // No limit or within the specified limit
|
||||
|
||||
if (shouldTransform(options.limit, src.length)) {
|
||||
const file = this.resourcePath; // Get MIME type
|
||||
if (shouldTransform(options.limit, content.length)) {
|
||||
const {
|
||||
resourcePath
|
||||
} = this;
|
||||
const mimetype = getMimetype(options.mimetype, resourcePath);
|
||||
const encoding = getEncoding(options.encoding);
|
||||
|
||||
const mimetype = options.mimetype || _mime.default.getType(file);
|
||||
|
||||
if (typeof src === 'string') {
|
||||
src = Buffer.from(src);
|
||||
if (typeof content === 'string') {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
content = Buffer.from(content);
|
||||
}
|
||||
|
||||
return `${options.esModules ? 'export default' : 'module.exports ='} ${JSON.stringify(`data:${mimetype || ''};base64,${src.toString('base64')}`)}`;
|
||||
const encodedData = getEncodedData(options.generator, mimetype, encoding, content, resourcePath);
|
||||
const esModule = typeof options.esModule !== 'undefined' ? options.esModule : true;
|
||||
return `${esModule ? 'export default' : 'module.exports ='} ${JSON.stringify(encodedData)}`;
|
||||
} // Normalize the fallback.
|
||||
|
||||
|
||||
@@ -65,6 +115,7 @@ function loader(src) {
|
||||
loader: fallbackLoader,
|
||||
options: fallbackOptions
|
||||
} = (0, _normalizeFallback.default)(options.fallback, options); // Require the fallback.
|
||||
// eslint-disable-next-line global-require, import/no-dynamic-require
|
||||
|
||||
const fallback = require(fallbackLoader); // 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.
|
||||
@@ -73,7 +124,7 @@ function loader(src) {
|
||||
const fallbackLoaderContext = Object.assign({}, this, {
|
||||
query: fallbackOptions
|
||||
});
|
||||
return fallback.call(fallbackLoaderContext, src);
|
||||
return fallback.call(fallbackLoaderContext, content);
|
||||
} // Loader Mode
|
||||
|
||||
|
||||
|
||||
+34
-2
@@ -5,9 +5,40 @@
|
||||
"description": "Enables/Disables transformation target file into base64 URIs (https://github.com/webpack-contrib/url-loader#limit).",
|
||||
"type": ["boolean", "number", "string"]
|
||||
},
|
||||
"encoding": {
|
||||
"description": "Specify the encoding which the file will be in-lined with.",
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"enum": [
|
||||
"utf8",
|
||||
"utf16le",
|
||||
"latin1",
|
||||
"base64",
|
||||
"hex",
|
||||
"ascii",
|
||||
"binary",
|
||||
"ucs2"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"mimetype": {
|
||||
"description": "The MIME type for the file to be transformed (https://github.com/webpack-contrib/url-loader#mimetype).",
|
||||
"type": "string"
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "boolean"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
]
|
||||
},
|
||||
"generator": {
|
||||
"description": "Adding custom implementation for encoding files.",
|
||||
"instanceof": "Function"
|
||||
},
|
||||
"fallback": {
|
||||
"description": "An alternative loader to use when a target file's size exceeds the limit set in the limit option (https://github.com/webpack-contrib/url-loader#fallback).",
|
||||
@@ -38,7 +69,8 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"esModules": {
|
||||
"esModule": {
|
||||
"description": "By default, url-loader generates JS modules that use the ES modules syntax.",
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user