如何用一个输出分别汇总多个目录

所以,我有一个目录:

mods/ -core/ --index.js --scripts/ ---lots of stuff imported by core/index 

这与典型的汇总时尚,如果你想要汇总到例如mods/core/index.min.js

但是我有很多这些mods/**/目录,我想利用它们被汇总成iifes的事实。 每个mods/**/index.js将会被分配给一个我们假定提供的全局variables,而不是export

mods/core/index.js

 import ui from './scripts/ui/' global.ui = ui 

mods/someMod/scripts/moddedClass.js

 export default class moddedClass extends global.ui.something { /* some functionality extension */} 

mods/someMod/index.js

 import moddedClass from './scripts/moddedClass' global.ui.something = moddedClass 

所以希望你能看到每个mod目录是如何以典型的方式被汇总的,但是,我需要把实际的iifes放在另一个里面,这样:

mods/compiled.js

 (function compiled() { const global = {}; (function core() { //typical rollup iife })(); (function someMod() { //typical rollup iife })(); //a footer like return global or global.init() })(); 

任何帮助这一目标将不胜感激。 我想,最简单的答案是我可以简单地为每个mod的iife获取一个string值,而不是汇总写入文件。

在这一点上,我可以迭代/mods/目录,按照某个modlist.json或其他指定的顺序,然后调用每个/mod/index.js汇总,然后从string中自行构build外部iife。

不过,我想这不是一个完整的解决scheme的源代码映射? 或者可以包含多个内联源代码? 考虑到源映射,我想知道是否需要另一个构build步骤,每个模块在这个系统甚至到达之前都被转发。

使用rollup的bundle.generate api来生成多个iif,并使用fs.appendFile将它们写入一个文件。

对于源代码,你可以使用这个模块(它来自相同的作者汇总) https://github.com/rich-harris/sorcery

好吧,我最终解决这个问题的方式是使用source-map-concat

它基本上是我所描述的,开箱即用。 我唯一需要做的就是在将结果传递给source-map-concat之前, asynchronous迭代mod目录并汇总每个mod,因为rollup.rollup返回一个Promise。

我也最终想要在线源代码,以便代码可以直接注入而不是写入文件,所以我使用convert-source-map

剩下的唯一问题就是子源映射。 如果我正在生成文件,巫术会很好,但我想保留它作为string来源。 现在,它至less会告诉我一个错误来自哪里,而不是它来自的子文件。 如果任何人有关于如何做一个巫术风格操作的信息让我知道。

以下是我的文件中的相关最终代码:

 const rollup = require("rollup") const concat = require("source-map-concat") const convert = require("convert-source-map") const fs = require("fs") const path = require("path") const modsPath = path.join(__dirname, "mods") const getNames = _ => JSON.parse(fs.readFileSync(path.join(modsPath, "loadList.json"), "utf8")) const wrap = (node, mod) => { node.prepend("\n// File: " + mod.source + "\n") } const rolls = {} const bundles = {} const rollupMod = (modName, after) => { let dir = path.join(modsPath, modName), file = path.join(dir, "index.js") rollup.rollup({ entry: file, external: "G", plugins: [] }).then(bundle => { rolls[modName] = bundle.generate({ format: "iife", moduleName: modName, exports: "none", useStrict: false, sourceMap: true }) after() }) } const rollupMods = after => { let names = getNames(), i = 0, rollNext = _ => rollupMod(names[i++], _ => i < names.length - 1? rollNext() : after()) rollNext() } const bundleCode = after => { rollupMods(_ => { let mods = concat(getNames().map(modName => { let mod = rolls[modName] return { source: path.join(modsPath, modName), code: mod.code, map: mod.map } }), { delimiter: "\n", process: wrap }) mods.prepend("(function(){\n") mods.add("\n})();") let result = mods.toStringWithSourceMap({ file: path.basename('.') }) bundles.code = result.code + "\n" + convert.fromObject(result.map).toComment() after(bundles.code) }) } exports.bundleCode = bundleCode