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

build: use cjs/mjs extensions for cjs/esm builds #1157

Merged
merged 6 commits into from May 15, 2022

Conversation

danielroe
Copy link
Member

@danielroe danielroe commented Oct 16, 2021

Summary

This PR proposes using .cjs and .mjs instead of the current .js extensions. That will allow usage of this library in a native Node module context.

old new
vue-router.cjs.js vue-router.cjs
vue-router.cjs.prod.js vue-router.prod.cjs
vue-router.esm-bundler.js vue-router.mjs

Notes

  • As is, this is a breaking change as it changes the packaging expectations. I've added stubs that re-export these .mjs/.cjs files for backwards compatibility.
  • Because this package imports vue, vue will also need to provide appropriately named native Node module filenames. (This should not negatively affect existing users who will not be using this module natively anyway.)

Background issue

At the moment, importing vue-router within an ESM context produces the following error:

(node:73025) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
Server Side Rendering Error: /project/node_modules/vue-router/dist/vue-router.esm-bundler.js:6
import { getCurrentInstance, inject, onUnmounted, onDeactivated, onActivated, computed, unref, watchEffect, defineComponent, reactive, h, provide, ref, watch, shallowRef, nextTick } from 'vue';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:199:29)
    at ModuleJob.run (internal/modules/esm/module_job.js:169:25)
    at async Loader.import (internal/modules/esm/loader.js:177:24)
    at async __instantiateModule__ (file:///project/playground/.nuxt/dist/server/server.mjs:1844:3)

@danielroe danielroe changed the title build: use cjs/mjs extensions for cjs/esm builds build!: use cjs/mjs extensions for cjs/esm builds Oct 16, 2021
@danielroe danielroe changed the title build!: use cjs/mjs extensions for cjs/esm builds build: use cjs/mjs extensions for cjs/esm builds Oct 16, 2021
@posva
Copy link
Member

posva commented Oct 17, 2021

Thanks a lot for this!

I think we should align with Vue. Regarding the changes, it's IMO okay to release this in 4.1.0 with information about the changes necessary for the user.

@posva
Copy link
Member

posva commented Oct 20, 2021

Published and testable under the egde tag, vue-router@edge

@danielroe
Copy link
Member Author

A repo with vite & vue cli examples using the published version: https://github.com/danielroe/vue-esm-test and https://github.com/danielroe/vue-esm-test/tree/vue-cli.

@posva posva added this to To Do in v4.1.0 Nov 5, 2021
@posva posva changed the base branch from main to v4.1 April 20, 2022 13:24
src/globalExtensions.ts Outdated Show resolved Hide resolved
@posva posva merged commit 79c6de2 into vuejs:v4.1 May 15, 2022
4 checks passed
@posva posva moved this from To Do to Done in v4.1.0 May 15, 2022
posva added a commit that referenced this pull request May 16, 2022
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
posva added a commit that referenced this pull request May 16, 2022
posva added a commit that referenced this pull request May 16, 2022
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
posva added a commit that referenced this pull request May 31, 2022
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
posva added a commit that referenced this pull request Jun 10, 2022
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
@posva posva mentioned this pull request Jun 17, 2022
posva added a commit that referenced this pull request Jun 29, 2022
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
posva added a commit that referenced this pull request Jun 30, 2022
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants