Giter Club home page Giter Club logo

esbuild-plugin-tsc's Introduction

esbuild-plugin-tsc

An esbuild plugin which uses tsc to compile typescript files.

Why?

Esbuild natively supports typescript files - so if you are not using exotic typescript features this plugin is not meant for you!

I wanted to use typescripts emitDecoratorMetadata feature, which is not supported by esbuild. If you are only using typescript decorators in some files, this plugin allows you to convert only the files containing decorators and lets esbuild handle all other files. If for some reason you want to use typescripts compiler for all files you can simple set the force option.

Basic Usage

  1. Install this plugin in your project:

    npm install --save-dev esbuild-plugin-tsc typescript
  2. Add this plugin to your esbuild build script:

    Javascript:

    +const esbuildPluginTsc = require('esbuild-plugin-tsc');
     ...
     esbuild.build({
       ...
       plugins: [
    +    esbuildPluginTsc(),
       ],
     })

    Typescript:

    +import esbuildPluginTsc from 'esbuild-plugin-tsc';
     ...
     esbuild.build({
       ...
       plugins: [
    +    esbuildPluginTsc(),
       ],
     })

Config

esbuildPluginTsc({
    // If empty, uses tsconfig.json
    tsconfigPath?: string,
    // If true, force compilation with tsc
    force?: boolean,
    // If true, enables tsx file support
    tsx?: boolean
})

esbuild-plugin-tsc's People

Contributors

arsenyyankovsky avatar brian-mcbride avatar bryaneaton13 avatar dependabot[bot] avatar kennethwkz avatar seokju-na avatar takker99 avatar tgandrews avatar thomaschaaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

esbuild-plugin-tsc's Issues

"Reflect.hasOwnMetadata" is not a function

Hello,

Thx for your work, it's a step further to enable typescript decorator in esbuild ;)

I still got the error Reflect.hasOwnMetadata is not a function. I added your plugin in my configuration for Esbuild (I am using Vite JS for bundling), so here is my vite.config.ts file.

import { defineConfig } from 'vite';
import reactRefresh from '@vitejs/plugin-react-refresh';
import NodeModulePolyfill from '@esbuild-plugins/node-modules-polyfill';
import NodeGlobalsPolyfill from '@esbuild-plugins/node-globals-polyfill';
import EsbuildTypescript from 'esbuild-plugin-tsc';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [reactRefresh()],
  define: {
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  },
  optimizeDeps: {
    esbuildOptions: {
      plugins: [
        EsbuildTypescript(),
        NodeModulePolyfill(),
        NodeGlobalsPolyfill(),
      ],
    },
  },
});

And here is my tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": false,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "include": ["./src"]
}

Do you have any idea why i still got an error ? I thought that your plugin would fix the problem of Typescript decorator.

Anyway, thanks again for your work ;)

Best regards,

Don't work with nuxt 3

If I pot that to nuxt config I see nothing

vite: {
  optimizeDeps: {
      esbuildOptions: {
        plugins: [
          esbuildPluginTsc(),
        ],
      },
   },
}

Don't know where to add it.

Using this plugin with SST and AWS CDK

I came across this plugin when I was trying to get TypeGraphQL to work with SST which under the hood uses AWS CDK which internally uses esbuild.

I was wondering if it's possible you were trying to accomplish the same thing and found a way to get emitDecoratorMetadata working in an SST app. My project currently uses a combination of the serverless framework and serverless-bundle to build my code but would like to use SST instead.

I thought it was worth a shot to check to see if you are in fact trying to do the same, if not, don't mind me!

Does it work with function metadata?

Hi :),

I am using this plugin with serverless-esbuild to add support for decorator metadata, but I am hitting a block road and I am unsure if this is related to this or something else.

I am using it with MikroORM, which accepts properties that are functions:

@ManyToOne({entity: () => RelatedEntity, mappedBy: 'foo'})

MikroORM also supports setting string properties, which works:

@ManyToOne({entity: 'RelatedEntity', mappedBy: 'foo'})

But I would prefer using the function one, as it helps TypeScript to figure out the entity properties.

When ESBuild attempts to read the data from a function attribute, it raises errors like this:

โœ– Cannot read properties of undefined (reading 'name')

I wonder if this is a limitation of this plugin that only support scalar values, or if this is completely unrelated :).

Thanks!

Not working in Serverless CI/CD pipeline when using typescript 5.2.2

When using typescript version 5.2.2 in combination with the esbuild-plugin-tsc my ci/cd deployment fails at deployment step. When going down to version 5.0.4 pipeline deploys successfull.
Unfortunately the serverless-framework logs in their ci/cd does not give any hint about what is causing it

Enable bundle broke the entities

Getting two different errors so far I know when enabling "bundle: true" , and without that would be fine

Class constructor BaseEntity cannot be invoked without 'new'",
Entity metadata for BaseEntity#Metric was not found. Check if you specified a correct entity object and if it's connected in the connection options.",

    "options": {
        "platform": "node",
        "outputPath": "dist/apps/api",
        "format": ["cjs"],
        "bundle": true,
        "sourcemap": true,
        "main": "apps/api/src/main.ts",
        "tsConfig": "apps/api/tsconfig.app.json",
        "esbuildConfig": "apps/api/esbuild.config.js",
        "assets": ["apps/api/src/assets", "apps/api/src/views/**"]
      },

Wrong tsconfig passed to `typescript.transpileModule`

source: https://github.com/thomaschaaf/esbuild-plugin-tsc/blob/main/src/index.js#L54

In typescript compiler api d.ts, the second param of transpileModule is:

interface TranspileOptions {
        compilerOptions?: CompilerOptions;
        fileName?: string;
        reportDiagnostics?: boolean;
        moduleName?: string;
        renamedDependencies?: MapLike<string>;
        transformers?: CustomTransformers;
}

So it should not directly pass the parsedTsConfig to it or it will use default empty config for transpiling. The correct way is:

const program = typescript.transpileModule(ts, { compilerOptions: parsedTsConfig.options });

Should "force: true" apply to all files including those without decorators?

Thanks for writing this plugin - it's ideal for the use case I'm after.

I was checking the implementation of force: true and noticed on line 50 of the current release it will return if a file doesn't contain any decorators. My initial understanding of force: true is that it would run all files through tsc regardless of whether they have decorators. Is that the intention?

I'm happy to raise a PR if so although I know it's a quick change to fix this. But I wanted to check first whether I understood the goal behind the force option.

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.