例如我们有多个条目:
entry: {
main: "./app/entry.js",
view: "./app/entry.js",
},
如何将当前名称(主要或 View )传递给 entry.js?
理想的解决方案应该是这样的:
new webpack.DefinePlugin({
'_ENTRY_': '[name]'
}),
就像其他配置选项一样,但当然 DefinePlugin 不知道如何处理这个......
最佳答案
如果您在 Node.js 中运行代码,那么您可以使用 __filename 和 __dirname .
Webpack 可以在非 Node.js 环境中模拟它们。
请看Webpack Node configuration
node: {
__filename: true,
__dirname: true
}
在这种情况下,您可以像通常在 Node.js 环境中那样使用 __filename 和 __dirname 全局变量。
if(__filename.indexOf('index.js') != -1) {
// Code here
}
node.__filename
Default: "mock"
Options:
true: The filename of the input file relative to the context option.
false: The regular Node.js __filename behavior. The filename of the output file when run in a Node.js environment.
"mock": The fixed value "index.js".
关于javascript - Webpack:将当前条目名称传递给 js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879872/