@@ -6,48 +6,33 @@ import babel from 'rollup-plugin-babel';
66import { uglify } from 'rollup-plugin-uglify' ;
77
88/**
9- * get bundle config
9+ * returns the config for the given file
10+ *@param {boolean } _uglify - boolean value indicating if file should be minified
11+ *@param {string } format - the expected output format
12+ *@param {string } src - the file source directory
13+ *@param {string } dest - the file destination directory, omit the .js extension
14+ *@param {string } name - the file module name
15+ *@param {Array } externals - array of module externals
16+ *@returns {Object }
1017*/
11- function getBundleConfig ( _uglify , format , name , file , externalModules ) {
18+ function getConfig ( _uglify , format , src , dest , name , externals ) {
1219 let plugins = [
1320 resolve ( ) ,
1421 babel ( {
1522 exclude : 'node_modules/**' ,
1623 plugins : [ "external-helpers" ]
1724 } )
1825 ] ;
19- if ( _uglify )
20- plugins . push ( uglify ( ) ) ;
21-
22- return {
23- input : 'src/exports.js' ,
24- output : {
25- file : `dist/${ _uglify ? file + '.min' : file } .js` ,
26- format : format ,
27- name : name ,
28- interop : false ,
29- } ,
30- plugins : plugins ,
31- external : externalModules
32- } ;
33- }
3426
35- function getModuleConfig ( _uglify , src , dest , externals ) {
36- let plugins = [
37- resolve ( ) ,
38- babel ( {
39- exclude : 'node_modules/**' ,
40- plugins : [ "external-helpers" ]
41- } )
42- ] ;
4327 if ( _uglify )
4428 plugins . push ( uglify ( ) ) ;
4529
4630 return {
4731 input : src ,
4832 output : {
4933 file : dest + ( _uglify ? '.min.js' : '.js' ) ,
50- format : 'cjs' ,
34+ format : format ,
35+ name : name ,
5136 interop : false ,
5237 //sourcemap: true
5338 } ,
@@ -56,35 +41,117 @@ function getModuleConfig(_uglify, src, dest, externals) {
5641 } ;
5742}
5843
59- function getModules ( dir ) {
44+ /**
45+ * resolves the pattern into a regex object
46+ *@param {Array|string } patterns -array of patterns or string pattern
47+ */
48+ function resolveRegex ( patterns ) {
49+ if ( patterns === '*' )
50+ return [ new RegExp ( '.*' ) ] ;
51+
52+ if ( Object . prototype . toString . call ( patterns ) === '[object Array]' ) {
53+ return patterns . map ( ( pattern ) => {
54+ pattern = pattern . replace ( / \. / g, '\.' ) . replace ( / \* { 2 } / g, '.*' ) . replace ( / \* / g, '[^/]+' ) ;
55+ return new RegExp ( pattern , 'i' ) ;
56+ } ) ;
57+ }
58+
59+ return [ ] ;
60+ }
61+
62+ /**
63+ * gets all modules
64+ *@param {string } dir - the root module directory to iterate
65+ *@param {string } mainModuleName - the global module name for the main export file.
66+ * others are mapped to file names
67+ *@returns {Array }
68+ */
69+ function getModules ( dir , mainModuleName ) {
6070 let modules = [ ] ;
6171 fs . readdirSync ( path . resolve ( __dirname , dir ) ) . forEach ( file => {
6272 let filePath = path . resolve ( __dirname , dir ) + '/' + file ;
63- if ( fs . statSync ( filePath ) . isFile ( ) )
64- modules . push ( { name : path . basename ( filePath , '.js' ) , path : filePath } ) ;
65- else
73+ if ( fs . statSync ( filePath ) . isFile ( ) ) {
74+ let name = path . basename ( filePath , '.js' ) ;
75+ if ( name === 'main' )
76+ name = mainModuleName ;
77+
78+ modules . push ( { name : name , path : filePath } ) ;
79+ }
80+ else {
6681 modules . push ( ...getModules ( filePath ) ) ;
82+ }
6783 } ) ;
6884 return modules ;
6985}
7086
71- let modules = getModules ( 'src/modules' ) ,
72- externalModules = modules . map ( module => module . path ) ;
87+ /**
88+ * returns the allowed exports for each build kind
89+ *@param {string } outDir - the out directory for the build kind
90+ *@param {string } format - the output format for all included modules in this build kind
91+ *@param {Array } modules - the modules list to build from
92+ *@param {Array } externalModules - array of external modules
93+ *@returns {Array }
94+ */
95+ function getExports ( outDir , format , uglify , modules , externalModules , includes , excludes ) {
96+ let exports = [ ] ,
97+ src = null ,
98+ regexMatches = function ( regex ) {
99+ return regex . test ( src ) ;
100+ } ,
101+ filterExternalModules = function ( externalModule ) {
102+ return externalModule !== src ;
103+ } ;
73104
74- let exports = modules . reduce ( ( exports , module ) => {
75- let src = module . path ,
76- dest = 'dist/' + src . replace ( / ^ .* \/ s r c \/ / , '' ) . replace ( / \. j s $ / , '' ) ;
105+ for ( let _module of modules ) {
106+ src = _module . path ;
107+ if ( includes . some ( regexMatches ) && ! excludes . some ( regexMatches ) ) {
108+ let dest = outDir + '/' + src . replace ( / ^ .* \/ s r c \/ / , '' ) . replace ( / \. j s $ / , '' ) ,
109+ externals = externalModules . filter ( filterExternalModules ) ;
77110
78- let externals = externalModules . filter ( ( externalModule ) => externalModule !== src ) ;
79- exports . push ( getModuleConfig ( false , src , dest , externals ) ) ;
111+ exports . push ( getConfig ( false , format , src , dest , _module . name , externals ) ) ;
112+ if ( uglify )
113+ exports . push ( getConfig ( true , format , src , dest , _module . name , externals ) ) ;
114+ }
115+ }
80116
81117 return exports ;
82- } , [ ] ) ;
118+ }
83119
84- exports . push ( getBundleConfig ( false , 'cjs' , '' , 'main' , externalModules ) ) ;
85- exports . push ( getBundleConfig ( true , 'cjs' , '' , 'main' , externalModules ) ) ;
120+ import buildConfig from './build.config.json' ;
121+
122+ let config = buildConfig ;
123+
124+ if ( typeof config . distConfig === 'undefined' )
125+ config . distConfig = { } ;
126+
127+ if ( typeof config . libConfig === 'undefined' )
128+ config . libConfig = { } ;
129+
130+ //get modules & external modules
131+ let modules = getModules ( 'src' , config . mainModuleName || 'Module' ) ,
132+ externalModules = modules . map ( module => module . path ) ;
86133
87- exports . push ( getBundleConfig ( false , 'iife' , 'XMLSerializer' , 'browser' , [ ] ) ) ;
88- exports . push ( getBundleConfig ( true , 'iife' , 'XMLSerializer' , 'browser' , [ ] ) ) ;
134+ //resolve the default includes and exclude patterns
135+ let includes = resolveRegex ( config . include || '*' ) ,
136+ excludes = resolveRegex ( config . exclude || null ) ;
89137
90- export default exports ;
138+ export default [
139+ ...getExports (
140+ 'lib' ,
141+ config . libConfig . format || 'cjs' ,
142+ config . libConfig . uglify ? true : config . uglify ,
143+ modules ,
144+ externalModules ,
145+ config . libConfig . include ? resolveRegex ( config . libConfig . include ) : includes ,
146+ config . libConfig . exclude ? resolveRegex ( config . libConfig . exclude ) : excludes
147+ ) ,
148+ ...getExports (
149+ 'dist' ,
150+ config . distConfig . format || 'iife' ,
151+ config . distConfig . uglify ? true : config . uglify ,
152+ modules ,
153+ [ ] ,
154+ config . distConfig . include ? resolveRegex ( config . distConfig . include ) : includes ,
155+ config . distConfig . exclude ? resolveRegex ( config . distConfig . exclude ) : excludes
156+ )
157+ ] ;
0 commit comments