热重载会破坏应用程序。最终加载器没有 return 缓冲区或字符串
Hot reloading breaks the app. Final loader didn't return a Buffer or String
我有一个自定义加载器可以加载以下文件 data.js
const data = {
a: ()=> 8885555,
b: ()=> 55555
}
module.exports = name => {
return data[name] && data[name]()
}
当我更改上述文件(如更改 b
值时,应用程序中断
ERROR in ../data.js?name=a
Module build failed: Error: Final loader (../data-loader.js) didn't return a Buffer or String
at runLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\webpack\lib\NormalModule.js:319:18)
at C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:373:3
at iterateNormalLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:214:10)
at iterateNormalLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:221:10)
at C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:236:3
at runSyncOrAsync (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:124:12)
at iterateNormalLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:232:2)
at Array.<anonymous> (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:205:4)
at Storage.finished (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:43:16)
at provider (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:79:9)
@ ./index.js 1:10-40
@ multi webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000 ./index.js
这是自定义的 webpack 加载器
const devalue = require('devalue')
const vm = require('vm');
module.exports = async function(source, map) {
const callback = this.async()
this.addDependency(this.resourcePath);
const sandbox = {
require,
module
}
vm.createContext(sandbox);
const mod = vm.runInContext(source, sandbox)
const result = await mod(this.resourceQuery.replace('?name=', ''))
return callback(null, `export default ${devalue(result)}`, map);
}
这里是复制品repo
所以这是应该做的
const devalue = require('devalue')
const vm = require('vm');
module.exports = async function(source, map) {
const callback = this.async()
this.addDependency(this.resourcePath);
const sandbox = {
require,
module: {exports: {}}
}
vm.createContext(sandbox);
const mod = vm.runInContext(source, sandbox)
const result = await mod(this.resourceQuery.replace('?name=', ''))
return callback(null, `export default ${devalue(result)}`, map);
}
问题是我用 data.js
文件 module.exports
覆盖了加载程序 module.exports
因为我将相同的 module
传递给 sandbox
因此,当热重新加载时,加载程序中的导出函数将被替换为 data.js
函数,该函数不 return 字符串(在这种情况下,因为 data[name]
将是 undefined
),这就是错误的原因
我有一个自定义加载器可以加载以下文件 data.js
const data = {
a: ()=> 8885555,
b: ()=> 55555
}
module.exports = name => {
return data[name] && data[name]()
}
当我更改上述文件(如更改 b
值时,应用程序中断
ERROR in ../data.js?name=a
Module build failed: Error: Final loader (../data-loader.js) didn't return a Buffer or String
at runLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\webpack\lib\NormalModule.js:319:18)
at C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:373:3
at iterateNormalLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:214:10)
at iterateNormalLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:221:10)
at C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:236:3
at runSyncOrAsync (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:124:12)
at iterateNormalLoaders (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:232:2)
at Array.<anonymous> (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\loader-runner\lib\LoaderRunner.js:205:4)
at Storage.finished (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:43:16)
at provider (C:\Users0\Saber.js\webpack-hmr-3-ways\middleware\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:79:9)
@ ./index.js 1:10-40
@ multi webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000 ./index.js
这是自定义的 webpack 加载器
const devalue = require('devalue')
const vm = require('vm');
module.exports = async function(source, map) {
const callback = this.async()
this.addDependency(this.resourcePath);
const sandbox = {
require,
module
}
vm.createContext(sandbox);
const mod = vm.runInContext(source, sandbox)
const result = await mod(this.resourceQuery.replace('?name=', ''))
return callback(null, `export default ${devalue(result)}`, map);
}
这里是复制品repo
所以这是应该做的
const devalue = require('devalue')
const vm = require('vm');
module.exports = async function(source, map) {
const callback = this.async()
this.addDependency(this.resourcePath);
const sandbox = {
require,
module: {exports: {}}
}
vm.createContext(sandbox);
const mod = vm.runInContext(source, sandbox)
const result = await mod(this.resourceQuery.replace('?name=', ''))
return callback(null, `export default ${devalue(result)}`, map);
}
问题是我用 data.js
文件 module.exports
覆盖了加载程序 module.exports
因为我将相同的 module
传递给 sandbox
因此,当热重新加载时,加载程序中的导出函数将被替换为 data.js
函数,该函数不 return 字符串(在这种情况下,因为 data[name]
将是 undefined
),这就是错误的原因