【问题标题】:Bundle node_modules for browser and keep "app.js" untouched为浏览器捆绑 node_modules 并保持“app.js”不变
【发布时间】:2017-03-04 12:58:38
【问题描述】:

使用 npm 节点模块 greetings 的 js 文件,我在浏览器中使用我的 app.js 和 browserify:

我的 index.html 看起来像这样:

我知道文件app.js的源代码现在嵌入在文件bundle.js中。

我现在的要求是我的生产代码包含app.js 和一个包含节点模块的附加库,如下所示:

这意味着客户端获得了app.js 文件和bundle.js 文件,我现在可以像这样使用app.js 中的节点模块:

基本上我只想要一个bundle.js 用作我的app.js 的库并包含节点模块,我可以将它们包含在浏览器端JavaScript 中。

我也厌倦了使用客户端文件和模块加载器。 require.js 加载 node_modules 但根据this thread 它不起作用。

【问题讨论】:

    标签: npm browserify node-modules


    【解决方案1】:

    你需要使用browserify的-r & -x选项:https://github.com/substack/node-browserify#multiple-bundles

    要编译您的代码,请运行:

    browserify app.js -r ./greetings -o dist/bundle.js
    browserify app.js -x ./greetings -o dist/app.js
    

    dist/bundle.js 将包含 greetings 模块,dist/app.js 将包含您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 1970-01-01
      • 2014-07-04
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多