所以,我有一个目录:
mods/
-core/
--index.js
--scripts/
---lots of stuff imported by core/index如果您想要汇总到例如mods/core/index.min.js,这与典型的汇总方式一起工作
但我有许多这样的mods/**/目录,我想利用它们被汇总到iifes中的事实。每个mods/**/index.js,而不是export,将赋值给我们假定提供的全局变量:
mods/core/index.js
import ui from './scripts/ui/'
global.ui = uimods/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目录是如何以典型的方式被汇总的,但是,我需要把实际的one放到另一个目录中,这样:
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的生命周期获取一个字符串值,而不是汇总将其写入文件。
在这一点上,我可以按照某个modlist.json或其他东西指定的顺序迭代/mods/目录,并在每个/mod/index.js上调用rollup,然后自己用字符串构建外部生命。
然而,我认为这不是源映射的完整解决方案?或者是否可以包含多个内联源地图?考虑到源代码映射,我想知道是否有必要进行另一个构建步骤,在这个系统到达它之前,每个mod都会被转译。
发布于 2017-05-29 12:32:30
使用rollup的bundle.generate api生成多个file,并使用fs.appendFile将它们写入一个文件。
对于源地图,您可以使用此模块(它来自rollup的同一作者) https://github.com/rich-harris/sorcery
发布于 2017-05-31 06:18:17
好的,我最终解决这个问题的方法是使用source-map-concat
它基本上做到了我所描述的,开箱即用。我唯一需要做的就是异步迭代mod目录,并在将结果传递给source-map-concat之前汇总每个mod,因为rollup.rollup返回一个Promise。
我还想要内嵌的源代码映射,这样代码就可以直接注入,而不是写到文件中,所以我使用了convert-source-map。
剩下的唯一问题就是子源映射。如果我在生成文件,巫术将会非常有效,但我希望将其保留为字符串源。到目前为止,它至少会告诉我错误是来自哪个模块,而不是它来自的子文件。如果任何人有关于如何在字符串上进行巫术风格的操作的信息,请告诉我。
以下是我的文件中的相关最终代码:
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 = bundleCodehttps://stackoverflow.com/questions/44232242
复制相似问题