Giter Site home page Giter Site logo

vite-plugin-using's Introduction

A quick and easy way to start using USING today!

Note: This project is deprecated! The behavior is easily accessible inside of VITE using `esbuild.target: 'es2022' which will be a lot better than this dirty package anyway.

99% test coverage

This plugin allows using the upcoming JS keyword using in your code today! It transpiles using and await using to try/finally blocks to get the same effect more easily!

Installation

pnpm add vite-plugin-using

Add to your vite.config.ts

import ViteUsing from 'vite-plugin-using';

export default defineConfig({
  plugins: [ViteUsing()],
});

You will need to polyfill the new @@dispose and @@asyncDispose symbols as such somewhere in your runtime:

Symbol.dispose ??= Symbol('@@dispose');
Symbol.asyncDispose ??= Symbol('@@asyncDispose');

Usage

Just start using!!

class disposable {
  constructor() {}
  [Symbol.dispose]() {
    console.log('disposing')
  }
  [Symbol.asyncDispose]() {
    console.log('async disposing')
  }
}

{
  using obj = new disposable();
  console.log('made one obj');
  await using obj2 = new disposable();
  console.log('made two obj');
  console.log(obj, obj2)
}

Dang That's Cool!

vite-plugin-using's People

Contributors

bytenik avatar ekwoka avatar mdreal32 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

mdreal32 bytenik

vite-plugin-using's Issues

Line breaks seem to cause this plugin to break

The following code works using this plugin:

const comparator = (x: Point) => x.x - point[timestampColumnName];
using searchRes = binarySearch(this.highchartSeries!.points, comparator);

The following code does not work:

using searchRes = binarySearch(
    this.highchartSeries!.points,
    (x: Point) => x.x - point[timestampColumnName],
);

It generates the following error:

11:10:36 PM [vite] Internal server error: 
  × Expression expected
     ╭─[file.ts:323:1]
 323 │                         
 324 │       const searchRes = binarySearch(
 325 │                             
 326 │       try {
     ·       ───
 327 │         this.highchartSeries!.points,
 328 │                             (x: Point) => x.x - point[timestampColumnName],
 329 │                         );
     ╰────


Caused by:
    Syntax Error
  Plugin: vite:react-swc
  File: file.ts:323:1
  

[TypeScript] Found 0 errors. Watching for file changes.

From the error, it seems like the transpiled try block is starting on the wrong line.

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.