Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing typescript migration #2404

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"presets": [
["@babel/preset-typescript"],
["@babel/preset-env", {
"targets": {
"browsers": "defaults and supports webgl2"
Expand All @@ -8,7 +9,10 @@
}]
],
"plugins": [
["module-resolver", { "root": ["./src"] } ],
["module-resolver", {
"root": ["./src"],
"extensions": [".js", ".ts"]
}],
["babel-plugin-inline-import", {
"extensions": [
".json",
Expand All @@ -17,7 +21,9 @@
".css"
]
}],
["module-extension-resolver"],
["module-extension-resolver", {
"srcExtensions": [ ".ts", ".js" ]
}],
["@babel/plugin-transform-runtime", {
"regenerator": false
}],
Expand Down
8 changes: 7 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
extends: [
'eslint-config-airbnb-base',
'eslint-config-airbnb-base/rules/strict',
'plugin:@typescript-eslint/recommended',
],
parserOptions: {
ecmaVersion: 13,
Expand All @@ -13,7 +14,9 @@ module.exports = {
},
settings: {
'import/resolver': {
'babel-module': {},
'babel-module': {
extensions: ['.js', '.ts'],
},
},
},
env: {
Expand All @@ -23,6 +26,9 @@ module.exports = {
commonjs: true,
},
rules: {
'import/extensions': [
'off',
],
'no-trailing-spaces': 'warn',
'padded-blocks': 'warn',
'no-unused-vars': 'warn',
Expand Down
20 changes: 18 additions & 2 deletions config/babel-register/babel-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ async function transpile(source, context) {
* the Node.js default resolve hook after the last user-supplied resolve hook
*/
export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context);
const url = new URL(specifier, context.parentURL);

try {
return await nextResolve(specifier, context);
} catch (err) {
if (err.code === 'ERR_MODULE_NOT_FOUND') {
url.pathname = url.pathname.replace(/\.[^/.]+$/, '.ts');
return nextResolve(url.href, context);
}
throw err;
}
}

/**
Expand All @@ -87,7 +97,13 @@ export async function resolve(specifier, context, nextResolve) {
* the last user-supplied load hook
*/
export async function load(url, context, nextLoad) {
const { format, shortCircuit, source } = await nextLoad(url, context);
const { format, shortCircuit, source } = await nextLoad(url, context).catch(async (error) => {
if (error.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
return nextLoad(url, { ...context, format: 'module' });
}

throw error;
});

if (format !== 'module' && format !== 'commonjs') {
return { format, shortCircuit, source };
Expand Down
Loading
Loading