I am attempting to use the exports-loader
with my webpack config, but I'm running into an issue when trying to configure it.
webpack.config.js
module.exports = {
module: {
rules: [
{
test: require.resolve('./src/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'); }
}
./src/index.js
import { file } from './globals.js'
console.log('the file', file);
My application builds fine, but when I attempt to run it I get:
WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.module.rules[0].test should be one of these:
- configuration.module.rules[0].test: The provided value "./src/globals.js" is not an absolute path!
To be completely clear, I understand the difference between an absolute and a relative path, and I know that the value I'm using is a relative path. My confusion is two-fold:
require.resolve
allows relative paths, so why can I not use it here?- If I cannot use a relative path, how can I refer to that file otherwise?
I tried using an absolute path for the test
property like so:
test: require.resolve(path.join(__dirname, 'src/globals.js'))
But I get this error when I attempt to run:
Error: Cannot find module '/workspace/my-app/dist/src/globals.js
So I'm stuck. How can I properly configure this loader to reference that file appropriately?