foundry-dsa41-game/node_modules/@joneff/baka/dist/parse.js

100 lines
3.3 KiB
JavaScript

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var parse_exports = {};
__export(parse_exports, {
parse: () => parse
});
module.exports = __toCommonJS(parse_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_sass_import_resolver = __toESM(require("@joneff/sass-import-resolver"));
const RE_IMPORT = /^[ \t]*@import[ \t]+["']?(.*?)["']?;[ \t]*(?:\/\/)?[ \t]*(.*?)?$/gm;
function normalizePath(url, cwd) {
let result = import_path.default.posix.resolve(url);
if (typeof cwd === "string") {
result = result.replace(`${cwd}/`, "");
}
return result;
}
function importReplacer(matchedLine, matchedPath, annotation, context) {
const {
importedPaths,
ignoredFiles
} = context;
const cwd = import_path.default.resolve(context.cwd || process.cwd());
const nodeModules = import_path.default.resolve(cwd, context.nodeModules || "node_modules");
const result = [];
let url;
let directive;
if (typeof annotation === "string" && annotation.startsWith("baka:")) {
directive = annotation.substring(5);
}
url = import_sass_import_resolver.default.resolve({
file: matchedPath,
prev: importedPaths[importedPaths.length - 1],
includePaths: [nodeModules],
nodeModules
});
if (ignoredFiles.has(url)) {
return matchedLine;
}
if (directive === "skip") {
return matchedLine;
}
if (directive === "ignore") {
ignoredFiles.add(url);
return matchedLine;
}
result.push(`// #region ${matchedLine} -> ${normalizePath(url, cwd)}`);
result.push(parse(url, context));
result.push("// #endregion");
return result.join("\n");
}
function parse(file, options) {
const {
importedFiles,
importedPaths,
ignoredFiles
} = options;
if (ignoredFiles.has(file)) {
return "";
}
if (importedFiles.has(file)) {
return "// File already imported_once. Skipping output.";
}
const buffer = import_fs.default.readFileSync(file, "utf8");
let output = "";
importedPaths.push(import_path.default.dirname(file));
importedFiles.add(file);
output += buffer.replace(RE_IMPORT, (match, filePath, annotation) => {
return importReplacer(match, filePath, annotation, options);
});
importedPaths.pop();
return output;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
parse
});