Skip to content

Commit

Permalink
only reparse changed pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kommander committed Jul 21, 2017
1 parent aa082e0 commit da4712c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
62 changes: 49 additions & 13 deletions lib/aden.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ function applyDevConfig(pages, webpackConfigs) {
}));
}

function devRebuild() {
return this.parseGraphs(this.pages)
function devRebuild(pages) {
return this.parseGraphs(pages)
.then(() => this.postParseLoadSetup(this.pages))
.then(({ pages }) => this.generateWebpackConfig(pages))
.then(({ pages, webpackConfigs }) => {
Expand Down Expand Up @@ -99,6 +99,7 @@ function devRebuild() {
});

this.watcher.watchings.forEach((watching, index) => {
// watching.close(); // needs to be checked if needed
Object.assign(watching, {
compiler: this.compiler.compilers[index],
});
Expand Down Expand Up @@ -151,11 +152,15 @@ function startWatcher(cb) {
.map((pair) => pair.right)
);

const watchings = this.watcher.watchings.filter((watching) =>
invalid.includes(watching.compiler.name)
);
if (invalid.length > 0) {
const watchings = this.watcher.watchings.filter((watching) =>
invalid.includes(watching.compiler.name)
);

this.log.info(`Invalidating ${invalid}`);

watchings.forEach((watching) => watching.invalidate());
watchings.forEach((watching) => watching.invalidate());
}
}
} else {
initialBuild = false;
Expand All @@ -169,6 +174,34 @@ function startWatcher(cb) {
});
}

function selectPagesForReparse(addedFiles, addedDirs, removedFiles, removedDirs) {
return Promise.resolve().then(() => {
const dirPaths = _.uniq(addedFiles
.concat(addedDirs)
.concat(removedFiles)
.concat(removedDirs)
.map((filePath) => path.parse(filePath).dir));

const rootDirs = dirPaths
.map((dirPath) => ({
dirPath,
isInside: dirPaths
.filter((dir) => (dir !== dirPath))
.find((dir) => pathIsInside(dirPath, dir)),
}))
.filter((result) => !result.isInside)
.map((result) => result.dirPath);

const pagesToParse = rootDirs.map((dir) => this.registerPage(dir));

if (pagesToParse.length > 0) {
this.log.info(`Selected ${pagesToParse.map((page) => page.path.resolved)} for reparse`);
}

return pagesToParse;
});
}

function setupDev(pages, webpackConfigs) {
return Promise.resolve().then(() => new Promise((resolve) => {
if (!this.isDEV) {
Expand All @@ -183,24 +216,26 @@ function setupDev(pages, webpackConfigs) {
this, this.inputFileSystem
);

this.watchFileSystem.on('hardChanges', (added, removals) => {
if (removals.length > 0) {
// TODO: filter removal paths to dirs, get page for dirs,
// -> get assets from last stats, delete .dist assets for page.
this.watchFileSystem.on('hardChanges', (addedFiles, addedDirs, removedFiles, removedDirs) => {
this.log.event('watch:hardChanges', { addedFiles, addedDirs, removedFiles, removedDirs });

if (removedDirs.length > 0) {
const pagesToBeRemoved = this.pages.filter((page) =>
removals.includes(page.path.resolved)
removedDirs.includes(page.path.resolved)
);

if (pagesToBeRemoved.length > 0) {
const removed = pagesToBeRemoved
.map((page) => this.removePage(page));

return Promise.all(removed)
.then(() => this.devRebuild());
.then(() => this.selectPagesForReparse(addedFiles, addedDirs, removedFiles, removedDirs))
.then((pages) => this.devRebuild(pages));
}
}

this.devRebuild();
this.selectPagesForReparse(addedFiles, addedDirs, removedFiles, removedDirs)
.then((pages) => this.devRebuild(pages));
});

if (webpackConfigs.length > 0) {
Expand Down Expand Up @@ -248,6 +283,7 @@ function invalidate(left, right) {
}

module.exports = {
selectPagesForReparse,
startWatcher,
devRebuild,
applyDevConfig,
Expand Down
32 changes: 26 additions & 6 deletions lib/webpack/AdenWatchFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ class AdenWatchFileSystem extends EventEmitter {
.map((evt) => evt.filePath)
);

const removedFiles = _.uniq(
events
.filter((evt) => ['unlink', 'delete'].includes(evt.type))
.map((evt) => evt.filePath)
);

const removedDirs = _.uniq(
events
.filter((evt) => ['unlinkDir'].includes(evt.type))
.map((evt) => evt.filePath)
);

const changes = _.uniq(
events
.map((evt) => evt.filePath)
Expand All @@ -104,8 +116,17 @@ class AdenWatchFileSystem extends EventEmitter {
this.inputFileSystem.purge(changes);
}

const added = _.uniq(events.filter((evt) => ['add', 'addDir'].includes(evt.type))
.map((evt) => evt.filePath));
const addedFiles = _.uniq(events
.filter((evt) => ['add'].includes(evt.type))
.filter((evt) => !removals.includes(evt.filePath))
.map((evt) => evt.filePath)
);

const addedDirs = _.uniq(events
.filter((evt) => ['addDir'].includes(evt.type))
.filter((evt) => !removals.includes(evt.filePath))
.map((evt) => evt.filePath)
);

const watchers = _.uniq(
events
Expand All @@ -127,8 +148,8 @@ class AdenWatchFileSystem extends EventEmitter {
[key]: this.files[key].mtime,
}), {});

if (added.length > 0 || removals.length > 0) {
this.emit('hardChanges', added, removals);
if (addedFiles.length > 0 || addedDirs.length > 0 || removals.length > 0) {
this.emit('hardChanges', addedFiles, addedDirs, removedFiles, removedDirs);
}

watchers
Expand All @@ -139,8 +160,7 @@ class AdenWatchFileSystem extends EventEmitter {
changes, // files
[], // dirs - not implemented yet
removals,
this.times,
added
this.times
));

return Promise.resolve();
Expand Down

0 comments on commit da4712c

Please sign in to comment.