Skip to content

Refactor: convert to ESM module (.js files) #19719

Closed

Description

  • var mod = require('mod'); =>

    • if mod is callable then import mod from 'mod'
    • else mod is not callable the import { a, b } from "mod" and replace mod.a and mod.b
    • namespace import should ever be used?
  • var mod= require('mod').a; => import { a } from "mod";

  • const { a } = require('mod'); => import { a } from "mod";

  • if (__DEV__) { var warning = require('warning'); }

    • disallowed?
    • have two actions? one sync and one async?
  • function f() { var warning = require('warning'); } => async function f() { var warning = await import('warning'); }

    • maybe do nothing here as well
  • module.exports = EventPlugin; => export default EventPlugin;

  • fix imports in other files
  • should we also fix require("mod") to require("mod").default?
  • module.exports.blah = 0 => export const blah = 0

    • should we support this too
      var myModule = module.exports = exports = {};
      myModule.a = 0;
      
    • this is another common pattern in CJS modules that we should consider supporting
    • we already have handling for this in the binder in .js files
  • module.exports = require('mod'); => export * from 'mod';

  • module.exports = {foo: function () { } ... } => export function foo() {} ?

    • is this even common enough to support?

Activity

  1. aluanhaddad commented on Nov 7, 2017

    @aluanhaddad
    Contributor

    Of the last two, perhaps the first should be

    module.exports = require('mod');
        // => 
           export * from 'mod';
           export {default} from 'mod';

    since the export * should exclude default.

    And perhaps the second should be

    module.exports = {foo: function () { } ... };
        // =>
            export default {foo: function () {}};

    as, if the module is consumed natively as an ESM by a NodeJS ESM, the named export foo would not be available at all otherwise.

  2. aluanhaddad commented on Nov 10, 2017

    @aluanhaddad
    Contributor

    I don't believe that's always the case. It doesn't appear to be true for Babel's
    CommonJS output and TypeScript's System.register transform also behaved differently the last time I checked.

  3. aluanhaddad commented on Nov 10, 2017

    @aluanhaddad
    Contributor

    Can you clarify what you mean by the second comment? If I write export function foo() {} I can access it from a .mjs file just fine with import { foo } from "./a";.

    Yes, I did not mean to imply otherwise. All exports (except the default export if present) are reexported by export * from "m";

    Here is the specification text that describes the default exclusion.

    https://tc39.github.io/ecma262/#sec-getexportednames

    See section 7.c.i

  4. rbuckton commented on Nov 10, 2017

    @rbuckton
    Contributor

    Aluan Haddad (@aluanhaddad) is correct, default is excluded from re-exports using export * from ... syntax.

  5. aluanhaddad commented on Nov 10, 2017

    @aluanhaddad
    Contributor

    Andy (Andrewkraft) (@Andy-MS)

    By second I meant the one starting with And perhaps the second should be.

    Ah, sorry, my brain kind of went to sleep for a second 😪.

    I meant that since Node.js is not yet supporting named exports, unless something has changed, and since this concerns a refactoring transformation that might be output using --module es2015, it might make sense for the refactoring to place properties on the default export instead of creating named exports.
    I'm not sure about this since it depends on how the imports are transpiled if at all.

    module.exports = {foo: function () { } ... };
        // =>
            export default {foo: function () {}};
  6. ghost closed this as completedin #19916on Jan 9, 2018
  7. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: LS: Refactoringse.g. extract to constant or function, rename symbolFixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions