Skip to content

Add support for explicit resource management syntax #10197

@mark-monteiro

Description

@mark-monteiro

🙋 feature request

I would like to use explicit resource management as added in Typescript 5.2: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management

Example:

// The `using` keyword here is what causes an error during compilation
using file = new TempFile(".some_temp_file");

class TempFile implements Disposable {
    #path: string;
    #handle: number;
    constructor(path: string) {
        this.#path = path;
        this.#handle = fs.openSync(path, "w+");
    }
    // other methods
    [Symbol.dispose]() {
        // Close the file and delete it.
        fs.closeSync(this.#handle);
        fs.unlinkSync(this.#path);
    }
}

🤔 Expected Behavior

Code using explicit resource management should compile without errors using `parcel build'

😯 Current Behavior

The following error is thrown:

> parcel build
Building...
Bundling...
Packaging & Optimizing...
🚨 Build failed.
@parcel/optimizer-swc: Using declaration is not enabled. Set 
jsc.parser.explicitResourceManagement to true
  /builds/senti-solutions/senti-internal-tools/src/lib/gas-api.ts:76:14
    75 |   // NOTE: Use a unique channel name based on the request ID so that we
  > 76 |   await using eventStream = new SupabaseRealtimePostgresEventStream<
  >    |              ^
    77 |     REALTIME_POSTGRES_CHANGES_LISTEN_EVENT.UPDATE,

💁 Possible Solution

An easy fix would be to add support for an .swcrc config file so we can manually set jsc.parser.explicitResourceManagement to true.

🔦 Context

Being able to use explicit resource management greatly simplifies code that needs cleanup logic.

Before:

export function doSomeWork() {
    const file = new TempFile(".some_temp_file");
    try {
        // ...
    }
    finally {
        file[Symbol.dispose]();
    }
}

After:

export function doSomeWork() {
    using file = new TempFile(".some_temp_file");
    // ...
}

Workaround

I currently am using the following patch in the @parcel/optimizer-swc package with patch-package to add support for configuring SWC with an .swcrc file:

diff --git a/node_modules/@parcel/optimizer-swc/lib/SwcOptimizer.js b/node_modules/@parcel/optimizer-swc/lib/SwcOptimizer.js
index dc77ee4..a314309 100644
--- a/node_modules/@parcel/optimizer-swc/lib/SwcOptimizer.js
+++ b/node_modules/@parcel/optimizer-swc/lib/SwcOptimizer.js
@@ -95,8 +95,9 @@ var _default = exports.default = new (_plugin().Optimizer)({
         isModule: bundle.env.outputFormat === 'esmodule',
         minify: true,
         sourceMaps: !!bundle.env.sourceMap,
-        configFile: false,
-        swcrc: false
+         configFile: options.projectRoot + '/.swcrc'
       });
     } catch (err) {
       // SWC doesn't give us nice error objects, so we need to parse the message.

which then allows me to use the following .swcrc file in order to make the compilation work

{
  "$schema": "https://swc.rs/schema.json",
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "explicitResourceManagement": true
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions