Webpack provides the example below in its shimming documentation. In the global exports portion of that page, it gives the following example.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: require.resolve('globals.js'),
use: exports-loader?file,parse=helpers.parse
}
]
}
}
./src/globals.js
var file = 'blah.txt';
var helpers = {
test: function() { console.log('test something'); },
parse: function() { console.log('parse something'); }
};
But when I attempt to build, I get:
ERROR in ./webpack.config.js
Module not found: Error: Can't resolve 'globals.js' in '/workspace/my-app'
Why is globals.js not resolving, and why does the example in their documentation assume it will? Am I missing something? Thanks.
Getting this to work with a global
exports-loaderconfigurationI have this working with the following setup:
src/deps.js // this file just declare a global
filevariablesrc/app.js // entry point of the webpack bundle. It
import'sdeps.js(even ifdeps.jsdoes not have anexportstatement, thanks toexport-loader):webpack.config.js // webpack configuration file
package.json // so we can run webpack locally to the project
With this setup, assuming
webpack.config.js,package.jsonandsrc/are in the root of the project, do:$ npm run webpackTo bundle the scripts, then:
$ node dist/main.jsto check that thefilevariable is being loaded (to load this in a browser will do the same).Getting this to work with an
importspecific configuration.(this comes from this other answer).
In order to do so, you need to
usejust theexports-loader, without any further configuration when you load it in thewebpack.config.js:use: 'exports-loader',And then specify the variables to wrap in an
exportclause in everyimportstatement:import file from 'exports-loader?file!./deps.js'Why the
require.resolve()syntax is not working?I really don't know. The
testclause expects a regex as far as I know (that's why it is calledtestin fact, because of thetestmethod of regex's in javascript) and I'm not used to other kind of valid syntaxes. I see that in your snippet:The
usevalue does not have string quotes. I wonder if this is broking the config and then you get a misleading error, I don't know. I actually believe you just didn't paste the quotes when copy and pasting to stack overflow.