Skip to content

Commit

Permalink
add modules config
Browse files Browse the repository at this point in the history
  • Loading branch information
maiwenan committed Oct 17, 2019
1 parent 4551f09 commit 6e69e91
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 37 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ The value can be one or more than one of the following:
* esm
* system

### modules

* Type: `Object[]`
* Default: undefined

Build the library in multi-module mode. Each item spacifies the `elf.config.js` attrs and extends `elf.config.js`
attrs value

### configureRollup

* Type: `Function | Object`
Expand Down Expand Up @@ -155,7 +163,7 @@ module.exports = {
modes: 'esm',
lintOnSave: false,
configureRollup: null,
solution: [],
modules: undefined,
pluginOptions: {
replace: {},
alias: {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/stats.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<body>
<h1>RollUp Visualizer</h1>
<div id="charts"></div>
<script>window.nodesData = [{"id":"rate.umd.js","root":{"name":"root","children":[{"name":"\u0000rollupPluginBabelHelpers.js","children":[],"size":1926,"originalSize":42833},{"name":"rate.js","children":[],"size":513,"originalSize":253},{"name":"style.scss","children":[],"size":0,"originalSize":2805},{"name":"index.js","children":[],"size":0,"originalSize":72}]}}];</script>
<script>window.nodesData = [{"id":"rate.js","root":{"name":"root","children":[{"name":"\u0000rollupPluginBabelHelpers.js","children":[],"size":1926,"originalSize":42833},{"name":"rate.js","children":[],"size":507,"originalSize":253},{"name":"style.scss","children":[],"size":0,"originalSize":2805},{"name":"index.js","children":[],"size":0,"originalSize":72}]}}];</script>
<script>(function () {
'use strict';

Expand Down
2 changes: 1 addition & 1 deletion examples/vue/stats.html

Large diffs are not rendered by default.

53 changes: 35 additions & 18 deletions lib/ElfScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const dotenv = require('dotenv')
const rollup = require('rollup')
const _ = require('lodash')

const RollupConfig = require('./config/index')
const parseConfig = require('./config/config.parser')
const Config = require('./config/index')
const { parseConfig } = require('./config/config.parser')
const utils = require('./utils/utils')
const defaultConfig = require('./config/config.default')
const {
Expand All @@ -19,9 +19,6 @@ class ElfScripts {

this.mode = mode
this.pkg = this.loadPkg()
this.elfConfig = this.loadElfConfig(defaultConfig)
this.elfConfig.name = this.elfConfig.name || utils.parseName(this.pkg.name)

this.options = {
pkg: this.pkg,
variables,
Expand All @@ -31,17 +28,36 @@ class ElfScripts {
iifeMode: mode === 'iife',
serveMode: mode === 'serve'
}
this.rollupCfg = new RollupConfig(this.elfConfig, this.options, this.pkg)

const {
config,
options
} = this.loadElfConfig(defaultConfig, this.pkg)
this.elfConfig = config
this.rollupOptions = options
}

loadElfConfig (defaultConfig) {
loadElfConfig (defaultConfig, pkg) {
let userConfig = utils.importCjsModule('elf.config.js')
let options = []
let config = null

if (typeof userConfig === 'function') {
userConfig = userConfig(this.mode)
}
config = _.merge({}, defaultConfig, userConfig)
config.name = config.name || utils.parseName(pkg.name)

return _.merge({}, defaultConfig, userConfig)
if (Array.isArray(config.modules) && config.module.length > 0) {
options = config.modules.map(opt => _.merge({}, config, opt))
} else {
options = [config]
}

return {
config,
options
}
}

loadEnv (envName) {
Expand All @@ -63,38 +79,39 @@ class ElfScripts {
return utils.importCjsModule('package.json')
}

async build (config) {
async build (opt) {
const rollupCfg = new Config(opt, this.options, this.pkg)
const config = await rollupCfg.get()
const { inputs, outputs } = parseConfig(config)
const bundle = await rollup.rollup(inputs)

await Promise.all(outputs.map(output => bundle.write(output)))
return bundle
}

async batchBuild (config) {
const configs = Array.isArray(config) ? config : [config]
async batchBuild (options) {
let result = null

for (const cfg of configs) {
result = await this.build(cfg)
for (const opt of options) {
result = await this.build(opt)
}

return result
}

async runBuild () {
const rollupCfg = await this.rollupCfg.get()
const result = await this.batchBuild(rollupCfg)
const result = await this.batchBuild(this.rollupOptions)
const timing = result.getTimings()

success(timing)
return result
}

async runServe () {
const { serveDir, watchOptions } = this.elfConfig
const rollupCfg = await this.rollupCfg.get()
const config = Array.isArray(rollupCfg) ? rollupCfg[0] : rollupCfg
const opt = this.rollupOptions[0]
const { serveDir, watchOptions } = opt
const rollupCfg = new Config(opt, this.options, this.pkg)
const config = await rollupCfg.get()
const { inputs, outputs } = parseConfig(config)
const watcher = rollup.watch({
...inputs,
Expand Down
11 changes: 7 additions & 4 deletions lib/config/config.parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ function parse (config, fields) {
return result
}

module.exports = function parseConfig (config) {
return {
inputs: parse(config, inputOptions),
outputs: config.output
module.exports = {
parse,
parseConfig: config => {
return {
inputs: parse(config, inputOptions),
outputs: config.output
}
}
}
22 changes: 10 additions & 12 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const beforePlugin = (cfg, fn) => {
}

class Config {
constructor (elfConfig, options, pkg) {
const { entry } = elfConfig
constructor (rollupOption, options, pkg) {
const { entry } = rollupOption

this.elfConfig = elfConfig
this.rollupOption = rollupOption
this.options = options
this.pkg = pkg
this.isMultiEntry = typeof entry !== 'string'
Expand All @@ -39,9 +39,9 @@ class Config {
}

async get () {
const { elfConfig, options } = this
const { rollupOption, options } = this
const entry = await this.entry()
const { configureRollup } = elfConfig
const { configureRollup } = rollupOption
const { serveMode } = options
let config = {
input: entry,
Expand All @@ -53,8 +53,6 @@ class Config {

if (typeof configureRollup === 'function') {
config = configureRollup(config)
} else if (Array.isArray(configureRollup)) {
config = configureRollup.map(cfg => _.merge({}, config, cfg))
} else if (configureRollup && typeof configureRollup === 'object') {
config = _.merge({}, config, configureRollup)
}
Expand All @@ -63,7 +61,7 @@ class Config {
}

async entry () {
const { entry } = this.elfConfig
const { entry } = this.rollupOption

if (this.isInnerMulti) {
const result = utils.readEntryModule(utils.resolvePath(entry))
Expand All @@ -74,8 +72,8 @@ class Config {
}

output () {
const { elfConfig, options } = this
const { name, serveDir, outputDir, modes } = elfConfig
const { rollupOption, options } = this
const { name, serveDir, outputDir, modes } = rollupOption
const { serveMode, umdMode, iifeMode } = options
const file = serveMode
? utils.resolvePath(`${serveDir}/${name}.js`)
Expand Down Expand Up @@ -115,13 +113,13 @@ class Config {
}

plugins () {
const { elfConfig, options } = this
const { rollupOption, options } = this
const {
name,
serveDir,
outputDir,
pluginOptions
} = elfConfig
} = rollupOption
const { variables, serveMode, umdMode, iifeMode } = options
let extract = true

Expand Down

0 comments on commit 6e69e91

Please sign in to comment.