@@ -20,7 +20,15 @@ exports.interfaceVersion = 2
20
20
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
21
21
* @param {string } source - the module to resolve; i.e './some-module'
22
22
* @param {string } file - the importing file's full path; i.e. '/usr/local/bin/file.js'
23
- * TODO: take options as a third param, with webpack config file name
23
+ * @param {object } settings - the webpack config file name, as well as cwd
24
+ * @example
25
+ * options: {
26
+ * // Path to the webpack config
27
+ * config: 'webpack.config.js',
28
+ * // Path to be used to determine where to resolve webpack from
29
+ * // (may differ from the cwd in some cases)
30
+ * cwd: process.cwd()
31
+ * }
24
32
* @return {string? } the resolved path to source, undefined if not resolved, or null
25
33
* if resolved to a non-FS resource (i.e. script tag at page load)
26
34
*/
@@ -41,6 +49,11 @@ exports.resolve = function (source, file, settings) {
41
49
var webpackConfig
42
50
43
51
var configPath = get ( settings , 'config' )
52
+ /**
53
+ * Attempt to set the current working directory.
54
+ * If none is passed, default to the `cwd` where the config is located.
55
+ */
56
+ , cwd = get ( settings , 'cwd' )
44
57
, configIndex = get ( settings , 'config-index' )
45
58
, env = get ( settings , 'env' )
46
59
, argv = get ( settings , 'argv' , { } )
@@ -114,7 +127,7 @@ exports.resolve = function (source, file, settings) {
114
127
}
115
128
116
129
// otherwise, resolve "normally"
117
- var resolveSync = getResolveSync ( configPath , webpackConfig )
130
+ var resolveSync = getResolveSync ( configPath , webpackConfig , cwd )
118
131
119
132
try {
120
133
return { found : true , path : resolveSync ( path . dirname ( file ) , source ) }
@@ -130,13 +143,13 @@ exports.resolve = function (source, file, settings) {
130
143
131
144
var MAX_CACHE = 10
132
145
var _cache = [ ]
133
- function getResolveSync ( configPath , webpackConfig ) {
146
+ function getResolveSync ( configPath , webpackConfig , cwd ) {
134
147
var cacheKey = { configPath : configPath , webpackConfig : webpackConfig }
135
148
var cached = find ( _cache , function ( entry ) { return isEqual ( entry . key , cacheKey ) } )
136
149
if ( ! cached ) {
137
150
cached = {
138
151
key : cacheKey ,
139
- value : createResolveSync ( configPath , webpackConfig ) ,
152
+ value : createResolveSync ( configPath , webpackConfig , cwd ) ,
140
153
}
141
154
// put in front and pop last item
142
155
if ( _cache . unshift ( cached ) > MAX_CACHE ) {
@@ -146,15 +159,18 @@ function getResolveSync(configPath, webpackConfig) {
146
159
return cached . value
147
160
}
148
161
149
- function createResolveSync ( configPath , webpackConfig ) {
162
+ function createResolveSync ( configPath , webpackConfig , cwd ) {
150
163
var webpackRequire
151
164
, basedir = null
152
165
153
166
if ( typeof configPath === 'string' ) {
154
- basedir = path . dirname ( configPath )
167
+ // This can be changed via the settings passed in when defining the resolver
168
+ basedir = cwd || configPath
169
+ log ( `Attempting to load webpack path from ${ basedir } ` )
155
170
}
156
171
157
172
try {
173
+ // Attempt to resolve webpack from the given `basedir`
158
174
var webpackFilename = resolve . sync ( 'webpack' , { basedir, preserveSymlinks : false } )
159
175
var webpackResolveOpts = { basedir : path . dirname ( webpackFilename ) , preserveSymlinks : false }
160
176
0 commit comments