dnt icon indicating copy to clipboard operation
dnt copied to clipboard

TextEncoder/TextDecoder not yet available

Open brickpop opened this issue 3 years ago • 5 comments

First of all, thank you for the great work behind DNT and Deno itself!

TextDecoder and TextEncoder cannot be used yet to compile to NPM modules. I know it is expected, just adding for reference.

Source:

const hexDecode = (s: string) => new TextEncoder().encode(s);

console.log(hexDecode("1234"));

Build output:

[dnt] Building project...
[dnt] Type checking...
src/mod.ts:9:38 - error TS2304: Cannot find name 'TextEncoder'.

9 const hexDecode = (s: string) => new TextEncoder().encode(s);
                                       ~~~~~~~~~~~

error: Uncaught (in promise) Error: Had 1 diagnostics.
        throw new Error(`Had ${diagnostics.length} diagnostics.`);
              ^
    at build (https://deno.land/x/[email protected]/mod.ts:299:15)

Thank you!

brickpop avatar Jun 28 '22 20:06 brickpop

Is this fixed if you add a @types/node dev dependency?

await build({
  // etc...
  package: {
    // etc...
    devDependencies: {
      "@types/node": "^16.11.37"
    }
  }
});

Otherwise, if you're targetting the browser, then enable DOM types: https://github.com/denoland/dnt#dom-types

dsherret avatar Jul 04 '22 17:07 dsherret

The devDependency is there indeed:

image


And npm install is being run:

image

brickpop avatar Jul 04 '22 23:07 brickpop

So the main issue here is that @types/node doesn't have TextEncoder or TextDecoder as a global (I did not know this until just now). I looked into fixing this in DefinitelyTyped, but I'm not sure how the typings would look like without conflicting with existing lib.dom.ts globals (if present). There's some discussion here on fixing this: https://github.com/microsoft/TypeScript/issues/31535

Browser Workaround

Enable DOM types, which you should be doing anyway if you're targeting the browser: https://github.com/denoland/dnt#dom-types

Node Workaround

If you're targeting for Node, add a custom shim that shims TextEncoder or TextDecoder to use util:

await build({
  // etc...
  shims: {
    custom: [{
      globalNames: ["TextEncoder", "TextDecoder"],
      module: "util",
    }]
  },
});

I added a test for this here: https://github.com/denoland/dnt/pull/192

dsherret avatar Jul 06 '22 21:07 dsherret

Thank you @dsherret, the change made the build pass! (yet a different issue popped up)

Should the readme be updated or some warning be displayed as a workaround?

brickpop avatar Jul 08 '22 16:07 brickpop

If anyone needs an "isomorphic" TextEncoder/TextDecoder shim that works in both Node and the browser (where it will just use the built-in version):

await build({
  // etc...
  shims: {
    custom: [{
      package: {
        name: "textencoder-ponyfill",
          version: "1.0.2",
        },
        globalNames: ["TextEncoder", "TextDecoder"],
      },
    ]
  },
});

sgwilym avatar Sep 07 '22 15:09 sgwilym