2017-05-28 51 views
2

所以分别汇总多个目录中,我有一个目录:如何与一个输出

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 = 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目录可以以典型的方式来rollup'd但是,我需要再放入实际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获取一个字符串值,而不是汇总写入文件。

在这一点上,我可以迭代/mods/目录,按某些modlist.json或某事指定的顺序,然后调用每个/mod/index.js汇总,然后从字符串自己构建外部iife。

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

回答

1

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

对于您可以使用此模块sourcemaps(从汇总的同一作者的)https://github.com/rich-harris/sorcery

+0

嘿感谢你这一点,至少在正确的方向迈出的一步。虽然你认为你可以解释它,好像我是一个白痴吗?一些基本的代码示例可能?我只是有一些麻烦把整个图片放在一起 –

+0

@DanieClawson我认为rollup的wiki是一个很好的开始,https://github.com/rollup/rollup/wiki/JavaScript-API,你可以参考代码示例维基,如果你有正确的方向,代码很简单。 –

+0

那么现在汇总结束是非常明确的,是的。我被困在理解我需要给魔法的东西上,以便得到正确的最终输出。 具体来说,巫术说它遵循源图的“链”回到根。问题在于,这对我来说意味着我必须创建一个来自单个mod iifes的源代码映射 - >以某种方式来说。 –

1

好了,所以我结束了解决这个问题的方法是使用source-map-concat

,它基本上是我所描述的,正确的盒子外面。我必须做的唯一事情就是异步在将结果传递给source-map-concat之前迭代mod目录并汇总每个mod,因为rollup.rollup返回一个Promise。

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

剩下要解决的唯一问题是子源映射。如果我正在生成文件,巫术会很好,但我想保留它作为字符串来源。现在至少它会告诉我一个错误来自哪个mod,但不是它来自的子文件。如果任何人有关于如何对字符串进行巫术风格操作的信息,请告诉我。

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

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