webpack resolve 解析

webpack resolve 解析

var path = require('path');
module.exports = {

   ...

   resolve:{
        alias:{
            "jquery":__dirname+"/assets/static/js/jquery-3.0.0.min.js",
        "iscroll_zoom":__dirname+"/assets/static/js/iscroll-zoom.js",
        "hammer":__dirname+"/assets/static/js/hammer.js",
        "lrz":__dirname+"/assets/static/js/lrz.all.bundle.js"
        },
        root: [
            path.resolve('./app/modules'),
            path.resolve('./vendor/modules')
        ]
    ...
   }
}

上面代码是webpack.config.js配置文件中的resolve的配置

resolve Options affecting the resolving of modules. 影响模块解析的选项。

resolve.alias

用其他模块或路径替换模块。

Expected is a object with keys being module names. The value is the new path. It’s similar to a replace but a bit more clever. If the the key ends with onlytheexactmatch(withoutthe ) will be replaced.期望的是具有键的模块名称的对象。 该值是新路径。 它类似于一个替换,但有点更聪明。 如果键以 )。

If the value is a relative path it will be relative to the file containing the require.如果值是相对路径,它将相对于包含require的文件。

Importent:在实际项目运用中webpack.config.js配置文件中,alias在绝对路径中,使用node.js的__dirname+"/path/"的方式代替"./path"的方式,不知道原因,但是用"./path"这种方式会报错,

ERROR in ./assets/static/js/PhotoClip.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./assets/static/js/iscroll-zoom.js in E:\working-demo\bjxdai-rewrite\assets\static\js
 @ ./assets/static/js/PhotoClip.js 42:2-62

使用__dirname+"/path/"这种形式则不会报错

resolve.root

The directory (absolute path) that contains your modules. May also be an array of directories. This setting should be used to add individual directories to the search path.包含模块的目录(绝对路径)。 也可能是一个目录数组。 此设置应用于将单个目录添加到搜索路径。

It must be an absolute path! Don’t pass something like ./app/modules.
必须是一个绝对路径

Example:

var path = require('path');

// ...
resolve: {
  root: [
    path.resolve('./app/modules'),
    path.resolve('./vendor/modules')
  ]
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值