Skip to content

Commit b9676ca

Browse files
danielroeposva
andcommitted
build: use cjs/mjs extensions for cjs/esm builds (#1157)
Co-authored-by: Eduardo San Martin Morote <posva13@gmail.com>
1 parent d609e18 commit b9676ca

File tree

6 files changed

+552
-426
lines changed

6 files changed

+552
-426
lines changed

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!--email_off-->
88

9-
[Unpkg.com](https://unpkg.com) provides npm-based CDN links. The above link will always point to the latest release on npm. You can also use a specific version/tag via URLs like `https://unpkg.com/vue-router@4.0.5/dist/vue-router.global.js`.
9+
[Unpkg.com](https://unpkg.com) provides npm-based CDN links. The above link will always point to the latest release on npm. You can also use a specific version/tag via URLs like `https://unpkg.com/vue-router@4.0.15/dist/vue-router.global.js`.
1010

1111
<!--/email_off-->
1212

docs/zh/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!--email_off-->
88

9-
[Unpkg.com](https://unpkg.com) 提供了基于 npm 的 CDN 链接。上述链接将始终指向 npm 上的最新版本。 你也可以通过像 `https://unpkg.com/vue-router@3.0.0/dist/vue-router.js` 这样的 URL 来使用特定的版本或 Tag。
9+
[Unpkg.com](https://unpkg.com) 提供了基于 npm 的 CDN 链接。上述链接将始终指向 npm 上的最新版本。 你也可以通过像 `https://unpkg.com/vue-router@4.0.15/dist/vue-router.global.js` 这样的 URL 来使用特定的版本或 Tag。
1010

1111
<!--/email_off-->
1212

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/vue-router.prod.cjs')
5+
} else {
6+
module.exports = require('./dist/vue-router.cjs')
7+
}

package.json

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
{
22
"name": "vue-router",
33
"version": "4.0.15",
4-
"main": "dist/vue-router.cjs.js",
4+
"main": "index.js",
55
"unpkg": "dist/vue-router.global.js",
66
"jsdelivr": "dist/vue-router.global.js",
7-
"module": "dist/vue-router.esm-bundler.js",
7+
"module": "dist/vue-router.mjs",
88
"types": "dist/vue-router.d.ts",
9+
"exports": {
10+
".": {
11+
"browser": "./dist/vue-router.esm-browser.js",
12+
"node": {
13+
"import": {
14+
"production": "./dist/vue-router.prod.cjs",
15+
"development": "./dist/vue-router.mjs",
16+
"default": "./dist/vue-router.mjs"
17+
},
18+
"require": {
19+
"production": "./dist/vue-router.prod.cjs",
20+
"development": "./dist/vue-router.cjs",
21+
"default": "./index.js"
22+
}
23+
},
24+
"import": "./dist/vue-router.mjs"
25+
},
26+
"./dist/*": "./dist/*",
27+
"./vetur/*": "./vetur/*",
28+
"./package.json": "./package.json"
29+
},
930
"sideEffects": false,
1031
"funding": "https://github.com/sponsors/posva",
1132
"license": "MIT",
@@ -18,7 +39,8 @@
1839
},
1940
"homepage": "https://github.com/vuejs/router#readme",
2041
"files": [
21-
"dist/*.js",
42+
"index.js",
43+
"dist/*.{js,cjs,mjs}",
2244
"dist/vue-router.d.ts",
2345
"vetur/tags.json",
2446
"vetur/attributes.json",
@@ -28,7 +50,7 @@
2850
"dev": "vite --config playground/vite.config.js",
2951
"release": "bash scripts/release.sh",
3052
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
31-
"build": "rollup -c rollup.config.js",
53+
"build": "rimraf dist && rollup -c rollup.config.js",
3254
"build:dts": "api-extractor run --local --verbose && tail -n +9 src/globalExtensions.ts >> dist/vue-router.d.ts",
3355
"build:playground": "vue-tsc --noEmit && vite build --config playground/vite.config.js",
3456
"build:e2e": "vue-tsc --noEmit && vite build --config e2e/vite.config.js",
@@ -104,6 +126,7 @@
104126
"nightwatch": "^1.7.11",
105127
"nightwatch-helpers": "^1.2.0",
106128
"prettier": "^2.4.1",
129+
"rimraf": "^3.0.2",
107130
"rollup": "^2.68.0",
108131
"rollup-plugin-analyzer": "^4.0.0",
109132
"rollup-plugin-terser": "^7.0.2",

rollup.config.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path'
2+
import { promises as fsp } from 'fs'
23
import ts from 'rollup-plugin-typescript2'
34
import replace from '@rollup/plugin-replace'
45
import resolve from '@rollup/plugin-node-resolve'
@@ -17,47 +18,52 @@ const banner = `/*!
1718
let hasTSChecked = false
1819

1920
const outputConfigs = {
20-
// each file name has the format: `dist/${name}.${format}.js`
21+
// each file name has the format: `dist/${name}.${format}.${ext}`
2122
// format being a key of this object
22-
'esm-bundler': {
23+
mjs: {
2324
file: pkg.module,
2425
format: `es`,
2526
},
2627
cjs: {
27-
file: pkg.main,
28+
file: 'dist/vue-router.cjs',
2829
format: `cjs`,
2930
},
3031
global: {
3132
file: pkg.unpkg,
3233
format: `iife`,
3334
},
34-
esm: {
35-
file: pkg.browser || pkg.module.replace('bundler', 'browser'),
35+
browser: {
36+
file: 'dist/vue-router.esm-browser.js',
3637
format: `es`,
3738
},
3839
}
3940

40-
const allFormats = Object.keys(outputConfigs)
41+
const stubs = {
42+
'dist/vue-router.cjs': 'vue-router.cjs.js',
43+
'dist/vue-router.mjs': 'vue-router.esm-bundler.js',
44+
'dist/vue-router.prod.cjs': 'vue-router.cjs.prod.js',
45+
}
46+
47+
const packageBuilds = Object.keys(outputConfigs)
4148
// in vue-router there are not that many
42-
const packageFormats = allFormats
43-
const packageConfigs = packageFormats.map(format =>
44-
createConfig(format, outputConfigs[format])
49+
const packageConfigs = packageBuilds.map(buildName =>
50+
createConfig(buildName, outputConfigs[buildName])
4551
)
4652

4753
// only add the production ready if we are bundling the options
48-
packageFormats.forEach(format => {
49-
if (format === 'cjs') {
50-
packageConfigs.push(createProductionConfig(format))
51-
} else if (format === 'global') {
52-
packageConfigs.push(createMinifiedConfig(format))
54+
packageBuilds.forEach(buildName => {
55+
if (buildName === 'cjs') {
56+
packageConfigs.push(createProductionConfig(buildName))
57+
} else if (buildName === 'global') {
58+
packageConfigs.push(createMinifiedConfig(buildName))
5359
}
5460
})
5561

5662
export default packageConfigs
5763

58-
function createConfig(format, output, plugins = []) {
64+
function createConfig(buildName, output, plugins = []) {
5965
if (!output) {
60-
console.log(require('chalk').yellow(`invalid format: "${format}"`))
66+
console.log(require('chalk').yellow(`invalid format: "${buildName}"`))
6167
process.exit(1)
6268
}
6369

@@ -70,11 +76,11 @@ function createConfig(format, output, plugins = []) {
7076
// '@vue/devtools-api': 'VueDevtoolsApi',
7177
}
7278

73-
const isProductionBuild = /\.prod\.js$/.test(output.file)
74-
const isGlobalBuild = format === 'global'
75-
const isRawESMBuild = format === 'esm'
76-
const isNodeBuild = format === 'cjs'
77-
const isBundlerESMBuild = /esm-bundler/.test(format)
79+
const isProductionBuild = /\.prod\.[cm]?js$/.test(output.file)
80+
const isGlobalBuild = buildName === 'global'
81+
const isRawESMBuild = buildName === 'browser'
82+
const isNodeBuild = buildName === 'cjs'
83+
const isBundlerESMBuild = buildName === 'mjs'
7884

7985
if (isGlobalBuild) output.name = 'VueRouter'
8086

@@ -122,6 +128,20 @@ function createConfig(format, output, plugins = []) {
122128
),
123129
...nodePlugins,
124130
...plugins,
131+
{
132+
async writeBundle() {
133+
const stub = stubs[output.file]
134+
if (!stub) return
135+
136+
const contents =
137+
buildName === 'cjs'
138+
? `module.exports = require('../${output.file}')`
139+
: `export * from '../${output.file}'`
140+
141+
await fsp.writeFile(path.resolve(__dirname, `dist/${stub}`), contents)
142+
console.log(`created stub ${require('chalk').bold(`dist/${stub}`)}`)
143+
},
144+
},
125145
],
126146
output,
127147
// onwarn: (msg, warn) => {
@@ -174,8 +194,10 @@ function createReplacePlugin(
174194
}
175195

176196
function createProductionConfig(format) {
197+
const extension = format === 'cjs' ? 'cjs' : 'js'
198+
const descriptor = format === 'cjs' ? '' : `.${format}`
177199
return createConfig(format, {
178-
file: `dist/${name}.${format}.prod.js`,
200+
file: `dist/${name}${descriptor}.prod.${extension}`,
179201
format: outputConfigs[format].format,
180202
})
181203
}

0 commit comments

Comments
 (0)