Goal: For some libraries matching a pattern P, have webpack
emit / compile the require
statement as is.
Example:
Suppose I have a mylib
that I want to got through as-is
, so that require works at run-time.
and code like this.
var b = require("./some.stuff.that.webpack.should.inline");
a = require('mylib/should/stay/a/Require');
I want output that looks like this
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
a = __webpack_require__(1);
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = require('mylib/should/stay/a/Require');
I know a Plugin can do this, but I am having trouble intercepting the right event/ understanding the plugin docs
Tried so far:
1. external
... this assumes definition is somewhere else
2. IgnorePlugin
gives a webpackMissingModule
... the opposite of what I want.
Webpack is an aggressive and powerful module bundler for JavaScript applications. It packages all the modules in your application into one or more bundles (often, just one) and serves it to the browser.
Webpack externals tell Webpack to exclude a certain import from the bundle. Often externals are used to exclude imports that will be loaded via CDN. For example, suppose you are implementing server-side rendering with Vue and Express, but your client-side code imports Vue via a CDN.
Webpack is a free and open-source module bundler for JavaScript. It is made primarily for JavaScript, but it can transform front-end assets such as HTML, CSS, and images if the corresponding loaders are included. Webpack takes modules with dependencies and generates static assets representing those modules.
With your first attempt you probably was referring to https://webpack.js.org/configuration/externals/, so most likely you were very close to come up with a solution.
To be honest using externals
might be bit unintuitive, as it is not fully documented and it requires to specify the loading mechanism as a part of a string (instead of proper js object).
To instruct webpack to leave some require
as-is, please use something like that in your config file (tested with v4.25.5):
const IGNORED = ['dep1', 'dep2']
module.exports = {
// ...
// other options
// ...
externals: IGNORED.reduce((acc, p) => (acc[p] = `commonjs ${p}`, acc), {})
};
If you need a bit more flexibility use the function
approach:
externals: (_, req, cb) => {
if (IGNORED.indexOf(req) >= 0) {
return cb(null, `commonjs ${req}`)
}
cb()
}
If you omit commonjs
global scope will be used to resolve the dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With