Skip to content

Commit

Permalink
feat(build): enable named esm module import on node.js >= 14
Browse files Browse the repository at this point in the history
  • Loading branch information
kiaking committed Nov 22, 2020
1 parent a991513 commit 4f4a909
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"version": "4.0.0-rc.1",
"description": "state management for Vue.js",
"main": "dist/vuex.cjs.js",
"exports": {
".": {
"require": "./dist/vuex.cjs.js",
"import": "./dist/vuex.mjs"
},
"./": "./"
},
"module": "dist/vuex.esm-bundler.js",
"browser": "dist/vuex.esm-browser.js",
"unpkg": "dist/vuex.global.js",
Expand All @@ -20,11 +27,12 @@
"dev": "node examples/server.js",
"build": "node scripts/build.js",
"lint": "eslint src test",
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e",
"test": "npm run lint && npm run build && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e && npm run test:esm",
"test:unit": "jest --testPathIgnorePatterns test/e2e",
"test:e2e": "start-server-and-test dev http://localhost:8080 'jest --testPathIgnorePatterns test/unit'",
"test:ssr": "cross-env VUE_ENV=server jest --testPathIgnorePatterns test/e2e",
"test:types": "tsc -p types/test",
"test:esm": "node test/esm/esm-test.js",
"coverage": "jest --testPathIgnorePatterns test/e2e --coverage",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js",
Expand Down
6 changes: 5 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ const files = [
]

async function run() {
await build()
await Promise.all([build(), copy()])
checkAllSizes()
}

async function build() {
await execa('rollup', ['-c', 'rollup.config.js'], { stdio: 'inherit' })
}

async function copy() {
await fs.copy('src/index.mjs', 'dist/vuex.mjs')
}

function checkAllSizes() {
console.log()
files.map((f) => checkSize(f))
Expand Down
30 changes: 30 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Vuex from '../dist/vuex.cjs.js'

const {
version,
createStore,
Store,
install,
useStore,
mapState,
mapMutations,
mapGetters,
mapActions,
createNamespacedHelpers,
createLogger
} = Vuex

export {
Vuex as default,
version,
createStore,
Store,
install,
useStore,
mapState,
mapMutations,
mapGetters,
mapActions,
createNamespacedHelpers,
createLogger
}
30 changes: 30 additions & 0 deletions test/esm/esm-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'assert'

import { createRequire } from 'module'

import Vuex, {
Store,
install,
version,
mapState,
mapMutations,
mapGetters,
mapActions,
createNamespacedHelpers,
createLogger
} from 'vuex'

const require = createRequire(import.meta.url)

const cjs = require('vuex')

assert.equal(Vuex, cjs)
assert.equal(Store, cjs.Store)
assert.equal(install, cjs.install)
assert.equal(version, cjs.version)
assert.equal(mapState, cjs.mapState)
assert.equal(mapMutations, cjs.mapMutations)
assert.equal(mapGetters, cjs.mapGetters)
assert.equal(mapActions, cjs.mapActions)
assert.equal(createNamespacedHelpers, cjs.createNamespacedHelpers)
assert.equal(createLogger, cjs.createLogger)
8 changes: 8 additions & 0 deletions test/esm/esm-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// only test esm entry points on Node.14 or higher
const [major] = process.versions.node.split('.')

if (+major >= 14) {
(async function () {
await import('./esm-import.mjs')
})().catch(console.error)
}

0 comments on commit 4f4a909

Please sign in to comment.