webpack - webpack 中的当前文件路径

标签 webpack

有没有办法接收当前文件路径,就像在 requirejs 中一样?

define(['module'], function (module) {
    console.log(module.uri)
});

最佳答案

是的,有一个:__filename .

但默认情况下,webpack 不会泄漏路径信息,您需要设置一个配置标志来获取真实的文件名而不是模拟文件名("/index.js")。

// /home/project/webpack.config.js
module.exports = {
  context: __dirname,
  node: {
    __filename: true
  }
}

您可以使用__filename获取相对于context选项的当前文件名:

// in /home/project/dir/file.js
console.log(__filename);
// => logs "dir/file.js"

文件名仅嵌入到使用 __filename 的模块中。因此您不必担心路径从其他模块泄漏。

关于webpack - webpack 中的当前文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25553868/

相关文章:

javascript - 关键依赖 : the request of a dependency is an expression -- react-universal-component

webpack - 为什么 Tailwind 中的清除选项不适用于 Webpack

laravel - 找不到模块 : Error: Can't resolve 'timers'

javascript - Webpack 无法解析相对路径

requirejs - Webpack:表达模块依赖关系

javascript - 使用 react js 和 webpack 开发的 Web 应用程序的缓存问题

javascript - 如何将库与 webpack 捆绑在一起?

javascript - 我正在使用 create-react-app,我需要在没有服务器的情况下直接在浏览器中打开 build/index.html

node.js - 这些相关模块没有找到 vue cli

reactjs - Webpack Code Splitting 'Loading chunk failed' 错误文件路径错误