Skip to content

Commit

Permalink
chore: update babel and webpack configs to support TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie authored and Desplandis committed Oct 3, 2024
1 parent ebf37dd commit 8830d6d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
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", ".tsx"]
}],
["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
25 changes: 23 additions & 2 deletions config/babel-register/babel-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ 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);
// Try to resolve the path of an imported module.
// If the resolver failed, retry substituting the extension with '.ts'.
try {
return await nextResolve(specifier, context);
} catch (err) {
if (err.code === 'ERR_MODULE_NOT_FOUND') {
const url = new URL(specifier, context.parentURL);
url.pathname = url.pathname.replace(/\.[^/.]+$/, '.ts');
return nextResolve(url.href, context);
}
throw err;
}
}

/**
Expand All @@ -87,7 +98,17 @@ 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);
// Try to load the file using the default loader (which supports both
// ESM/CJS modules) but does not support '.ts' extensions. Use the ESM
// module loader for typescript files.
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
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@babel/core": "^7.25.2",
"@babel/plugin-transform-runtime": "^7.25.4",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@babel/register": "^7.24.6",
"@types/three": "^0.168.0",
"@xmldom/xmldom": "^0.9.2",
Expand Down
8 changes: 7 additions & 1 deletion webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ module.exports = () => {
return {
mode,
context: path.resolve(__dirname),
resolve: {
extensions: ['.ts', '.js'],
extensionAlias: {
'.js': ['.ts', '.js'],
},
},
entry: {
itowns: [
'core-js',
Expand Down Expand Up @@ -76,7 +82,7 @@ module.exports = () => {
module: {
rules: [
{
test: /\.js$/,
test: /\.(js|ts)$/,
exclude,
include,
use: babelLoaderOptions,
Expand Down

0 comments on commit 8830d6d

Please sign in to comment.