@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
133 lines (131 loc) • 6.54 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_utils_parallelize = require('../utils/parallelize.cjs');
const require_filterInvalidDictionaries = require('../filterInvalidDictionaries.cjs');
const require_buildIntlayerDictionary_processContentDeclaration = require('../buildIntlayerDictionary/processContentDeclaration.cjs');
const require_loadDictionaries_getIntlayerBundle = require('./getIntlayerBundle.cjs');
const require_loadDictionaries_loadMarkdownContentDeclaration = require('./loadMarkdownContentDeclaration.cjs');
const require_loadDictionaries_loadYamlContentDeclaration = require('./loadYamlContentDeclaration.cjs');
const require_loadDictionaries_logTypeScriptErrors = require('./logTypeScriptErrors.cjs');
let node_fs_promises = require("node:fs/promises");
let node_path = require("node:path");
let _intlayer_config_utils = require("@intlayer/config/utils");
let _intlayer_config_file = require("@intlayer/config/file");
//#region src/loadDictionaries/loadContentDeclaration.ts
const formatLocalDictionaries = (dictionariesRecord, configuration) => Object.entries(dictionariesRecord).map(([relativePath, dict]) => ({
...dict,
location: dict.location ?? configuration.dictionary?.location ?? "local",
localId: `${dict.key}::local::${relativePath}`,
filePath: relativePath
}));
const ensureIntlayerBundle = async (configuration) => {
const { system } = configuration;
const { set, isValid } = (0, _intlayer_config_utils.cacheDisk)(configuration, ["intlayer-bundle"], { ttlMs: 1e3 * 60 * 60 * 24 * 5 });
const filePath = (0, node_path.join)(system.cacheDir, "intlayer-bundle.cjs");
if (!await isValid()) {
await (0, node_fs_promises.writeFile)(filePath, await require_loadDictionaries_getIntlayerBundle.getIntlayerBundle(configuration));
await set("ok");
}
return filePath;
};
let cachedExternalDeps = null;
const getExternalDeps = async (baseDir) => {
if (cachedExternalDeps) return cachedExternalDeps;
try {
const packageJSON = await (0, node_fs_promises.readFile)((0, _intlayer_config_utils.getPackageJsonPath)(baseDir).packageJsonPath, "utf-8");
const parsedPackages = JSON.parse(packageJSON);
const allDependencies = Object.keys({
...parsedPackages.dependencies,
...parsedPackages.devDependencies
});
const esmPackagesToBundle = [];
const externalDeps = allDependencies.filter((dep) => !esmPackagesToBundle.includes(dep));
externalDeps.push("esbuild");
cachedExternalDeps = externalDeps;
} catch (error) {
console.warn("Could not read package.json for externalizing dependencies, fallback to empty array", error);
cachedExternalDeps = ["esbuild"];
}
return cachedExternalDeps;
};
const loadContentDeclaration = async (path, configuration, bundleFilePath, options) => {
if ((0, node_path.extname)(path) === ".md" || (0, node_path.extname)(path) === ".mdx") return require_loadDictionaries_loadMarkdownContentDeclaration.loadMarkdownContentDeclaration(path);
if ((0, node_path.extname)(path) === ".yaml" || (0, node_path.extname)(path) === ".yml") return require_loadDictionaries_loadYamlContentDeclaration.loadYamlContentDeclaration(path);
const { build, system } = configuration;
const externalDeps = await getExternalDeps(system.baseDir);
const resolvedBundleFilePath = bundleFilePath ?? await ensureIntlayerBundle(configuration);
try {
return await (0, _intlayer_config_file.loadExternalFile)(path, {
logError: options?.logError,
projectRequire: build.require ?? (0, _intlayer_config_utils.getProjectRequire)(),
buildOptions: {
packages: void 0,
external: externalDeps,
banner: { js: [
`var __filename = ${JSON.stringify(path)};`,
`var __dirname = ${JSON.stringify((0, node_path.dirname)(path))};`,
`globalThis.INTLAYER_FILE_PATH = '${path}';`,
`globalThis.INTLAYER_BASE_DIR = '${configuration.system.baseDir}';`
].join("\n") }
},
aliases: { intlayer: resolvedBundleFilePath },
preloadGlobals: {
INTLAYER_FILE_PATH: path,
INTLAYER_BASE_DIR: configuration.system.baseDir
}
});
} catch (error) {
console.error(`Error loading content declaration at ${path}:`, error);
return;
}
};
const loadContentDeclarations = async (contentDeclarationFilePath, configuration, onStatusUpdate, options) => {
const { build, system } = configuration;
if (build.checkTypes) require_loadDictionaries_logTypeScriptErrors.logTypeScriptErrors(contentDeclarationFilePath, configuration).catch((e) => {
console.error("Error during TypeScript validation:", e);
});
const bundleFilePath = await ensureIntlayerBundle(configuration);
try {
const dictionariesPromises = contentDeclarationFilePath.map(async (path) => {
return {
relativePath: (0, node_path.relative)(system.baseDir, path),
dictionary: await loadContentDeclaration(path, configuration, bundleFilePath, options)
};
});
const contentDeclarations = formatLocalDictionaries((await Promise.all(dictionariesPromises)).reduce((acc, { relativePath, dictionary }) => {
if (dictionary) acc[relativePath] = dictionary;
return acc;
}, {}), configuration).filter((dictionary) => dictionary.location !== "remote");
const listFoundDictionaries = contentDeclarations.map((declaration) => ({
dictionaryKey: declaration.key,
type: "local",
status: "found"
}));
onStatusUpdate?.(listFoundDictionaries);
return require_filterInvalidDictionaries.filterInvalidDictionaries(await require_utils_parallelize.parallelize(contentDeclarations, async (contentDeclaration) => {
if (!contentDeclaration) return;
onStatusUpdate?.([{
dictionaryKey: contentDeclaration.key,
type: "local",
status: "building"
}]);
const processedContentDeclaration = await require_buildIntlayerDictionary_processContentDeclaration.processContentDeclaration(contentDeclaration, configuration);
if (!processedContentDeclaration) return;
onStatusUpdate?.([{
dictionaryKey: processedContentDeclaration.key,
type: "local",
status: "built"
}]);
return processedContentDeclaration;
}), configuration, { checkSchema: false });
} catch {
console.error("Error loading content declarations");
}
return [];
};
//#endregion
exports.ensureIntlayerBundle = ensureIntlayerBundle;
exports.formatLocalDictionaries = formatLocalDictionaries;
exports.loadContentDeclaration = loadContentDeclaration;
exports.loadContentDeclarations = loadContentDeclarations;
//# sourceMappingURL=loadContentDeclaration.cjs.map