Created
June 10, 2019 01:05
-
-
Save Jessidhia/2a91d34b5618948874664c4ed56b3ce8 to your computer and use it in GitHub Desktop.
Trivial webpack plugin / inline function to fail build if something is imported
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
/** | |
* Put in webpack's resolve.plugins[], direct reference. | |
* For example, `resolve: { plugins: [superDeprecate] }`. | |
* | |
*/ | |
function superDeprecate(this: any) { | |
// request is the module name as written in the "import" declaration/expression / "require" call | |
// issuer is the absolute path to the file making the request | |
// I don't remember what path is | |
interface ResolveQuery { | |
context: { | |
issuer: string | |
} | |
path: string | |
request: string | |
} | |
this.hooks.resolve.tapPromise( | |
'super-deprecate', | |
async ({ context: { issuer }, request }: ResolveQuery) => { | |
if (/\bjquery\b/.test(request)) { | |
throw new Error( | |
"Don't import 'jquery' or modules that depend on it." | |
) | |
} | |
if (/\bhandlebars\b/.test(request)) { | |
throw new Error("Don't import 'handlebars' from spa.") | |
} | |
if (/\bhooks[\\/]legacy\b/.test(request)) { | |
throw new Error('Legacy hooks are only allowed in non-spa components.') | |
} | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment