vite-plugin-public-path
Version:
Vite's equivalent of `__webpack_public_path__` in Webpack. Works for `index.html` and modern/legacy build.
88 lines (87 loc) • 3.67 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceStringWithExpression = exports.PlaceholderReplacer = void 0;
const swc = __importStar(require("@swc/core"));
const Visitor_1 = require("@swc/core/Visitor");
class PlaceholderReplacer extends Visitor_1.Visitor {
constructor(placeholder, expression) {
super();
this.placeholder = placeholder;
this.expression = PlaceholderReplacer.parseExpression(expression);
}
static parseExpression(expression) {
return swc.parseSync(expression).body[0].expression;
}
// @ts-ignore We changed expression type
visitStringLiteral(node) {
const stringParts = node.value.split(this.placeholder);
if (stringParts.length === 1)
return node;
const createStringExpression = (str) => ({
type: "StringLiteral",
span: { start: 0, end: 0, ctxt: 0 },
value: str,
hasEscape: true
});
let subExpressions = Array(stringParts.length * 2 - 1);
for (let i = 0; i < stringParts.length; i++) {
subExpressions[i * 2] = createStringExpression(stringParts[i]);
if (i !== stringParts.length - 1) {
subExpressions[i * 2 + 1] = this.expression;
}
}
subExpressions = subExpressions.filter(expr => expr.type !== "StringLiteral" || expr.value !== "");
if (subExpressions.length === 1)
return subExpressions[0];
const createAddExpression = (left) => ({
type: "BinaryExpression",
span: { start: 0, end: 0, ctxt: 0 },
operator: "+",
left: left,
right: null
});
let rootExpression;
let previousExpression;
for (let i = 0; i < subExpressions.length; i++) {
const currentSubExpression = subExpressions[i];
if (i === 0) {
previousExpression = rootExpression = createAddExpression(currentSubExpression);
}
else if (i === subExpressions.length - 1) {
previousExpression.right = currentSubExpression;
}
else {
previousExpression.right = createAddExpression(currentSubExpression);
previousExpression = previousExpression.right;
}
}
return rootExpression;
}
}
exports.PlaceholderReplacer = PlaceholderReplacer;
async function replaceStringWithExpression(code, placeholder, expression, minify) {
const ast = await swc.parse(code);
const replacer = new PlaceholderReplacer(placeholder, expression);
replacer.visitModule(ast);
return (await swc.print(ast, { minify })).code;
}
exports.replaceStringWithExpression = replaceStringWithExpression;