javascript - Resolving require paths with webpack -
i'm still confused how resolve module paths webpack. write:
myfile = require('../../mydir/myfile.js')
but i'd write
myfile = require('mydir/myfile.js')
i thinking resolve.alias may since see similar example using { xyz: "/some/dir" }
alias can require("xyz/file.js")
.
but if set alias { mydir: '/absolute/path/mydir' }
, require('mydir/myfile.js')
won't work.
i feel dumb because i've read doc many times , feel i'm missing something. right way avoid writing relative requires ../../
etc?
webpack >2.0
see wtk's answer.
webpack 1.0
a more straightforward way use resolve.root.
http://webpack.github.io/docs/configuration.html#resolve-root
resolve.root
the directory (absolute path) contains modules. may array of directories. setting should used add individual directories search path.
in case:
webpack config
var path = require('path'); // ... resolve: { root: path.resolve('./mydir'), extensions: ['', '.js'] }
consuming module
require('myfile')
or
require('myfile.js')
see also: http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories
Comments
Post a Comment