From 972be6108693042000689e3c4ae191af7aae38d7 Mon Sep 17 00:00:00 2001 From: codermarcos Date: Wed, 27 Mar 2024 23:38:58 -0300 Subject: [PATCH 1/4] chore: remove unused require From fork codermarcos/interpret On branch master Changes to be committed: modified: scripts/plugin.mjs --- scripts/plugin.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/plugin.mjs b/scripts/plugin.mjs index 3bfec4c..d8aa7da 100644 --- a/scripts/plugin.mjs +++ b/scripts/plugin.mjs @@ -1,5 +1,4 @@ import fs from 'fs'; -import path from 'path'; import yaml from 'js-yaml'; import { codeImport } from 'remark-code-import'; From 5c551c761a2e96bcf145b4e33b4ad1d333643221 Mon Sep 17 00:00:00 2001 From: codermarcos Date: Wed, 27 Mar 2024 23:42:10 -0300 Subject: [PATCH 2/4] feat: Add new version of SWC to fix #98 From fork codermarcos/interpret On branch master Changes to be committed: modified: index.js --- index.js | 315 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 233 insertions(+), 82 deletions(-) diff --git a/index.js b/index.js index b951fd9..fbc2c4a 100644 --- a/index.js +++ b/index.js @@ -263,100 +263,204 @@ var extensions = { hook.registerTSX(config); }, }, - '.swc.js': { - module: '@swc/register', - register: function (hook, config) { - config = config || { - only: [endsInSwcJs], - ignore: [isNodeModules], - jsc: { - parser: { - syntax: 'ecmascript', + '.swc.js': [ + { + module: '@swc-node/register', + register: function (mod, config) { + config = config || { + only: [endsInSwcJs], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'ecmascript', + }, }, - }, - module: { - type: 'commonjs', - }, - }; + module: { + type: 'commonjs', + }, + }; - hook( - Object.assign({}, config, { - extensions: '.js', - }) - ); + mod.register( + Object.assign({}, config, { + extensions: '.js', + }) + ); + }, }, - }, - '.swc.jsx': { - module: '@swc/register', - register: function (hook, config) { - config = config || { - only: [endsInSwcJsx], - ignore: [isNodeModules], - jsc: { - parser: { - syntax: 'ecmascript', - jsx: true, + { + module: '@swc/register', + register: function (hook, config) { + config = config || { + only: [endsInSwcJs], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'ecmascript', + }, }, - }, - module: { - type: 'commonjs', - }, - }; + module: { + type: 'commonjs', + }, + }; + + console.log(hook) + + hook( + Object.assign({}, config, { + extensions: '.js', + }) + ); + }, + } + ], + '.swc.jsx': [ + { + module: '@swc-node/register', + register: function (mod, config) { + config = config || { + only: [endsInSwcJsx], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'ecmascript', + jsx: true, + }, + }, + module: { + type: 'commonjs', + }, + }; - hook( - Object.assign({}, config, { - extensions: '.jsx', - }) - ); + mod.register( + Object.assign({}, config, { + extensions: '.jsx', + }) + ); + }, }, - }, - '.swc.ts': { - module: '@swc/register', - register: function (hook, config) { - config = config || { - only: [endsInSwcTs], - ignore: [isNodeModules], - jsc: { - parser: { - syntax: 'typescript', + { + module: '@swc/register', + register: function (hook, config) { + config = config || { + only: [endsInSwcJsx], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'ecmascript', + jsx: true, + }, }, - }, - module: { - type: 'commonjs', - }, - }; + module: { + type: 'commonjs', + }, + }; + + hook( + Object.assign({}, config, { + extensions: '.jsx', + }) + ); + }, + } + ], + '.swc.ts': [ + { + module: '@swc-node/register', + register: function (mod, config) { + config = config || { + only: [endsInSwcTs], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'typescript', + }, + }, + module: { + type: 'commonjs', + }, + }; - hook( - Object.assign({}, config, { - extensions: '.ts', - }) - ); + mod.register( + Object.assign({}, config, { + extensions: '.ts', + }) + ); + }, }, - }, - '.swc.tsx': { - module: '@swc/register', - register: function (hook, config) { - config = config || { - only: [endsInSwcTsx], - ignore: [isNodeModules], - jsc: { - parser: { - syntax: 'typescript', - tsx: true, + { + module: '@swc/register', + register: function (hook, config) { + config = config || { + only: [endsInSwcTs], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'typescript', + }, }, - }, - module: { - type: 'commonjs', - }, - }; + module: { + type: 'commonjs', + }, + }; + + hook( + Object.assign({}, config, { + extensions: '.ts', + }) + ); + }, + } + ], + '.swc.tsx': [ + { + module: '@swc-node/register', + register: function (mod, config) { + config = config || { + only: [endsInSwcTsx], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'typescript', + tsx: true, + }, + }, + module: { + type: 'commonjs', + }, + }; - hook( - Object.assign({}, config, { - extensions: '.tsx', - }) - ); + mod.register( + Object.assign({}, config, { + extensions: '.tsx', + }) + ); + }, }, - }, + { + module: '@swc/register', + register: function (hook, config) { + config = config || { + only: [endsInSwcTsx], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'typescript', + tsx: true, + }, + }, + module: { + type: 'commonjs', + }, + }; + + hook( + Object.assign({}, config, { + extensions: '.tsx', + }) + ); + }, + } + ], '.toml': { module: 'toml-require', register: function (hook, config) { @@ -401,6 +505,29 @@ var extensions = { ); }, }, + { + module: '@swc-node/register', + register: function (mod, config) { + config = config || { + only: [endsInTs], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'typescript', + }, + }, + module: { + type: 'commonjs', + }, + }; + + mod.register( + Object.assign({}, config, { + extensions: '.ts', + }) + ); + }, + }, { module: '@swc/register', register: function (hook, config) { @@ -474,6 +601,30 @@ var extensions = { ); }, }, + { + module: '@swc-node/register', + register: function (mod, config) { + config = config || { + only: [endsInTsx], + ignore: [isNodeModules], + jsc: { + parser: { + syntax: 'typescript', + tsx: true, + }, + }, + module: { + type: 'commonjs', + }, + }; + + mod.register( + Object.assign({}, config, { + extensions: '.tsx', + }) + ); + }, + }, { module: '@swc/register', register: function (hook, config) { From 94097d216466a3a98064fd0cb4d904d68efa4fcd Mon Sep 17 00:00:00 2001 From: codermarcos Date: Wed, 27 Mar 2024 23:43:09 -0300 Subject: [PATCH 3/4] fix: Fix tests From fork codermarcos/interpret On branch master Changes to be committed: modified: test/fixtures/swc.js/0/package.json new file: test/fixtures/swc.js/1/package.json new file: test/fixtures/swc.js/1/test.swc.js modified: test/fixtures/swc.jsx/0/package.json new file: test/fixtures/swc.jsx/1/package.json new file: test/fixtures/swc.jsx/1/test.swc.jsx modified: test/fixtures/swc.ts/0/package.json new file: test/fixtures/swc.ts/1/package.json new file: test/fixtures/swc.ts/1/test.swc.ts modified: test/fixtures/swc.tsx/0/package.json new file: test/fixtures/swc.tsx/1/package.json new file: test/fixtures/swc.tsx/1/test.swc.tsx modified: test/fixtures/ts/4/package.json new file: test/fixtures/ts/5/package.json new file: test/fixtures/ts/5/test.ts modified: test/fixtures/tsx/4/package.json new file: test/fixtures/tsx/5/package.json new file: test/fixtures/tsx/5/test.tsx modified: test/index.js --- test/fixtures/swc.js/0/package.json | 2 +- test/fixtures/swc.js/1/package.json | 6 ++++++ test/fixtures/swc.js/1/test.swc.js | 14 ++++++++++++++ test/fixtures/swc.jsx/0/package.json | 2 +- test/fixtures/swc.jsx/1/package.json | 6 ++++++ test/fixtures/swc.jsx/1/test.swc.jsx | 28 ++++++++++++++++++++++++++++ test/fixtures/swc.ts/0/package.json | 2 +- test/fixtures/swc.ts/1/package.json | 6 ++++++ test/fixtures/swc.ts/1/test.swc.ts | 14 ++++++++++++++ test/fixtures/swc.tsx/0/package.json | 2 +- test/fixtures/swc.tsx/1/package.json | 6 ++++++ test/fixtures/swc.tsx/1/test.swc.tsx | 18 ++++++++++++++++++ test/fixtures/ts/4/package.json | 2 +- test/fixtures/ts/5/package.json | 6 ++++++ test/fixtures/ts/5/test.ts | 15 +++++++++++++++ test/fixtures/tsx/4/package.json | 2 +- test/fixtures/tsx/5/package.json | 6 ++++++ test/fixtures/tsx/5/test.tsx | 18 ++++++++++++++++++ test/index.js | 2 +- 19 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/swc.js/1/package.json create mode 100644 test/fixtures/swc.js/1/test.swc.js create mode 100644 test/fixtures/swc.jsx/1/package.json create mode 100644 test/fixtures/swc.jsx/1/test.swc.jsx create mode 100644 test/fixtures/swc.ts/1/package.json create mode 100644 test/fixtures/swc.ts/1/test.swc.ts create mode 100644 test/fixtures/swc.tsx/1/package.json create mode 100644 test/fixtures/swc.tsx/1/test.swc.tsx create mode 100644 test/fixtures/ts/5/package.json create mode 100644 test/fixtures/ts/5/test.ts create mode 100644 test/fixtures/tsx/5/package.json create mode 100644 test/fixtures/tsx/5/test.tsx diff --git a/test/fixtures/swc.js/0/package.json b/test/fixtures/swc.js/0/package.json index e345156..b7943f8 100644 --- a/test/fixtures/swc.js/0/package.json +++ b/test/fixtures/swc.js/0/package.json @@ -1,6 +1,6 @@ { "dependencies": { "@swc/core": "^1.2.110", - "@swc/register": "^0.1.7" + "@swc-node/register": "^1.9.0" } } diff --git a/test/fixtures/swc.js/1/package.json b/test/fixtures/swc.js/1/package.json new file mode 100644 index 0000000..e345156 --- /dev/null +++ b/test/fixtures/swc.js/1/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@swc/core": "^1.2.110", + "@swc/register": "^0.1.7" + } +} diff --git a/test/fixtures/swc.js/1/test.swc.js b/test/fixtures/swc.js/1/test.swc.js new file mode 100644 index 0000000..5dfbff3 --- /dev/null +++ b/test/fixtures/swc.js/1/test.swc.js @@ -0,0 +1,14 @@ +class Foo { + #x = 1; + #y = 2; +} + +export default { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; diff --git a/test/fixtures/swc.jsx/0/package.json b/test/fixtures/swc.jsx/0/package.json index e345156..b7943f8 100644 --- a/test/fixtures/swc.jsx/0/package.json +++ b/test/fixtures/swc.jsx/0/package.json @@ -1,6 +1,6 @@ { "dependencies": { "@swc/core": "^1.2.110", - "@swc/register": "^0.1.7" + "@swc-node/register": "^1.9.0" } } diff --git a/test/fixtures/swc.jsx/1/package.json b/test/fixtures/swc.jsx/1/package.json new file mode 100644 index 0000000..e345156 --- /dev/null +++ b/test/fixtures/swc.jsx/1/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@swc/core": "^1.2.110", + "@swc/register": "^0.1.7" + } +} diff --git a/test/fixtures/swc.jsx/1/test.swc.jsx b/test/fixtures/swc.jsx/1/test.swc.jsx new file mode 100644 index 0000000..448a430 --- /dev/null +++ b/test/fixtures/swc.jsx/1/test.swc.jsx @@ -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 = ; + +export default { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; diff --git a/test/fixtures/swc.ts/0/package.json b/test/fixtures/swc.ts/0/package.json index e345156..b7943f8 100644 --- a/test/fixtures/swc.ts/0/package.json +++ b/test/fixtures/swc.ts/0/package.json @@ -1,6 +1,6 @@ { "dependencies": { "@swc/core": "^1.2.110", - "@swc/register": "^0.1.7" + "@swc-node/register": "^1.9.0" } } diff --git a/test/fixtures/swc.ts/1/package.json b/test/fixtures/swc.ts/1/package.json new file mode 100644 index 0000000..e345156 --- /dev/null +++ b/test/fixtures/swc.ts/1/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@swc/core": "^1.2.110", + "@swc/register": "^0.1.7" + } +} diff --git a/test/fixtures/swc.ts/1/test.swc.ts b/test/fixtures/swc.ts/1/test.swc.ts new file mode 100644 index 0000000..9a0862f --- /dev/null +++ b/test/fixtures/swc.ts/1/test.swc.ts @@ -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; diff --git a/test/fixtures/swc.tsx/0/package.json b/test/fixtures/swc.tsx/0/package.json index e345156..b7943f8 100644 --- a/test/fixtures/swc.tsx/0/package.json +++ b/test/fixtures/swc.tsx/0/package.json @@ -1,6 +1,6 @@ { "dependencies": { "@swc/core": "^1.2.110", - "@swc/register": "^0.1.7" + "@swc-node/register": "^1.9.0" } } diff --git a/test/fixtures/swc.tsx/1/package.json b/test/fixtures/swc.tsx/1/package.json new file mode 100644 index 0000000..e345156 --- /dev/null +++ b/test/fixtures/swc.tsx/1/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@swc/core": "^1.2.110", + "@swc/register": "^0.1.7" + } +} diff --git a/test/fixtures/swc.tsx/1/test.swc.tsx b/test/fixtures/swc.tsx/1/test.swc.tsx new file mode 100644 index 0000000..d26bcf1 --- /dev/null +++ b/test/fixtures/swc.tsx/1/test.swc.tsx @@ -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 ; diff --git a/test/fixtures/ts/4/package.json b/test/fixtures/ts/4/package.json index e345156..b7943f8 100644 --- a/test/fixtures/ts/4/package.json +++ b/test/fixtures/ts/4/package.json @@ -1,6 +1,6 @@ { "dependencies": { "@swc/core": "^1.2.110", - "@swc/register": "^0.1.7" + "@swc-node/register": "^1.9.0" } } diff --git a/test/fixtures/ts/5/package.json b/test/fixtures/ts/5/package.json new file mode 100644 index 0000000..e345156 --- /dev/null +++ b/test/fixtures/ts/5/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@swc/core": "^1.2.110", + "@swc/register": "^0.1.7" + } +} diff --git a/test/fixtures/ts/5/test.ts b/test/fixtures/ts/5/test.ts new file mode 100644 index 0000000..5044977 --- /dev/null +++ b/test/fixtures/ts/5/test.ts @@ -0,0 +1,15 @@ +var test = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; + +var main = { + default: test, +}; + +export = main; diff --git a/test/fixtures/tsx/4/package.json b/test/fixtures/tsx/4/package.json index e345156..b7943f8 100644 --- a/test/fixtures/tsx/4/package.json +++ b/test/fixtures/tsx/4/package.json @@ -1,6 +1,6 @@ { "dependencies": { "@swc/core": "^1.2.110", - "@swc/register": "^0.1.7" + "@swc-node/register": "^1.9.0" } } diff --git a/test/fixtures/tsx/5/package.json b/test/fixtures/tsx/5/package.json new file mode 100644 index 0000000..e345156 --- /dev/null +++ b/test/fixtures/tsx/5/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "@swc/core": "^1.2.110", + "@swc/register": "^0.1.7" + } +} diff --git a/test/fixtures/tsx/5/test.tsx b/test/fixtures/tsx/5/test.tsx new file mode 100644 index 0000000..d26bcf1 --- /dev/null +++ b/test/fixtures/tsx/5/test.tsx @@ -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 ; diff --git a/test/index.js b/test/index.js index 1f9ca83..71bd1b7 100644 --- a/test/index.js +++ b/test/index.js @@ -131,7 +131,7 @@ describe('interpret.extensions', function () { } // Skip any swc test on linux due to https://github.com/swc-project/swc/issues/4107 - if (name === '@swc/register' && process.platform === 'linux') { + if ((name === '@swc/register' || '@swc-node/register') && process.platform === 'linux') { this.skip(); } From b9c367e19a0f36794b62e0519b7a97b65974c142 Mon Sep 17 00:00:00 2001 From: codermarcos Date: Thu, 28 Mar 2024 10:20:35 -0300 Subject: [PATCH 4/4] fix: Remove console log On branch master Changes to be committed: modified: index.js --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index fbc2c4a..d9f3a1c 100644 --- a/index.js +++ b/index.js @@ -303,7 +303,6 @@ var extensions = { }, }; - console.log(hook) hook( Object.assign({}, config, {