0

How to bundle through webpack or gulp+browserify except specified requires, for example ignore var module = require(pathToModule); so, that the resulting bundle also contain var module = require(pathToModule);

966647
  • 35
  • 1
  • 6
  • "ignore var module = require(pathToModule); so, that the resulting bundle also contain var module = require(pathToModule);" Do you want to ignore the require or include it? – Mike Cluck May 16 '16 at 22:18
  • "Do you want to ignore the require or include it?" - i want to ignore some requires in bundler and include it in result bundle (in my espruino there are modules like "@amperka/button" that connect via require) – 966647 May 16 '16 at 22:55

2 Answers2

2

This can be done with webpack externals option

You can use the externals options for applications too, when you want to import an existing API into the bundle. I.e. you want to use jquery from CDN (separate <script> tag) and still want to require("jquery") in your bundle. Just specify it as external: { externals: { jquery: "jQuery" } }.

webpack.config

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

If you want webpack to ignore the external module and keep the require statement in the output you can use a null loader

loaders: [{
    test: /@amperka\/.*/,
    loader: 'null'
}
Steffen
  • 3,327
  • 2
  • 26
  • 30
  • unfortunately this decision is if there is a global vars, but in my case I need to have some `require('module')` in result bundle - espruino himself resolves this requires – 966647 May 16 '16 at 23:04
  • 1
    Does [this](https://github.com/sheerun/external-loader) help? It allows to require and external module on disk and writes the require statement to the bundle. This also might help: http://stackoverflow.com/a/32061956/1787868 – Steffen May 16 '16 at 23:21
  • whatever say `Module not found: Error: Cannot resolve module '@amperka/buzzer' ... ` – 966647 May 17 '16 at 15:56
  • Not sure why you get this error. could you please provide more information about your environment, sourcecode samples and detailed error (stacktrace) in you question? thanks – Steffen May 20 '16 at 19:11
0

Use the externals setting in Webpack to specify that a required module will be loaded outside of the bundle.

{
  ...

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

  ...
}

So then calls to require("jquery") from within your bundle will instead reference the global variable jQuery - this obviously requires that you have loaded jQuery before your bundle e.g.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
<script src="bundle.js"></script>
spiralx
  • 1,035
  • 7
  • 16