Skip to content

Instantly share code, notes, and snippets.

@kapowaz
Created February 25, 2022 11:26
Show Gist options
  • Save kapowaz/d4509ce1beb40b83aad0da491c6017c7 to your computer and use it in GitHub Desktop.
Save kapowaz/d4509ce1beb40b83aad0da491c6017c7 to your computer and use it in GitHub Desktop.
// IgnoreNotFoundExportPlugin.js
// see https://github.com/TypeStrong/ts-loader/issues/653 for details on why this is needed
const ModuleDependencyWarning = require('webpack/lib/ModuleDependencyWarning');
// ↓ Based on https://github.com/sindresorhus/escape-string-regexp
const escapeStringForRegExp = (string) => string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
module.exports = class IgnoreNotFoundExportPlugin {
constructor(exportsToIgnore) {
this.exportsToIgnore = exportsToIgnore || [];
}
getMessageRegExp() {
if (this.exportsToIgnore.length > 0) {
const exportsPattern = '(' + this.exportsToIgnore.map(escapeStringForRegExp).join('|') + ')';
return new RegExp(`export '${this.exportsToIgnore}'( \\(reexported as '.*'\\))? was not found in`);
} else {
return /export '.*'( \(reexported as '.*'\))? was not found in/;
}
}
apply(compiler) {
const messageRegExp = this.getMessageRegExp();
const doneHook = (stats) => {
stats.compilation.warnings = stats.compilation.warnings.filter((warn) => {
if (warn instanceof ModuleDependencyWarning && messageRegExp.test(warn.message)) {
return false;
}
return true;
});
};
if (compiler.hooks) {
compiler.hooks.done.tap('IgnoreNotFoundExportPlugin', doneHook);
} else {
compiler.plugin('done', doneHook);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment