Skip to content

Conversation

@trxcllnt
Copy link

@trxcllnt trxcllnt commented Nov 20, 2017

This PR adds support for using text-encoding-utf-8 with node's new --experimental-modules flag for ESModules support:

  1. rename lib/encoding.cjs.js to lib/encoding.lib.js
  2. copy the src/encoding.js file to lib/encoding.lib.mjs
  3. remove the extension from the "main" entry in package.json, lib/encoding.lib.js -> lib/encoding.lib

Points 2 and 3 are necessary because node will automatically select the "mjs" file when run with --experimental-modules, but only if the "main" export doesn't specify a file extension. If no "mjs" file exists, node imports the CJS form and synthesizes a default export from the module.exports object, leading to the following behavior:

// $ node index.js
// works great
const { TextDecoder } = require('text-encoding-utf-8');
// $ node index.mjs --experimental-modules
// throws "SyntaxError: The requested module does not provide an export named 'TextDecoder'"
import { TextDecoder } from 'text-encoding-utf-8';

For context: we are using this polyfill in the Apache Arrow JS project (and really appreciate your efforts), and are now supporting ESModules in our library. Let me know if there's anything else I can help to get this PR merged and a new version published soon. Thanks!

Copy link
Owner

@arv arv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. LGTM

@arv
Copy link
Owner

arv commented Nov 20, 2017

I'll have to do another push to npm later when I'm in front of my laptop.

@arv arv merged commit 082bf9d into arv:master Nov 20, 2017
@trxcllnt
Copy link
Author

@arv thanks so much! I believe our next release is after thanksgiving, maybe even early next week, so no rush.

@arv
Copy link
Owner

arv commented Nov 20, 2017

I just pushed to npm (1.0.2).

wesm pushed a commit to apache/arrow that referenced this pull request Nov 21, 2017
…les support

Updates the `text-encoding-utf-8` dependency to version 1.0.2, [which now supports](arv/text-encoding-utf-8#2) ESModules in node >= v8.6.0 via the `--experimental-modules` flag.

We currently support ESModules in node in the main `apache-arrow` package, but need our dependencies to also expose ESModule forms as well. I have also issued a [PR to flatbuffers](google/flatbuffers#4504) to add ESModules support, and are using a temporary [fork of flatbuffers](https://github.com/trxcllnt/flatbuffers-esm) in my github until that PR is merged.

This PR enables the following workflow:

```js
// file - index.mjs
// run via `node --experimental-modules index.mjs`
import util from 'util';
import * as fs from 'fs';
import { Table } from 'apache-arrow';
(async () => {
  const buffer = await util.promisify(fs.readFile)('simple.arrow');
  console.log(Table.from([buffer]).toString());
/*
 foo,  bar,  baz
   1,    1,   aa
null, null, null
   3, null, null
   4,    4,  bbb
   5,    5, cccc
*/
})();
```

Author: Paul Taylor <[email protected]>

Closes #1338 from trxcllnt/update-text-encoding-utf8 and squashes the following commits:

fbecde5 [Paul Taylor] synthesize an mjs file for tslib on postinstall
4b050c9 [Paul Taylor] update text-encoding-utf-8 dependency with node ESModules support
kou pushed a commit to apache/arrow-js that referenced this pull request May 14, 2025
…les support

Updates the `text-encoding-utf-8` dependency to version 1.0.2, [which now supports](arv/text-encoding-utf-8#2) ESModules in node >= v8.6.0 via the `--experimental-modules` flag.

We currently support ESModules in node in the main `apache-arrow` package, but need our dependencies to also expose ESModule forms as well. I have also issued a [PR to flatbuffers](google/flatbuffers#4504) to add ESModules support, and are using a temporary [fork of flatbuffers](https://github.com/trxcllnt/flatbuffers-esm) in my github until that PR is merged.

This PR enables the following workflow:

```js
// file - index.mjs
// run via `node --experimental-modules index.mjs`
import util from 'util';
import * as fs from 'fs';
import { Table } from 'apache-arrow';
(async () => {
  const buffer = await util.promisify(fs.readFile)('simple.arrow');
  console.log(Table.from([buffer]).toString());
/*
 foo,  bar,  baz
   1,    1,   aa
null, null, null
   3, null, null
   4,    4,  bbb
   5,    5, cccc
*/
})();
```

Author: Paul Taylor <[email protected]>

Closes #1338 from trxcllnt/update-text-encoding-utf8 and squashes the following commits:

fbecde59 [Paul Taylor] synthesize an mjs file for tslib on postinstall
4b050c95 [Paul Taylor] update text-encoding-utf-8 dependency with node ESModules support
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants