Webpack填充失败,原因是来自exports-loader的模块未定义

时间:2019-03-11 15:16:07

标签: javascript webpack exports-loader

我正在尝试使用Webpack的exports-loader导出一个全局变量以在我的输入模块中使用。我基本上已经编写了Webpack文档here中提供的示例。但是,当我尝试使用已导入的模块时,它就是undefined

webpack.config.js

module.exports = {
    module: {
        rules: [
            {
                test: path.join(__dirname, '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); // file is undefined

在配置和使用此加载程序时是否缺少某些内容?我的示例仅以一种方式与Webpack文档中的示例有所不同,那就是配置文件中test属性的值。该示例使用test: require.resolve('globals.js'),但是该代码在构建时就产生了Module not found错误。不知道该示例应如何按记录工作。

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案
相关问题