@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
1 lines • 8.61 kB
Source Map (JSON)
{"version":3,"sources":["../../src/lib/babel/helpers.ts"],"names":["getPluginName","plugin","isSetString","Array","isArray","length","_name","name","undefined","isDuplicatePlugin","plugins","some","existing","filterPluginByRuntimeId","context","runtimeId","sourceFile","vfs","isMatchingRuntimeId","id","addPluginFilter","pluginOrPlugins","filter","isFunction","isObject","Error","foundIndex","findIndex","toLowerCase"],"mappings":";;;;;;;AAmCO,SAASA,cAAcC,MAAAA,EAAuB;AACnD,EAAA,OAAOC,uBAAAA,CAAYD,MAAAA,CAAAA,GACfA,MAAAA,GACAE,MAAMC,OAAAA,CAAQH,MAAAA,CAAAA,IAAWA,MAAAA,CAAOI,MAAAA,GAAS,CAAA,GACvCL,cAAcC,MAAAA,CAAO,CAAA,CAAE,CAAA,GACtBA,MAAAA,CAAuBK,KAAAA,IAAUL,OAAuBM,IAAAA,GACtDN,MAAAA,CAAuBK,KAAAA,IAAUL,MAAAA,CAAuBM,IAAAA,GACzDC,MAAAA;AACV;AARgBR,wBAAAA,CAAAA,aAAAA,EAAAA,eAAAA,CAAAA;AAiBT,SAASS,iBAAAA,CACdC,SACAT,MAAAA,EAAuB;AAEvB,EAAA,OAAO,CAAC,EACND,aAAAA,CAAcC,MAAAA,CAAAA,IACdS,QAAQC,IAAAA,CACNC,CAAAA,QAAAA,KAAYZ,aAAAA,CAAcY,SAAS,CAAA,CAAE,CAAA,KAAMZ,aAAAA,CAAcC,MAAAA,CAAAA,CAAAA,CAAAA;AAG/D;AAVgBQ,wBAAAA,CAAAA,iBAAAA,EAAAA,mBAAAA,CAAAA;AAYT,SAASI,uBAAAA,CAAwBC,SAAkBC,SAAAA,EAAiB;AACzE,EAAA,OAAOC,CAAAA,eACL,CAACF,OAAAA,CAAQG,IAAIC,mBAAAA,CAAoBH,SAAAA,EAAWC,WAAWG,EAAE,CAAA;AAC7D;AAHgBN,wBAAAA,CAAAA,uBAAAA,E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;AAER,EAAA;AAEA,EAAA,IAAI,CAACf,IAAAA,EAAM;AACT,IAAA,MAAM,IAAIkB,MACR,oFAAA,CAAA;AAEJ,EAAA;AAEA,EAAA,MAAMC,UAAAA,GAAaL,eAAAA,CAAgBM,SAAAA,CACjC1B,CAAAA,MAAAA,KAAUD,aAAAA,CAAcC,MAAAA,CAAAA,EAAS2B,WAAAA,EAAAA,KAAkBrB,IAAAA,CAAKqB,WAAAA,EAAW,CAAA;AAErE,EAAA,IAAIF,aAAa,EAAA,EAAI;AACnBL,IAAAA,eAAAA,CAAgBK,UAAAA,IAAcN,eAAAA,CAC5BN,OAAAA,EACAO,gBAAgBK,UAAAA,CAAAA,EAChBJ,QACAf,IAAAA,CAAAA;AAEJ,EAAA;AAEA,EAAA,OAAOc,eAAAA;AACT;AA3DgBD,wBAAAA,CAAAA,eAAAA,EAAAA,iBAAAA,CAAAA","file":"chunk-23BVYW67.cjs","sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Storm Stack\n\n This code was released as part of the Storm Stack project. Storm Stack\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/storm-stack.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/storm-stack\n Documentation: https://docs.stormsoftware.com/projects/storm-stack\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { isFunction } from \"@stryke/type-checks/is-function\";\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isSetString } from \"@stryke/type-checks/is-set-string\";\nimport {\n BabelPlugin,\n BabelPluginItem,\n BabelPluginTarget\n} from \"../../types/babel\";\nimport { CompilerOptions } from \"../../types/compiler\";\nimport { Context } from \"../../types/context\";\n\n/**\n * Get the name of the Babel plugin.\n *\n * @param plugin - The Babel plugin to get the name from.\n * @returns The name of the Babel plugin, or undefined if not found.\n */\nexport function getPluginName(plugin: BabelPluginItem): string | undefined {\n return isSetString(plugin)\n ? plugin\n : Array.isArray(plugin) && plugin.length > 0\n ? getPluginName(plugin[0])\n : (plugin as BabelPlugin)._name || (plugin as BabelPlugin).name\n ? (plugin as BabelPlugin)._name || (plugin as BabelPlugin).name\n : undefined;\n}\n\n/**\n * Check if a Babel plugin is a duplicate of another plugin in the list.\n *\n * @param plugins - The list of existing Babel plugins.\n * @param plugin - The Babel plugin to check for duplicates.\n * @returns True if the plugin is a duplicate, false otherwise.\n */\nexport function isDuplicatePlugin(\n plugins: BabelPluginItem[],\n plugin: BabelPluginItem\n): boolean {\n return !!(\n getPluginName(plugin) &&\n plugins.some(\n existing => getPluginName(existing[0]) === getPluginName(plugin)\n )\n );\n}\n\nexport function filterPluginByRuntimeId(context: Context, runtimeId: string) {\n return sourceFile =>\n !context.vfs.isMatchingRuntimeId(runtimeId, sourceFile.id);\n}\n\n/**\n * Adds a filter to a Babel plugin or a list of Babel plugins.\n *\n * @param context - The context in which the plugin is being added.\n * @param plugins - The Babel plugins to add the filter to.\n * @param filter - The filter function to apply to the plugins.\n * @param name - The name of the plugin to add the filter to.\n * @returns The updated list of Babel plugins with the filter applied.\n */\nexport function addPluginFilter(\n context: Context,\n plugins: BabelPluginItem[],\n filter: NonNullable<CompilerOptions[\"filter\"]>,\n name: string\n): BabelPluginItem[];\n\n/**\n * Adds a filter to a Babel plugin or a list of Babel plugins.\n *\n * @param context - The context in which the plugin is being added.\n * @param plugin - The Babel plugin to add the filter to.\n * @param filter - The filter function to apply to the plugin.\n * @returns The updated Babel plugin with the filter applied.\n */\nexport function addPluginFilter(\n context: Context,\n plugin: BabelPluginTarget | BabelPluginItem,\n filter: NonNullable<CompilerOptions[\"filter\"]>\n): BabelPluginItem;\n/**\n * Adds a filter to a Babel plugin or a list of Babel plugins.\n *\n * @param context - The context in which the plugin is being added.\n * @param pluginOrPlugins - The Babel plugin or plugins to add the filter to.\n * @param filter - The filter function to apply to the plugins.\n * @param name - The name of the plugin to add the filter to.\n * @returns The updated list of Babel plugins with the filter applied.\n */\nexport function addPluginFilter<\n T extends BabelPluginTarget | BabelPluginItem | BabelPluginItem[]\n>(\n context: Context,\n pluginOrPlugins: T,\n filter: NonNullable<CompilerOptions[\"filter\"]>,\n name?: string\n): T extends BabelPluginItem[] ? BabelPluginItem[] : BabelPluginItem {\n if (\n !Array.isArray(pluginOrPlugins) ||\n (!pluginOrPlugins.some(plugin => Array.isArray(plugin)) &&\n pluginOrPlugins.length < 4 &&\n pluginOrPlugins.length > 0 &&\n (isSetString(pluginOrPlugins[0]) ||\n isFunction(pluginOrPlugins[0]) ||\n (pluginOrPlugins.length > 1 && isObject(pluginOrPlugins[1])) ||\n (pluginOrPlugins.length > 2 && isObject(pluginOrPlugins[2]))))\n ) {\n return Array.isArray(pluginOrPlugins)\n ? [\n pluginOrPlugins[0],\n pluginOrPlugins.length > 1 ? pluginOrPlugins[1] : {},\n {\n filter: sourceFile =>\n filter(sourceFile) &&\n (pluginOrPlugins.length < 2 ||\n !isFunction(pluginOrPlugins[2]?.filter) ||\n // eslint-disable-next-line ts/no-unsafe-call\n pluginOrPlugins[2]?.filter?.(sourceFile))\n }\n ]\n : [\n pluginOrPlugins,\n {},\n {\n filter\n }\n ];\n }\n\n if (!name) {\n throw new Error(\n \"No name was provided to \\`addPluginFilter\\`, could not find babel plugin without it.\"\n );\n }\n\n const foundIndex = pluginOrPlugins.findIndex(\n plugin => getPluginName(plugin)?.toLowerCase() === name.toLowerCase()\n );\n if (foundIndex > -1) {\n pluginOrPlugins[foundIndex] = addPluginFilter(\n context,\n pluginOrPlugins[foundIndex],\n filter,\n name\n );\n }\n\n return pluginOrPlugins;\n}\n"]}