Reskindex to a temp file, to confuse the babel watcher less (#908)
I'm seeing a lot of instances where the babel watcher picks up a half-written src/component-index, and generates an empty lib file - which it then doesn't update when src/component-index is updated. Empirically, this seems to make it better.
This commit is contained in:
parent
19d727ab88
commit
4df51a2b0b
1 changed files with 19 additions and 8 deletions
|
@ -6,6 +6,7 @@ var args = require('optimist').argv;
|
||||||
var chokidar = require('chokidar');
|
var chokidar = require('chokidar');
|
||||||
|
|
||||||
var componentIndex = path.join('src', 'component-index.js');
|
var componentIndex = path.join('src', 'component-index.js');
|
||||||
|
var componentIndexTmp = componentIndex+".tmp";
|
||||||
var componentsDir = path.join('src', 'components');
|
var componentsDir = path.join('src', 'components');
|
||||||
var componentGlob = '**/*.js';
|
var componentGlob = '**/*.js';
|
||||||
var prevFiles = [];
|
var prevFiles = [];
|
||||||
|
@ -20,7 +21,7 @@ function reskindex() {
|
||||||
var header = args.h || args.header;
|
var header = args.h || args.header;
|
||||||
var packageJson = JSON.parse(fs.readFileSync('./package.json'));
|
var packageJson = JSON.parse(fs.readFileSync('./package.json'));
|
||||||
|
|
||||||
var strm = fs.createWriteStream(componentIndex);
|
var strm = fs.createWriteStream(componentIndexTmp);
|
||||||
|
|
||||||
if (header) {
|
if (header) {
|
||||||
strm.write(fs.readFileSync(header));
|
strm.write(fs.readFileSync(header));
|
||||||
|
@ -35,13 +36,16 @@ function reskindex() {
|
||||||
strm.write(" */\n\n");
|
strm.write(" */\n\n");
|
||||||
|
|
||||||
if (packageJson['matrix-react-parent']) {
|
if (packageJson['matrix-react-parent']) {
|
||||||
|
const parentIndex = packageJson['matrix-react-parent'] +
|
||||||
|
'/lib/component-index';
|
||||||
strm.write(
|
strm.write(
|
||||||
"module.exports.components = require('"+
|
`let components = require('${parentIndex}').components;
|
||||||
packageJson['matrix-react-parent']+
|
if (!components) {
|
||||||
"/lib/component-index').components;\n\n"
|
throw new Error("'${parentIndex}' didn't export components");
|
||||||
);
|
}
|
||||||
|
`);
|
||||||
} else {
|
} else {
|
||||||
strm.write("module.exports.components = {};\n");
|
strm.write("let components = {};\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < files.length; ++i) {
|
for (var i = 0; i < files.length; ++i) {
|
||||||
|
@ -51,14 +55,21 @@ function reskindex() {
|
||||||
var importName = moduleName.replace(/\./g, "$");
|
var importName = moduleName.replace(/\./g, "$");
|
||||||
|
|
||||||
strm.write("import " + importName + " from './components/" + file + "';\n");
|
strm.write("import " + importName + " from './components/" + file + "';\n");
|
||||||
strm.write(importName + " && (module.exports.components['"+moduleName+"'] = " + importName + ");");
|
strm.write(importName + " && (components['"+moduleName+"'] = " + importName + ");");
|
||||||
strm.write('\n');
|
strm.write('\n');
|
||||||
strm.uncork();
|
strm.uncork();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strm.write("export {components};\n");
|
||||||
strm.end();
|
strm.end();
|
||||||
|
fs.rename(componentIndexTmp, componentIndex, function(err) {
|
||||||
|
if(err) {
|
||||||
|
console.error("Error moving new index into place: " + err);
|
||||||
|
} else {
|
||||||
console.log('Reskindex: completed');
|
console.log('Reskindex: completed');
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Expects both arrays of file names to be sorted
|
// Expects both arrays of file names to be sorted
|
||||||
function filesHaveChanged(files, prevFiles) {
|
function filesHaveChanged(files, prevFiles) {
|
||||||
|
|
Loading…
Reference in a new issue