Skip to content

Commit

Permalink
[Loaders] Only return the format if it's builtin or commonjs (fixes #287
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bjornstar committed Apr 17, 2022
1 parent 4d92c49 commit 4897b33
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
5 changes: 4 additions & 1 deletion lib/loaders/get-format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export async function getFormat(url, context, defaultGetFormat) {
return await defaultGetFormat(url, context, defaultGetFormat);
} catch (error) {
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
return require('get-package-type')(required).then(format => ({ format }));
return require('get-package-type')(required).then(format => {
if (!['builtin', 'commonjs'].includes(format)) throw error;
return { format };
});
}
}
5 changes: 4 additions & 1 deletion lib/loaders/load.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export async function load(url, context, defaultLoad) {
return await defaultLoad(url, context, defaultLoad);
} catch (error) {
if (error.code !== 'ERR_UNKNOWN_FILE_EXTENSION') throw error;
return require('get-package-type')(required).then(format => ({ format, source: null }));
return require('get-package-type')(required).then(format => {
if (!['builtin', 'commonjs'].includes(format)) throw error;
return { format };
});
}
}
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@
"dynamic-dedupe": "^0.3.0",
"filewatcher": "~3.0.0",
"get-package-type": "^0.1.0",
"minimist": "^1.2.5",
"minimist": "^1.2.6",
"node-notifier": "^8.0.1",
"resolve": "^1.0.0",
"semver": "^7.3.5"
"resolve": "^1.22.0",
"semver": "^7.3.7"
},
"devDependencies": {
"@types/node": "^16.11.3",
"eslint": "^8.10.0",
"eslint-plugin-import": "^2.22.1",
"@types/node": "^17.0.24",
"eslint": "^8.13.0",
"eslint-plugin-import": "^2.26.0",
"husky": "^7.0.4",
"lint-staged": "^11.2.3",
"prettier": "^2.2.1",
"tap": "^15.1.6",
"lint-staged": "^12.3.8",
"prettier": "^2.6.2",
"tap": "^16.0.1",
"tap-xunit": "^2.4.1",
"touch": "^3.1.0",
"ts-node": "^10.3.1",
"typescript": "^4.1.5"
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
},
"lint-staged": {
"*.{js,mjs}": "eslint --cache --fix",
Expand Down
2 changes: 2 additions & 0 deletions test/fixture/typescript-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// We should not be able to load typescript files as ESModules
export default ['test', 'data'] as const
1 change: 1 addition & 0 deletions test/fixture/typescript-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
14 changes: 14 additions & 0 deletions test/spawn/typescript-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const tap = require('tap');

const { spawn } = require('../utils');

tap.test(
'Gives ERR_UNKNOWN_FILE_EXTENSION when loading typescript files and package type is "module"',
t => {
spawn('typescript-module/index.ts', out => {
if (out.match(/ERR_UNKNOWN_FILE_EXTENSION/)) {
return { exit: t.end.bind(t) };
}
});
}
);

0 comments on commit 4897b33

Please sign in to comment.