forked from karlprieb/babel-plugin-add-import-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.js
107 lines (103 loc) · 3.79 KB
/
output.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
const _rewrite = (specifier, options) => {
if (typeof specifier != 'string') {
throw new TypeError(
`rewrite error: expected specifier of type string, not ${typeof specifier}`
);
}
let replacementMap;
replacementMap = undefined;
if (options.replaceExtensions) {
Object.entries(options.replaceExtensions).some(([target, replacement]) => {
if (target.startsWith('^') || target.endsWith('$')) {
const targetRegExp = new RegExp(target);
const capturingGroups = Array.from(specifier.match(targetRegExp) || []);
if (capturingGroups.length) {
replacementMap = [targetRegExp, replacement, capturingGroups];
return true;
}
} else if (specifier.endsWith(target)) {
replacementMap = [target, replacement, []];
return true;
}
});
}
let finalImportPath = specifier;
if (replacementMap) {
const [target, replacement, capturingGroups] = replacementMap;
finalImportPath = finalImportPath.replace(
target,
typeof replacement == 'string'
? replacement
: replacement({
specifier,
capturingGroups
})
);
}
const isRelative =
finalImportPath.startsWith('./') ||
finalImportPath.startsWith('.\\') ||
finalImportPath.startsWith('../') ||
finalImportPath.startsWith('..\\') ||
finalImportPath == '.' ||
finalImportPath == '..';
if (options.appendExtension && isRelative) {
const endsWithSlash = /(\/|\\)$/.test(finalImportPath);
const basenameIsDots = /(^\.?\.(\/|\\)?$)|((\/|\\)\.?\.(\/|\\)?$)/.test(
finalImportPath
);
const extensionToAppend =
typeof options.appendExtension == 'string'
? options.appendExtension
: options.appendExtension({
specifier,
capturingGroups: []
});
if (extensionToAppend !== undefined) {
if (basenameIsDots) {
finalImportPath += `${endsWithSlash ? '' : '/'}index${extensionToAppend}`;
} else {
const hasRecognizedExtension =
!endsWithSlash &&
options.recognizedExtensions.some((extension) => {
return finalImportPath.endsWith(extension);
});
if (!hasRecognizedExtension) {
finalImportPath = endsWithSlash
? finalImportPath + `index${extensionToAppend}`
: finalImportPath + extensionToAppend;
}
}
}
}
return finalImportPath;
},
_rewrite_options = {
appendExtension: '.mjs',
recognizedExtensions: ['.js', '.jsx', '.mjs', '.cjs', '.json', '.css'],
replaceExtensions: {
'.ts': '.mjs',
'^package$': '/absolute/path/to/project/package.json',
'(.+?)\\.less$': '$1.css'
}
};
import { name as pkgName } from '/absolute/path/to/project/package.json';
import fs from 'node:fs';
import { primary } from './index.mjs';
import { secondary } from '../index.mjs';
import { tertiary } from '../../index.mjs';
import dirImport from './some-dir/index.mjs';
import jsConfig from './jsconfig.json';
import projectConfig from './project.config.cjs';
import { add, double } from './src/numbers.mjs';
import { curry } from './src/typed/curry.mjs';
import styles from './src/less/styles.css';
// Note that, unless otherwise configured, babel deletes type-only imports
export { triple, quadruple } from './lib/num-utils.mjs';
// Note that, unless otherwise configured, babel deletes type-only imports
const thing = await import('./thing.mjs');
const anotherThing = require('./another-thing.mjs');
const thing2 = await import(
_rewrite(someFn(`./${someVariable}`) + '.json', _rewrite_options)
);
const anotherThing2 = require(_rewrite(someOtherVariable, _rewrite_options));