Skip to content

Commit 47836b5

Browse files
committed
fix(imports): fix resolving scoped pkg imports
1 parent 4596b86 commit 47836b5

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

src/util.ts

+26-8
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,15 @@ export function getRenderOptions(opts: d.PluginOptions, sourceText: string, file
7373
if (typeof url === 'string') {
7474
if (url.startsWith('~')) {
7575
try {
76-
const orgUrl = url.substr(1);
77-
const parts = orgUrl.split('/');
78-
const moduleId = parts.shift();
79-
const filePath = parts.join('/');
76+
const m = getModuleId(url);
8077

81-
if (moduleId) {
78+
if (m.moduleId) {
8279
context.sys.resolveModuleId({
83-
moduleId,
84-
containingFile: fileName
80+
moduleId: m.moduleId,
81+
containingFile: m.filePath
8582
}).then((resolved) => {
8683
if (resolved.pkgDirPath) {
87-
const resolvedPath = path.join(resolved.pkgDirPath, filePath);
84+
const resolvedPath = path.join(resolved.pkgDirPath, m.filePath);
8885
done({
8986
file: context.sys.normalizePath(resolvedPath)
9087
});
@@ -153,6 +150,27 @@ export function normalizePath(str: string) {
153150
return str;
154151
}
155152

153+
export function getModuleId(orgImport: string) {
154+
if (orgImport.startsWith('~')) {
155+
orgImport = orgImport.substring(1);
156+
}
157+
const splt = orgImport.split('/');
158+
const m = {
159+
moduleId: null as string,
160+
filePath: null as string,
161+
};
162+
163+
if (orgImport.startsWith('@') && splt.length > 1) {
164+
m.moduleId = splt.slice(0, 2).join('/');
165+
m.filePath = splt.slice(2).join('/');
166+
} else {
167+
m.moduleId = splt[0];
168+
m.filePath = splt.slice(1).join('/');
169+
}
170+
171+
return m;
172+
};
173+
156174
const EXTENDED_PATH_REGEX = /^\\\\\?\\/;
157175
const NON_ASCII_REGEX = /[^\x00-\x80]+/;
158176
const SLASH_REGEX = /\\/g;

test/utils.spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,43 @@ describe('createResultsId', () => {
104104
});
105105

106106
});
107+
108+
describe('getModuleId', () => {
109+
110+
it('getModuleId non-scoped ~ package', () => {
111+
const m = util.getModuleId('~ionicons/dist/css/ionicons.css');
112+
expect(m.moduleId).toBe('ionicons');
113+
expect(m.filePath).toBe('dist/css/ionicons.css');
114+
});
115+
116+
it('getModuleId non-scoped package', () => {
117+
const m = util.getModuleId('ionicons/dist/css/ionicons.css');
118+
expect(m.moduleId).toBe('ionicons');
119+
expect(m.filePath).toBe('dist/css/ionicons.css');
120+
});
121+
122+
it('getModuleId non-scoped package, no path', () => {
123+
const m = util.getModuleId('ionicons');
124+
expect(m.moduleId).toBe('ionicons');
125+
expect(m.filePath).toBe('');
126+
});
127+
128+
it('getModuleId scoped ~ package', () => {
129+
const m = util.getModuleId('~@ionic/core/dist/ionic/css/ionic.css');
130+
expect(m.moduleId).toBe('@ionic/core');
131+
expect(m.filePath).toBe('dist/ionic/css/ionic.css');
132+
});
133+
134+
it('getModuleId scoped package', () => {
135+
const m = util.getModuleId('@ionic/core/dist/ionic/css/ionic.css');
136+
expect(m.moduleId).toBe('@ionic/core');
137+
expect(m.filePath).toBe('dist/ionic/css/ionic.css');
138+
});
139+
140+
it('getModuleId scoped package, no path', () => {
141+
const m = util.getModuleId('@ionic/core');
142+
expect(m.moduleId).toBe('@ionic/core');
143+
expect(m.filePath).toBe('');
144+
});
145+
146+
});

0 commit comments

Comments
 (0)