Skip to content

Commit 4897b33

Browse files
committed
[Loaders] Only return the format if it's builtin or commonjs (fixes #287)
1 parent 4d92c49 commit 4897b33

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

lib/loaders/get-format.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export async function getFormat(url, context, defaultGetFormat) {
1313
return await defaultGetFormat(url, context, defaultGetFormat);
1414
} catch (error) {
1515
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
16-
return require('get-package-type')(required).then(format => ({ format }));
16+
return require('get-package-type')(required).then(format => {
17+
if (!['builtin', 'commonjs'].includes(format)) throw error;
18+
return { format };
19+
});
1720
}
1821
}

lib/loaders/load.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export async function load(url, context, defaultLoad) {
1313
return await defaultLoad(url, context, defaultLoad);
1414
} catch (error) {
1515
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
16-
return require('get-package-type')(required).then(format => ({ format, source: null }));
16+
return require('get-package-type')(required).then(format => {
17+
if (!['builtin', 'commonjs'].includes(format)) throw error;
18+
return { format };
19+
});
1720
}
1821
}

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@
3636
"dynamic-dedupe": "^0.3.0",
3737
"filewatcher": "~3.0.0",
3838
"get-package-type": "^0.1.0",
39-
"minimist": "^1.2.5",
39+
"minimist": "^1.2.6",
4040
"node-notifier": "^8.0.1",
41-
"resolve": "^1.0.0",
42-
"semver": "^7.3.5"
41+
"resolve": "^1.22.0",
42+
"semver": "^7.3.7"
4343
},
4444
"devDependencies": {
45-
"@types/node": "^16.11.3",
46-
"eslint": "^8.10.0",
47-
"eslint-plugin-import": "^2.22.1",
45+
"@types/node": "^17.0.24",
46+
"eslint": "^8.13.0",
47+
"eslint-plugin-import": "^2.26.0",
4848
"husky": "^7.0.4",
49-
"lint-staged": "^11.2.3",
50-
"prettier": "^2.2.1",
51-
"tap": "^15.1.6",
49+
"lint-staged": "^12.3.8",
50+
"prettier": "^2.6.2",
51+
"tap": "^16.0.1",
5252
"tap-xunit": "^2.4.1",
5353
"touch": "^3.1.0",
54-
"ts-node": "^10.3.1",
55-
"typescript": "^4.1.5"
54+
"ts-node": "^10.7.0",
55+
"typescript": "^4.6.3"
5656
},
5757
"lint-staged": {
5858
"*.{js,mjs}": "eslint --cache --fix",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// We should not be able to load typescript files as ESModules
2+
export default ['test', 'data'] as const
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

test/spawn/typescript-module.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const tap = require('tap');
2+
3+
const { spawn } = require('../utils');
4+
5+
tap.test(
6+
'Gives ERR_UNKNOWN_FILE_EXTENSION when loading typescript files and package type is "module"',
7+
t => {
8+
spawn('typescript-module/index.ts', out => {
9+
if (out.match(/ERR_UNKNOWN_FILE_EXTENSION/)) {
10+
return { exit: t.end.bind(t) };
11+
}
12+
});
13+
}
14+
);

0 commit comments

Comments
 (0)