@@ -20,6 +20,7 @@ import shebangPlugin from 'rollup-plugin-preserve-shebang';
2020import flow from 'rollup-plugin-flow' ;
2121import camelCase from 'camelcase' ;
2222
23+ const interopRequire = m => m . default || m ;
2324const readFile = promisify ( fs . readFile ) ;
2425const stat = promisify ( fs . stat ) ;
2526const isDir = name => stat ( name ) . then ( stats => stats . isDirectory ( ) ) . catch ( ( ) => false ) ;
@@ -72,11 +73,13 @@ export default async function microbundle(options) {
7273 options . multipleEntries = entries . length > 1 ;
7374
7475 let formats = ( options . format || options . formats ) . split ( ',' ) ;
76+ // always compile cjs first if it's there:
77+ formats . sort ( ( a , b ) => a === 'cjs' ? - 1 : a > b ? 1 : 0 ) ;
7578
7679 let steps = [ ] ;
7780 for ( let i = 0 ; i < entries . length ; i ++ ) {
7881 for ( let j = 0 ; j < formats . length ; j ++ ) {
79- steps . push ( createConfig ( options , entries [ i ] , formats [ j ] ) ) ;
82+ steps . push ( createConfig ( options , entries [ i ] , formats [ j ] , i === 0 && j === 0 ) ) ;
8083 }
8184 }
8285
@@ -121,7 +124,7 @@ export default async function microbundle(options) {
121124}
122125
123126
124- function createConfig ( options , entry , format ) {
127+ function createConfig ( options , entry , format , writeMeta ) {
125128 let { pkg } = options ;
126129
127130 let external = [ 'dns' , 'fs' , 'path' , 'url' ] . concat (
@@ -170,11 +173,14 @@ function createConfig(options, entry, format) {
170173
171174 // let rollupName = safeVariableName(basename(entry).replace(/\.js$/, ''));
172175
176+ let nameCache = { } ;
177+ let mangleOptions = options . pkg . mangle || false ;
178+
173179 let config = {
174180 inputOptions : {
175181 input : entry ,
176182 external,
177- plugins : [
183+ plugins : [ ] . concat (
178184 postcss ( {
179185 plugins : [
180186 autoprefixer ( )
@@ -199,7 +205,10 @@ function createConfig(options, entry, format) {
199205 exclude : 'node_modules/**' ,
200206 jsx : options . jsx || 'h' ,
201207 objectAssign : options . assign || 'Object.assign' ,
202- transforms : { dangerousForOf : true , dangerousTaggedTemplateString : true }
208+ transforms : {
209+ dangerousForOf : true ,
210+ dangerousTaggedTemplateString : true
211+ }
203212 } ) ,
204213 useNodeResolve && commonjs ( {
205214 include : 'node_modules/**'
@@ -224,19 +233,37 @@ function createConfig(options, entry, format) {
224233 // [`export default ${rollupName};`]: '',
225234 // [`var ${rollupName} =`]: 'export default'
226235 // }),
227- format !== 'es' && options . compress !== false && uglify ( {
236+ options . compress !== false && uglify ( {
228237 output : { comments : false } ,
229238 mangle : {
230- toplevel : format === 'cjs'
231- }
232- } ) ,
233- {
234- ongenerate ( { bundle } , { code } ) {
235- config . _code = bundle . _code = code ;
239+ toplevel : format === 'cjs' || format === 'es' ,
240+ properties : mangleOptions ? {
241+ regex : mangleOptions . regex ? new RegExp ( mangleOptions . regex ) : null ,
242+ reserved : mangleOptions . reserved || [ ]
243+ } : false
244+ } ,
245+ nameCache
246+ } , format === 'es' ? interopRequire ( require ( 'uglify-es' ) ) . minify : undefined ) ,
247+ mangleOptions && {
248+ // before hook
249+ options ( ) {
250+ try {
251+ nameCache = JSON . parse ( fs . readFileSync ( resolve ( options . cwd , 'mangle.json' ) , 'utf8' ) ) ;
252+ }
253+ catch ( e ) { }
254+ } ,
255+ // after hook
256+ onwrite ( ) {
257+ if ( writeMeta && nameCache ) {
258+ fs . writeFile ( resolve ( options . cwd , 'mangle.json' ) , JSON . stringify ( nameCache , null , 2 ) , Object ) ;
259+ }
236260 }
237261 } ,
262+ { ongenerate ( { bundle } , { code } ) {
263+ config . _code = bundle . _code = code ;
264+ } } ,
238265 shebangPlugin ( )
239- ] . filter ( Boolean )
266+ ) . filter ( Boolean )
240267 } ,
241268
242269 outputOptions : {
0 commit comments