Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: native Node.js ES Modules (wrapper approach) #423

Merged
merged 1 commit into from
Apr 29, 2020
Merged

Commits on Apr 29, 2020

  1. feat: native Node.js ES Modules (wrapper approach)

    BREAKING CHANGE: Native ES Modules is still an experimental API in
    Node.js 14.0.0 and has so far not officially been supported by the
    `uuid` module.
    
    Since Node.js allows importing CommonJS modules it was possible to
    import the `uuid` module like this:
    
    ```js
    import uuid from 'uuid';
    console.log(uuid.v4()); // -> 'cd6c3b08-0adc-4f4b-a6ef-36087a1c9869'
    ```
    
    This will no longer work with proper ES Module exports in place. You
    can now import the `uuid` library as described in the documentation:
    
    ```js
    import { v4 as uuidv4 } from 'uuid';
    uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
    ```
    
    or
    
    ```js
    import * as uuid from 'uuid';
    console.log(uuid.v4()); // -> 'cd6c3b08-0adc-4f4b-a6ef-36087a1c9869'
    ```
    
    Enabling native ES Modules for Node.js requires some special care for
    the v1 algorithm which needs internal state. This makes this library
    susceptible to the dual package hazard described in
    https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_dual_commonjs_es_module_packages
    
    While the "isolated state" solution seems to make more sense it causes
    trouble with rollup which supports CommonJS files only with an
    additional plugin, see rollup/rollup#3514.
    
    It is worth noting that webpack could deal with the "isolated state"
    solution since webpack supports CommonJS sources out of the box without
    further plugins and also doesn't get confused by `.cjs` file extensions
    that would have to be used in the state isolation approach for
    compatibility with Node.js.
    
    The wrapper approach should however work fine. Here's what code will be
    used in each case:
    
    1. Node.js `require('uuid')`
      -> dist/index.js (CommonJS) -> dist/v1.js (CommonJS)
    2. Node.js `import { v1 as uuidv1 } from 'uuid'`
      -> wrapper.mjs (ESM) -> dist/v1.js (CommonJS)
    3. rollup/webpack (targeting Node.js environments)
      -> dist/esm-node/index.js (ESM) -> dist/esm-node/v1.js (ESM)
    4. rollup/webpack (targeting Browser environments)
      -> dist/esm-browser/index.js (ESM) -> dist/esm-browser/v1.js (ESM)
    ctavan committed Apr 29, 2020
    Configuration menu
    Copy the full SHA
    5c133ff View commit details
    Browse the repository at this point in the history