webpack 中的当前文件路径 [英] Current file path in webpack

查看:93
本文介绍了webpack 中的当前文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法接收当前文件路径,比如在 requirejs 中?

Is there any way to receive current file path, like in requirejs?

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

推荐答案

是的,有一个:__filename.

Yep there is one: __filename.

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

But by default webpack doesn't leak path information and you need to set a config flag to get real filename instead of a mock ("/index.js").

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

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

Than you can use __filename get the current filename relative to the context option:

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

文件名仅嵌入到使用 __filename 的模块中.所以你不必担心路径从其他模块泄漏.

The filename is only embedded into modules where __filename is used. So you don't have to be affraid that paths are leaked from other modules.

这篇关于webpack 中的当前文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆