From 1e3d0f81c0aa96e6ea084a75a54af2bf3be53aef Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Fri, 1 Apr 2022 18:40:44 -0700 Subject: [PATCH] feat: Support `.babel.(jsx|tsx)` extensions --- index.js | 22 ++++++++++++++++++++++ test/fixtures/babel.jsx/0/.babelrc | 3 +++ test/fixtures/babel.jsx/0/package.json | 7 +++++++ test/fixtures/babel.jsx/0/test.babel.jsx | 17 +++++++++++++++++ test/fixtures/babel.tsx/0/.babelrc | 13 +++++++++++++ test/fixtures/babel.tsx/0/data.babel.tsx | 19 +++++++++++++++++++ test/fixtures/babel.tsx/0/package.json | 9 +++++++++ test/fixtures/babel.tsx/0/test.babel.tsx | 20 ++++++++++++++++++++ test/index.js | 1 + 9 files changed, 111 insertions(+) create mode 100644 test/fixtures/babel.jsx/0/.babelrc create mode 100644 test/fixtures/babel.jsx/0/package.json create mode 100644 test/fixtures/babel.jsx/0/test.babel.jsx create mode 100644 test/fixtures/babel.tsx/0/.babelrc create mode 100644 test/fixtures/babel.tsx/0/data.babel.tsx create mode 100644 test/fixtures/babel.tsx/0/package.json create mode 100644 test/fixtures/babel.tsx/0/test.babel.tsx diff --git a/index.js b/index.js index 56adccd..f3de9ae 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,9 @@ var endsInJsx = /\.jsx$/; var endsInTs = /\.ts$/; var endsInTsx = /\.tsx$/; var endsInBabelJs = /\.babel\.js$/; +var endsInBabelJsx = /\.babel\.jsx$/; var endsInBabelTs = /\.babel\.ts$/; +var endsInBabelTsx = /\.babel\.tsx$/; var endsInEsbuildJs = /\.esbuild\.js$/; var endsInEsbuildJsx = /\.esbuild\.jsx$/; var endsInEsbuildTs = /\.esbuild\.ts$/; @@ -57,6 +59,16 @@ var extensions = { }, }, ], + '.babel.jsx': { + module: '@babel/register', + register: function(hook) { + hook({ + extensions: '.jsx', + rootMode: 'upward-optional', + overrides: [{ only: [endsInBabelJsx] }], + }); + }, + }, '.babel.ts': [ { module: '@babel/register', @@ -69,6 +81,16 @@ var extensions = { }, }, ], + '.babel.tsx': { + module: '@babel/register', + register: function(hook) { + hook({ + extensions: '.tsx', + rootMode: 'upward-optional', + overrides: [{ only: [endsInBabelTsx] }], + }); + }, + }, '.buble.js': 'buble/register', '.cirru': 'cirru-script/lib/register', '.cjsx': 'node-cjsx/register', diff --git a/test/fixtures/babel.jsx/0/.babelrc b/test/fixtures/babel.jsx/0/.babelrc new file mode 100644 index 0000000..90bbf88 --- /dev/null +++ b/test/fixtures/babel.jsx/0/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["@babel/preset-react"], +} diff --git a/test/fixtures/babel.jsx/0/package.json b/test/fixtures/babel.jsx/0/package.json new file mode 100644 index 0000000..3d1a2a6 --- /dev/null +++ b/test/fixtures/babel.jsx/0/package.json @@ -0,0 +1,7 @@ +{ + "dependencies": { + "@babel/core": "^7.2.2", + "@babel/preset-react": "^7.0.0", + "@babel/register": "^7.0.0" + } +} diff --git a/test/fixtures/babel.jsx/0/test.babel.jsx b/test/fixtures/babel.jsx/0/test.babel.jsx new file mode 100644 index 0000000..e9bb137 --- /dev/null +++ b/test/fixtures/babel.jsx/0/test.babel.jsx @@ -0,0 +1,17 @@ +const React = { + createElement: function (Component) { + return Component(); + } +}; + +// Test harmony arrow functions +const Component = () => { + var trueKey = true; + var falseKey = false; + var subKey = { subProp: 1 }; + // Test harmony object short notation + return { data: { trueKey, falseKey, subKey } }; +}; + +// Test JSX syntax +module.exports = ; diff --git a/test/fixtures/babel.tsx/0/.babelrc b/test/fixtures/babel.tsx/0/.babelrc new file mode 100644 index 0000000..5bcced6 --- /dev/null +++ b/test/fixtures/babel.tsx/0/.babelrc @@ -0,0 +1,13 @@ +{ + "presets": [ + "@babel/preset-env", + "@babel/preset-react", + [ + "@babel/preset-typescript", + { + "isTSX": true, + "allExtensions": true + } + ] + ] +} diff --git a/test/fixtures/babel.tsx/0/data.babel.tsx b/test/fixtures/babel.tsx/0/data.babel.tsx new file mode 100644 index 0000000..e41be4f --- /dev/null +++ b/test/fixtures/babel.tsx/0/data.babel.tsx @@ -0,0 +1,19 @@ +var test: { + data: { + trueKey: boolean, + falseKey: boolean, + subKey: { + subProp: number + } + } +} = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1 + } + } +} + +export default test diff --git a/test/fixtures/babel.tsx/0/package.json b/test/fixtures/babel.tsx/0/package.json new file mode 100644 index 0000000..5fa8c33 --- /dev/null +++ b/test/fixtures/babel.tsx/0/package.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + "@babel/core": "^7.2.2", + "@babel/preset-env": "^7.2.3", + "@babel/preset-react": "^7.0.0", + "@babel/preset-typescript": "^7.1.0", + "@babel/register": "^7.0.0" + } +} diff --git a/test/fixtures/babel.tsx/0/test.babel.tsx b/test/fixtures/babel.tsx/0/test.babel.tsx new file mode 100644 index 0000000..7324803 --- /dev/null +++ b/test/fixtures/babel.tsx/0/test.babel.tsx @@ -0,0 +1,20 @@ +import data from "./data.babel" + +const React = { + createElement(Component: () => any) { + return Component() + } +} + +// Test harmony arrow functions. +const Component = () => { + var trueKey: boolean = true + var falseKey: boolean = false + var subKey = { subProp: 1 } + + // Test harmony object short notation. + return { data: { trueKey, falseKey, subKey } } +} + +// Test TSX syntax. +export default diff --git a/test/index.js b/test/index.js index b0ccad2..cb0ce83 100644 --- a/test/index.js +++ b/test/index.js @@ -156,6 +156,7 @@ describe('interpret.extensions', function() { case '.ts': case '.tsx': case '.esm.js': + case '.babel.tsx': case '.esbuild.js': case '.esbuild.jsx': case '.esbuild.ts':