@farmfe/core
Version:
Farm is a extremely fast web build tool written in Rust. Farm can start a project in milliseconds and perform HMR within 10ms, making it much faster than similar tools like webpack and vite.
62 lines • 1.95 kB
JavaScript
import fs from 'node:fs';
import { isAbsolute, join } from 'node:path';
export const getAdditionContext = async (cwd, option, currentFile, content, ctx, pluginName) => {
const { globals = [], additionalData } = option;
const result = globals.reduce((result, file) => {
let filepath;
if (isAbsolute(file)) {
filepath = file;
}
else {
filepath = join(cwd, file);
}
try {
result.push(fs.readFileSync(filepath, 'utf-8'));
ctx.addWatchFile(currentFile, filepath);
}
catch (error) {
throwError(pluginName, 'read', error);
}
return result;
}, []);
if (additionalData) {
if (typeof additionalData === 'string') {
result.push(additionalData);
}
else {
result.push(await additionalData(content, currentFile));
}
}
return result.join('\n');
};
export function throwError(pluginName, type, error) {
throw new Error(`[${pluginName} ${type} Error] ${error?.stack ?? error}`);
}
export function getAliasEntries(entries) {
if (!entries || !Object.keys(entries).length) {
return [];
}
if (Array.isArray(entries)) {
return entries.map((entry) => {
return {
find: entry.find,
replacement: entry.replacement
// TODO add support for customResolver
};
});
}
else if (typeof entries === 'object') {
return Object.entries(entries).map(([key, value]) => {
return { find: key, replacement: value };
});
}
// If entries is neither an array nor an object, return an empty array
return [];
}
export function transformAliasWithVite(alias) {
return alias.reduce((acc, item) => {
acc[item.find] = item.replacement;
return acc;
}, {});
}
//# sourceMappingURL=plugin-utils.js.map