Code Monkey home page Code Monkey logo

ts-plugin-inferno's Introduction

ts-plugin-inferno

travis npm version

Typescript JSX transformer for InfernoJS.

This is a plugin for Typescript compiler that compiles Typescript JSX syntax ( TSX ) directly to Inferno API to avoid createElement method calls.

Install

yarn add -D ts-plugin-inferno typescript

General usage

const transformInferno = require('ts-plugin-inferno').default

// Typescript compiler options
options: {
    getCustomTransformers: () => ({
        after: [transformInferno()],
    }),
},

It's different depending on what bundler you're using. Please check the examples folder.

Breaking change in v6.0.0

Since version v6.0.0 this plugin only supports Javascript runtime environment where Object.assign is available. It's also highly recommended to set typescript settings as follows

  • compilerOptions.module to ES2015 or ES6 or higher.
  • commpilerOptions.target to ES2015 or higher

Usage with FuseBox

Look into the examples/fuse-box folder and the fuse.js file.

You could also try to build the project by running the following commands:

cd examples/fuse-box && npm install

npm run start:dev or npm run start:prod

Usage with webpack and ts-loader

Look into the examples/webpack folder and its webpack config.

You could also try to build the project by running the following commands:

cd examples/webpack && npm install

npm run build:prod or npm start

Testing

You can run the following command to test: npm test

Adding test cases

Write your test in a .tsx file and add it to tests/cases.

Compile with npm test and look into the tests/temp and verify.

Overwrite references by running the following command: npm run overwrite-references

Run npm test again to verify that all tests are passing.

Credits

This is fork of awesome ts-transform-inferno!

ts-plugin-inferno's People

Contributors

deamme avatar disposaboy avatar havunen avatar hronro avatar maijaheiskanen avatar nykula avatar salvatorepreviti avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

maijaheiskanen

ts-plugin-inferno's Issues

Type imports are not being removed during compilation

Type imports are not being removed during compilation

Type imports are not being removed during compilation when they are in the first inferno import statement (import {...} from 'inferno') of a file.

Steps to reproduce:

Steps to reproduce:

  1. Clone https://github.com/MaijaHeiskanen/ts-plugin-inferno/tree/type-imports-are-not-removed and checkout branch "type-imports-are-not-removed"
  2. Go to examples/rollup
  3. yarn to install dependencies
  4. yarn build to build the project

Output has an error:

yarn run v1.22.19
$ rimraf dist && cross-env NODE_ENV=production rollup -c

src/index.tsx → dist/app.js...
(!) Error when using sourcemap for reporting an error: Can't resolve original location of error.
src/components/Visualizer.tsx (6:9)
(!) Missing exports
https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module
src/components/Visualizer.tsx
InfernoNode is not exported by node_modules/inferno/index.esm.js
4:  */
5: // import {createVNode} from "inferno"; // If this line is uncommented, no problems
6: import { InfernoNode, createVNode } from "inferno";
            ^
7: import { a } from "./test";
8: export function Visualizer(_a) {
created dist/app.js in 679ms
Done in 1.34s.
  1. Go to examples/rollup/src/components/Visualizer.tsx
  2. Uncomment row 5 which is commented (// import {createVNode} from "inferno"; // If this line is uncommented, no problems)
  3. yarn build to build the project again

Output does not have an error anymore:

yarn run v1.22.19
$ rimraf dist && cross-env NODE_ENV=production rollup -c

src/index.tsx → dist/app.js...
created dist/app.js in 650ms
Done in 1.32s.

The transformer uses the first existing inferno import when adding stuff to inferno import. It seems like the transformer updating the import statement somehow messes up the process of removing imported types. This leads to a situation where the imported type is trying to be resolved from node_modules/inferno/index.esm.js instead of node_modules/inferno/index.d.ts.

Expected behavior

Type imports should have been removed when TS files have been compiled to JS.

TypeError: Cannot read properties of undefined (reading 'substring')

 at getVNodeProps (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:392:39)
    at createVNode (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:168:26)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:56:28)
    at getVNodeChildren (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:485:29)
    at createVNode (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:163:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:54:28)
    at visitNode (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86560:21)
    at visitEachChildOfParenthesizedExpression (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87238:32)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitNode (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86560:21)
    at visitEachChildOfReturnStatement (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87475:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitArrayWorker (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86622:51)
    at visitNodes2 (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86593:21)
    at visitEachChildOfBlock (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87396:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitNode (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86560:21)
    at visitFunctionBody (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86766:21)
    at visitEachChildOfMethodDeclaration (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86904:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitArrayWorker (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86622:51)
    at visitNodes2 (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86593:21)
    at visitEachChildOfClassDeclaration (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87547:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitArrayWorker (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86622:51)
    at visitNodes2 (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86593:21)
    at visitEachChildOfBlock (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87396:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitNode (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86560:21)
    at visitFunctionBody (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86766:21)
    at visitEachChildOfArrowFunction (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87261:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitArrayWorker (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86622:51)
    at visitNodes2 (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86593:21)
    at visitEachChildOfCallExpression (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87209:13)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitNode (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86560:21)
    at visitEachChildOfExpressionStatement (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:87409:32)
    at visitEachChild (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86810:35)
    at visitor (/home/sampo/git/inferno/node_modules/ts-plugin-inferno/dist/index.js:75:60)
    at visitArrayWorker (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86622:51)
    at visitNodes2 (/home/sampo/git/inferno/node_modules/typescript/lib/typescript.js:86593:21)

webpack hot-reload sometimes crashes to error

<e> [webpack-dev-middleware] HookWebpackError: Cannot read properties of undefined (reading 'kind')
<e>     at makeWebpackError (DIR.TestAutomation\viewer\node_modules\webpack\lib\HookWebpackError.js:48:9)
<e>     at DIR.TestAutomation\viewer\node_modules\webpack\lib\Compilation.js:3068:12
<e>     at eval (eval at create (DIR.TestAutomation\viewer\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:43:1)
<e>     at fn (DIR.TestAutomation\viewer\node_modules\webpack\lib\Compilation.js:481:17)
<e>     at _next0 (eval at create (DIR.TestAutomation\viewer\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:41:1)
<e>     at eval (eval at create (DIR.TestAutomation\viewer\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:54:1)
<e>     at Hook.eval [as callAsync] (eval at create (DIR.TestAutomation\viewer\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
<e>     at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (DIR.TestAutomation\viewer\node_modules\tapable\lib\Hook.js:18:14)
<e>     at fn (DIR.TestAutomation\viewer\node_modules\webpack\lib\Compilation.js:487:45)
<e>     at Hook.eval [as callAsync] (eval at create (DIR.TestAutomation\viewer\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:50:1)
<e> -- inner error --
<e> TypeError: Cannot read properties of undefined (reading 'kind')
<e>     at visitor (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:50:26)
<e>     at getValue (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\utils\getValue.js:9:16)
<e>     at getVNodeProps (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:453:62)
<e>     at createVNode (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:162:26)
<e>     at visitor (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:54:28)
<e>     at getVNodeChildren (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:485:29)
<e>     at createVNode (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:163:35)
<e>     at visitor (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:54:28)
<e>     at getVNodeChildren (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:485:29)
<e>     at createVNode (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:163:35)
<e> caused by plugins in Compilation.hooks.processAssets
<e> TypeError: Cannot read properties of undefined (reading 'kind')
<e>     at visitor (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:50:26)
<e>     at getValue (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\utils\getValue.js:9:16)
<e>     at getVNodeProps (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:453:62)
<e>     at createVNode (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:162:26)
<e>     at visitor (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:54:28)
<e>     at getVNodeChildren (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:485:29)
<e>     at createVNode (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:163:35)
<e>     at visitor (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:54:28)
<e>     at getVNodeChildren (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:485:29)
<e>     at createVNode (DIR.TestAutomation\viewer\node_modules\ts-plugin-inferno\dist\index.js:163:35)

Unexpected token error with TypeScript 4.9.3

When trying to do webpack or rollup build with latest TypeScript v4.9.3, it results in unexpected token errors from tsx-files.

To reproduce try running f.e. the webpack example from examples folder. You will get the following error:

ERROR in ./src/index.tsx 27:16
Module parse failed: Unexpected token (27:16)
File was processed with these loaders:
 * ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.
|     }
|     MyComponent.prototype.render = function () {
>         return (<div>
|         <h1>{"Welcome to Inferno ".concat(version, " TSX ").concat(this.tsxVersion)}</h1>
|         <Incrementer name={"Crazy button"}/>

webpack 5.75.0 compiled with 1 error in 3009 ms

ts-loader claims to be compatible TypeScript: 3.6.3+ so seems like ts-plugin-inferno is the issue here?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.