Skip to content

Commit

Permalink
fix: Only emit files if contents changed (#7)
Browse files Browse the repository at this point in the history
fix #5
  • Loading branch information
mattcompiles authored and michaeltaranto committed Jan 10, 2019
1 parent 073b1d4 commit 90f3131
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const getTypeMismatchError = ({ filename, expected, actual }) => {
return new Error(
`Generated type declaration file is outdated. Run webpack and commit the updated type declaration for '${filename}'\n\n${diff}`
);
}
};

const cssModuleToNamedExports = cssModuleKeys => {
return cssModuleKeys
Expand All @@ -35,8 +35,21 @@ const filenameToTypingsFilename = filename => {

const validModes = ['emit', 'verify'];

const isFileNotFound = err => err && err.code === 'ENOENT';

const makeDoneHandlers = (callback, content, rest) => ({
failed: e => callback(e),
success: () => callback(null, content, ...rest)
});

const makeFileHandlers = filename => ({
read: handler => fs.readFile(filename, { encoding: 'utf-8' }, handler),
write: (content, handler) =>
fs.writeFile(filename, content, { encoding: 'utf-8' }, handler)
});

module.exports = function(content, ...rest) {
const callback = this.async();
const { failed, success } = makeDoneHandlers(this.async(), content, rest);

const filename = this.resourcePath;
const { mode = 'emit' } = loaderUtils.getOptions(this) || {};
Expand All @@ -45,6 +58,7 @@ module.exports = function(content, ...rest) {
}

const cssModuleInterfaceFilename = filenameToTypingsFilename(filename);
const { read, write } = makeFileHandlers(cssModuleInterfaceFilename);

const keyRegex = /"([^\\"]+)":/g;
let match;
Expand All @@ -61,43 +75,44 @@ module.exports = function(content, ...rest) {
)}`;

if (mode === 'verify') {
fs.readFile(
cssModuleInterfaceFilename,
{ encoding: 'utf-8' },
(err, fileContents) => {
if (err) {
const error =
err.code === 'ENOENT'
? getNoDeclarationFileError({
filename: cssModuleInterfaceFilename
})
: err;
return callback(error);
}

if (cssModuleDefinition !== fileContents) {
return callback(getTypeMismatchError({
read((err, fileContents) => {
if (isFileNotFound(err)) {
return failed(
getNoDeclarationFileError({
filename: cssModuleInterfaceFilename
})
);
}

if (err) {
return failed(err);
}

if (cssModuleDefinition !== fileContents) {
return failed(
getTypeMismatchError({
filename: cssModuleInterfaceFilename,
expected: cssModuleDefinition,
actual: fileContents
}));
}

return callback(null, content, ...rest);
})
);
}
);

return success();
});
} else {
fs.writeFile(
cssModuleInterfaceFilename,
cssModuleDefinition,
{ encoding: 'utf-8' },
err => {
if (err) {
return callback(err);
} else {
return callback(null, content, ...rest);
}
read((_, fileContents) => {
if (cssModuleDefinition !== fileContents) {
write(cssModuleDefinition, err => {
if (err) {
failed(err);
} else {
success();
}
});
} else {
success();
}
);
});
}
};

0 comments on commit 90f3131

Please sign in to comment.