Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
fix: cush.config.js reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 22, 2018
1 parent 07f9592 commit 98c43a2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
25 changes: 14 additions & 11 deletions src/Bundle/index.coffee
Expand Up @@ -15,6 +15,9 @@ empty = []
resolved = Promise.resolve null
nodeModulesRE = /\/node_modules\//

jsParser = require.resolve '../parsers/js'
cssParser = require.resolve '../parsers/css'

INIT = 1 # never built before
LAZY = 2 # no automatic rebuilds
IDLE = 3 # waiting for changes
Expand All @@ -37,12 +40,10 @@ class Bundle extends Emitter
@assets = []
@packages = Object.create null
@plugins = opts.plugins or []
@parsers = opts.parsers or []
@project = null
@status = INIT
@state = null
@time = 0
@_init = opts.init
@_extRE = null
@_config = null
@_events = null
Expand Down Expand Up @@ -156,12 +157,18 @@ class Bundle extends Emitter
@_config = @_getInitialConfig()
@_events = events = {}

if @_init
try await @_init()
catch err
log.error err
# Add the built-in parsers.
@merge 'parsers', [jsParser, cssParser]

# Call format-provided plugins.
if @constructor.plugins
await @use @constructor.plugins

# Call plugins provided during creation.
if @plugins.length
await @use @plugins

await @_callPlugins()
# Apply `cush.config.js` configuration.
await @project._configure(this)

if events.config
Expand All @@ -172,10 +179,6 @@ class Bundle extends Emitter
loadBundle this
return this

_callPlugins: ->
if @plugins.length
return @use @plugins

_build: ->
if @status isnt INIT
@emitSync 'rebuild'
Expand Down
12 changes: 1 addition & 11 deletions src/bundles.coffee
Expand Up @@ -3,11 +3,6 @@ cush = require 'cush'
path = require 'path'
fs = require 'saxon/sync'

builtinParsers = [
require.resolve('./parsers/js')
require.resolve('./parsers/css')
]

# Bundle constructor
cush.bundle = (main, opts) ->

Expand Down Expand Up @@ -36,19 +31,14 @@ cush.bundle = (main, opts) ->

# Load the project.
project = cush.project root
if bundles = project.config.bundles
defaults = merge {}, bundles[path.relative root, main]
opts = merge defaults, opts
opts.format or= project.get(main).format

# Resolve the bundle format.
if !Bundle = opts.format or resolveFormat main
uhoh '`main` has no bundle format: ' + main, 'NO_FORMAT'

# Create the bundle.
bundle = new Bundle opts
if Bundle.plugins
bundle.plugins.unshift ...Bundle.plugins
bundle.parsers.push ...builtinParsers

# Load the root package.
pack = bundle._loadPackage root
Expand Down
24 changes: 23 additions & 1 deletion src/projects.coffee
Expand Up @@ -20,6 +20,13 @@ class Project extends Emitter
@watcher = null
@bundles = new Set

get: (main) ->
if bundles = @config.bundles
if !path.isAbsolute main
main = path.relative @root, main
return bundles[main] or {}
return {}

watch: ->
@watcher or=
wch.stream @root,
Expand All @@ -43,8 +50,23 @@ class Project extends Emitter
@watcher.destroy()
delete projects[@root.path]

# Format-specific configuration
_configure: (bundle) ->

# Bundle-specific configuration
if config = @config.bundles?[bundle.main.name]

if typeof config.init is 'function'
try await config.init.call bundle
catch err
log.error err

if Array.isArray config.parsers
bundle.merge 'parsers', config.parsers

if Array.isArray config.plugins
await bundle.use config.plugins

# Format-specific configuration
if fn = @config[bundle.constructor.id]
try await fn.call bundle
catch err
Expand Down
2 changes: 1 addition & 1 deletion src/workers/Bundle.coffee
Expand Up @@ -49,7 +49,7 @@ class Bundle

_configure: ({plugins, parsers}) ->

parsers.forEach (filename) =>
@_config.parsers.forEach (filename) =>
for ext, parse of require(filename)
parse.source = {path: filename}
@hook 'parse' + ext, parse
Expand Down
1 change: 0 additions & 1 deletion src/workers/index.coffee
Expand Up @@ -65,7 +65,6 @@ module.exports =
target: bundle.target
config: bundle._config
plugins: bundle._workers
parsers: bundle.parsers
}]

loadAsset: (asset) ->
Expand Down

0 comments on commit 98c43a2

Please sign in to comment.