Skip to content

Commit ef7466c

Browse files
committed
Fix: [#20] Check external package import path
1 parent c3ea0f1 commit ef7466c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/utils/getImportedPackages.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,43 @@ describe('getImportedPackages', () => {
238238
archiver: '3.2.1',
239239
});
240240
});
241+
242+
it('skips relative specifiers marked as external', () => {
243+
const metafileMock: Metafile = {
244+
inputs: {
245+
'../../../common/slack/parseMiddleware.js': {
246+
bytes: 1000,
247+
imports: [],
248+
},
249+
'example/example.ts': {
250+
bytes: 130,
251+
imports: [
252+
{
253+
path: '../../../common/slack/parseMiddleware.js',
254+
kind: 'import-statement',
255+
external: true,
256+
},
257+
{
258+
path: 'archiver',
259+
kind: 'import-statement',
260+
external: true,
261+
},
262+
],
263+
},
264+
},
265+
outputs: {
266+
'example/dist/testfunc.js': {
267+
imports: [],
268+
exports: [],
269+
entryPoint: 'example/example.ts',
270+
inputs: {},
271+
bytes: 82693,
272+
},
273+
},
274+
};
275+
const packages = getImportedPackages('example/example.ts', metafileMock);
276+
expect(packages).toEqual({
277+
archiver: '3.2.1',
278+
});
279+
});
241280
});

src/utils/getImportedPackages.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ const getImportedPackages = (
6565
}
6666
*/
6767
// TODO: Extract to separate file and add full tests for various scenarios
68-
if (inputImport.external === true) {
68+
69+
// Node Package decision based on https://nodejs.org/api/esm.html#terminology
70+
// Check if it's a relative specifier (./ or ../) or absolute specifier (/ or file://)
71+
const isRelativeOrAbsoluteSpecifier =
72+
inputImport.path.startsWith('./') ||
73+
inputImport.path.startsWith('../') ||
74+
inputImport.path.startsWith('/') ||
75+
inputImport.path.startsWith('file://');
76+
77+
if (inputImport.external === true && !isRelativeOrAbsoluteSpecifier) {
6978
const packageName = inputImport.path.startsWith('@')
7079
? inputImport.path.split('/').slice(0, 2).join('/')
7180
: inputImport.path.split('/')[0];

0 commit comments

Comments
 (0)