Skip to content

Commit

Permalink
Use the before emit hook to check if files exist (#36)
Browse files Browse the repository at this point in the history
* Use the before emit hook to check if files exist

- Add an option to use this hook.
- When this plugin is used with other loaders such as ts-loader and
vue-loader, this option improves the overall build time. In our case,
the build time reduced by 5-10 seconds.
  • Loading branch information
sunilbandla authored and Urthen committed Nov 29, 2018
1 parent c2b6ca1 commit c821025
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions index.js
Expand Up @@ -142,32 +142,63 @@ CaseSensitivePathsPlugin.prototype.apply = function (compiler) {
this.reset();
};

const checkFile = (pathName, data, done) => {
this.fileExistsWithCase(pathName, (realName) => {
if (realName) {
if (realName === '!nonexistent') {
// If file does not exist, let Webpack show a more appropriate error.
done(null, data);
} else {
done(new Error(`[CaseSensitivePathsPlugin] \`${pathName}\` does not match the corresponding path on disk ${realName}`));
}
} else {
done(null, data);
}
});
};

const onAfterResolve = (data, done) => {
this.primeCache(() => {
// Trim ? off, since some loaders add that to the resource they're attemping to load
let pathName = data.resource.split('?')[0];
pathName = pathName.normalize ? pathName.normalize('NFC') : pathName;

this.fileExistsWithCase(pathName, (realName) => {
if (realName) {
if (realName === '!nonexistent') {
// If file does not exist, let Webpack show a more appropriate error.
done(null, data);
} else {
done(new Error(`[CaseSensitivePathsPlugin] \`${pathName}\` does not match the corresponding path on disk ${realName}`));
}
} else {
done(null, data);
}
});
checkFile(pathName, data, done);
});
};

if (compiler.hooks) {
compiler.hooks.done.tap('CaseSensitivePathsPlugin', onDone);
compiler.hooks.normalModuleFactory.tap('CaseSensitivePathsPlugin', (nmf) => {
nmf.hooks.afterResolve.tapAsync('CaseSensitivePathsPlugin', onAfterResolve);
});
if (this.options.useBeforeEmitHook) {
if (this.options.debug) {
console.log('[CaseSensitivePathsPlugin] Using the hook for before emit.');
}
compiler.hooks.emit.tapAsync('CaseSensitivePathsPlugin', (compilation, callback) => {
let resolvedFilesCount = 0;
const errors = [];
this.primeCache(() => {
compilation.fileDependencies.forEach((filename) => {
checkFile(filename, filename, (error) => {
resolvedFilesCount += 1;
if (error) {
errors.push(error);
}
if (resolvedFilesCount === compilation.fileDependencies.size) {
if (errors.length) {
// Send all errors to webpack
compilation.errors.push(...errors);
}
callback();
}
});
});
});
});
} else {
compiler.hooks.normalModuleFactory.tap('CaseSensitivePathsPlugin', (nmf) => {
nmf.hooks.afterResolve.tapAsync('CaseSensitivePathsPlugin', onAfterResolve);
});
}
} else {
compiler.plugin('done', onDone);
compiler.plugin('normal-module-factory', (nmf) => {
Expand Down

0 comments on commit c821025

Please sign in to comment.