Skip to content

Commit e42f369

Browse files
committed
refactor(src): revert package import fix for now
1 parent 0528506 commit e42f369

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { name as pkgName } from 'package';
1+
// TODO: replace with 'package'
2+
import { name as pkgName } from '../package.json';
23
import debugFactory from 'debug';
34
import template from '@babel/template';
45
import * as util from '@babel/types';

test/integration/assets/output-1.js

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
1-
const _rewrite = (importPath, options) => {
2-
if (typeof importPath != 'string') {
3-
throw new TypeError(`rewrite error: expected import specifier of type string, not ${typeof importPath}`);
1+
const _rewrite = (specifier, options) => {
2+
if (typeof specifier != 'string') {
3+
throw new TypeError(`rewrite error: expected specifier of type string, not ${typeof specifier}`);
44
}
5-
let finalImportPath = importPath;
65
let replacementMap;
76
replacementMap = undefined;
87
if (options.replaceExtensions) {
98
Object.entries(options.replaceExtensions).some(([target, replacement]) => {
109
if (target.startsWith('^') || target.endsWith('$')) {
1110
const targetRegExp = new RegExp(target);
12-
if (targetRegExp.test(finalImportPath)) {
13-
replacementMap = [targetRegExp, replacement];
11+
const capturingGroups = Array.from(specifier.match(targetRegExp) || []);
12+
if (capturingGroups.length) {
13+
replacementMap = [targetRegExp, replacement, capturingGroups];
1414
return true;
1515
}
16-
} else if (finalImportPath.endsWith(target)) {
17-
replacementMap = [target, replacement];
16+
} else if (specifier.endsWith(target)) {
17+
replacementMap = [target, replacement, []];
1818
return true;
1919
}
2020
});
2121
}
22+
let finalImportPath = specifier;
2223
if (replacementMap) {
23-
const [target, replacement] = replacementMap;
24-
finalImportPath = finalImportPath.replace(target, replacement);
24+
const [target, replacement, capturingGroups] = replacementMap;
25+
finalImportPath = finalImportPath.replace(target, typeof replacement == 'string' ? replacement : replacement({
26+
specifier,
27+
capturingGroups
28+
}));
2529
}
26-
const isRelative = finalImportPath.startsWith('./') || finalImportPath.startsWith('../') || finalImportPath == '.' || finalImportPath == '..';
30+
const isRelative = finalImportPath.startsWith('./') || finalImportPath.startsWith('.\\') || finalImportPath.startsWith('../') || finalImportPath.startsWith('..\\') || finalImportPath == '.' || finalImportPath == '..';
2731
if (options.appendExtension && isRelative) {
28-
const endsWithSlash = finalImportPath.endsWith('/');
29-
if (/(^\.?\.\/?$)|(\/\.?\.\/?$)/.test(finalImportPath)) {
30-
finalImportPath += `${endsWithSlash ? '' : '/'}index${options.appendExtension}`;
31-
} else {
32-
const hasRecognizedExtension = !endsWithSlash && options.recognizedExtensions.some(extension => {
33-
return finalImportPath.endsWith(extension);
34-
});
35-
if (!hasRecognizedExtension) {
36-
finalImportPath = endsWithSlash ? finalImportPath + `index${options.appendExtension}` : finalImportPath + options.appendExtension;
32+
const endsWithSlash = /(\/|\\)$/.test(finalImportPath);
33+
const basenameIsDots = /(^\.?\.(\/|\\)?$)|((\/|\\)\.?\.(\/|\\)?$)/.test(finalImportPath);
34+
const extensionToAppend = typeof options.appendExtension == 'string' ? options.appendExtension : options.appendExtension({
35+
specifier,
36+
capturingGroups: []
37+
});
38+
if (extensionToAppend !== undefined) {
39+
if (basenameIsDots) {
40+
finalImportPath += `${endsWithSlash ? '' : '/'}index${extensionToAppend}`;
41+
} else {
42+
const hasRecognizedExtension = !endsWithSlash && options.recognizedExtensions.some(extension => {
43+
return finalImportPath.endsWith(extension);
44+
});
45+
if (!hasRecognizedExtension) {
46+
finalImportPath = endsWithSlash ? finalImportPath + `index${extensionToAppend}` : finalImportPath + extensionToAppend;
47+
}
3748
}
3849
}
3950
}

0 commit comments

Comments
 (0)