question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upgrade: v7 convert to ESM lets Disscuse the API

See original GitHub issue

I have forked this repo long time ago i would love to merge back as i think there are good improvements but i found out that we need maybe to incremental adopt changes as this gets used so often.

First things first

at present we got a export of the async del version under the name del and then we assign to that the sync version this would result into really bad ESM code i would vote for renaming the named Exports in this CJS version as also in the new ESM version to deprecate the old import and require behaviors and get better static analyzeable code.

del() gets delAsync() and del.sync gets delSync() while we add a deprecated default import so that the patterns that are in the section below still work while we deprecate them.

when we transpil that code to cjs with the following rollup config we can create a del.cjs inside the node_modules folder that has the old api from v6.0.0 the only thing changes is that you require(‘del/del.cjs’)

this pattern is the best for the future i guess i am from the NodeJS Package Maintainance Group and i was working on a Gold Standard for Packaging since then.

People will generate CJS files when they need them.

{
  input: ['del.js'],
  external: () => true, //
  output: [
    {
      preferConst: true,
      format: 'es',
      entryFileNames: `[name].cjs`,
      dir: `./node_modules/del`,
      interop: 'esModule',
      exports: 'named',
    }
  ]
}

i have the following readme snippet prepared

Deprecated Usage and How to Transpile to CJS

This usage mode resulted out of the fact that we wanted to support the old CJS patterns from v6.0.0

When you transpile this Module With Rollup to CJS

const del = require('./del.cjs'); // deprecated version but will work
// Replaced by Async/Await DynamicImport, DynamicImport as Promise use
const { delAsync, delSync } = require('./del.cjs')
import del, { sync } from 'del';
del(patterns, delOptions); // is now delAsync { delAsync } from 'del'; delAsync();
del.sync(patterns, delOptions); // is now delSync import { delSync } from 'del'; delSync();

Short Following up

i want to do the deprecation set already in this CJS version so that we can more smoth transition to esm

so long story short can we add module.exports.delAsync and exports.delSync as also translate everything away from module.exports to directly exports also drop exports default aka module.exports and communicate to do

const { delSync, delAsync} = require('del');

if yes i could create some pull requests for example i got this fully typed with automatic typegenration working as js with JSDOC Annotations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sindresorhuscommented, Jul 7, 2022

Does that mean you’d like to convert this library to TypeScript and compile both CommonJS and ESM?

No. The TS mention is about TS not being able to handle ESM until TS 4.7.

1reaction
sindresorhuscommented, Mar 22, 2022

I plan to move directly to ESM without any CommonJS support. I’m just waiting for TypeScript 4.7 (which will have full ESM support), so I don’t have to deal with too many TS support issues.

The API will be two named exports: delete and deleteSync.

It’s probably easier if I do the migration myself as there’s a lot of details to get right.

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upgrade your Node app: From CommonJS to ES Modules
cjs-to-es6 is a CLI that converts JavaScript files from CommonJS to ES6 Modules. It's unmaintained, but still the best tool I could find....
Read more >
The ESM move · Discussion #15 · sindresorhus/meta - GitHub
If a module only exports JSON files, using Node APIs to reexport them opens a whole new can of worms which include the...
Read more >
How to upgrade D7 variables to D8's state system - Drupal
Within your conversion issue work, convert one variable at a time. Determine the variable name to convert. Grep the entire Drupal code base...
Read more >
Getting Started with (and Surviving) Node.js ESM
If we want to upgrade, we can only get our import() asynchronously, and in this case means we'd have to change our API...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Core modules provide named exports of their public API. ... Live binding updates or new exports added to module.exports are not detected for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found