rollup
Version:
Next-generation ES module bundler
1,395 lines (1,301 loc) • 909 kB
JavaScript
/*
@license
Rollup.js v4.38.0
Sat, 29 Mar 2025 06:28:32 GMT - commit 22b64bcc511dfc40ce463e3f662a928915908713
https://github.com/rollup/rollup
Released under the MIT License.
*/
'use strict';
const parseAst_js = require('./parseAst.js');
const process$1 = require('node:process');
const path = require('node:path');
const require$$0 = require('path');
const native_js = require('../native.js');
const node_perf_hooks = require('node:perf_hooks');
const promises = require('node:fs/promises');
var version = "4.38.0";
function ensureArray$1(items) {
if (Array.isArray(items)) {
return items.filter(Boolean);
}
if (items) {
return [items];
}
return [];
}
var BuildPhase;
(function (BuildPhase) {
BuildPhase[BuildPhase["LOAD_AND_PARSE"] = 0] = "LOAD_AND_PARSE";
BuildPhase[BuildPhase["ANALYSE"] = 1] = "ANALYSE";
BuildPhase[BuildPhase["GENERATE"] = 2] = "GENERATE";
})(BuildPhase || (BuildPhase = {}));
let textEncoder;
const getHash64 = input => native_js.xxhashBase64Url(ensureBuffer(input));
const getHash36 = input => native_js.xxhashBase36(ensureBuffer(input));
const getHash16 = input => native_js.xxhashBase16(ensureBuffer(input));
const hasherByType = {
base36: getHash36,
base64: getHash64,
hex: getHash16
};
function ensureBuffer(input) {
if (typeof input === 'string') {
if (typeof Buffer === 'undefined') {
textEncoder ??= new TextEncoder();
return textEncoder.encode(input);
}
return Buffer.from(input);
}
return input;
}
function getOrCreate(map, key, init) {
const existing = map.get(key);
if (existing !== undefined) {
return existing;
}
const value = init();
map.set(key, value);
return value;
}
function getNewSet() {
return new Set();
}
function getNewArray() {
return [];
}
const chars$1 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
const base = 64;
function toBase64(value) {
let outString = '';
do {
const currentDigit = value % base;
value = (value / base) | 0;
outString = chars$1[currentDigit] + outString;
} while (value !== 0);
return outString;
}
// Four random characters from the private use area to minimize risk of
// conflicts
const hashPlaceholderLeft = '!~{';
const hashPlaceholderRight = '}~';
const hashPlaceholderOverhead = hashPlaceholderLeft.length + hashPlaceholderRight.length;
// This is the size of a 128-bits xxhash with base64url encoding
const MAX_HASH_SIZE = 21;
const DEFAULT_HASH_SIZE = 8;
const getHashPlaceholderGenerator = () => {
let nextIndex = 0;
return (optionName, hashSize) => {
if (hashSize > MAX_HASH_SIZE) {
return parseAst_js.error(parseAst_js.logFailedValidation(`Hashes cannot be longer than ${MAX_HASH_SIZE} characters, received ${hashSize}. Check the "${optionName}" option.`));
}
const placeholder = `${hashPlaceholderLeft}${toBase64(++nextIndex).padStart(hashSize - hashPlaceholderOverhead, '0')}${hashPlaceholderRight}`;
if (placeholder.length > hashSize) {
return parseAst_js.error(parseAst_js.logFailedValidation(`To generate hashes for this number of chunks (currently ${nextIndex}), you need a minimum hash size of ${placeholder.length}, received ${hashSize}. Check the "${optionName}" option.`));
}
return placeholder;
};
};
const REPLACER_REGEX = new RegExp(`${hashPlaceholderLeft}[0-9a-zA-Z_$]{1,${MAX_HASH_SIZE - hashPlaceholderOverhead}}${hashPlaceholderRight}`, 'g');
const replacePlaceholders = (code, hashesByPlaceholder) => code.replace(REPLACER_REGEX, placeholder => hashesByPlaceholder.get(placeholder) || placeholder);
const replaceSinglePlaceholder = (code, placeholder, value) => code.replace(REPLACER_REGEX, match => (match === placeholder ? value : match));
const replacePlaceholdersWithDefaultAndGetContainedPlaceholders = (code, placeholders) => {
const containedPlaceholders = new Set();
const transformedCode = code.replace(REPLACER_REGEX, placeholder => {
if (placeholders.has(placeholder)) {
containedPlaceholders.add(placeholder);
return `${hashPlaceholderLeft}${'0'.repeat(placeholder.length - hashPlaceholderOverhead)}${hashPlaceholderRight}`;
}
return placeholder;
});
return { containedPlaceholders, transformedCode };
};
const lowercaseBundleKeys = Symbol('bundleKeys');
const FILE_PLACEHOLDER = {
type: 'placeholder'
};
const getOutputBundle = (outputBundleBase) => {
const reservedLowercaseBundleKeys = new Set();
return new Proxy(outputBundleBase, {
deleteProperty(target, key) {
if (typeof key === 'string') {
reservedLowercaseBundleKeys.delete(key.toLowerCase());
}
return Reflect.deleteProperty(target, key);
},
get(target, key) {
if (key === lowercaseBundleKeys) {
return reservedLowercaseBundleKeys;
}
return Reflect.get(target, key);
},
set(target, key, value) {
if (typeof key === 'string') {
reservedLowercaseBundleKeys.add(key.toLowerCase());
}
return Reflect.set(target, key, value);
}
});
};
const removeUnreferencedAssets = (outputBundle) => {
const unreferencedAssets = new Set();
const bundleEntries = Object.values(outputBundle);
for (const asset of bundleEntries) {
if (asset.type === 'asset' && asset.needsCodeReference) {
unreferencedAssets.add(asset.fileName);
}
}
for (const chunk of bundleEntries) {
if (chunk.type === 'chunk') {
for (const referencedFile of chunk.referencedFiles) {
if (unreferencedAssets.has(referencedFile)) {
unreferencedAssets.delete(referencedFile);
}
}
}
}
for (const file of unreferencedAssets) {
delete outputBundle[file];
}
};
function renderNamePattern(pattern, patternName, replacements) {
if (parseAst_js.isPathFragment(pattern))
return parseAst_js.error(parseAst_js.logFailedValidation(`Invalid pattern "${pattern}" for "${patternName}", patterns can be neither absolute nor relative paths. If you want your files to be stored in a subdirectory, write its name without a leading slash like this: subdirectory/pattern.`));
return pattern.replace(/\[(\w+)(:\d+)?]/g, (_match, type, size) => {
if (!replacements.hasOwnProperty(type) || (size && type !== 'hash')) {
return parseAst_js.error(parseAst_js.logFailedValidation(`"[${type}${size || ''}]" is not a valid placeholder in the "${patternName}" pattern.`));
}
const replacement = replacements[type](size && Number.parseInt(size.slice(1)));
if (parseAst_js.isPathFragment(replacement))
return parseAst_js.error(parseAst_js.logFailedValidation(`Invalid substitution "${replacement}" for placeholder "[${type}]" in "${patternName}" pattern, can be neither absolute nor relative path.`));
return replacement;
});
}
function makeUnique(name, { [lowercaseBundleKeys]: reservedLowercaseBundleKeys }) {
if (!reservedLowercaseBundleKeys.has(name.toLowerCase()))
return name;
const extension = path.extname(name);
name = name.slice(0, Math.max(0, name.length - extension.length));
let uniqueName, uniqueIndex = 1;
while (reservedLowercaseBundleKeys.has((uniqueName = name + ++uniqueIndex + extension).toLowerCase()))
;
return uniqueName;
}
function generateAssetFileName(name, names, source, originalFileName, originalFileNames, sourceHash, outputOptions, bundle, inputOptions) {
const emittedName = outputOptions.sanitizeFileName(name || 'asset');
return makeUnique(renderNamePattern(typeof outputOptions.assetFileNames === 'function'
? outputOptions.assetFileNames({
// Additionally, this should be non-enumerable in the next major
get name() {
parseAst_js.warnDeprecation('Accessing the "name" property of emitted assets when generating the file name is deprecated. Use the "names" property instead.', parseAst_js.URL_GENERATEBUNDLE, false, inputOptions);
return name;
},
names,
// Additionally, this should be non-enumerable in the next major
get originalFileName() {
parseAst_js.warnDeprecation('Accessing the "originalFileName" property of emitted assets when generating the file name is deprecated. Use the "originalFileNames" property instead.', parseAst_js.URL_GENERATEBUNDLE, false, inputOptions);
return originalFileName;
},
originalFileNames,
source,
type: 'asset'
})
: outputOptions.assetFileNames, 'output.assetFileNames', {
ext: () => path.extname(emittedName).slice(1),
extname: () => path.extname(emittedName),
hash: size => sourceHash.slice(0, Math.max(0, size || DEFAULT_HASH_SIZE)),
name: () => emittedName.slice(0, Math.max(0, emittedName.length - path.extname(emittedName).length))
}), bundle);
}
function reserveFileNameInBundle(fileName, { bundle }, log) {
if (bundle[lowercaseBundleKeys].has(fileName.toLowerCase())) {
log(parseAst_js.LOGLEVEL_WARN, parseAst_js.logFileNameConflict(fileName));
}
else {
bundle[fileName] = FILE_PLACEHOLDER;
}
}
const emittedFileTypes = new Set(['chunk', 'asset', 'prebuilt-chunk']);
function hasValidType(emittedFile) {
return Boolean(emittedFile &&
emittedFileTypes.has(emittedFile.type));
}
function hasValidName(emittedFile) {
const validatedName = emittedFile.fileName || emittedFile.name;
return !validatedName || (typeof validatedName === 'string' && !parseAst_js.isPathFragment(validatedName));
}
function getValidSource(source, emittedFile, fileReferenceId) {
if (!(typeof source === 'string' || source instanceof Uint8Array)) {
const assetName = emittedFile.fileName || emittedFile.name || fileReferenceId;
return parseAst_js.error(parseAst_js.logFailedValidation(`Could not set source for ${typeof assetName === 'string' ? `asset "${assetName}"` : 'unnamed asset'}, asset source needs to be a string, Uint8Array or Buffer.`));
}
return source;
}
function getAssetFileName(file, referenceId) {
if (typeof file.fileName !== 'string') {
return parseAst_js.error(parseAst_js.logAssetNotFinalisedForFileName(file.name || referenceId));
}
return file.fileName;
}
function getChunkFileName(file, facadeChunkByModule) {
if (file.fileName) {
return file.fileName;
}
if (facadeChunkByModule) {
return facadeChunkByModule.get(file.module).getFileName();
}
return parseAst_js.error(parseAst_js.logChunkNotGeneratedForFileName(file.fileName || file.name));
}
class FileEmitter {
constructor(graph, options, baseFileEmitter) {
this.graph = graph;
this.options = options;
this.facadeChunkByModule = null;
this.nextIdBase = 1;
this.output = null;
this.outputFileEmitters = [];
this.emitFile = (emittedFile) => {
if (!hasValidType(emittedFile)) {
return parseAst_js.error(parseAst_js.logFailedValidation(`Emitted files must be of type "asset", "chunk" or "prebuilt-chunk", received "${emittedFile && emittedFile.type}".`));
}
if (emittedFile.type === 'prebuilt-chunk') {
return this.emitPrebuiltChunk(emittedFile);
}
if (!hasValidName(emittedFile)) {
return parseAst_js.error(parseAst_js.logFailedValidation(`The "fileName" or "name" properties of emitted chunks and assets must be strings that are neither absolute nor relative paths, received "${emittedFile.fileName || emittedFile.name}".`));
}
if (emittedFile.type === 'chunk') {
return this.emitChunk(emittedFile);
}
return this.emitAsset(emittedFile);
};
this.finaliseAssets = () => {
for (const [referenceId, emittedFile] of this.filesByReferenceId) {
if (emittedFile.type === 'asset' && typeof emittedFile.fileName !== 'string')
return parseAst_js.error(parseAst_js.logNoAssetSourceSet(emittedFile.name || referenceId));
}
};
this.getFileName = (fileReferenceId) => {
const emittedFile = this.filesByReferenceId.get(fileReferenceId);
if (!emittedFile)
return parseAst_js.error(parseAst_js.logFileReferenceIdNotFoundForFilename(fileReferenceId));
if (emittedFile.type === 'chunk') {
return getChunkFileName(emittedFile, this.facadeChunkByModule);
}
if (emittedFile.type === 'prebuilt-chunk') {
return emittedFile.fileName;
}
return getAssetFileName(emittedFile, fileReferenceId);
};
this.setAssetSource = (referenceId, requestedSource) => {
const consumedFile = this.filesByReferenceId.get(referenceId);
if (!consumedFile)
return parseAst_js.error(parseAst_js.logAssetReferenceIdNotFoundForSetSource(referenceId));
if (consumedFile.type !== 'asset') {
return parseAst_js.error(parseAst_js.logFailedValidation(`Asset sources can only be set for emitted assets but "${referenceId}" is an emitted chunk.`));
}
if (consumedFile.source !== undefined) {
return parseAst_js.error(parseAst_js.logAssetSourceAlreadySet(consumedFile.name || referenceId));
}
const source = getValidSource(requestedSource, consumedFile, referenceId);
if (this.output) {
this.finalizeAdditionalAsset(consumedFile, source, this.output);
}
else {
consumedFile.source = source;
for (const emitter of this.outputFileEmitters) {
emitter.finalizeAdditionalAsset(consumedFile, source, emitter.output);
}
}
};
this.setChunkInformation = (facadeChunkByModule) => {
this.facadeChunkByModule = facadeChunkByModule;
};
this.setOutputBundle = (bundle, outputOptions) => {
const getHash = hasherByType[outputOptions.hashCharacters];
const output = (this.output = {
bundle,
fileNamesBySourceHash: new Map(),
getHash,
outputOptions
});
for (const emittedFile of this.filesByReferenceId.values()) {
if (emittedFile.fileName) {
reserveFileNameInBundle(emittedFile.fileName, output, this.options.onLog);
}
}
const consumedAssetsByHash = new Map();
for (const consumedFile of this.filesByReferenceId.values()) {
if (consumedFile.type === 'asset' && consumedFile.source !== undefined) {
if (consumedFile.fileName) {
this.finalizeAdditionalAsset(consumedFile, consumedFile.source, output);
}
else {
const sourceHash = getHash(consumedFile.source);
getOrCreate(consumedAssetsByHash, sourceHash, () => []).push(consumedFile);
}
}
else if (consumedFile.type === 'prebuilt-chunk') {
this.output.bundle[consumedFile.fileName] = this.createPrebuiltChunk(consumedFile);
}
}
for (const [sourceHash, consumedFiles] of consumedAssetsByHash) {
this.finalizeAssetsWithSameSource(consumedFiles, sourceHash, output);
}
};
this.filesByReferenceId = baseFileEmitter
? new Map(baseFileEmitter.filesByReferenceId)
: new Map();
baseFileEmitter?.addOutputFileEmitter(this);
}
addOutputFileEmitter(outputFileEmitter) {
this.outputFileEmitters.push(outputFileEmitter);
}
assignReferenceId(file, idBase) {
let referenceId = idBase;
do {
referenceId = getHash64(referenceId).slice(0, 8).replaceAll('-', '$');
} while (this.filesByReferenceId.has(referenceId) ||
this.outputFileEmitters.some(({ filesByReferenceId }) => filesByReferenceId.has(referenceId)));
file.referenceId = referenceId;
this.filesByReferenceId.set(referenceId, file);
for (const { filesByReferenceId } of this.outputFileEmitters) {
filesByReferenceId.set(referenceId, file);
}
return referenceId;
}
createPrebuiltChunk(prebuiltChunk) {
return {
code: prebuiltChunk.code,
dynamicImports: [],
exports: prebuiltChunk.exports || [],
facadeModuleId: null,
fileName: prebuiltChunk.fileName,
implicitlyLoadedBefore: [],
importedBindings: {},
imports: [],
isDynamicEntry: false,
isEntry: false,
isImplicitEntry: false,
map: prebuiltChunk.map || null,
moduleIds: [],
modules: {},
name: prebuiltChunk.fileName,
preliminaryFileName: prebuiltChunk.fileName,
referencedFiles: [],
sourcemapFileName: prebuiltChunk.sourcemapFileName || null,
type: 'chunk'
};
}
emitAsset(emittedAsset) {
const source = emittedAsset.source === undefined
? undefined
: getValidSource(emittedAsset.source, emittedAsset, null);
const originalFileName = emittedAsset.originalFileName || null;
if (typeof originalFileName === 'string') {
this.graph.watchFiles[originalFileName] = true;
}
const consumedAsset = {
fileName: emittedAsset.fileName,
name: emittedAsset.name,
needsCodeReference: !!emittedAsset.needsCodeReference,
originalFileName,
referenceId: '',
source,
type: 'asset'
};
const referenceId = this.assignReferenceId(consumedAsset, emittedAsset.fileName || emittedAsset.name || String(this.nextIdBase++));
if (this.output) {
this.emitAssetWithReferenceId(consumedAsset, this.output);
}
else {
for (const fileEmitter of this.outputFileEmitters) {
fileEmitter.emitAssetWithReferenceId(consumedAsset, fileEmitter.output);
}
}
return referenceId;
}
emitAssetWithReferenceId(consumedAsset, output) {
const { fileName, source } = consumedAsset;
if (fileName) {
reserveFileNameInBundle(fileName, output, this.options.onLog);
}
if (source !== undefined) {
this.finalizeAdditionalAsset(consumedAsset, source, output);
}
}
emitChunk(emittedChunk) {
if (this.graph.phase > BuildPhase.LOAD_AND_PARSE) {
return parseAst_js.error(parseAst_js.logInvalidRollupPhaseForChunkEmission());
}
if (typeof emittedChunk.id !== 'string') {
return parseAst_js.error(parseAst_js.logFailedValidation(`Emitted chunks need to have a valid string id, received "${emittedChunk.id}"`));
}
const consumedChunk = {
fileName: emittedChunk.fileName,
module: null,
name: emittedChunk.name || emittedChunk.id,
referenceId: '',
type: 'chunk'
};
this.graph.moduleLoader
.emitChunk(emittedChunk)
.then(module => (consumedChunk.module = module))
.catch(() => {
// Avoid unhandled Promise rejection as the error will be thrown later
// once module loading has finished
});
return this.assignReferenceId(consumedChunk, emittedChunk.id);
}
emitPrebuiltChunk(emitPrebuiltChunk) {
if (typeof emitPrebuiltChunk.code !== 'string') {
return parseAst_js.error(parseAst_js.logFailedValidation(`Emitted prebuilt chunks need to have a valid string code, received "${emitPrebuiltChunk.code}".`));
}
if (typeof emitPrebuiltChunk.fileName !== 'string' ||
parseAst_js.isPathFragment(emitPrebuiltChunk.fileName)) {
return parseAst_js.error(parseAst_js.logFailedValidation(`The "fileName" property of emitted prebuilt chunks must be strings that are neither absolute nor relative paths, received "${emitPrebuiltChunk.fileName}".`));
}
const consumedPrebuiltChunk = {
code: emitPrebuiltChunk.code,
exports: emitPrebuiltChunk.exports,
fileName: emitPrebuiltChunk.fileName,
map: emitPrebuiltChunk.map,
referenceId: '',
type: 'prebuilt-chunk'
};
const referenceId = this.assignReferenceId(consumedPrebuiltChunk, consumedPrebuiltChunk.fileName);
if (this.output) {
this.output.bundle[consumedPrebuiltChunk.fileName] =
this.createPrebuiltChunk(consumedPrebuiltChunk);
}
return referenceId;
}
finalizeAdditionalAsset(consumedFile, source, { bundle, fileNamesBySourceHash, getHash, outputOptions }) {
let { fileName, name, needsCodeReference, originalFileName, referenceId } = consumedFile;
// Deduplicate assets if an explicit fileName is not provided
if (!fileName) {
const sourceHash = getHash(source);
fileName = fileNamesBySourceHash.get(sourceHash);
if (!fileName) {
fileName = generateAssetFileName(name, name ? [name] : [], source, originalFileName, originalFileName ? [originalFileName] : [], sourceHash, outputOptions, bundle, this.options);
fileNamesBySourceHash.set(sourceHash, fileName);
}
}
// We must not modify the original assets to avoid interaction between outputs
const assetWithFileName = { ...consumedFile, fileName, source };
this.filesByReferenceId.set(referenceId, assetWithFileName);
const existingAsset = bundle[fileName];
if (existingAsset?.type === 'asset') {
existingAsset.needsCodeReference &&= needsCodeReference;
if (name) {
existingAsset.names.push(name);
}
if (originalFileName) {
existingAsset.originalFileNames.push(originalFileName);
}
}
else {
const { options } = this;
bundle[fileName] = {
fileName,
get name() {
// Additionally, this should be non-enumerable in the next major
parseAst_js.warnDeprecation('Accessing the "name" property of emitted assets in the bundle is deprecated. Use the "names" property instead.', parseAst_js.URL_GENERATEBUNDLE, false, options);
return name;
},
names: name ? [name] : [],
needsCodeReference,
get originalFileName() {
// Additionally, this should be non-enumerable in the next major
parseAst_js.warnDeprecation('Accessing the "originalFileName" property of emitted assets in the bundle is deprecated. Use the "originalFileNames" property instead.', parseAst_js.URL_GENERATEBUNDLE, false, options);
return originalFileName;
},
originalFileNames: originalFileName ? [originalFileName] : [],
source,
type: 'asset'
};
}
}
finalizeAssetsWithSameSource(consumedFiles, sourceHash, { bundle, fileNamesBySourceHash, outputOptions }) {
const { names, originalFileNames } = getNamesFromAssets(consumedFiles);
let fileName = '';
let usedConsumedFile;
let needsCodeReference = true;
for (const consumedFile of consumedFiles) {
needsCodeReference &&= consumedFile.needsCodeReference;
const assetFileName = generateAssetFileName(consumedFile.name, names, consumedFile.source, consumedFile.originalFileName, originalFileNames, sourceHash, outputOptions, bundle, this.options);
if (!fileName ||
assetFileName.length < fileName.length ||
(assetFileName.length === fileName.length && assetFileName < fileName)) {
fileName = assetFileName;
usedConsumedFile = consumedFile;
}
}
fileNamesBySourceHash.set(sourceHash, fileName);
for (const consumedFile of consumedFiles) {
// We must not modify the original assets to avoid interaction between outputs
const assetWithFileName = { ...consumedFile, fileName };
this.filesByReferenceId.set(consumedFile.referenceId, assetWithFileName);
}
const { options } = this;
bundle[fileName] = {
fileName,
get name() {
// Additionally, this should be non-enumerable in the next major
parseAst_js.warnDeprecation('Accessing the "name" property of emitted assets in the bundle is deprecated. Use the "names" property instead.', parseAst_js.URL_GENERATEBUNDLE, false, options);
return usedConsumedFile.name;
},
names,
needsCodeReference,
get originalFileName() {
// Additionally, this should be non-enumerable in the next major
parseAst_js.warnDeprecation('Accessing the "originalFileName" property of emitted assets in the bundle is deprecated. Use the "originalFileNames" property instead.', parseAst_js.URL_GENERATEBUNDLE, false, options);
return usedConsumedFile.originalFileName;
},
originalFileNames,
source: usedConsumedFile.source,
type: 'asset'
};
}
}
function getNamesFromAssets(consumedFiles) {
const names = [];
const originalFileNames = [];
for (const { name, originalFileName } of consumedFiles) {
if (typeof name === 'string') {
names.push(name);
}
if (originalFileName) {
originalFileNames.push(originalFileName);
}
}
originalFileNames.sort();
// Sort by length first and then alphabetically so that the order is stable
// and the shortest names come first
names.sort((a, b) => a.length - b.length || (a > b ? 1 : a === b ? 0 : -1));
return { names, originalFileNames };
}
const doNothing = () => { };
async function asyncFlatten(array) {
do {
array = (await Promise.all(array)).flat(Infinity);
} while (array.some((v) => v?.then));
return array;
}
const getOnLog = (config, logLevel, printLog = defaultPrintLog) => {
const { onwarn, onLog } = config;
const defaultOnLog = getDefaultOnLog(printLog, onwarn);
if (onLog) {
const minimalPriority = parseAst_js.logLevelPriority[logLevel];
return (level, log) => onLog(level, addLogToString(log), (level, handledLog) => {
if (level === parseAst_js.LOGLEVEL_ERROR) {
return parseAst_js.error(normalizeLog(handledLog));
}
if (parseAst_js.logLevelPriority[level] >= minimalPriority) {
defaultOnLog(level, normalizeLog(handledLog));
}
});
}
return defaultOnLog;
};
const getDefaultOnLog = (printLog, onwarn) => onwarn
? (level, log) => {
if (level === parseAst_js.LOGLEVEL_WARN) {
onwarn(addLogToString(log), warning => printLog(parseAst_js.LOGLEVEL_WARN, normalizeLog(warning)));
}
else {
printLog(level, log);
}
}
: printLog;
const addLogToString = (log) => {
Object.defineProperty(log, 'toString', {
value: () => log.message,
writable: true
});
return log;
};
const normalizeLog = (log) => typeof log === 'string'
? { message: log }
: typeof log === 'function'
? normalizeLog(log())
: log;
const defaultPrintLog = (level, { message }) => {
switch (level) {
case parseAst_js.LOGLEVEL_WARN: {
return console.warn(message);
}
case parseAst_js.LOGLEVEL_DEBUG: {
return console.debug(message);
}
default: {
return console.info(message);
}
}
};
function warnUnknownOptions(passedOptions, validOptions, optionType, log, ignoredKeys = /$./) {
const validOptionSet = new Set(validOptions);
const unknownOptions = Object.keys(passedOptions).filter(key => !(validOptionSet.has(key) || ignoredKeys.test(key)));
if (unknownOptions.length > 0) {
log(parseAst_js.LOGLEVEL_WARN, parseAst_js.logUnknownOption(optionType, unknownOptions, [...validOptionSet].sort()));
}
}
const treeshakePresets = {
recommended: {
annotations: true,
correctVarValueBeforeDeclaration: false,
manualPureFunctions: parseAst_js.EMPTY_ARRAY,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: false
},
safest: {
annotations: true,
correctVarValueBeforeDeclaration: true,
manualPureFunctions: parseAst_js.EMPTY_ARRAY,
moduleSideEffects: () => true,
propertyReadSideEffects: true,
tryCatchDeoptimization: true,
unknownGlobalSideEffects: true
},
smallest: {
annotations: true,
correctVarValueBeforeDeclaration: false,
manualPureFunctions: parseAst_js.EMPTY_ARRAY,
moduleSideEffects: () => false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false,
unknownGlobalSideEffects: false
}
};
const jsxPresets = {
preserve: {
factory: null,
fragment: null,
importSource: null,
mode: 'preserve'
},
'preserve-react': {
factory: 'React.createElement',
fragment: 'React.Fragment',
importSource: 'react',
mode: 'preserve'
},
react: {
factory: 'React.createElement',
fragment: 'React.Fragment',
importSource: 'react',
mode: 'classic'
},
'react-jsx': {
factory: 'React.createElement',
importSource: 'react',
jsxImportSource: 'react/jsx-runtime',
mode: 'automatic'
}
};
const generatedCodePresets = {
es2015: {
arrowFunctions: true,
constBindings: true,
objectShorthand: true,
reservedNamesAsProps: true,
symbols: true
},
es5: {
arrowFunctions: false,
constBindings: false,
objectShorthand: false,
reservedNamesAsProps: true,
symbols: false
}
};
const objectifyOption = (value) => value && typeof value === 'object' ? value : {};
const objectifyOptionWithPresets = (presets, optionName, urlSnippet, additionalValues) => (value) => {
if (typeof value === 'string') {
const preset = presets[value];
if (preset) {
return preset;
}
parseAst_js.error(parseAst_js.logInvalidOption(optionName, urlSnippet, `valid values are ${additionalValues}${parseAst_js.printQuotedStringList(Object.keys(presets))}. You can also supply an object for more fine-grained control`, value));
}
return objectifyOption(value);
};
const getOptionWithPreset = (value, presets, optionName, urlSnippet, additionalValues) => {
const presetName = value?.preset;
if (presetName) {
const preset = presets[presetName];
if (preset) {
return { ...preset, ...value };
}
else {
parseAst_js.error(parseAst_js.logInvalidOption(`${optionName}.preset`, urlSnippet, `valid values are ${parseAst_js.printQuotedStringList(Object.keys(presets))}`, presetName));
}
}
return objectifyOptionWithPresets(presets, optionName, urlSnippet, additionalValues)(value);
};
const normalizePluginOption = async (plugins) => (await asyncFlatten([plugins])).filter(Boolean);
function getLogHandler(level, code, logger, pluginName, logLevel) {
if (parseAst_js.logLevelPriority[level] < parseAst_js.logLevelPriority[logLevel]) {
return doNothing;
}
return (log, pos) => {
if (pos != null) {
logger(parseAst_js.LOGLEVEL_WARN, parseAst_js.logInvalidLogPosition(pluginName));
}
log = normalizeLog(log);
if (log.code && !log.pluginCode) {
log.pluginCode = log.code;
}
log.code = code;
log.plugin = pluginName;
logger(level, log);
};
}
const ANONYMOUS_PLUGIN_PREFIX = 'at position ';
const ANONYMOUS_OUTPUT_PLUGIN_PREFIX = 'at output position ';
function createPluginCache(cache) {
return {
delete(id) {
return delete cache[id];
},
get(id) {
const item = cache[id];
if (!item)
return;
item[0] = 0;
return item[1];
},
has(id) {
const item = cache[id];
if (!item)
return false;
item[0] = 0;
return true;
},
set(id, value) {
cache[id] = [0, value];
}
};
}
function getTrackedPluginCache(pluginCache, onUse) {
return {
delete(id) {
onUse();
return pluginCache.delete(id);
},
get(id) {
onUse();
return pluginCache.get(id);
},
has(id) {
onUse();
return pluginCache.has(id);
},
set(id, value) {
onUse();
return pluginCache.set(id, value);
}
};
}
const NO_CACHE = {
delete() {
return false;
},
get() {
return undefined;
},
has() {
return false;
},
set() { }
};
function uncacheablePluginError(pluginName) {
if (pluginName.startsWith(ANONYMOUS_PLUGIN_PREFIX) ||
pluginName.startsWith(ANONYMOUS_OUTPUT_PLUGIN_PREFIX)) {
return parseAst_js.error(parseAst_js.logAnonymousPluginCache());
}
return parseAst_js.error(parseAst_js.logDuplicatePluginName(pluginName));
}
function getCacheForUncacheablePlugin(pluginName) {
return {
delete() {
return uncacheablePluginError(pluginName);
},
get() {
return uncacheablePluginError(pluginName);
},
has() {
return uncacheablePluginError(pluginName);
},
set() {
return uncacheablePluginError(pluginName);
}
};
}
function getPluginContext(plugin, pluginCache, graph, options, fileEmitter, existingPluginNames) {
const { logLevel, onLog } = options;
let cacheable = true;
if (typeof plugin.cacheKey !== 'string') {
if (plugin.name.startsWith(ANONYMOUS_PLUGIN_PREFIX) ||
plugin.name.startsWith(ANONYMOUS_OUTPUT_PLUGIN_PREFIX) ||
existingPluginNames.has(plugin.name)) {
cacheable = false;
}
else {
existingPluginNames.add(plugin.name);
}
}
let cacheInstance;
if (!pluginCache) {
cacheInstance = NO_CACHE;
}
else if (cacheable) {
const cacheKey = plugin.cacheKey || plugin.name;
cacheInstance = createPluginCache(pluginCache[cacheKey] || (pluginCache[cacheKey] = Object.create(null)));
}
else {
cacheInstance = getCacheForUncacheablePlugin(plugin.name);
}
return {
addWatchFile(id) {
graph.watchFiles[id] = true;
},
cache: cacheInstance,
debug: getLogHandler(parseAst_js.LOGLEVEL_DEBUG, 'PLUGIN_LOG', onLog, plugin.name, logLevel),
emitFile: fileEmitter.emitFile.bind(fileEmitter),
error(error_) {
return parseAst_js.error(parseAst_js.logPluginError(normalizeLog(error_), plugin.name));
},
getFileName: fileEmitter.getFileName,
getModuleIds: () => graph.modulesById.keys(),
getModuleInfo: graph.getModuleInfo,
getWatchFiles: () => Object.keys(graph.watchFiles),
info: getLogHandler(parseAst_js.LOGLEVEL_INFO, 'PLUGIN_LOG', onLog, plugin.name, logLevel),
load(resolvedId) {
return graph.moduleLoader.preloadModule(resolvedId);
},
meta: {
rollupVersion: version,
watchMode: graph.watchMode
},
parse: parseAst_js.parseAst,
resolve(source, importer, { attributes, custom, isEntry, skipSelf } = parseAst_js.BLANK) {
skipSelf ??= true;
return graph.moduleLoader.resolveId(source, importer, custom, isEntry, attributes || parseAst_js.EMPTY_OBJECT, skipSelf ? [{ importer, plugin, source }] : null);
},
setAssetSource: fileEmitter.setAssetSource,
warn: getLogHandler(parseAst_js.LOGLEVEL_WARN, 'PLUGIN_WARNING', onLog, plugin.name, logLevel)
};
}
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function getAugmentedNamespace(n) {
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;
var f = n.default;
if (typeof f == "function") {
var a = function a () {
if (this instanceof a) {
return Reflect.construct(f, arguments, this.constructor);
}
return f.apply(this, arguments);
};
a.prototype = f.prototype;
} else a = {};
Object.defineProperty(a, '__esModule', {value: true});
Object.keys(n).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(n, k);
Object.defineProperty(a, k, d.get ? d : {
enumerable: true,
get: function () {
return n[k];
}
});
});
return a;
}
var utils = {};
var constants;
var hasRequiredConstants;
function requireConstants () {
if (hasRequiredConstants) return constants;
hasRequiredConstants = 1;
const WIN_SLASH = '\\\\/';
const WIN_NO_SLASH = `[^${WIN_SLASH}]`;
/**
* Posix glob regex
*/
const DOT_LITERAL = '\\.';
const PLUS_LITERAL = '\\+';
const QMARK_LITERAL = '\\?';
const SLASH_LITERAL = '\\/';
const ONE_CHAR = '(?=.)';
const QMARK = '[^/]';
const END_ANCHOR = `(?:${SLASH_LITERAL}|$)`;
const START_ANCHOR = `(?:^|${SLASH_LITERAL})`;
const DOTS_SLASH = `${DOT_LITERAL}{1,2}${END_ANCHOR}`;
const NO_DOT = `(?!${DOT_LITERAL})`;
const NO_DOTS = `(?!${START_ANCHOR}${DOTS_SLASH})`;
const NO_DOT_SLASH = `(?!${DOT_LITERAL}{0,1}${END_ANCHOR})`;
const NO_DOTS_SLASH = `(?!${DOTS_SLASH})`;
const QMARK_NO_DOT = `[^.${SLASH_LITERAL}]`;
const STAR = `${QMARK}*?`;
const SEP = '/';
const POSIX_CHARS = {
DOT_LITERAL,
PLUS_LITERAL,
QMARK_LITERAL,
SLASH_LITERAL,
ONE_CHAR,
QMARK,
END_ANCHOR,
DOTS_SLASH,
NO_DOT,
NO_DOTS,
NO_DOT_SLASH,
NO_DOTS_SLASH,
QMARK_NO_DOT,
STAR,
START_ANCHOR,
SEP
};
/**
* Windows glob regex
*/
const WINDOWS_CHARS = {
...POSIX_CHARS,
SLASH_LITERAL: `[${WIN_SLASH}]`,
QMARK: WIN_NO_SLASH,
STAR: `${WIN_NO_SLASH}*?`,
DOTS_SLASH: `${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$)`,
NO_DOT: `(?!${DOT_LITERAL})`,
NO_DOTS: `(?!(?:^|[${WIN_SLASH}])${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
NO_DOT_SLASH: `(?!${DOT_LITERAL}{0,1}(?:[${WIN_SLASH}]|$))`,
NO_DOTS_SLASH: `(?!${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
QMARK_NO_DOT: `[^.${WIN_SLASH}]`,
START_ANCHOR: `(?:^|[${WIN_SLASH}])`,
END_ANCHOR: `(?:[${WIN_SLASH}]|$)`,
SEP: '\\'
};
/**
* POSIX Bracket Regex
*/
const POSIX_REGEX_SOURCE = {
alnum: 'a-zA-Z0-9',
alpha: 'a-zA-Z',
ascii: '\\x00-\\x7F',
blank: ' \\t',
cntrl: '\\x00-\\x1F\\x7F',
digit: '0-9',
graph: '\\x21-\\x7E',
lower: 'a-z',
print: '\\x20-\\x7E ',
punct: '\\-!"#$%&\'()\\*+,./:;<=>?@[\\]^_`{|}~',
space: ' \\t\\r\\n\\v\\f',
upper: 'A-Z',
word: 'A-Za-z0-9_',
xdigit: 'A-Fa-f0-9'
};
constants = {
MAX_LENGTH: 1024 * 64,
POSIX_REGEX_SOURCE,
// regular expressions
REGEX_BACKSLASH: /\\(?![*+?^${}(|)[\]])/g,
REGEX_NON_SPECIAL_CHARS: /^[^@![\].,$*+?^{}()|\\/]+/,
REGEX_SPECIAL_CHARS: /[-*+?.^${}(|)[\]]/,
REGEX_SPECIAL_CHARS_BACKREF: /(\\?)((\W)(\3*))/g,
REGEX_SPECIAL_CHARS_GLOBAL: /([-*+?.^${}(|)[\]])/g,
REGEX_REMOVE_BACKSLASH: /(?:\[.*?[^\\]\]|\\(?=.))/g,
// Replace globs with equivalent patterns to reduce parsing time.
REPLACEMENTS: {
'***': '*',
'**/**': '**',
'**/**/**': '**'
},
// Digits
CHAR_0: 48, /* 0 */
CHAR_9: 57, /* 9 */
// Alphabet chars.
CHAR_UPPERCASE_A: 65, /* A */
CHAR_LOWERCASE_A: 97, /* a */
CHAR_UPPERCASE_Z: 90, /* Z */
CHAR_LOWERCASE_Z: 122, /* z */
CHAR_LEFT_PARENTHESES: 40, /* ( */
CHAR_RIGHT_PARENTHESES: 41, /* ) */
CHAR_ASTERISK: 42, /* * */
// Non-alphabetic chars.
CHAR_AMPERSAND: 38, /* & */
CHAR_AT: 64, /* @ */
CHAR_BACKWARD_SLASH: 92, /* \ */
CHAR_CARRIAGE_RETURN: 13, /* \r */
CHAR_CIRCUMFLEX_ACCENT: 94, /* ^ */
CHAR_COLON: 58, /* : */
CHAR_COMMA: 44, /* , */
CHAR_DOT: 46, /* . */
CHAR_DOUBLE_QUOTE: 34, /* " */
CHAR_EQUAL: 61, /* = */
CHAR_EXCLAMATION_MARK: 33, /* ! */
CHAR_FORM_FEED: 12, /* \f */
CHAR_FORWARD_SLASH: 47, /* / */
CHAR_GRAVE_ACCENT: 96, /* ` */
CHAR_HASH: 35, /* # */
CHAR_HYPHEN_MINUS: 45, /* - */
CHAR_LEFT_ANGLE_BRACKET: 60, /* < */
CHAR_LEFT_CURLY_BRACE: 123, /* { */
CHAR_LEFT_SQUARE_BRACKET: 91, /* [ */
CHAR_LINE_FEED: 10, /* \n */
CHAR_NO_BREAK_SPACE: 160, /* \u00A0 */
CHAR_PERCENT: 37, /* % */
CHAR_PLUS: 43, /* + */
CHAR_QUESTION_MARK: 63, /* ? */
CHAR_RIGHT_ANGLE_BRACKET: 62, /* > */
CHAR_RIGHT_CURLY_BRACE: 125, /* } */
CHAR_RIGHT_SQUARE_BRACKET: 93, /* ] */
CHAR_SEMICOLON: 59, /* ; */
CHAR_SINGLE_QUOTE: 39, /* ' */
CHAR_SPACE: 32, /* */
CHAR_TAB: 9, /* \t */
CHAR_UNDERSCORE: 95, /* _ */
CHAR_VERTICAL_LINE: 124, /* | */
CHAR_ZERO_WIDTH_NOBREAK_SPACE: 65279, /* \uFEFF */
/**
* Create EXTGLOB_CHARS
*/
extglobChars(chars) {
return {
'!': { type: 'negate', open: '(?:(?!(?:', close: `))${chars.STAR})` },
'?': { type: 'qmark', open: '(?:', close: ')?' },
'+': { type: 'plus', open: '(?:', close: ')+' },
'*': { type: 'star', open: '(?:', close: ')*' },
'@': { type: 'at', open: '(?:', close: ')' }
};
},
/**
* Create GLOB_CHARS
*/
globChars(win32) {
return win32 === true ? WINDOWS_CHARS : POSIX_CHARS;
}
};
return constants;
}
/*global navigator*/
var hasRequiredUtils;
function requireUtils () {
if (hasRequiredUtils) return utils;
hasRequiredUtils = 1;
(function (exports) {
const {
REGEX_BACKSLASH,
REGEX_REMOVE_BACKSLASH,
REGEX_SPECIAL_CHARS,
REGEX_SPECIAL_CHARS_GLOBAL
} = /*@__PURE__*/ requireConstants();
exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str);
exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str);
exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1');
exports.toPosixSlashes = str => str.replace(REGEX_BACKSLASH, '/');
exports.isWindows = () => {
if (typeof navigator !== 'undefined' && navigator.platform) {
const platform = navigator.platform.toLowerCase();
return platform === 'win32' || platform === 'windows';
}
if (typeof process !== 'undefined' && process.platform) {
return process.platform === 'win32';
}
return false;
};
exports.removeBackslashes = str => {
return str.replace(REGEX_REMOVE_BACKSLASH, match => {
return match === '\\' ? '' : match;
});
};
exports.escapeLast = (input, char, lastIdx) => {
const idx = input.lastIndexOf(char, lastIdx);
if (idx === -1) return input;
if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1);
return `${input.slice(0, idx)}\\${input.slice(idx)}`;
};
exports.removePrefix = (input, state = {}) => {
let output = input;
if (output.startsWith('./')) {
output = output.slice(2);
state.prefix = './';
}
return output;
};
exports.wrapOutput = (input, state = {}, options = {}) => {
const prepend = options.contains ? '' : '^';
const append = options.contains ? '' : '$';
let output = `${prepend}(?:${input})${append}`;
if (state.negated === true) {
output = `(?:^(?!${output}).*$)`;
}
return output;
};
exports.basename = (path, { windows } = {}) => {
const segs = path.split(windows ? /[\\/]/ : '/');
const last = segs[segs.length - 1];
if (last === '') {
return segs[segs.length - 2];
}
return last;
};
} (utils));
return utils;
}
var scan_1;
var hasRequiredScan;
function requireScan () {
if (hasRequiredScan) return scan_1;
hasRequiredScan = 1;
const utils = /*@__PURE__*/ requireUtils();
const {
CHAR_ASTERISK, /* * */
CHAR_AT, /* @ */
CHAR_BACKWARD_SLASH, /* \ */
CHAR_COMMA, /* , */
CHAR_DOT, /* . */
CHAR_EXCLAMATION_MARK, /* ! */
CHAR_FORWARD_SLASH, /* / */
CHAR_LEFT_CURLY_BRACE, /* { */
CHAR_LEFT_PARENTHESES, /* ( */
CHAR_LEFT_SQUARE_BRACKET, /* [ */
CHAR_PLUS, /* + */
CHAR_QUESTION_MARK, /* ? */
CHAR_RIGHT_CURLY_BRACE, /* } */
CHAR_RIGHT_PARENTHESES, /* ) */
CHAR_RIGHT_SQUARE_BRACKET /* ] */
} = /*@__PURE__*/ requireConstants();
const isPathSeparator = code => {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
};
const depth = token => {
if (token.isPrefix !== true) {
token.depth = token.isGlobstar ? Infinity : 1;
}
};
/**
* Quickly scans a glob pattern and returns an object with a handful of
* useful properties, like `isGlob`, `path` (the leading non-glob, if it exists),
* `glob` (the actual pattern), `negated` (true if the path starts with `!` but not
* with `!(`) and `negatedExtglob` (true if the path starts with `!(`).
*
* ```js
* const pm = require('picomatch');
* console.log(pm.scan('foo/bar/*.js'));
* { isGlob: true, input: 'foo/bar/*.js', base: 'foo/bar', glob: '*.js' }
* ```
* @param {String} `str`
* @param {Object} `options`
* @return {Object} Returns an object with tokens and regex source string.
* @api public
*/
const scan = (input, options) => {
const opts = options || {};
const length = input.length - 1;
const scanToEnd = opts.parts === true || opts.scanToEnd === true;
const slashes = [];
const tokens = [];
const parts = [];
let str = input;
let index = -1;
let start = 0;
let lastIndex = 0;
let isBrace = false;
let isBracket = false;
let isGlob = false;
let isExtglob = false;
let isGlobstar = false;
let braceEscaped = false;
let backslashes = false;
let negated = false;
let negatedExtglob = false;
let finished = false;
let braces = 0;
let prev;
let code;
let token = { value: '', depth: 0, isGlob: false };
const eos = () => index >= length;
const peek = () => str.charCodeAt(index + 1);
const advance = () => {
prev = code;
return str.charCodeAt(++index);
};
while (index < length) {
code = advance();
let next;
if (code === CHAR_BACKWARD_SLASH) {
backslashes = token.backslashes = true;
code = advance();
if (code === CHAR_LEFT_CURLY_BRACE) {
braceEscaped = true;
}
continue;
}
if (braceEscaped === true || code === CHAR_LEFT_CURLY_BRACE) {
braces++;
while (eos() !== true && (code = advance())) {
if (code === CHAR_BACKWARD_SLASH) {
backslashes = token.backslashes = true;
advance();
continue;
}
if (code === CHAR_LEFT_CURLY_BRACE) {
braces++;
continue;
}
if (braceEscaped !== true && code === CHAR_DOT && (code = advance()) === CHAR_DOT) {
isBrace = token.isBrace = true;
isGlob = token.isGlob = true;
finished = true;
if (scanToEnd === true) {
continue;
}
break;
}
if (braceEscaped !== true && code === CHAR_COMMA) {
isBrace = token.isBrace = true;
isGlob = token.isGlob = true;
finished = true;
if (scanToEnd === true) {
continue;
}
break;
}
if (code === CHAR_RIGHT_CURLY_BRACE) {
braces--;
if (braces === 0) {
braceEscaped = false;
isBrace = token.isBrace = true;
finished = true;
break;
}
}
}
if (scanToEnd === true) {
continue;
}
break;
}
if (code === CHAR_FORWARD_SLASH) {
slashes.push(index);
tokens.push(token);
token = { value: '', depth: 0, isGlob: false };
if (finished === true) continue;
if (prev === CHAR_DOT && index === (start + 1)) {
start += 2;
continue;
}
lastIndex = index + 1;
continue;
}
if (opts.noext !== true) {
const isExtglobChar = code === CHAR_PLUS
|| code === CHAR_AT
|| code === CHAR_ASTERISK
|| code === CHAR_QUESTION_MARK
|| code === CHAR_EXCLAMATION_MARK;
if (isExtglobChar === true && peek() === CHAR_LEFT_PARENTHESES) {