1
1
import path from 'path'
2
+ import { promises as fsp } from 'fs'
2
3
import ts from 'rollup-plugin-typescript2'
3
4
import replace from '@rollup/plugin-replace'
4
5
import resolve from '@rollup/plugin-node-resolve'
@@ -17,47 +18,52 @@ const banner = `/*!
17
18
let hasTSChecked = false
18
19
19
20
const outputConfigs = {
20
- // each file name has the format: `dist/${name}.${format}.js `
21
+ // each file name has the format: `dist/${name}.${format}.${ext} `
21
22
// format being a key of this object
22
- 'esm-bundler' : {
23
+ mjs : {
23
24
file : pkg . module ,
24
25
format : `es` ,
25
26
} ,
26
27
cjs : {
27
- file : pkg . main ,
28
+ file : 'dist/vue-router.cjs' ,
28
29
format : `cjs` ,
29
30
} ,
30
31
global : {
31
32
file : pkg . unpkg ,
32
33
format : `iife` ,
33
34
} ,
34
- esm : {
35
- file : pkg . browser || pkg . module . replace ( 'bundler' , 'browser' ) ,
35
+ browser : {
36
+ file : 'dist/vue-router.esm- browser.js' ,
36
37
format : `es` ,
37
38
} ,
38
39
}
39
40
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 )
41
48
// 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 ] )
45
51
)
46
52
47
53
// 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 ) )
53
59
}
54
60
} )
55
61
56
62
export default packageConfigs
57
63
58
- function createConfig ( format , output , plugins = [ ] ) {
64
+ function createConfig ( buildName , output , plugins = [ ] ) {
59
65
if ( ! output ) {
60
- console . log ( require ( 'chalk' ) . yellow ( `invalid format: "${ format } "` ) )
66
+ console . log ( require ( 'chalk' ) . yellow ( `invalid format: "${ buildName } "` ) )
61
67
process . exit ( 1 )
62
68
}
63
69
@@ -70,11 +76,11 @@ function createConfig(format, output, plugins = []) {
70
76
// '@vue/devtools-api': 'VueDevtoolsApi',
71
77
}
72
78
73
- const isProductionBuild = / \. p r o d \. j s $ / . test ( output . file )
74
- const isGlobalBuild = format === 'global'
75
- const isRawESMBuild = format === 'esm '
76
- const isNodeBuild = format === 'cjs'
77
- const isBundlerESMBuild = / e s m - b u n d l e r / . test ( format )
79
+ const isProductionBuild = / \. p r o d \. [ c m ] ? j s $ / . test ( output . file )
80
+ const isGlobalBuild = buildName === 'global'
81
+ const isRawESMBuild = buildName === 'browser '
82
+ const isNodeBuild = buildName === 'cjs'
83
+ const isBundlerESMBuild = buildName === 'mjs'
78
84
79
85
if ( isGlobalBuild ) output . name = 'VueRouter'
80
86
@@ -122,6 +128,20 @@ function createConfig(format, output, plugins = []) {
122
128
) ,
123
129
...nodePlugins ,
124
130
...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
+ } ,
125
145
] ,
126
146
output,
127
147
// onwarn: (msg, warn) => {
@@ -174,8 +194,10 @@ function createReplacePlugin(
174
194
}
175
195
176
196
function createProductionConfig ( format ) {
197
+ const extension = format === 'cjs' ? 'cjs' : 'js'
198
+ const descriptor = format === 'cjs' ? '' : `.${ format } `
177
199
return createConfig ( format , {
178
- file : `dist/${ name } . ${ format } .prod.js ` ,
200
+ file : `dist/${ name } ${ descriptor } .prod.${ extension } ` ,
179
201
format : outputConfigs [ format ] . format ,
180
202
} )
181
203
}
0 commit comments