'use strict'; var fs = require('fs'); var bundle = require('@fluent/bundle'); function VitePluginFtl(options) { const { autoLoadData = false } = options ?? {}; async function transformFtl(file, content) { let ftlData = {}; let resultLines = []; if (autoLoadData) { const ftlDataFilePath = `${file}.json`; if (!fs.existsSync(ftlDataFilePath)) { throw new Error(`${ftlDataFilePath}.json is not found. If you want close auto load data, please set 'autoLoadData' false.`); } ftlData = JSON.parse(fs.readFileSync(`${file}.json`, { encoding: 'utf-8' })); resultLines.push(`import json from '${file}.json';`); } const rsrs = new bundle.FluentResource(content); resultLines.push(`export const rsrs = ${JSON.stringify(rsrs)};`); resultLines.push(`export const data = ${JSON.stringify(ftlData)};`); resultLines.push('export default { rsrs, data };'); return resultLines.join(''); } return { name: 'vite-plugin-fluent-js-ftl', enforce: 'pre', async transform(content, file) { if (!/\.ftl$/.test(file)) { return; } return await transformFtl(file, content); }, }; } module.exports = VitePluginFtl;