Skip to content

Commit 4f4a909

Browse files
committed
feat(build): enable named esm module import on node.js >= 14
1 parent a991513 commit 4f4a909

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
"version": "4.0.0-rc.1",
44
"description": "state management for Vue.js",
55
"main": "dist/vuex.cjs.js",
6+
"exports": {
7+
".": {
8+
"require": "./dist/vuex.cjs.js",
9+
"import": "./dist/vuex.mjs"
10+
},
11+
"./": "./"
12+
},
613
"module": "dist/vuex.esm-bundler.js",
714
"browser": "dist/vuex.esm-browser.js",
815
"unpkg": "dist/vuex.global.js",
@@ -20,11 +27,12 @@
2027
"dev": "node examples/server.js",
2128
"build": "node scripts/build.js",
2229
"lint": "eslint src test",
23-
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e",
30+
"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",
2431
"test:unit": "jest --testPathIgnorePatterns test/e2e",
2532
"test:e2e": "start-server-and-test dev http://localhost:8080 'jest --testPathIgnorePatterns test/unit'",
2633
"test:ssr": "cross-env VUE_ENV=server jest --testPathIgnorePatterns test/e2e",
2734
"test:types": "tsc -p types/test",
35+
"test:esm": "node test/esm/esm-test.js",
2836
"coverage": "jest --testPathIgnorePatterns test/e2e --coverage",
2937
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
3038
"release": "node scripts/release.js",

scripts/build.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ const files = [
1414
]
1515

1616
async function run() {
17-
await build()
17+
await Promise.all([build(), copy()])
1818
checkAllSizes()
1919
}
2020

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

25+
async function copy() {
26+
await fs.copy('src/index.mjs', 'dist/vuex.mjs')
27+
}
28+
2529
function checkAllSizes() {
2630
console.log()
2731
files.map((f) => checkSize(f))

src/index.mjs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Vuex from '../dist/vuex.cjs.js'
2+
3+
const {
4+
version,
5+
createStore,
6+
Store,
7+
install,
8+
useStore,
9+
mapState,
10+
mapMutations,
11+
mapGetters,
12+
mapActions,
13+
createNamespacedHelpers,
14+
createLogger
15+
} = Vuex
16+
17+
export {
18+
Vuex as default,
19+
version,
20+
createStore,
21+
Store,
22+
install,
23+
useStore,
24+
mapState,
25+
mapMutations,
26+
mapGetters,
27+
mapActions,
28+
createNamespacedHelpers,
29+
createLogger
30+
}

test/esm/esm-import.mjs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import assert from 'assert'
2+
3+
import { createRequire } from 'module'
4+
5+
import Vuex, {
6+
Store,
7+
install,
8+
version,
9+
mapState,
10+
mapMutations,
11+
mapGetters,
12+
mapActions,
13+
createNamespacedHelpers,
14+
createLogger
15+
} from 'vuex'
16+
17+
const require = createRequire(import.meta.url)
18+
19+
const cjs = require('vuex')
20+
21+
assert.equal(Vuex, cjs)
22+
assert.equal(Store, cjs.Store)
23+
assert.equal(install, cjs.install)
24+
assert.equal(version, cjs.version)
25+
assert.equal(mapState, cjs.mapState)
26+
assert.equal(mapMutations, cjs.mapMutations)
27+
assert.equal(mapGetters, cjs.mapGetters)
28+
assert.equal(mapActions, cjs.mapActions)
29+
assert.equal(createNamespacedHelpers, cjs.createNamespacedHelpers)
30+
assert.equal(createLogger, cjs.createLogger)

test/esm/esm-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// only test esm entry points on Node.14 or higher
2+
const [major] = process.versions.node.split('.')
3+
4+
if (+major >= 14) {
5+
(async function () {
6+
await import('./esm-import.mjs')
7+
})().catch(console.error)
8+
}

0 commit comments

Comments
 (0)