Created
February 25, 2022 11:26
-
-
Save kapowaz/d4509ce1beb40b83aad0da491c6017c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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