forked from karlprieb/babel-plugin-add-import-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.js
135 lines (131 loc) · 5.05 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
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
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: ({ specifier }) => {
return specifier.endsWith('/no-ext') ||
specifier.endsWith('..') ||
specifier == './another-thing'
? undefined
: '.mjs';
},
recognizedExtensions: ['.js', '.jsx', '.mjs', '.cjs', '.json'],
replaceExtensions: {
'^packages/([^/]+)(/.+)?': ({ specifier, capturingGroups }) => {
if (specifier == 'packages/root' || specifier.startsWith('packages/root/')) {
return `./monorepo-js${capturingGroups[2] ?? '/'}`;
} else if (!capturingGroups[2] || capturingGroups[2].startsWith('/src/index')) {
return `@monorepo/$1`;
} else if (capturingGroups[2].startsWith('/package.json')) {
return `@monorepo/$1$2`;
} else {
return `@monorepo/$1/dist$2`;
}
}
}
};
import { name as pkgName } from 'package';
import fs from 'node:fs';
import { primary } from './index.mjs';
import { secondary } from '..';
import { tertiary } from '../..';
import dirImport from './some-dir/index.mjs';
import dirImport2 from '/some-dir';
import jsConfig from './jsconfig.json';
import projectConfig from './project.config.mjs';
import { add, double } from './src/numbers.mjs';
import { curry } from './src/typed/curry.ts.mjs';
import styles from './src/less/styles.less.mjs';
import advanced1 from 'package/do/not/replace';
import advanced2 from '@monorepo/pkg-name';
import advanced3 from '@monorepo/pkg-name/package.json';
import advanced4 from '@monorepo/pkg-name';
import advanced5 from '@monorepo/pkg-name/dist/src/file-name';
import advanced6 from '@monorepo/pkg-name/dist/src/no-ext';
import advanced7 from '@monorepo/pkg-name/dist/lib/sub-pkg/file-name.js';
import advanced8 from './monorepo-js/index.mjs';
import advanced9 from './monorepo-js/package.json';
import advanced10 from './monorepo-js/src/index.mjs';
import advanced11 from './monorepo-js/src/file-name.mjs';
import advanced12 from './monorepo-js/src/no-ext';
import advanced13 from './monorepo-js/lib/sub-pkg/file-name.js';
// 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');
const thing2 = await import(
_rewrite(someFn(`./${someVariable}`) + '.json', _rewrite_options)
);
const anotherThing2 = require(_rewrite(someOtherVariable, _rewrite_options));