1 | "use strict";
|
2 |
|
3 | const path = require('path');
|
4 | const resolve = require('resolve');
|
5 | const pkgUp = require('pkg-up');
|
6 | const {
|
7 | resolvePath
|
8 | } = require('babel-plugin-module-resolver');
|
9 | const {
|
10 | OptionManager
|
11 | } = require('@babel/core');
|
12 | const fs = require('fs');
|
13 | function 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 |
|
25 |
|
26 |
|
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 | }
|
32 | function 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 | }
|
42 | function 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 |
|
53 | const finalBang = index ? source.lastIndexOf('!', index - 1) : -1;
|
54 | if (finalBang >= 0) {
|
55 | source = source.slice(finalBang + 1);
|
56 | }
|
57 |
|
58 |
|
59 | const finalQuestionMark = source.lastIndexOf('?');
|
60 | if (finalQuestionMark >= 0) {
|
61 | source = source.slice(0, finalQuestionMark);
|
62 | }
|
63 | return source;
|
64 | }
|
65 | exports.interfaceVersion = 2;
|
66 | const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | exports.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 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
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 |