我使用react-create-app创建了这个应用程序。当我导入.json5文件时,它将其作为静态文件,将其复制到build/dist文件夹,并在导入它的位置返回文件的相对路径:
import foo from './some-file.json5'
console.log(foo); // logs: /static/media/some-file.hash.json5然后,当我服务我的应用程序时,我可以轻松地获取这个文件。
如何使用rollup 及其插件来实现相同的功能?
发布于 2022-01-24 11:24:38
我刚为此写了我自己的插件,享受吧:
import path from "path";
import fs from "fs";
export default function copyAndExportPath() {
return {
name: "copy-and-export-path",
resolveId(source, importer) {
if (source.endsWith(".json5")) {
const absolutePath = path.resolve(path.dirname(importer), source);
return absolutePath;
}
},
load(id, importer) {
if (id.endsWith(".json5")) {
const [base, dest] = id.split("src");
const absolutePathDest = path.join(base, "dist", dest);
const destDir = path.dirname(absolutePathDest);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true }, (error) => {
if (error) {
throw error;
}
});
}
if (fs.existsSync(destDir)) {
fs.copyFileSync(id, absolutePathDest);
}
return `export default ".${dest}"`;
}
return null;
},
};
}https://stackoverflow.com/questions/70791055
复制相似问题