Skip to content

Instantly share code, notes, and snippets.

@MichaReiser
Last active July 3, 2017 11:20
Show Gist options
  • Save MichaReiser/2a96ebf287b4311f4c18 to your computer and use it in GitHub Desktop.
Save MichaReiser/2a96ebf287b4311f4c18 to your computer and use it in GitHub Desktop.
in memory fs usage in webpack
it("does not add translations twice if file is recompiled after change", function (done) {
var testCase = path.join(__dirname, "../fileChange.js");
var fs = new MemoryFS();
fs.mkdirpSync(path.join(__dirname, "../"));
fs.writeFileSync(testCase, "require('./otherFile.js');\n" +
"i18n.registerTranslation('NEW_USER', 'New user');\n" +
"i18n.registerTranslation('DELETE_USER', 'Delete User');\n" +
"i18n.registerTranslation('WillBeDeleted', 'Delete');");
fs.writeFileSync(path.join(__dirname, "../otherFile.js"), "i18n.registerTranslation('DELETE_USER', 'Delete User');");
var options = webpackOptions({
entry: "./fileChange.js"
});
var compiler = webpack(options);
compiler.inputFileSystem = fs;
compiler.resolvers.normal.fileSystem = compiler.inputFileSystem;
compiler.resolvers.context.fileSystem = compiler.inputFileSystem;
compiler.outputFileSystem = fs;
var firstRun = true;
var watching = compiler.watch({}, function (error, stats) {
"use strict";
assert.notOk(error, "Failed to compile the assets");
if (firstRun) {
assert.deepEqual(stats.compilation.errors, [], "First compilation failed with errors");
firstRun = false;
fs.writeFileSync(testCase, "i18n.registerTranslation('NEW_USER', 'Neuer Benutzer');");
watching.invalidate(); // watch doesn't seem to work with memory fs
} else {
assert.deepEqual(stats.compilation.errors, [], "The implementation should not emit duplicate translation errors after recompilation with different text.");
var translations = JSON.parse(fs.readFileSync(path.join(__dirname, "dist/translations.json")));
assert.propertyVal(translations, "NEW_USER", "Neuer Benutzer", "Uses the updated translation");
assert.propertyVal(translations, "DELETE_USER", "Delete User", "Does not delete translations used by other resources");
assert.notProperty(translations, "WillBeDeleted", "The property is not used by fileChange.js anymore, so we should remove it from the translations.json");
watching.close(done);
}
});
});
@perryao
Copy link

perryao commented Aug 5, 2016

Is this still working for you? I'm setting the fs as you are, but it still seems to be trying to resolve modules in memory fs instead of the actual file system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment