Created
August 10, 2021 19:39
-
-
Save ctjlewis/ac53e932d337be5135168d00b60ac504 to your computer and use it in GitHub Desktop.
Webpack shebang loader.
This file contains hidden or 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
/** | |
* Matches a string which begins with #!, followed by one or more non-return | |
* characters, followed by any number of return characters. | |
* | |
* This will match a Unix-style shebang and all following newlines. | |
*/ | |
const shebangPattern = /^#![^\n\r]+[\r\n]*/ | |
/** | |
* A Webpack loader which removes the first-line Unix-style shebang if it | |
* exists. | |
* | |
* @param source The source code to load. | |
* @returns The replaced source. | |
*/ | |
function removeShebang(source: string) { | |
/** @ts-ignore */ | |
this?.cacheable && this.cacheable() | |
const isString = typeof source === 'string' | |
const hasShebang = source.startsWith('#!') | |
if (isString && hasShebang) { | |
return source.replace(shebangPattern, '') | |
} | |
return source | |
} | |
export default removeShebang |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment