8000 feat(build): enable named esm module import on node.js >= 14 (#1872) · vuejs/vuex@acddab2 · GitHub
[go: up one dir, main page]

Skip to content

Commit acddab2

Browse files
feat(build): enable named esm module import on node.js >= 14 (#1872)
Co-authored-by: Kia King Ishii <kia.king.08@gmail.com>
1 parent 658359f commit acddab2

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
"version": "3.5.1",
44
"description": "state management for Vue.js",
55
"main": "dist/vuex.common.js",
6+
"exports": {
7+
".": {
8+
"require": "./dist/vuex.common.js",
9+
"import": "./dist/vuex.mjs"
10+
},
11+
"./": "./"
12+
},
613
"module": "dist/vuex.esm.js",
714
"unpkg": "dist/vuex.js",
815
"jsdelivr": "dist/vuex.js",
@@ -21,11 +28,12 @@
2128
"build:main": "node scripts/build-main.js",
2229
"build:logger": "node scripts/build-logger.js",
2330
"lint": "eslint src test",
24-
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e",
31+
"test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e && npm run test:esm",
2532
"test:unit": "jest --testPathIgnorePatterns test/e2e",
2633
"test:e2e": "start-server-and-test dev http://localhost:8080 'jest --testPathIgnorePatterns test/unit'",
2734
"test:ssr": "cross-env VUE_ENV=server jest --testPathIgnorePatterns test/e2e",
2835
"test:types": "tsc -p types/test",
36+
"test:esm": "node test/esm/esm-test.js",
2937
"coverage": "jest --testPathIgnorePatterns test/e2e --coverage",
3038
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
3139
"release": "node scripts/release.js",

scripts/build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ const { gzipSync } = require('zlib')
55
const { compress } = require('brotli')
66

77
async function run(config, files) {
8-
await build(config)
8+
await Promise.all([build(config), copy()])
99
checkAllSizes(files)
1010
}
1111

1212
async function build(config) {
1313
await execa('rollup', ['-c', config], { stdio: 'inherit' })
1414
}
1515

16+
async function copy() {
17+
await fs.copy('src/index.mjs', 'dist/vuex.mjs')
18+
}
19+
1620
function checkAllSizes(files) {
1721
console.log()
1822
files.map((f) => checkSize(f))

src/index.mjs

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

test/esm/esm-import.mjs

Lines changed: 30 additions & 0 deletions
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 7A50 (createLogger, cjs.createLogger)

test/esm/esm-test.js

Lines changed: 8 additions & 0 deletions
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)
0