Allow to setup custom exports module.exports
/export
for modules.
⚠ Be careful, existing exports (
module.exports
/exports
) will be overwritten.
To begin, you'll need to install exports-loader
:
$ npm install exports-loader --save-dev
Then add the loader to the desired require
calls. For example:
const { myFunction } = require('exports-loader?myFunction!./file.js');
// Adds the following code to the file's source:
//
// module.exports = exports = { 'myFunction': myFunction };
myFunction('Hello world');
const {
myVariable,
myFunction,
} = require('exports-loader?myVariable,myFunction=helpers.parse!./file.js');
// Adds the following code to the file's source:
//
// module.exports = exports = { 'myVariable' : myVariable, 'myFunction': helpers.parse };
const newVariable = myVariable + '!!!';
console.log(newVariable);
myFunction('Hello world');
const { file } = require('exports-loader?[name]!./file.js');
// Adds the following code to the file's source:
//
// module.exports = exports = { 'file' : file };
file('string');
webpack.config.js
module.exports = {
module: {
rules: [
{
// You can use `regexp`
// test: /vendor\.js/$
test: require.resolve('./path/to/vendor.js'),
loader: 'exports-loader',
options: {
myFunction: true,
myVariable: true,
myNestedFunction: 'lib.parse',
'[name]': true,
},
},
],
},
};
And run webpack
via your preferred method.
Please take a moment to read our contributing guidelines if you haven't yet done so.