UNPKG

@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.

158 lines 6.78 kB
import { Logger } from '../../utils/logger.js'; import { normalizePath, revertNormalizePath } from './utils.js'; const contextCache = new Map(); export function farmContextToViteContext(farmContext, currentHandlingFile, pluginName, hookName, config, hookContext) { const cacheKey = pluginName + hookName + currentHandlingFile; if (contextCache.has(cacheKey)) { return contextCache.get(cacheKey); } const logger = new Logger(); const log = (message) => { if (typeof message === 'function') { message = message(); } console.log(message); }; const cacheError = () => { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because cache(called by hook ${pluginName}.${hookName}) is not supported in Farm`); }; const viteContext = { addWatchFile: (id) => { if (!currentHandlingFile) { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because addWatchFile(called by hook ${pluginName}.${hookName}) can only be called in load hook or transform hook in Farm.`); } farmContext.addWatchFile(currentHandlingFile, id); }, debug: log, emitFile: (params) => { if (params.type === 'asset') { let content = []; if (typeof params.source === 'string') { content = [...Buffer.from(params.source)]; } else { content = [...params.source]; } farmContext.emitFile({ resolvedPath: currentHandlingFile ?? 'vite-plugin-adapter', name: params.fileName ?? params.name, content, resourceType: 'asset' }); return 'vite-plugin-adapter-unsupported-reference-id'; } else { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because emitFile(called by hook ${pluginName}.${hookName}) can only emit asset in Farm.`); } }, error: (message) => { let msgObj = message; if (typeof msgObj !== 'object') { msgObj = { message: message }; } if (msgObj.code && !msgObj.code.startsWith('PLUGIN_')) { msgObj.pluginCode = 'PLUGIN_ERROR'; } else { msgObj.code = 'PLUGIN_ERROR'; } msgObj.plugin = pluginName; msgObj.id = currentHandlingFile; msgObj.hook = hookName; farmContext.error(JSON.stringify(msgObj)); return undefined; }, getFileName: () => { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because getFileName(called by hook ${pluginName}.${hookName}) is not supported in Farm`); }, getModuleIds: () => { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because getModuleIds(called by hook ${pluginName}.${hookName}) is not supported in Farm`); }, getModuleInfo: () => { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because getModuleInfo(called by hook ${pluginName}.${hookName}) is not supported in Farm`); }, getWatchFiles: () => { return farmContext.getWatchFiles(); }, info: log, load: (_) => { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because load(called by hook ${pluginName}.${hookName}) is not supported in Farm`); }, meta: { rollupVersion: '3.29.4', watchMode: config.compilation?.mode !== 'production' }, parse: (_) => { throw new Error(`Vite plugin ${pluginName} is not compatible with Farm for now. Because parse(called by hook ${pluginName}.${hookName}) is not supported in Farm`); }, resolve: async (source, importer, options = {}) => { if (options.custom?.caller === `${pluginName}.${hookName}`) { return null; } // if importer is a windows style absolute path, replace all / with \\ to make it a valid windows path if (/^[a-zA-Z]:\//.test(importer)) { importer = revertNormalizePath(importer); } const farmResolveResult = await farmContext.resolve({ source, importer, kind: options.isEntry ? 'entry' : 'import' }, { meta: hookContext?.meta ?? {}, caller: `${pluginName}.${hookName}` }); if (farmResolveResult) { return { id: normalizePath(farmResolveResult.resolvedPath), external: farmResolveResult.external, resolvedBy: 'vite-plugin-adapter-farm-resolve', moduleSideEffects: farmResolveResult.sideEffects, meta: { ...farmResolveResult.meta, caller: `${pluginName}.${hookName}` }, // TODO these 2 options are not supported in farm assertions: {}, syntheticNamedExports: false }; } return null; }, setAssetSource(assetReferenceId, source) { this.emitFile({ type: 'asset', source, name: assetReferenceId }); }, warn: (message) => { if (typeof message === 'object') { farmContext.warn(JSON.stringify(message)); } else if (typeof message === 'function') { farmContext.warn(JSON.stringify(message())); } else { farmContext.warn(message); } }, cache: { set: cacheError, get: cacheError, delete: cacheError, has: cacheError }, moduleIds: new Set()[Symbol.iterator](), // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore Vite specific property getCombinedSourcemap() { logger.warn('`vite-plugin-adapter`: getCombinedSourcemap is not supported in Farm for now. It will always return undefined.'); return undefined; } }; contextCache.set(cacheKey, viteContext); return viteContext; } //# sourceMappingURL=farm-to-vite-context.js.map