Skip to content

Instantly share code, notes, and snippets.

@Jessidhia
Created June 10, 2019 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jessidhia/2a91d34b5618948874664c4ed56b3ce8 to your computer and use it in GitHub Desktop.
Save Jessidhia/2a91d34b5618948874664c4ed56b3ce8 to your computer and use it in GitHub Desktop.
Trivial webpack plugin / inline function to fail build if something is imported
/**
* 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