Skip to content

Commit

Permalink
feat: Add @swc/register as a loader for .ts and .tsx extensions (
Browse files Browse the repository at this point in the history
  • Loading branch information
HRKings committed Apr 2, 2022
1 parent 8a8df59 commit f160451
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function ignoreNonBabelAndNodeModules(file) {
path.relative(process.cwd(), file).split(path.sep).indexOf('node_modules') >= 0;
}

// Not part of the above check because it seems broken
function isNodeModules(file) {
return path.relative(process.cwd(), file).split(path.sep).indexOf('node_modules') >= 0;
}

var extensions = {
'.babel.js': [
{
Expand Down Expand Up @@ -246,6 +251,16 @@ var extensions = {
});
},
},
{
module: '@swc/register',
register: function(hook) {
hook({
extensions: '.ts',
only: [endsInTs],
ignore: [isNodeModules],
});
},
},
],
'.tsx': [
'ts-node/register',
Expand Down Expand Up @@ -273,6 +288,16 @@ var extensions = {
});
},
},
{
module: '@swc/register',
register: function(hook) {
hook({
extensions: '.tsx',
only: [endsInTsx],
ignore: [isNodeModules],
});
},
},
],
'.wisp': 'wisp/engine/node',
'.xml': 'require-xml',
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/ts/7/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
}
},
"module": {
"type": "commonjs"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/ts/7/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@swc/core": "^1.2.110",
"@swc/register": "^0.1.7"
}
}
15 changes: 15 additions & 0 deletions test/fixtures/ts/7/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var test = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};

var main = {
default: test
};

export = main;
11 changes: 11 additions & 0 deletions test/fixtures/tsx/5/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
}
},
"module": {
"type": "commonjs"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/tsx/5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@swc/core": "^1.2.110",
"@swc/register": "^0.1.7"
}
}
18 changes: 18 additions & 0 deletions test/fixtures/tsx/5/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 <Component />

0 comments on commit f160451

Please sign in to comment.