如何捆绑commonjs模块,除了指定的要求

时间:2016-05-16 22:16:09

标签: javascript gulp webpack browserify commonjs

如何通过webpack或gulp + browserify捆绑除指定的要求之外,例如忽略var module = require(pathToModule);所以生成的包也包含[username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message Log Message Overflowing Log Message Overflowing Log Message Overflowing Log Message Overflowing [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message Log Message Overflowing Log Message Overflowing Log Message Overflowing [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message [username] [object] [ip address] [datetime] [pid] :>MESSAGE TYPE:Message

2 个答案:

答案 0 :(得分:2)

这可以通过webpack externals option

完成
  

您也可以根据需要使用externals选项   将现有API导入到捆绑包中。即你想使用jquery   来自CDN(单独的<script&gt;标记),仍然想要require("jquery")   在你的包中。只需将其指定为external: { externals: { jquery: "jQuery" } }

webpack.config

{
    externals: {
        // require("jquery") is external and available
        //  on the global var jQuery
        "jquery": "jQuery"
    }
}

如果您希望webpack忽略外部模块并在输出中保留require语句,则可以使用null loader

loaders: [{
    test: /@amperka\/.*/,
    loader: 'null'
}

答案 1 :(得分:0)

使用Webpack中的externals设置指定将在捆绑包外部加载所需的模块。

{
  ...

  externals: {
      // require("jquery") is external and available on the global var jQuery
      "jquery": "jQuery"
  }

  ...
}

然后,在您的包中调用require("jquery")将改为引用全局变量jQuery - 这显然要求您在包之前加载jQuery,例如

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
<script src="bundle.js"></script>