UNPKG

4.2 kBJavaScriptView Raw
1"use strict";
2
3const path = require('path');
4const resolve = require('resolve');
5const pkgUp = require('pkg-up');
6const {
7 resolvePath
8} = require('babel-plugin-module-resolver');
9const {
10 OptionManager
11} = require('@babel/core');
12const fs = require('fs');
13function getPlugins(file, cwd, babelOptions) {
14 try {
15 const manager = new OptionManager();
16 const result = manager.init({
17 babelrc: true,
18 filename: file,
19 cwd,
20 ...babelOptions
21 });
22 return result.plugins.filter(plugin => plugin.key === 'module-resolver');
23 } catch (err) {
24 // This error should only occur if something goes wrong with babel's
25 // internals. Dump it to console so people know what's going on,
26 // elsewise the error will simply be squelched in the calling code.
27 console.error('[eslint-import-resolver-babel-module]', err);
28 console.error('See: https://github.com/tleunen/eslint-import-resolver-babel-module/pull/34');
29 return [];
30 }
31}
32function getPluginOptions(file, cwd, defaultOptions) {
33 const instances = getPlugins(file, cwd, defaultOptions.babelOptions);
34 return instances.reduce((config, plugin) => ({
35 cwd: plugin.options.cwd || config.cwd,
36 root: config.root.concat(plugin.options.root || []),
37 alias: Object.assign(config.alias, plugin.options.alias || {}),
38 resolvePath: plugin.options.resolvePath || config.resolvePath,
39 extensions: plugin.options.extensions || config.extensions
40 }), defaultOptions);
41}
42function stripWebpack(src, alias) {
43 let source = src;
44 const aliases = Object.keys(alias);
45 let index = source.length;
46 aliases.forEach(element => {
47 const i = source.indexOf(element);
48 if (i >= 0 && i < index) {
49 index = i;
50 }
51 });
52 // strip loaders
53 const finalBang = index ? source.lastIndexOf('!', index - 1) : -1;
54 if (finalBang >= 0) {
55 source = source.slice(finalBang + 1);
56 }
57
58 // strip resource query
59 const finalQuestionMark = source.lastIndexOf('?');
60 if (finalQuestionMark >= 0) {
61 source = source.slice(0, finalQuestionMark);
62 }
63 return source;
64}
65exports.interfaceVersion = 2;
66const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
67
68/**
69 * Find the full path to 'source', given 'file' as a full reference path.
70 *
71 * resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
72 * @param {string} source - the module to resolve; i.e './some-module'
73 * @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
74 * @param {object} options - the resolver options
75 * @return {object}
76 */
77exports.resolve = (source, file, opts) => {
78 const options = opts || {};
79 if (resolve.isCore(source)) return {
80 found: true,
81 path: null
82 };
83 const projectRootDir = path.dirname(pkgUp.sync({
84 cwd: file
85 }));
86 try {
87 const pluginOptions = getPluginOptions(file, projectRootDir, {
88 // if .babelrc doesn't exist, try to get the configuration information from `options`,
89 // which gets defined by the eslint configuration file.
90 // e.g. in .eslintrc file
91 // "import/resolver": {
92 // "babel-module": {
93 // "root": ["./src"],
94 // "extensions": [".js", ".jsx"]
95 // }
96 // }
97 cwd: options.cwd || projectRootDir,
98 root: options.root || [],
99 alias: options.alias || {},
100 resolvePath: options.resolvePath,
101 extensions: options.extensions || defaultExtensions,
102 babelOptions: options.babelOptions || {}
103 });
104 const finalSource = stripWebpack(source, pluginOptions.alias);
105 const resolvePathFunc = pluginOptions.resolvePath || resolvePath;
106 const src = resolvePathFunc(finalSource, file, pluginOptions);
107 if (src) {
108 const absoluteSrc = path.join(path.dirname(file), src);
109 if (options.allowExistingDirectories && fs.existsSync(absoluteSrc)) {
110 return {
111 found: true,
112 path: absoluteSrc
113 };
114 }
115 }
116 const extensions = options.extensions || pluginOptions.extensions;
117 return {
118 found: true,
119 path: resolve.sync(src || source, {
120 ...options,
121 extensions,
122 basedir: path.dirname(file)
123 })
124 };
125 } catch (e) {
126 return {
127 found: false
128 };
129 }
130};
\No newline at end of file