我正在使用 webpack 1.12.14 并且不能将图像要求为全局常量,例如
const icon = require("icon.svg");
class TestClass {
它失败并显示 [错误:找不到模块 'icon.svg'] 代码:'MODULE_NOT_FOUND'}
Error: Cannot find module 'icon.svg'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (Test.js:15:19)
at Module._compile (module.js:409:26)
at loader (PATH/node_modules/babel-register/lib/node.js:126:5)
at Object.require.extensions.(anonymous function) [as .js] (PATH/node_modules/babel-register/lib/node.js:136:7)
从我的 webpack.config 中:
resolve: {
modulesDirectories: ["web_modules", "node_modules", "images"]
},
和装载机:
{
test: /\.js(x?)$/,
loader: "babel",
exclude: /node_modules/,
query: babelOptions
},
{
test: /\.svg$/,
loaders: [
"file?name=[hash].[ext]"
]
}
我的 babelOptions.js
exports.babelOptions = {
presets: ["es2015", "react"]
}
我可以在函数或构造函数中要求,例如:
class TestClass {
constructor() {
const icon = require("icon.svg");
}
}