为什么 module.export 设置了多次?
Why is module.export set multiple times?
在这个 Webpacker config and Webpacker setup 中有一个通用设置:
const envConfig = module.exports = environment
const aliasConfig = module.exports = {...}
module.exports = merge(envConfig.toWebpackConfig(), aliasConfig)
多次设置 module.exports 有什么作用?
这仅仅是人们 copying/pasting+ 调整代码的结果还是它服务于特定的中间文件导出目的?
我想删除前两个分配,因为它们看起来没用,但我不确定 Webpack/Webpacker 是否有特定效果。
module.exports 只是告诉应该从这个文件中导出什么以便在其他文件中重用。
如果我刚刚阅读了您分享的 3 行代码,
const envConfig = module.exports = environment
最左侧 "environment" 分配给 "module.exports"
左侧 "module.exports" 到右侧 "envConfig"
const aliasConfig = module.exports = {...}
"module.exports" 现在被分配了另一个对象并丢失了对第一个对象的引用。
"aliasConfig" 现在引用“{...}”对象
module.exports = merge(envConfig.toWebpackConfig(), aliasConfig)
"module.exports" 被赋值为merge函数的结果(如果是使用webpack-merge包,这个对象就是两者的深度合并)。
它丢失了对先前对象的引用。
没有必要第一次声明 module.exports。
Webpack 配置像任何 nodejs 模块一样工作。
这是 module.exports https://nodejs.org/api/modules.html#modules_module_exports
文档的 link
另请注意,由于 javascript 中提升的工作方式,使用双重赋值语法可能被认为是危险的。如果我记得创建全局变量很容易,我们不会那样做 want/expect。
在这个 Webpacker config and Webpacker setup 中有一个通用设置:
const envConfig = module.exports = environment
const aliasConfig = module.exports = {...}
module.exports = merge(envConfig.toWebpackConfig(), aliasConfig)
多次设置 module.exports 有什么作用?
这仅仅是人们 copying/pasting+ 调整代码的结果还是它服务于特定的中间文件导出目的?
我想删除前两个分配,因为它们看起来没用,但我不确定 Webpack/Webpacker 是否有特定效果。
module.exports 只是告诉应该从这个文件中导出什么以便在其他文件中重用。
如果我刚刚阅读了您分享的 3 行代码,
const envConfig = module.exports = environment
最左侧 "environment" 分配给 "module.exports" 左侧 "module.exports" 到右侧 "envConfig"
const aliasConfig = module.exports = {...}
"module.exports" 现在被分配了另一个对象并丢失了对第一个对象的引用。 "aliasConfig" 现在引用“{...}”对象
module.exports = merge(envConfig.toWebpackConfig(), aliasConfig)
"module.exports" 被赋值为merge函数的结果(如果是使用webpack-merge包,这个对象就是两者的深度合并)。 它丢失了对先前对象的引用。
没有必要第一次声明 module.exports。
Webpack 配置像任何 nodejs 模块一样工作。 这是 module.exports https://nodejs.org/api/modules.html#modules_module_exports
文档的 link另请注意,由于 javascript 中提升的工作方式,使用双重赋值语法可能被认为是危险的。如果我记得创建全局变量很容易,我们不会那样做 want/expect。