UNPKG

j-vite-plugin-dynamic-base

Version:

Resolve all resource files dynamic publicPath, like Webpack's __webpack_public_path__

77 lines 3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.collectMatchingStringLiterals = exports.parseCode = exports.StringAsBytes = exports.StringLiteralCollector = void 0; const core_1 = require("@swc/core"); const Visitor_1 = __importDefault(require("@swc/core/Visitor")); /** * Traverses an AST (or parts of it) to collect all StringLiterals that contain * needle in their value. */ class StringLiteralCollector extends Visitor_1.default { constructor(needle) { super(); this.baseStringLiterals = []; this.needle = needle; } visitStringLiteral(n) { if (n.value.indexOf(this.needle) !== -1) { this.baseStringLiterals.push(n); } return super.visitStringLiteral(n); } } exports.StringLiteralCollector = StringLiteralCollector; /** * Represents a string as bytes, so it can be sliced via * byte-positions. */ class StringAsBytes { constructor(string) { this.decoder = new TextDecoder(); this.string = (new TextEncoder()).encode(string); } /** * Returns a slice of the string by providing byte indices. * @param from - Byte index to slice from * @param to - Optional byte index to slice to */ slice(from, to) { return this.decoder.decode(new DataView(this.string.buffer, from, to !== undefined ? to - from : undefined)); } } exports.StringAsBytes = StringAsBytes; /** * Parses js code into a AST. * @param code */ function parseCode(code) { return __awaiter(this, void 0, void 0, function* () { const module = yield (0, core_1.parse)(code, { target: 'esnext', syntax: 'ecmascript' }); return [module.span.start, module.body]; }); } exports.parseCode = parseCode; /** * Returns an array of StringLiterals from an AST that contain needle in their value. * @param needle * @param ast */ function collectMatchingStringLiterals(needle, ast) { const visitor = new StringLiteralCollector(needle); visitor.visitModuleItems(ast); return visitor.baseStringLiterals; } exports.collectMatchingStringLiterals = collectMatchingStringLiterals; //# sourceMappingURL=ast.js.map