Skip to content

Commit

Permalink
Make --watch deal with configuration changes.
Browse files Browse the repository at this point in the history
This change means that you can run `bundle / build --watch` and have it
respect config changes.

If in tab one you have:

  jspm bundle app --watch

And then in another you run:

  jspm install jquery

When you change your app to `import $ from jQuery` it will pick up the
change and include jQuery in the generated bundle. Previously at this
point it would error and complain that it couldn't find jQuery, because
the config hadn't been reloaded.
  • Loading branch information
jackfranklin committed Jun 16, 2016
1 parent b6203ca commit a1bdee8
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var fs = require('fs');
var asp = require('bluebird').Promise.promisify;
var extendSystemConfig = require('./common').extendSystemConfig;
var toFileURL = require('./common').toFileURL;
var JspmSystemConfig = require('./config/loader').JspmSystemConfig;

function camelCase(name, capitalizeFirst) {
return name.split('-').map(function(part, index) {
Expand Down Expand Up @@ -149,7 +150,9 @@ exports.bundle = function(moduleExpression, fileName, opts) {
opts = opts || {};
fileName = fileName || path.resolve(config.pjson.baseURL, 'build.js');

function bundle() {
function bundle(builder) {
var latestBuilder = builder || systemBuilder;

return Promise.resolve()
.then(function() {
if (!opts.sourceMaps)
Expand All @@ -158,7 +161,7 @@ exports.bundle = function(moduleExpression, fileName, opts) {
.then(function() {
ui.log('info', 'Building the bundle tree for %' + moduleExpression + '%...');

return systemBuilder.bundle(moduleExpression, fileName, opts);
return latestBuilder.bundle(moduleExpression, fileName, opts);
})
.then(function(output) {
logTree(output.modules);
Expand Down Expand Up @@ -218,7 +221,9 @@ exports.build = function(expression, fileName, opts) {
opts = opts || {};
fileName = fileName || path.resolve(config.pjson.baseURL, 'build.js');

function build() {
function build(newBuilder) {
var latestBuilder = newBuilder || systemBuilder;

return Promise.resolve()
.then(function() {
if (!opts.sourceMaps)
Expand All @@ -227,7 +232,7 @@ exports.build = function(expression, fileName, opts) {
.then(function() {
ui.log('info', 'Creating the single-file build for %' + expression + '%...');

return systemBuilder.buildStatic(expression, fileName, opts);
return latestBuilder.buildStatic(expression, fileName, opts);
})
.then(function(output) {
logTree(output.modules, output.inlineMap ? output.inlineMap : opts.rollup);
Expand Down Expand Up @@ -299,7 +304,7 @@ function createWatcher(files, opts) {

var watcher;
// share the existing watcher if possible
if (watchman && lowestDir != watchDir ||
if (watchman && lowestDir != watchDir ||
!watchman && (lowestDir != watchDir || JSON.stringify(watchFiles) != JSON.stringify(relFiles))) {
var sane = require('sane');
if (curWatcher)
Expand Down Expand Up @@ -360,6 +365,12 @@ function buildWatch(builder, output, build) {
return path.resolve(config.pjson.baseURL, file);
});

var configFiles = [config.pjson.configFile];
if (config.pjson.configFileBrowser) configFiles.push(config.pjson.configFileBrowser);
if (config.pjson.configFileDev) configFiles.push(config.pjson.configFileDev);

files = files.concat(configFiles);

var unwatch = createWatcher(files, {
ready: function(dir, watchman) {
ui.log('info', 'Watching %' + (path.relative(process.cwd(), dir) || '.') + '% for changes ' + (watchman ? 'with Watchman' : 'with Node native watcher') + '...');
Expand All @@ -377,7 +388,15 @@ function buildWatch(builder, output, build) {

var building = false;
var rebuild = false;
var changeWasConfigFile = false;
function changed(file) {
changeWasConfigFile = ( configFiles.indexOf(file) > -1 );

if (changeWasConfigFile) {
config.loader = new JspmSystemConfig(config.pjson.configFile);
builder = new Builder();
}

if (file) {
builder.invalidate(toFileURL(file));
// also invalidate any plugin syntax cases
Expand All @@ -391,17 +410,26 @@ function buildWatch(builder, output, build) {

building = true;
rebuild = false;
if (file)
if (file)
ui.log('ok', 'File `' + path.relative(process.cwd(), file) + '` changed, rebuilding...');
else
ui.log('ok', 'File changes made during previous build, rebuilding...');
build().then(function(output) {
return buildWatch(builder, output, build);
}, function(err) {
ui.log('err', err.stack || err);
return buildWatch(builder, output, build);
})
.then(function(newWatcherRefresh) {

var buildPromise;
if (changeWasConfigFile) {
// don't bother doing a rebuild for a config file change
// but just keep watching so the next time a file changes
// the bundle is rebuilt but with the new Builder & config
buildPromise = buildWatch(builder, output, build);
} else {
buildPromise = build(builder).then(function(output) {
return buildWatch(builder, output, build);
}, function(err) {
ui.log('err', err.stack || err);
return buildWatch(builder, output, build);
});
}
return buildPromise.then(function(newWatcherRefresh) {
unwatch();
if (rebuild)
newWatcherRefresh();
Expand Down

0 comments on commit a1bdee8

Please sign in to comment.