Skip to content

Commit

Permalink
feat: Add support for .esbuild.(js|jsx|ts|tsx) extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 2, 2022
1 parent 5680b3f commit fcb9672
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 1 deletion.
54 changes: 53 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var path = require('path');
var endsInTs = /\.ts$/;
var endsInTsx = /\.tsx$/;
var endsInBabelJs = /\.babel\.[jt]s(x)$/;
var endsInEsbuildJs = /\.esbuild\.js$/;
var endsInEsbuildJsx = /\.esbuild\.jsx$/;
var endsInEsbuildTs = /\.esbuild\.ts$/;
var endsInEsbuildTsx = /\.esbuild\.tsx$/;

var mjsStub = path.join(__dirname, 'mjs-stub');

Expand Down Expand Up @@ -71,11 +75,59 @@ var extensions = {
'.coffee.md': ['coffeescript/register', 'coffee-script/register', 'coffeescript', 'coffee-script'],
'.csv': 'require-csv',
'.eg': 'earlgrey/register',
'.esbuild.js': {
module: 'esbuild-register/dist/node',
register: function(mod) {
mod.register({
extensions: ['.js'],
target: 'node' + process.version.slice(1),
hookMatcher: function(file) {
return endsInEsbuildJs.test(file);
},
});
},
},
'.esbuild.jsx': {
module: 'esbuild-register/dist/node',
register: function(mod) {
mod.register({
extensions: ['.jsx'],
target: 'node' + process.version.slice(1),
hookMatcher: function(file) {
return endsInEsbuildJsx.test(file);
},
});
},
},
'.esbuild.ts': {
module: 'esbuild-register/dist/node',
register: function(mod) {
mod.register({
extensions: ['.ts'],
target: 'node' + process.version.slice(1),
hookMatcher: function(file) {
return endsInEsbuildTs.test(file);
},
});
},
},
'.esbuild.tsx': {
module: 'esbuild-register/dist/node',
register: function(mod) {
mod.register({
extensions: ['.tsx'],
target: 'node' + process.version.slice(1),
hookMatcher: function(file) {
return endsInEsbuildTsx.test(file);
},
});
},
},
'.esm.js': {
module: 'esm',
register: function(hook) {
// register on .js extension due to https://github.com/joyent/node/blob/v0.12.0/lib/module.js#L353
// which only captures the final extension (.babel.js -> .js)
// which only captures the final extension (.esm.js -> .js)
var esmLoader = hook(module);
require.extensions['.js'] = esmLoader('module')._extensions['.js'];
},
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/esbuild.js/0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"esbuild": "^0.14.29",
"esbuild-register": "^3.3.2"
}
}
14 changes: 14 additions & 0 deletions test/fixtures/esbuild.js/0/test.esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Foo {
#x = 1
#y = 2
}

export default {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1,
},
},
};
6 changes: 6 additions & 0 deletions test/fixtures/esbuild.jsx/0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"esbuild": "^0.14.29",
"esbuild-register": "^3.3.2"
}
}
28 changes: 28 additions & 0 deletions test/fixtures/esbuild.jsx/0/test.esbuild.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const React = {
createElement(Component) {
return Component()
}
}

class Foo {
#x = 1
#y = 2
}

function Bar() {
const foo = new Foo();

return foo;
}

const a = <Bar />

export default {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1,
},
},
};
6 changes: 6 additions & 0 deletions test/fixtures/esbuild.ts/0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"esbuild": "^0.14.29",
"esbuild-register": "^3.3.2"
}
}
14 changes: 14 additions & 0 deletions test/fixtures/esbuild.ts/0/test.esbuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Foo {
#x: number = 1
#y: number = 2
}

export default {
data: {
trueKey: true as boolean,
falseKey: false as boolean,
subKey: {
subProp: 1,
},
},
} as const;
6 changes: 6 additions & 0 deletions test/fixtures/esbuild.tsx/0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"esbuild": "^0.14.29",
"esbuild-register": "^3.3.2"
}
}
28 changes: 28 additions & 0 deletions test/fixtures/esbuild.tsx/0/test.esbuild.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const React = {
createElement(Component) {
return Component()
}
}

class Foo {
#x: number = 1
#y: number = 2
}

function Bar() {
const foo = new Foo();

return foo;
}

const a = <Bar />

export default {
data: {
trueKey: true as boolean,
falseKey: false as boolean,
subKey: {
subProp: 1,
},
},
} as const;
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ describe('interpret.extensions', function() {
case '.ts':
case '.tsx':
case '.esm.js':
case '.esbuild.js':
case '.esbuild.jsx':
case '.esbuild.ts':
case '.esbuild.tsx':
expected = {
default: {
data: {
Expand Down

0 comments on commit fcb9672

Please sign in to comment.