This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
@@ -2,7 +2,14 @@ import { ASTNode } from './utils';
export interface AssetURLOptions {
[name: string]: string | string[];
}
declare const _default: (userOptions?: AssetURLOptions | undefined) => {
export interface TransformAssetUrlsOptions {
/**
* If base is provided, instead of transforming relative asset urls into
* imports, they will be directly rewritten to absolute urls.
*/
base?: string;
}
declare const _default: (userOptions?: AssetURLOptions | undefined, transformAssetUrlsOption?: TransformAssetUrlsOptions | undefined) => {
postTransformNode: (node: ASTNode) => void;
};
export default _default;
@@ -10,35 +10,35 @@ const defaultOptions = {
image: ['xlink:href', 'href'],
use: ['xlink:href', 'href']
};
exports.default = (userOptions) => {
exports.default = (userOptions, transformAssetUrlsOption) => {
const options = userOptions
? Object.assign({}, defaultOptions, userOptions)
: defaultOptions;
return {
postTransformNode: (node) => {
transform(node, options);
transform(node, options, transformAssetUrlsOption);
}
};
};
function transform(node, options) {
function transform(node, options, transformAssetUrlsOption) {
for (const tag in options) {
if ((tag === '*' || node.tag === tag) && node.attrs) {
const attributes = options[tag];
if (typeof attributes === 'string') {
node.attrs.some(attr => rewrite(attr, attributes));
node.attrs.some(attr => rewrite(attr, attributes, transformAssetUrlsOption));
}
else if (Array.isArray(attributes)) {
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)));
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item, transformAssetUrlsOption)));
}
}
}
}
function rewrite(attr, name) {
function rewrite(attr, name, transformAssetUrlsOption) {
if (attr.name === name) {
const value = attr.value;
// only transform static URLs
if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
attr.value = utils_1.urlToRequire(value.slice(1, -1));
attr.value = utils_1.urlToRequire(value.slice(1, -1), transformAssetUrlsOption);
return true;
}
}
@@ -1,5 +1,6 @@
import { ASTNode } from './utils';
declare const _default: () => {
import { TransformAssetUrlsOptions } from './assetUrl';
declare const _default: (transformAssetUrlsOptions?: TransformAssetUrlsOptions | undefined) => {
postTransformNode: (node: ASTNode) => void;
};
export default _default;
@@ -2,14 +2,14 @@
// vue compiler module for transforming `img:srcset` to a number of `require`s
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
exports.default = () => ({
exports.default = (transformAssetUrlsOptions) => ({
postTransformNode: (node) => {
transform(node);
transform(node, transformAssetUrlsOptions);
}
});
// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g;
function transform(node) {
function transform(node, transformAssetUrlsOptions) {
const tags = ['img', 'source'];
if (tags.indexOf(node.tag) !== -1 && node.attrs) {
node.attrs.forEach(attr => {
@@ -30,7 +30,10 @@ function transform(node) {
.replace(escapedSpaceCharacters, ' ')
.trim()
.split(' ', 2);
return { require: utils_1.urlToRequire(url), descriptor };
return {
require: utils_1.urlToRequire(url, transformAssetUrlsOptions),
descriptor
};
});
// "require(url1)"
// "require(url1) 1x"
@@ -1,3 +1,4 @@
import { TransformAssetUrlsOptions } from './assetUrl';
export interface Attr {
name: string;
value: string;
@@ -6,4 +7,4 @@ export interface ASTNode {
tag: string;
attrs: Attr[];
}
export declare function urlToRequire(url: string): string;
export declare function urlToRequire(url: string, transformAssetUrlsOption?: TransformAssetUrlsOptions): string;
@@ -1,16 +1,32 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.urlToRequire = void 0;
const url_1 = require("url");
function urlToRequire(url) {
const path_1 = __importDefault(require("path"));
function urlToRequire(url, transformAssetUrlsOption = {}) {
const returnValue = `"${url}"`;
// same logic as in transform-require.js
const firstChar = url.charAt(0);
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
if (firstChar === '~') {
const secondChar = url.charAt(1);
url = url.slice(secondChar === '/' ? 2 : 1);
if (firstChar === '~') {
const secondChar = url.charAt(1);
url = url.slice(secondChar === '/' ? 2 : 1);
}
const uriParts = parseUriParts(url);
if (transformAssetUrlsOption.base) {
// explicit base - directly rewrite the url into absolute url
// does not apply to absolute urls or urls that start with `@`
// since they are aliases
if (firstChar === '.' || firstChar === '~') {
// when packaged in the browser, path will be using the posix-
// only version provided by rollup-plugin-node-builtins.
return `"${(path_1.default.posix || path_1.default).join(transformAssetUrlsOption.base, uriParts.path + (uriParts.hash || ''))}"`;
}
const uriParts = parseUriParts(url);
return returnValue;
}
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
if (!uriParts.hash) {
return `require("${url}")`;
}