[Pkg-javascript-devel] Update package with new type & api

Yadd yadd at debian.org
Tue Mar 29 14:54:30 BST 2022


Hi all,

some packages, especially those from Sindre Sorhus, changed both:
  * language from commonjs to ECMA only ("type": "module")
  * API:
      const foo = require("foo"); // gives also foo.bar
    becomes:
      import { foo, fooBar } from "foo";

I build new node-find-up, node-chalk (exp) and node-globy (exp) 
following this:
  * build commonjs file using rollup
  * add an index.cjs file which keeps old and new API
  * modify package.json to replace "type=module" by an extended "exports"

Since new module is "type=module" then we are sure that no reverse 
dependency will call it using "require" but wanted new behavior. This 
permits to have a transition: node-find-up for example seems usable for 
any reverse dependency. Only package which are built from ECMA but 
wanted old version could have their build broken.

Globby example:

   const { globby, globbySync, globbyStream, generateGlobTasks,
           generateGlobTasksSync, ignoreFiles, isDynamicPattern,
           isGitIgnored, isGitIgnoredSync } = require("./globby.cjs");

   const res = (...args) => {
     return globby(...args);
   }

   // old API
   res.sync = globbySync;
   res.stream = globbyStream;
   res.hasMagic = isDynamicPattern;
   res.gitignore = isGitIgnored;
   res.gitignore.sync = isGitIgnoredSync;

   // identical name
   res.generateGlobTasks = generateGlobTasks;

   // This is for package which sources are ECMA and build files are
   // Commonjs: we have to replace `import globby from "globby"` by
   //           import { globby } from "globby"
   res.globby = globby;

   // new API
   res.globbySync = globbySync;
   res.globbyStream = globbyStream;
   res.generateGlobTasksSync = generateGlobTasksSync;
   res.isDynamicPattern = isDynamicPattern;
   res.ignoreFiles = ignoreFiles;
   res.isGitIgnored = isGitIgnored;

   module.exports = res;



More information about the Pkg-javascript-devel mailing list