/
babel-plugin-transform-import-duplicate.js
125 lines (101 loc) · 3.6 KB
/
babel-plugin-transform-import-duplicate.js
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
"use strict";
const pathModule = require('path')
const fs = require('fs')
const nodeResolveSync = require('resolve').sync;
const wrapListener = require('babel-plugin-detective/wrap-listener');
const miniMatch = require('minimatch');
const FILES_EQUAL_CACHE = {};
function filesEqual(source, target) {
if ( false === FILES_EQUAL_CACHE.hasOwnProperty(source) ) {
FILES_EQUAL_CACHE[source] = {};
}
if ( false === FILES_EQUAL_CACHE[source].hasOwnProperty(target) ) {
const equal = fs.readFileSync(source, "utf-8") === fs.readFileSync(target, "utf-8");
FILES_EQUAL_CACHE[source][target] = equal;
}
return FILES_EQUAL_CACHE[source][target];
}
function resolveTarget(inPath) {
let nodeModule = inPath.match(/node_modules\/(.+?)\..+?$/);
if ( (null === nodeModule) || (nodeModule.length === 0) ) {
throw new Error(`couldn't resolve ${inPath} back to node_module`);
}
nodeModule = nodeModule[1];
if ( nodeModule.endsWith('index') ) {
nodeModule = pathModule.dirname(nodeModule);
}
return nodeModule;
}
function handleMapping(path, file, externals, originalPath, target) {
if ( false === filesEqual(originalPath, target) ) {
// Files are not equal, cannot replace.
return;
}
// Files are equal, resolve dependancy and replace import
const resolvedTarget = resolveTarget(target);
if ( externals.indexOf(resolvedTarget) <= -1 ) {
// Sanity to make sure it is known export.
throw new Error(`${resolvedTarget} is not an external, please add it to both plugin and rollup externals`);
}
path.node.value = resolvedTarget;
}
function handleImport(path, file, opts) {
const dirName = pathModule.dirname(file.parserOpts.sourceFileName);
const importPath = nodeResolveSync(pathModule.join(dirName, path.node.value));
if ( opts.newFiles.some((x) => x === importPath) ) {
// this is declared as new file, import always.
return;
}
// Search for the mapping;
const matching = Object.keys(opts.mapping).map((key) => {
const regexKey = new RegExp(key);
const match = importPath.match(regexKey);
if ( !match || match.length === 0 ) {
return undefined;
}
return importPath.replace(regexKey, opts.mapping[key]);
})
.filter((v) => v)
.map((v) => pathModule.resolve(v));
if ( matching.length === 0 ) {
// Mapping does not exists
return;
} else if ( matching.length !== 1 ) {
// More then one mapping.
throw new Error('more then one matching! please check your config');
}
// Mapping found :)
return handleMapping(path, file, opts.externals, importPath, matching[0]);
}
function onImportListener(path, file, opts) {
const mapping = (opts && opts.mapping) || undefined;
const exclude = opts.exclude || [];
const externals = opts.external || [];
const newFiles = opts.newFiles || [];
// Make sure mapping exists
if ( undefined === mapping ) {
throw new Error('mapping option is missing');
}
opts.exclude = exclude;
opts.externals = externals;
opts.newFiles = newFiles;
// filter literal values
if (false === path.isLiteral()) {
return;
}
const relPath = pathModule.relative(__dirname, file.parserOpts.sourceFileName);
// Filter excluded & non-relative.
let excluded = exclude.reduce((lastResult, globPath) => {
if ( lastResult === true ) {
return lastResult;
}
return miniMatch(relPath, globPath);
}, false);
// filter non-relative
excluded = excluded || (!path.node.value) || (false === path.node.value.startsWith('.'));
if (excluded) {
return;
}
return handleImport(path, file, opts);
}
module.exports = wrapListener(onImportListener, 'transform-import-duplicate');