1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
36x
36x
3x
36x
36x
2x
36x
1x
35x
35x
35x
35x
35x
31x
31x
35x
35x
35x
35x
2x
2x
35x
2x
1x
1x
2x
35x
35x
32x
32x
16x
16x
16x
32x
32x
32x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
32x
32x
32x
32x
64x
40x
24x
6x
6x
12x
18x
112x
83x
61x
60x
39x
39x
17x
17x
17x
17x
17x
22x
22x
22x
21x
1x
35x
2415x
35x
9x
9x
243x
24x
219x
9x
9x
5x
26x
702x
652x
50x
50x
26x
26x
35x
35x
37x
4x
4x
2x
2x
2x
2x
2x
| Evar findRoot = require('find-root')
, path = require('path')
, get = require('lodash.get')
, find = require('array-find')
, interpret = require('interpret')
// not available on 0.10.x
, isAbsolute = path.isAbsolute || require('is-absolute')
, fs = require('fs')
, coreLibs = require('node-libs-browser')
const log = require('debug')('eslint-plugin-import:resolver:webpack')
exports.interfaceVersion = 2
/**
* Find the full path to 'source', given 'file' as a full reference path.
*
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* TODO: take options as a third param, with webpack config file name
* @return {string?} the resolved path to source, undefined if not resolved, or null
* if resolved to a non-FS resource (i.e. script tag at page load)
*/
exports.resolve = function (source, file, settings) {
// strip loaders
var finalBang = source.lastIndexOf('!')
if (finalBang >= 0) {
source = source.slice(finalBang + 1)
}
// strip resource query
var finalQuestionMark = source.lastIndexOf('?')
if (finalQuestionMark >= 0) {
source = source.slice(0, finalQuestionMark)
}
if (source in coreLibs) {
return { found: true, path: coreLibs[source] }
}
var webpackConfig
try {
var configPath = get(settings, 'config')
, configIndex = get(settings, 'config-index')
, packageDir
if (configPath) log('Config path from settings:', configPath)
// see if we've got an absolute path
if (!configPath || !isAbsolute(configPath)) {
// if not, find ancestral package.json and use its directory as base for the path
packageDir = findRoot(path.resolve(file))
if (!packageDir) Ithrow new Error('package not found above ' + file)
}
configPath = findConfigPath(configPath, packageDir)
log('Config path resolved to:', configPath)
webpackConfig = require(configPath)
if (webpackConfig && webpackConfig.default) {
log('Using ES6 module "default" key instead of module.exports.')
webpackConfig = webpackConfig.default
}
} catch (err) {
log('Error during config lookup:', err)
webpackConfig = {}
}
if (Array.isArray(webpackConfig)) {
if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) {
webpackConfig = webpackConfig[configIndex]
}
else {
webpackConfig = find(webpackConfig, function findFirstWithResolve(config) {
return !!config.resolve
})
}
}
log('Using config: ', webpackConfig)
// externals
if (findExternal(source, webpackConfig.externals, path.dirname(file))) return { found: true, path: null }
var otherPlugins = []
// support webpack.ResolverPlugin
if (webpackConfig.plugins) {
webpackConfig.plugins.forEach(function (plugin) {
if (plugin.constructor && plugin.constructor.name === 'ResolverPlugin' && Array.isArray(plugin.plugins)) {
otherPlugins.push.apply(otherPlugins, plugin.plugins);
}
});
}
// otherwise, resolve "normally"
var resolver = createResolver(webpackConfig.resolve || {}, otherPlugins)
try {
return { found: true, path: resolver.resolveSync(path.dirname(file), source) }
} catch (err) {
log('Error during module resolution:', err)
return { found: false }
}
}
var Resolver = require('enhanced-resolve/lib/Resolver')
var SyncNodeJsInputFileSystem = require('enhanced-resolve/lib/SyncNodeJsInputFileSystem')
var syncFS = new SyncNodeJsInputFileSystem()
var ModuleAliasPlugin = require('enhanced-resolve/lib/ModuleAliasPlugin')
var ModulesInDirectoriesPlugin = require('enhanced-resolve/lib/ModulesInDirectoriesPlugin')
var ModulesInRootPlugin = require('enhanced-resolve/lib/ModulesInRootPlugin')
var ModuleAsFilePlugin = require('enhanced-resolve/lib/ModuleAsFilePlugin')
var ModuleAsDirectoryPlugin = require('enhanced-resolve/lib/ModuleAsDirectoryPlugin')
var DirectoryDescriptionFilePlugin = require('enhanced-resolve/lib/DirectoryDescriptionFilePlugin')
var DirectoryDefaultFilePlugin = require('enhanced-resolve/lib/DirectoryDefaultFilePlugin')
var FileAppendPlugin = require('enhanced-resolve/lib/FileAppendPlugin')
var ResultSymlinkPlugin = require('enhanced-resolve/lib/ResultSymlinkPlugin')
var DirectoryDescriptionFileFieldAliasPlugin =
require('enhanced-resolve/lib/DirectoryDescriptionFileFieldAliasPlugin')
// adapted from tests &
// https://github.com/webpack/webpack/blob/v1.13.0/lib/WebpackOptionsApply.js#L322
function createResolver(resolve, otherPlugins) {
var resolver = new Resolver(syncFS)
resolver.apply(
resolve.packageAlias
? new DirectoryDescriptionFileFieldAliasPlugin('package.json', resolve.packageAlias)
: function() {},
new ModuleAliasPlugin(resolve.alias || {}),
makeRootPlugin('module', resolve.root),
new ModulesInDirectoriesPlugin('module', resolve.modulesDirectories || ['web_modules', 'node_modules']),
makeRootPlugin('module', resolve.fallback),
new ModuleAsFilePlugin('module'),
new ModuleAsDirectoryPlugin('module'),
new DirectoryDescriptionFilePlugin('package.json', ['jsnext:main'].concat(resolve.packageMains || defaultMains)),
new DirectoryDefaultFilePlugin(['index']),
new FileAppendPlugin(resolve.extensions || ['', '.webpack.js', '.web.js', '.js']),
new ResultSymlinkPlugin()
)
resolver.apply.apply(resolver, otherPlugins);
return resolver
}
/* eslint-disable */
// from https://github.com/webpack/webpack/blob/v1.13.0/lib/WebpackOptionsApply.js#L365
function makeRootPlugin(name, root) {
if(typeof root === "string")
return new ModulesInRootPlugin(name, root);
else if(Array.isArray(root)) {
return function() {
root.forEach(function(root) {
this.apply(new ModulesInRootPlugin(name, root));
}, this);
};
}
return function() {};
}
/* eslint-enable */
function findExternal(source, externals, context) {
if (!externals) return false
// string match
if (typeof externals === 'string') return (source === externals)
// array: recurse
if (externals instanceof Array) {
return externals.some(function (e) { return findExternal(source, e, context) })
}
if (externals instanceof RegExp) I{
return externals.test(source)
}
if (typeof externals === 'function') {
var functionExternalFound = false
externals.call(null, context, source, function(err, value) {
if (err) I{
functionExternalFound = false
} else {
functionExternalFound = findExternal(source, value, context)
}
})
return functionExternalFound
}
// else, vanilla object
for (var key in externals) {
if (!externals.hasOwnProperty(key)) Icontinue
if (source === key) return true
}
return false
}
/**
* webpack defaults: http://webpack.github.io/docs/configuration.html#resolve-packagemains
* @type {Array}
*/
var defaultMains = [
'webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main',
]
function findConfigPath(configPath, packageDir) {
var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length
})
, extension
if (configPath) {
// extensions is not reused below, so safe to mutate it here.
extensions.reverse()
extensions.forEach(function (maybeExtension) {
if (extension) {
return
}
if (configPath.substr(-maybeExtension.length) === maybeExtension) {
extension = maybeExtension
}
})
// see if we've got an absolute path
if (!isAbsolute(configPath)) {
configPath = path.join(packageDir, configPath)
}
} else {
extensions.forEach(function (maybeExtension) {
if (extension) {
return
}
var maybePath = path.resolve(
path.join(packageDir, 'webpack.config' + maybeExtension)
)
if (fs.existsSync(maybePath)) {
configPath = maybePath
extension = maybeExtension
}
})
}
registerCompiler(interpret.extensions[extension])
return configPath
}
function registerCompiler(moduleDescriptor) {
if(moduleDescriptor) {
if(typeof moduleDescriptor === 'string') I{
require(moduleDescriptor)
} else if(!Array.isArray(moduleDescriptor)) {
moduleDescriptor.register(require(moduleDescriptor.module))
} else {
for(var i = 0; i < moduleDescriptor.length; i++) {
try {
registerCompiler(moduleDescriptor[i])
break
} catch(e) {
log('Failed to register compiler for moduleDescriptor[]:', i, moduleDescriptor)
}
}
}
}
}
|