@powerlines/plugin-babel 0.13.119 → 0.13.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"filters.mjs","names":[],"sources":["../../src/helpers/filters.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\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 { Context } from \"powerlines\";\nimport {\n BabelTransformPlugin,\n BabelTransformPluginFilter,\n BabelTransformPluginOptions,\n BabelTransformPresetOptions,\n ResolvedBabelTransformPluginOptions,\n ResolvedBabelTransformPresetOptions\n} from \"../types/config\";\n\nexport function getPluginName(\n plugin?:\n | BabelTransformPluginOptions\n | string\n | false\n | object\n | ((...args: any[]) => any)\n | undefined\n): string | undefined {\n if (isSetString(plugin)) {\n return plugin;\n }\n\n if (Array.isArray(plugin) && plugin.length > 0) {\n return getPluginName(plugin[0]);\n }\n\n if (!plugin) {\n return undefined;\n }\n\n return (\n (plugin as BabelTransformPlugin).$$name ||\n (plugin as BabelTransformPlugin).name ||\n undefined\n );\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 includesPlugin<\n T extends BabelTransformPluginOptions | ResolvedBabelTransformPluginOptions\n>(plugins: T[], plugin: T): boolean {\n return !!(\n getPluginName(plugin) &&\n plugins.some(\n existing =>\n Array.isArray(existing) &&\n getPluginName(existing[0]) === getPluginName(plugin)\n )\n );\n}\n\n/**\n * Check if a Babel preset is a duplicate of another preset in the list.\n *\n * @param presets - The list of existing Babel presets.\n * @param preset - The Babel preset to check for duplicates.\n * @returns True if the preset is a duplicate, false otherwise.\n */\nexport function includesPreset<\n T extends BabelTransformPresetOptions | ResolvedBabelTransformPresetOptions\n>(presets: T[], preset: T): boolean {\n return !!(\n getPluginName(preset) &&\n presets.some(\n existing =>\n Array.isArray(existing) &&\n getPluginName(existing[0]) === getPluginName(preset)\n )\n );\n}\n\n/**\n * Filters a Babel plugin by its runtime ID.\n *\n * @param context - The context in which the filter is applied.\n * @param fileId - The file ID to filter by.\n * @returns A filter function that checks if a plugin's ID matches the runtime ID.\n */\nexport function filterPluginByFileId<TContext extends Context = Context>(\n context: TContext,\n fileId: string\n): BabelTransformPluginFilter {\n return (code: string, id: string): boolean =>\n fileId !== id &&\n context.fs.ids[fileId] !== id &&\n fileId !== context.fs.ids[id] &&\n context.fs.ids[fileId] !== context.fs.ids[id] &&\n context.fs.paths[fileId] !== id &&\n fileId !== context.fs.paths[id] &&\n context.fs.paths[fileId] !== context.fs.paths[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: BabelTransformPluginOptions[],\n filter: BabelTransformPluginFilter | null | undefined,\n name: string\n): BabelTransformPluginOptions[];\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: BabelTransformPlugin | BabelTransformPluginOptions,\n filter: NonNullable<BabelTransformPluginFilter>\n): BabelTransformPluginOptions;\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 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\n | BabelTransformPlugin\n | BabelTransformPluginOptions\n | BabelTransformPluginOptions[]\n>(\n context: Context,\n pluginOrPlugins: T,\n filter: NonNullable<BabelTransformPluginFilter>,\n name?: string\n): T extends BabelTransformPluginOptions[]\n ? BabelTransformPluginOptions[]\n : BabelTransformPluginOptions {\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 if (Array.isArray(pluginOrPlugins)) {\n return [\n pluginOrPlugins[0],\n pluginOrPlugins.length > 1 ? pluginOrPlugins[1] : {},\n {\n filter: (code, id) =>\n filter(code, id) &&\n (pluginOrPlugins.length < 3 ||\n !isFunction(pluginOrPlugins[2]) ||\n (pluginOrPlugins[2] as BabelTransformPluginFilter)?.(code, id))\n }\n ] as any;\n }\n return [\n pluginOrPlugins,\n {},\n {\n filter\n }\n ] as any;\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] as BabelTransformPluginOptions[],\n filter,\n name\n );\n }\n\n return pluginOrPlugins as any;\n}\n"],"mappings":";;;;;AA+BA,SAAgB,cACd,QAOoB;CACpB,IAAI,YAAY,MAAM,GACpB,OAAO;CAGT,IAAI,MAAM,QAAQ,MAAM,KAAK,OAAO,SAAS,GAC3C,OAAO,cAAc,OAAO,EAAE;CAGhC,IAAI,CAAC,QACH;CAGF,OACG,OAAgC,UAChC,OAAgC,QACjC;AAEJ;;;;;;;;AASA,SAAgB,eAEd,SAAc,QAAoB;CAClC,OAAO,CAAC,EACN,cAAc,MAAM,KACpB,QAAQ,MACN,aACE,MAAM,QAAQ,QAAQ,KACtB,cAAc,SAAS,EAAE,MAAM,cAAc,MAAM,CACvD;AAEJ;;;;;;;;AASA,SAAgB,eAEd,SAAc,QAAoB;CAClC,OAAO,CAAC,EACN,cAAc,MAAM,KACpB,QAAQ,MACN,aACE,MAAM,QAAQ,QAAQ,KACtB,cAAc,SAAS,EAAE,MAAM,cAAc,MAAM,CACvD;AAEJ;;;;;;;;AASA,SAAgB,qBACd,SACA,QAC4B;CAC5B,QAAQ,MAAc,OACpB,WAAW,MACX,QAAQ,GAAG,IAAI,YAAY,MAC3B,WAAW,QAAQ,GAAG,IAAI,OAC1B,QAAQ,GAAG,IAAI,YAAY,QAAQ,GAAG,IAAI,OAC1C,QAAQ,GAAG,MAAM,YAAY,MAC7B,WAAW,QAAQ,GAAG,MAAM,OAC5B,QAAQ,GAAG,MAAM,YAAY,QAAQ,GAAG,MAAM;AAClD;;;;;;;;;;AAyCA,SAAgB,gBAMd,SACA,iBACA,QACA,MAG8B;CAC9B,IACE,CAAC,MAAM,QAAQ,eAAe,KAC7B,CAAC,gBAAgB,MAAK,WAAU,MAAM,QAAQ,MAAM,CAAC,KACpD,gBAAgB,SAAS,KACzB,gBAAgB,SAAS,MACxB,YAAY,gBAAgB,EAAE,KAC7B,WAAW,gBAAgB,EAAE,KAC5B,gBAAgB,SAAS,KAAK,SAAS,gBAAgB,EAAE,KACzD,gBAAgB,SAAS,KAAK,SAAS,gBAAgB,EAAE,IAC9D;EACA,IAAI,MAAM,QAAQ,eAAe,GAC/B,OAAO;GACL,gBAAgB;GAChB,gBAAgB,SAAS,IAAI,gBAAgB,KAAK,CAAC;GACnD,EACE,SAAS,MAAM,OACb,OAAO,MAAM,EAAE,MACd,gBAAgB,SAAS,KACxB,CAAC,WAAW,gBAAgB,EAAE,KAC7B,gBAAgB,KAAoC,MAAM,EAAE,GACnE;EACF;EAEF,OAAO;GACL;GACA,CAAC;GACD,EACE,OACF;EACF;CACF;CAEA,IAAI,CAAC,MACH,MAAM,IAAI,MACR,oFACF;CAGF,MAAM,aAAa,gBAAgB,WACjC,WAAU,cAAc,MAAM,GAAG,YAAY,MAAM,KAAK,YAAY,CACtE;CACA,IAAI,aAAa,IACf,gBAAgB,cAAc,gBAC5B,SACA,gBAAgB,aAChB,QACA,IACF;CAGF,OAAO;AACT"}
1
+ {"version":3,"file":"filters.mjs","names":[],"sources":["../../src/helpers/filters.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\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 { Context } from \"powerlines\";\nimport {\n BabelTransformPlugin,\n BabelTransformPluginFilter,\n BabelTransformPluginOptions,\n BabelTransformPresetOptions,\n ResolvedBabelTransformPluginOptions,\n ResolvedBabelTransformPresetOptions\n} from \"../types/config\";\n\nexport function getPluginName(\n plugin?:\n | BabelTransformPluginOptions\n | string\n | false\n | object\n | ((...args: any[]) => any)\n | undefined\n): string | undefined {\n if (isSetString(plugin)) {\n return plugin;\n }\n\n if (Array.isArray(plugin) && plugin.length > 0) {\n return getPluginName(plugin[0]);\n }\n\n if (!plugin) {\n return undefined;\n }\n\n return (\n (plugin as BabelTransformPlugin).$$name ||\n (plugin as BabelTransformPlugin).name ||\n undefined\n );\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 includesPlugin<\n T extends BabelTransformPluginOptions | ResolvedBabelTransformPluginOptions\n>(plugins: T[], plugin: T): boolean {\n return !!(\n getPluginName(plugin) &&\n plugins.some(\n existing =>\n Array.isArray(existing) &&\n getPluginName(existing[0]) === getPluginName(plugin)\n )\n );\n}\n\n/**\n * Check if a Babel preset is a duplicate of another preset in the list.\n *\n * @param presets - The list of existing Babel presets.\n * @param preset - The Babel preset to check for duplicates.\n * @returns True if the preset is a duplicate, false otherwise.\n */\nexport function includesPreset<\n T extends BabelTransformPresetOptions | ResolvedBabelTransformPresetOptions\n>(presets: T[], preset: T): boolean {\n return !!(\n getPluginName(preset) &&\n presets.some(\n existing =>\n Array.isArray(existing) &&\n getPluginName(existing[0]) === getPluginName(preset)\n )\n );\n}\n\n/**\n * Filters a Babel plugin by its runtime ID.\n *\n * @param context - The context in which the filter is applied.\n * @param fileId - The file ID to filter by.\n * @returns A filter function that checks if a plugin's ID matches the runtime ID.\n */\nexport function filterPluginByFileId<TContext extends Context = Context>(\n context: TContext,\n fileId: string\n): BabelTransformPluginFilter {\n return (code: string, id: string): boolean =>\n fileId !== id &&\n context.fs.ids[fileId] !== id &&\n fileId !== context.fs.ids[id] &&\n context.fs.ids[fileId] !== context.fs.ids[id] &&\n context.fs.paths[fileId] !== id &&\n fileId !== context.fs.paths[id] &&\n context.fs.paths[fileId] !== context.fs.paths[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: BabelTransformPluginOptions[],\n filter: BabelTransformPluginFilter | null | undefined,\n name: string\n): BabelTransformPluginOptions[];\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: BabelTransformPlugin | BabelTransformPluginOptions,\n filter: NonNullable<BabelTransformPluginFilter>\n): BabelTransformPluginOptions;\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 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\n | BabelTransformPlugin\n | BabelTransformPluginOptions\n | BabelTransformPluginOptions[]\n>(\n context: Context,\n pluginOrPlugins: T,\n filter: NonNullable<BabelTransformPluginFilter>,\n name?: string\n): T extends BabelTransformPluginOptions[]\n ? BabelTransformPluginOptions[]\n : BabelTransformPluginOptions {\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 if (Array.isArray(pluginOrPlugins)) {\n return [\n pluginOrPlugins[0],\n pluginOrPlugins.length > 1 ? pluginOrPlugins[1] : {},\n {\n filter: (code, id) =>\n filter(code, id) &&\n (pluginOrPlugins.length < 3 ||\n !isFunction(pluginOrPlugins[2]) ||\n (pluginOrPlugins[2] as BabelTransformPluginFilter)?.(code, id))\n }\n ] as any;\n }\n return [\n pluginOrPlugins,\n {},\n {\n filter\n }\n ] as any;\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] as BabelTransformPluginOptions[],\n filter,\n name\n );\n }\n\n return pluginOrPlugins as any;\n}\n"],"mappings":";;;;;AA+BA,SAAgB,cACd,QAOoB;CACpB,IAAI,YAAY,MAAM,GACpB,OAAO;CAGT,IAAI,MAAM,QAAQ,MAAM,KAAK,OAAO,SAAS,GAC3C,OAAO,cAAc,OAAO,EAAE;CAGhC,IAAI,CAAC,QACH;CAGF,OACG,OAAgC,UAChC,OAAgC,QACjC;AAEJ;;;;;;;;AASA,SAAgB,eAEd,SAAc,QAAoB;CAClC,OAAO,CAAC,EACN,cAAc,MAAM,KACpB,QAAQ,MACN,aACE,MAAM,QAAQ,QAAQ,KACtB,cAAc,SAAS,EAAE,MAAM,cAAc,MAAM,CACvD;AAEJ;;;;;;;;AASA,SAAgB,eAEd,SAAc,QAAoB;CAClC,OAAO,CAAC,EACN,cAAc,MAAM,KACpB,QAAQ,MACN,aACE,MAAM,QAAQ,QAAQ,KACtB,cAAc,SAAS,EAAE,MAAM,cAAc,MAAM,CACvD;AAEJ;;;;;;;;AASA,SAAgB,qBACd,SACA,QA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],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { NodePath } from \"@babel/core\";\nimport { ParseResult } from \"@babel/parser\";\nimport * as t from \"@babel/types\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { ImportSpecifier } from \"../types/config\";\nimport { parseAst } from \"./ast-utils\";\n\n/**\n * Finds an export in the given Babel AST program by its key.\n *\n * @param ast - The parsed Babel AST result containing the program body.\n * @param key - The name of the export to find (e.g., \"default\" or a named export).\n * @returns The declaration of the export if found, otherwise undefined.\n */\nexport function findExport(ast: ParseResult<t.File>, key: string) {\n const type =\n key === \"default\" ? \"ExportDefaultDeclaration\" : \"ExportNamedDeclaration\";\n\n for (const node of ast.program.body) {\n if (node.type === type) {\n if (key === \"default\") {\n return node.declaration;\n }\n if (node.declaration && \"declarations\" in node.declaration) {\n const declaration = node.declaration.declarations[0];\n if (\n declaration &&\n \"name\" in declaration.id &&\n declaration.id.name === key\n ) {\n return declaration.init as any;\n }\n }\n }\n }\n}\n\n/**\n * Lists all exports from the given Babel AST program.\n *\n * @param codeOrAst - The parsed Babel AST result containing the program body.\n * @returns An array of export names, including \"default\" for default exports.\n */\nexport function listExports(codeOrAst: ParseResult<t.File> | string) {\n const ast = isString(codeOrAst) ? parseAst(codeOrAst) : codeOrAst;\n\n return ast.program.body\n .flatMap(i => {\n if (i.type === \"ExportDefaultDeclaration\") {\n return [\"default\"];\n }\n if (\n i.type === \"ExportNamedDeclaration\" &&\n i.declaration &&\n \"declarations\" in i.declaration\n ) {\n return i.declaration.declarations.map(d =>\n \"name\" in d.id ? d.id.name : \"\"\n );\n }\n return [];\n })\n .filter(Boolean);\n}\n\n/**\n * Lists all imports from the given Babel AST program.\n *\n * @param ast - The parsed Babel AST result containing the program body.\n * @returns An array of import names, including \"default\" for default imports.\n */\nexport function listImports(ast: ParseResult<t.File> | t.File) {\n return ast.program.body\n .flatMap(i => {\n if (i.type === \"ImportDeclaration\") {\n return i.specifiers.map(s => {\n if (s.type === \"ImportDefaultSpecifier\") {\n return \"default\";\n }\n if (s.type === \"ImportSpecifier\" && \"imported\" in s) {\n return s.imported.type === \"Identifier\"\n ? s.imported.name\n : s.imported.value;\n }\n return \"\";\n });\n }\n\n return [];\n })\n .filter(Boolean);\n}\n\nexport function isImportCall(\n calleePath: NodePath<t.CallExpression | t.NewExpression>\n) {\n return t.isImport(calleePath.node.callee);\n}\n\n/**\n * Gets the import declaration for a given name and specifier.\n *\n * @param specifier - The specifier of the import.\n * @param name - The name of the import.\n * @param named - Optional named import.\n * @returns The import declaration.\n */\nexport function getImport(\n specifier: string,\n name: string,\n named?: string\n): t.ImportDeclaration {\n return t.importDeclaration(\n [t.importSpecifier(t.identifier(name), t.stringLiteral(named || name))],\n t.stringLiteral(specifier)\n );\n}\n\n/**\n * Adds an import to the program if it doesn't already exist.\n *\n * @param path - The current NodePath in the AST.\n * @param specifier - The import specifier.\n */\nexport function addImport(path: NodePath<any>, specifier: ImportSpecifier) {\n addImportsToProgram(\n path.scope.getProgramParent().path as NodePath<t.Program>,\n specifier\n );\n}\n\n/*\n * Matches `import { ... } from <moduleName>;`\n * but not `import * as React from <moduleName>;`\n * `import type { Foo } from <moduleName>;`\n */\nfunction isNonNamespacedImport(\n importDeclPath: NodePath<t.ImportDeclaration>\n): boolean {\n return (\n importDeclPath\n .get(\"specifiers\")\n .filter(Boolean)\n .every(specifier => specifier?.isImportSpecifier()) &&\n importDeclPath.node.importKind !== \"type\" &&\n importDeclPath.node.importKind !== \"typeof\"\n );\n}\n\nfunction getExistingImports(\n program: NodePath<t.Program>\n): Map<string, NodePath<t.ImportDeclaration>> {\n const existingImports = new Map<string, NodePath<t.ImportDeclaration>>();\n program.traverse({\n ImportDeclaration(path) {\n if (isNonNamespacedImport(path)) {\n existingImports.set(path.node.source.value, path);\n }\n }\n });\n return existingImports;\n}\n\nexport function addImportsToProgram(\n path: NodePath<t.Program>,\n specifier: ImportSpecifier\n): void {\n const existingImports = getExistingImports(path);\n\n /**\n * If an existing import of this module exists (ie \\`import \\{ ... \\} from\n * '<moduleName>'\\`), inject new imported specifiers into the list of\n * destructured variables.\n */\n if (!existingImports.get(specifier.module)) {\n path.unshiftContainer(\n \"body\",\n t.importDeclaration(\n [\n t.importSpecifier(\n t.identifier(specifier.name || specifier.imported),\n t.identifier(specifier.imported)\n )\n ],\n t.stringLiteral(specifier.module)\n )\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;AAgCA,SAAgB,WAAW,KAA0B,KAAa;CAChE,MAAM,OACJ,QAAQ,YAAY,6BAA6B;CAEnD,KAAK,MAAM,QAAQ,IAAI,QAAQ,MAC7B,IAAI,KAAK,SAAS,MAAM;EACtB,IAAI,QAAQ,WACV,OAAO,KAAK;EAEd,IAAI,KAAK,eAAe,kBAAkB,KAAK,aAAa;GAC1D,MAAM,cAAc,KAAK,YAAY,aAAa;GAClD,IACE,eACA,UAAU,YAAY,MACtB,YAAY,GAAG,SAAS,KAExB,OAAO,YAAY;EAEvB;CACF;AAEJ;;;;;;;AAQA,SAAgB,YAAY,WAAyC;CAGnE,QAFY,SAAS,SAAS,IAAI,SAAS,SAAS,IAAI,WAE7C,QAAQ,KAChB,SAAQ,MAAK;EACZ,IAAI,EAAE,SAAS,4BACb,OAAO,CAAC,SAAS;EAEnB,IACE,EAAE,SAAS,4BACX,EAAE,eACF,kBAAkB,EAAE,aAEpB,OAAO,EAAE,YAAY,aAAa,KAAI,MACpC,UAAU,EAAE,KAAK,EAAE,GAAG,OAAO,EAC/B;EAEF,OAAO,CAAC;CACV,CAAC,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E,EAAE,gBACA,EAAE,WAAW,UAAU,QAAQ,UAAU,QAAQ,GACjD,EAAE,WAAW,UAAU,QAAQ,CACjC,CACF,GACA,EAAE,cAAc,UAAU,MAAM,CAClC,CACF;AAEJ"}
1
+ {"version":3,"file":"module-helpers.mjs","names":[],"sources":["../../src/helpers/module-helpers.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { NodePath } from \"@babel/core\";\nimport { ParseResult } from \"@babel/parser\";\nimport * as t from \"@babel/types\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { ImportSpecifier } from \"../types/config\";\nimport { parseAst } from \"./ast-utils\";\n\n/**\n * Finds an export in the given Babel AST program by its key.\n *\n * @param ast - The parsed Babel AST result containing the program body.\n * @param key - The name of the export to find (e.g., \"default\" or a named export).\n * @returns The declaration of the export if found, otherwise undefined.\n */\nexport function findExport(ast: ParseResult<t.File>, key: string) {\n const type =\n key === \"default\" ? \"ExportDefaultDeclaration\" : \"ExportNamedDeclaration\";\n\n for (const node of ast.program.body) {\n if (node.type === type) {\n if (key === \"default\") {\n return node.declaration;\n }\n if (node.declaration && \"declarations\" in node.declaration) {\n const declaration = node.declaration.declarations[0];\n if (\n declaration &&\n \"name\" in declaration.id &&\n declaration.id.name === key\n ) {\n return declaration.init as any;\n }\n }\n }\n }\n}\n\n/**\n * Lists all exports from the given Babel AST program.\n *\n * @param codeOrAst - The parsed Babel AST result containing the program body.\n * @returns An array of export names, including \"default\" for default exports.\n */\nexport function listExports(codeOrAst: ParseResult<t.File> | string) {\n const ast = isString(codeOrAst) ? parseAst(codeOrAst) : codeOrAst;\n\n return ast.program.body\n .flatMap(i => {\n if (i.type === \"ExportDefaultDeclaration\") {\n return [\"default\"];\n }\n if (\n i.type === \"ExportNamedDeclaration\" &&\n i.declaration &&\n \"declarations\" in i.declaration\n ) {\n return i.declaration.declarations.map(d =>\n \"name\" in d.id ? d.id.name : \"\"\n );\n }\n return [];\n })\n .filter(Boolean);\n}\n\n/**\n * Lists all imports from the given Babel AST program.\n *\n * @param ast - The parsed Babel AST result containing the program body.\n * @returns An array of import names, including \"default\" for default imports.\n */\nexport function listImports(ast: ParseResult<t.File> | t.File) {\n return ast.program.body\n .flatMap(i => {\n if (i.type === \"ImportDeclaration\") {\n return i.specifiers.map(s => {\n if (s.type === \"ImportDefaultSpecifier\") {\n return \"default\";\n }\n if (s.type === \"ImportSpecifier\" && \"imported\" in s) {\n return s.imported.type === \"Identifier\"\n ? s.imported.name\n : s.imported.value;\n }\n return \"\";\n });\n }\n\n return [];\n })\n .filter(Boolean);\n}\n\nexport function isImportCall(\n calleePath: NodePath<t.CallExpression | t.NewExpression>\n) {\n return t.isImport(calleePath.node.callee);\n}\n\n/**\n * Gets the import declaration for a given name and specifier.\n *\n * @param specifier - The specifier of the import.\n * @param name - The name of the import.\n * @param named - Optional named import.\n * @returns The import declaration.\n */\nexport function getImport(\n specifier: string,\n name: string,\n named?: string\n): t.ImportDeclaration {\n return t.importDeclaration(\n [t.importSpecifier(t.identifier(name), t.stringLiteral(named || name))],\n t.stringLiteral(specifier)\n );\n}\n\n/**\n * Adds an import to the program if it doesn't already exist.\n *\n * @param path - The current NodePath in the AST.\n * @param specifier - The import specifier.\n */\nexport function addImport(path: NodePath<any>, specifier: ImportSpecifier) {\n addImportsToProgram(\n path.scope.getProgramParent().path as NodePath<t.Program>,\n specifier\n );\n}\n\n/*\n * Matches `import { ... } from <moduleName>;`\n * but not `import * as React from <moduleName>;`\n * `import type { Foo } from <moduleName>;`\n */\nfunction isNonNamespacedImport(\n importDeclPath: NodePath<t.ImportDeclaration>\n): boolean {\n return (\n importDeclPath\n .get(\"specifiers\")\n .filter(Boolean)\n .every(specifier => specifier?.isImportSpecifier()) &&\n importDeclPath.node.importKind !== \"type\" &&\n importDeclPath.node.importKind !== \"typeof\"\n );\n}\n\nfunction getExistingImports(\n program: NodePath<t.Program>\n): Map<string, NodePath<t.ImportDeclaration>> {\n const existingImports = new Map<string, NodePath<t.ImportDeclaration>>();\n program.traverse({\n ImportDeclaration(path) {\n if (isNonNamespacedImport(path)) {\n existingImports.set(path.node.source.value, path);\n }\n }\n });\n return existingImports;\n}\n\nexport function addImportsToProgram(\n path: NodePath<t.Program>,\n specifier: ImportSpecifier\n): void {\n const existingImports = getExistingImports(path);\n\n /**\n * If an existing import of this module exists (ie \\`import \\{ ... \\} from\n * '<moduleName>'\\`), inject new imported specifiers into the list of\n * destructured variables.\n */\n if (!existingImports.get(specifier.module)) {\n path.unshiftContainer(\n \"body\",\n t.importDeclaration(\n [\n t.importSpecifier(\n t.identifier(specifier.name || specifier.imported),\n t.identifier(specifier.imported)\n )\n ],\n t.stringLiteral(specifier.module)\n )\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;AAgCA,SAAgB,WAAW,KAA0B,KAAa;CAChE,MAAM,OACJ,QAAQ,YAAY,6BAA6B;CAEnD,KAAK,MAAM,QAAQ,IAAI,QAAQ,MAC7B,IAAI,KAAK,SAAS,MAAM;EACtB,IAAI,QAAQ,WACV,OAAO,KAAK;EAEd,IAAI,KAAK,eAAe,kBAAkB,KAAK,aAAa;GAC1D,MAAM,cAAc,KAAK,YAAY,aAAa;GAClD,IACE,eACA,UAAU,YAAY,MACtB,YAAY,GAAG,SAAS,KAExB,OAAO,YAAY;EAEvB;CACF;AAEJ;;;;;;;AAQA,SAAgB,YAAY,WAAyC;CAGnE,QAFY,SAAS,SAAS,IAAI,SAAS,SAAS,IAAI,UAE9C,CAAC,QAAQ,KAChB,SAAQ,MAAK;EACZ,IA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tB,IAAI,sBAAsB,IAAI,GAC5B,gBAAgB,IAAI,KAAK,KAAK,OAAO,OAAO,IAAI;CAEpD,EACF,CAAC;CACD,OAAO;AACT;AAEA,SAAgB,oBACd,MACA,WACM;;;;;;CAQN,IAAI,CAPoB,mBAAmB,IAOxB,CAAC,CAAC,IAAI,UAAU,MAAM,GACvC,KAAK,iBACH,QACA,EAAE,kBACA,CACE,EAAE,gBACA,EAAE,WAAW,UAAU,QAAQ,UAAU,QAAQ,GACjD,EAAE,WAAW,UAAU,QAAQ,CACjC,CACF,GACA,EAAE,cAAc,UAAU,MAAM,CAClC,CACF;AAEJ"}
@@ -1 +1 @@
1
- {"version":3,"file":"options.mjs","names":[],"sources":["../../src/helpers/options.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n PluginItem,\n PluginTarget,\n PresetItem,\n PresetTarget\n} from \"@babel/core\";\nimport { isFunction } from \"@stryke/type-checks/is-function\";\nimport chalk from \"chalk\";\nimport { Context } from \"powerlines\";\nimport {\n BabelTransformPluginOptions,\n BabelTransformPresetOptions,\n ResolvedBabelTransformPluginOptions,\n ResolvedBabelTransformPresetOptions\n} from \"../types/config\";\nimport { getPluginName, includesPlugin, includesPreset } from \"./filters\";\n\nexport function resolvePluginFunction(\n context: Context,\n plugin:\n | any\n | PluginTarget\n | any[]\n | [PluginTarget, PluginItem]\n | [PluginTarget, PluginItem, string | undefined]\n): BabelTransformPluginOptions {\n try {\n return Array.isArray(plugin) && plugin.length > 0 && plugin[0]\n ? isFunction(plugin[0])\n ? plugin[0](context)\n : plugin[0]\n : isFunction(plugin)\n ? plugin(context)\n : plugin;\n } catch {\n return plugin[0];\n }\n}\n\nexport function resolvePresetFunction(\n context: Context,\n preset:\n | any\n | PresetTarget\n | any[]\n | [PresetTarget, PresetItem]\n | [PresetTarget, PresetItem, string | undefined]\n): BabelTransformPresetOptions {\n try {\n return Array.isArray(preset) && preset.length > 0 && preset[0]\n ? isFunction(preset[0])\n ? preset[0](context)\n : preset[0]\n : isFunction(preset)\n ? preset(context)\n : preset;\n } catch {\n return preset[0];\n }\n}\n\n/**\n * Resolve the [Babel](https://babeljs.io/) plugin.\n *\n * @param context - The context for the transformation.\n * @param code - The code to be transformed.\n * @param id - The ID of the source file.\n * @param plugin - The Babel plugin to resolve.\n * @returns The resolved Babel plugin options, or undefined if the plugin is filtered out.\n */\nexport function resolveBabelPlugin(\n context: Context,\n code: string,\n id: string,\n plugin: BabelTransformPluginOptions\n): ResolvedBabelTransformPluginOptions | undefined {\n if (Array.isArray(plugin) && plugin.length > 0 && plugin[0]) {\n if (\n plugin.length > 2 &&\n plugin[2] &&\n isFunction(plugin[2]) &&\n !plugin[2](code, id)\n ) {\n context.trace(\n `Skipping filtered Babel plugin ${chalk.bold.cyanBright(\n getPluginName(plugin) || \"unnamed\"\n )} for ${id}`\n );\n\n return undefined;\n }\n\n return (\n plugin.length > 2\n ? [resolvePluginFunction(context, plugin), plugin[1], plugin[2]]\n : [resolvePluginFunction(context, plugin), plugin[1], null]\n ) as ResolvedBabelTransformPluginOptions;\n }\n\n return [\n resolvePluginFunction(context, plugin),\n {},\n null\n ] as ResolvedBabelTransformPluginOptions;\n}\n\n/**\n * Resolve the [Babel](https://babeljs.io/) preset.\n *\n * @param context - The context for the transformation.\n * @param code - The code to be transformed.\n * @param id - The ID of the source file.\n * @param preset - The Babel preset to resolve.\n * @returns The resolved Babel preset options, or undefined if the preset is filtered out.\n */\nexport function resolveBabelPreset(\n context: Context,\n code: string,\n id: string,\n preset: BabelTransformPresetOptions\n): ResolvedBabelTransformPresetOptions | undefined {\n if (Array.isArray(preset) && preset.length > 0 && preset[0]) {\n if (\n preset.length > 2 &&\n preset[2] &&\n isFunction(preset[2]) &&\n !preset[2](code, id)\n ) {\n context.trace(\n `Skipping filtered Babel preset ${chalk.bold.cyanBright(\n getPluginName(preset) || \"unnamed\"\n )} for ${id}`\n );\n\n return undefined;\n }\n\n return (\n preset.length > 2\n ? [resolvePresetFunction(context, preset), preset[1], preset[2]]\n : [resolvePresetFunction(context, preset), preset[1], null]\n ) as ResolvedBabelTransformPresetOptions;\n }\n\n return [\n resolvePresetFunction(context, preset),\n {},\n null\n ] as ResolvedBabelTransformPresetOptions;\n}\n\n/**\n * Get a list of unique Babel plugins, filtering out duplicates based on their names.\n *\n * @param plugins - The list of Babel plugins to filter for uniqueness.\n * @returns A list of unique Babel plugins.\n */\nexport function getUniquePlugins<\n T extends BabelTransformPluginOptions | ResolvedBabelTransformPluginOptions\n>(plugins: T[]): T[] {\n return plugins.reduce((ret: T[], plugin: T) => {\n if (plugin && !includesPlugin(ret, plugin)) {\n ret.push(plugin);\n }\n\n return ret;\n }, []);\n}\n\n/**\n * Get a list of unique Babel presets, filtering out duplicates based on their names.\n *\n * @param presets - The list of Babel presets to filter for uniqueness.\n * @returns A list of unique Babel presets.\n */\nexport function getUniquePresets<\n T extends BabelTransformPresetOptions | ResolvedBabelTransformPresetOptions\n>(presets: T[]): T[] {\n return presets.reduce((ret: T[], preset: T) => {\n if (preset && !includesPreset(ret, preset)) {\n ret.push(preset);\n }\n\n return ret;\n }, []);\n}\n"],"mappings":";;;;;AAmCA,SAAgB,sBACd,SACA,QAM6B;CAC7B,IAAI;EACF,OAAO,MAAM,QAAQ,MAAM,KAAK,OAAO,SAAS,KAAK,OAAO,KACxD,WAAW,OAAO,EAAE,IAClB,OAAO,GAAG,OAAO,IACjB,OAAO,KACT,WAAW,MAAM,IACf,OAAO,OAAO,IACd;CACR,QAAQ;EACN,OAAO,OAAO;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GAAI;EAAI;CAEhE;CAEA,OAAO;EACL,sBAAsB,SAAS,MAAM;EACrC,CAAC;EACD;CACF;AACF;;;;;;;AAQA,SAAgB,iBAEd,SAAmB;CACnB,OAAO,QAAQ,QAAQ,KAAU,WAAc;EAC7C,IAAI,UAAU,CAAC,eAAe,KAAK,MAAM,GACvC,IAAI,KAAK,MAAM;EAGjB,OAAO;CACT,GAAG,CAAC,CAAC;AACP;;;;;;;AAQA,SAAgB,iBAEd,SAAmB;CACnB,OAAO,QAAQ,QAAQ,KAAU,WAAc;EAC7C,IAAI,UAAU,CAAC,eAAe,KAAK,MAAM,GACvC,IAAI,KAAK,MAAM;EAGjB,OAAO;CACT,GAAG,CAAC,CAAC;AACP"}
1
+ {"version":3,"file":"options.mjs","names":[],"sources":["../../src/helpers/options.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport {\n PluginItem,\n PluginTarget,\n PresetItem,\n PresetTarget\n} from \"@babel/core\";\nimport { isFunction } from \"@stryke/type-checks/is-function\";\nimport chalk from \"chalk\";\nimport { Context } from \"powerlines\";\nimport {\n BabelTransformPluginOptions,\n BabelTransformPresetOptions,\n ResolvedBabelTransformPluginOptions,\n ResolvedBabelTransformPresetOptions\n} from \"../types/config\";\nimport { getPluginName, includesPlugin, includesPreset } from \"./filters\";\n\nexport function resolvePluginFunction(\n context: Context,\n plugin:\n | any\n | PluginTarget\n | any[]\n | [PluginTarget, PluginItem]\n | [PluginTarget, PluginItem, string | undefined]\n): BabelTransformPluginOptions {\n try {\n return Array.isArray(plugin) && plugin.length > 0 && plugin[0]\n ? isFunction(plugin[0])\n ? plugin[0](context)\n : plugin[0]\n : isFunction(plugin)\n ? plugin(context)\n : plugin;\n } catch {\n return plugin[0];\n }\n}\n\nexport function resolvePresetFunction(\n context: Context,\n preset:\n | any\n | PresetTarget\n | any[]\n | [PresetTarget, PresetItem]\n | [PresetTarget, PresetItem, string | undefined]\n): BabelTransformPresetOptions {\n try {\n return Array.isArray(preset) && preset.length > 0 && preset[0]\n ? isFunction(preset[0])\n ? preset[0](context)\n : preset[0]\n : isFunction(preset)\n ? preset(context)\n : preset;\n } catch {\n return preset[0];\n }\n}\n\n/**\n * Resolve the [Babel](https://babeljs.io/) plugin.\n *\n * @param context - The context for the transformation.\n * @param code - The code to be transformed.\n * @param id - The ID of the source file.\n * @param plugin - The Babel plugin to resolve.\n * @returns The resolved Babel plugin options, or undefined if the plugin is filtered out.\n */\nexport function resolveBabelPlugin(\n context: Context,\n code: string,\n id: string,\n plugin: BabelTransformPluginOptions\n): ResolvedBabelTransformPluginOptions | undefined {\n if (Array.isArray(plugin) && plugin.length > 0 && plugin[0]) {\n if (\n plugin.length > 2 &&\n plugin[2] &&\n isFunction(plugin[2]) &&\n !plugin[2](code, id)\n ) {\n context.trace(\n `Skipping filtered Babel plugin ${chalk.bold.cyanBright(\n getPluginName(plugin) || \"unnamed\"\n )} for ${id}`\n );\n\n return undefined;\n }\n\n return (\n plugin.length > 2\n ? [resolvePluginFunction(context, plugin), plugin[1], plugin[2]]\n : [resolvePluginFunction(context, plugin), plugin[1], null]\n ) as ResolvedBabelTransformPluginOptions;\n }\n\n return [\n resolvePluginFunction(context, plugin),\n {},\n null\n ] as ResolvedBabelTransformPluginOptions;\n}\n\n/**\n * Resolve the [Babel](https://babeljs.io/) preset.\n *\n * @param context - The context for the transformation.\n * @param code - The code to be transformed.\n * @param id - The ID of the source file.\n * @param preset - The Babel preset to resolve.\n * @returns The resolved Babel preset options, or undefined if the preset is filtered out.\n */\nexport function resolveBabelPreset(\n context: Context,\n code: string,\n id: string,\n preset: BabelTransformPresetOptions\n): ResolvedBabelTransformPresetOptions | undefined {\n if (Array.isArray(preset) && preset.length > 0 && preset[0]) {\n if (\n preset.length > 2 &&\n preset[2] &&\n isFunction(preset[2]) &&\n !preset[2](code, id)\n ) {\n context.trace(\n `Skipping filtered Babel preset ${chalk.bold.cyanBright(\n getPluginName(preset) || \"unnamed\"\n )} for ${id}`\n );\n\n return undefined;\n }\n\n return (\n preset.length > 2\n ? [resolvePresetFunction(context, preset), preset[1], preset[2]]\n : [resolvePresetFunction(context, preset), preset[1], null]\n ) as ResolvedBabelTransformPresetOptions;\n }\n\n return [\n resolvePresetFunction(context, preset),\n {},\n null\n ] as ResolvedBabelTransformPresetOptions;\n}\n\n/**\n * Get a list of unique Babel plugins, filtering out duplicates based on their names.\n *\n * @param plugins - The list of Babel plugins to filter for uniqueness.\n * @returns A list of unique Babel plugins.\n */\nexport function getUniquePlugins<\n T extends BabelTransformPluginOptions | ResolvedBabelTransformPluginOptions\n>(plugins: T[]): T[] {\n return plugins.reduce((ret: T[], plugin: T) => {\n if (plugin && !includesPlugin(ret, plugin)) {\n ret.push(plugin);\n }\n\n return ret;\n }, []);\n}\n\n/**\n * Get a list of unique Babel presets, filtering out duplicates based on their names.\n *\n * @param presets - The list of Babel presets to filter for uniqueness.\n * @returns A list of unique Babel presets.\n */\nexport function getUniquePresets<\n T extends BabelTransformPresetOptions | ResolvedBabelTransformPresetOptions\n>(presets: T[]): T[] {\n return presets.reduce((ret: T[], preset: T) => {\n if (preset && !includesPreset(ret, preset)) {\n ret.push(preset);\n }\n\n return ret;\n }, []);\n}\n"],"mappings":";;;;;AAmCA,SAAgB,sBACd,SACA,QAM6B;CAC7B,IAAI;EACF,OAAO,MAAM,QAAQ,MAAM,KAAK,OAAO,SAAS,KAAK,OAAO,KACxD,WAAW,OAAO,EAAE,IAClB,OAAO,EAAE,CAAC,OAAO,IACjB,OAAO,KACT,WAAW,MAAM,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AAO;GAAI,OAAO;EAAE,IAC7D;GAAC,sBAAsB,SAAS,MAAM;GAAG,OAAO;GAAI;EAAI;CAEhE;CAEA,OAAO;EACL,sBAAsB,SAAS,MAAM;EACrC,CAAC;EACD;CACF;AACF;;;;;;;AAQA,SAAgB,iBAEd,SAAmB;CACnB,OAAO,QAAQ,QAAQ,KAAU,WAAc;EAC7C,IAAI,UAAU,CAAC,eAAe,KAAK,MAAM,GACvC,IAAI,KAAK,MAAM;EAGjB,OAAO;CACT,GAAG,CAAC,CAAC;AACP;;;;;;;AAQA,SAAgB,iBAEd,SAAmB;CACnB,OAAO,QAAQ,QAAQ,KAAU,WAAc;EAC7C,IAAI,UAAU,CAAC,eAAe,KAAK,MAAM,GACvC,IAAI,KAAK,MAAM;EAGjB,OAAO;CACT,GAAG,CAAC,CAAC;AACP"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { PluginItem, PresetItem } from \"@babel/core\";\nimport { transformAsync } from \"@babel/core\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { findFileExtensionSafe } from \"@stryke/path/file-path-fns\";\nimport { isEmptyObject } from \"@stryke/type-checks/is-empty-object\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport defu from \"defu\";\nimport { isSet } from \"node:util/types\";\nimport type { Plugin } from \"powerlines\";\nimport { removeVirtualPrefix } from \"powerlines/plugin-utils\";\nimport { includesPlugin, includesPreset } from \"./helpers/filters\";\nimport {\n getUniquePlugins,\n getUniquePresets,\n resolveBabelPlugin,\n resolveBabelPreset\n} from \"./helpers/options\";\nimport type {\n ResolvedBabelTransformPluginOptions,\n ResolvedBabelTransformPresetOptions\n} from \"./types/config\";\nimport type { BabelPluginContext, BabelPluginOptions } from \"./types/plugin\";\n\nexport * from \"./helpers\";\nexport type * from \"./types\";\n\ndeclare module \"powerlines\" {\n interface Config {\n babel?: BabelPluginOptions;\n }\n}\n\n/**\n * Babel plugin for Powerlines.\n *\n * @param options - The Babel plugin user configuration options.\n * @returns A Powerlines plugin that integrates Babel transformations.\n */\nexport const plugin = <\n TContext extends BabelPluginContext = BabelPluginContext\n>(\n options: BabelPluginOptions = {}\n): Plugin<TContext> => {\n return {\n name: \"babel\",\n config() {\n if (!isSetObject(options)) {\n return undefined;\n }\n\n return {\n babel: options\n };\n },\n configResolved: {\n order: \"pre\",\n handler() {\n this.config.babel = defu(this.config.babel ?? {}, {\n plugins: [],\n presets: []\n });\n\n this.config.babel.plugins = getUniquePlugins(this.config.babel.plugins);\n this.config.babel.presets = getUniquePresets(this.config.babel.presets);\n }\n },\n async transform(code: string, id: string) {\n if (\n // isParentPath(id, this.powerlinesPath) ||\n code.includes(\"/* @powerlines-ignore */\") ||\n code.includes(\"/* @powerlines-disable */\")\n ) {\n this.trace(`Skipping Babel transformation for: ${id}`);\n\n return { code, id };\n }\n\n const plugins = getUniquePlugins(\n this.config.babel.plugins\n .map(plugin => resolveBabelPlugin(this, code, id, plugin))\n .filter(Boolean) as ResolvedBabelTransformPluginOptions[]\n );\n const presets = getUniquePresets(\n this.config.babel.presets\n .map(preset => resolveBabelPreset(this, code, id, preset))\n .filter(Boolean) as ResolvedBabelTransformPresetOptions[]\n );\n\n if (\n Array.isArray(plugins) &&\n plugins.length === 0 &&\n Array.isArray(presets) &&\n presets.length === 0\n ) {\n return { code, id };\n }\n\n if (!this.config.babel?.skipConfigResolution) {\n if (!includesPlugin(plugins, \"@babel/plugin-transform-json-modules\")) {\n plugins.push(\"@babel/plugin-transform-json-modules\");\n }\n\n if (\n /^(?:m|c)?tsx?$/.test(\n findFileExtensionSafe(id, {\n fullExtension: true\n })\n ) &&\n !includesPlugin(plugins, \"@babel/plugin-syntax-typescript\") &&\n !includesPreset(presets, \"@babel/preset-typescript\")\n ) {\n plugins.unshift(\"@babel/plugin-syntax-typescript\");\n }\n\n if (\n /^(?:t|j)sx$/.test(\n findFileExtensionSafe(id, {\n fullExtension: true\n })\n ) &&\n !includesPlugin(plugins, \"@babel/plugin-syntax-jsx\") &&\n !includesPreset(presets, \"@babel/preset-react\")\n ) {\n plugins.unshift(\"@babel/plugin-syntax-jsx\");\n }\n }\n\n if (\n !includesPlugin(plugins, \"@babel/plugin-transform-json-modules\") &&\n !includesPreset(presets, \"@babel/preset-env\") &&\n findFileExtensionSafe(id).startsWith(\"json\")\n ) {\n // Skipping transformation for JSON files if the transform-json-modules plugin is not included, as Babel cannot process JSON files without it.\n return { code, id };\n }\n\n this.trace(\n `Running babel transformations with ${plugins.length} plugins and ${\n presets.length\n } presets for file: ${id}`\n );\n\n const result = await transformAsync(code, {\n cwd: this.config.cwd,\n highlightCode: true,\n code: true,\n ast: false,\n cloneInputAst: false,\n comments: true,\n sourceType: \"module\",\n configFile: false,\n babelrc: false,\n envName: this.config.mode,\n caller: {\n name: this.config.framework?.name || \"powerlines\"\n },\n ...omit(this.config.babel ?? {}, [\n \"skipConfigResolution\",\n \"skipTransform\"\n ]),\n filename: removeVirtualPrefix(id),\n plugins: plugins\n .map(plugin => {\n if (Array.isArray(plugin) && plugin.length >= 2) {\n if (\n plugin\n .slice(1)\n .every(item => !isSet(item) || isEmptyObject(item))\n ) {\n return plugin[0];\n }\n\n return [\n plugin[0],\n plugin.length > 1 && plugin[1] ? plugin[1] : {}\n ];\n }\n\n return plugin;\n })\n .filter(Boolean) as PluginItem<object>[],\n presets: presets\n .map(preset => {\n if (Array.isArray(preset) && preset.length >= 2) {\n if (\n preset\n .slice(1)\n .every(item => !isSet(item) || isEmptyObject(item))\n ) {\n return preset[0];\n }\n\n return [\n preset[0],\n preset.length > 1 && preset[1] ? preset[1] : {}\n ];\n }\n\n return preset;\n })\n .filter(Boolean) as PresetItem<object>[]\n });\n if (!result?.code) {\n throw new Error(`Powerlines - Babel plugin failed to compile ${id}`);\n }\n\n this.trace(`Completed babel transformations for file: ${id}`);\n\n return {\n code: this.config.babel?.skipTransform ? code : result.code,\n id\n };\n }\n } as Plugin<TContext>;\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAwDA,MAAa,UAGX,UAA8B,CAAC,MACV;CACrB,OAAO;EACL,MAAM;EACN,SAAS;GACP,IAAI,CAAC,YAAY,OAAO,GACtB;GAGF,OAAO,EACL,OAAO,QACT;EACF;EACA,gBAAgB;GACd,OAAO;GACP,UAAU;IACR,KAAK,OAAO,QAAQ,KAAK,KAAK,OAAO,SAAS,CAAC,GAAG;KAChD,SAAS,CAAC;KACV,SAAS,CAAC;IACZ,CAAC;IAED,KAAK,OAAO,MAAM,UAAU,iBAAiB,KAAK,OAAO,MAAM,OAAO;IACtE,KAAK,OAAO,MAAM,UAAU,iBAAiB,KAAK,OAAO,MAAM,OAAO;GACxE;EACF;EACA,MAAM,UAAU,MAAc,IAAY;GACxC,IAEE,KAAK,SAAS,0BAA0B,KACxC,KAAK,SAAS,2BAA2B,GACzC;IACA,KAAK,MAAM,sCAAsC,IAAI;IAErD,OAAO;KAAE;KAAM;IAAG;GACpB;GAEA,MAAM,UAAU,iBACd,KAAK,OAAO,MAAM,QACf,KAAI,WAAU,mBAAmB,MAAM,MAAM,IAAI,MAAM,CAAC,EACxD,OAAO,OAAO,CACnB;GACA,MAAM,UAAU,iBACd,KAAK,OAAO,MAAM,QACf,KAAI,WAAU,mBAAmB,MAAM,MAAM,IAAI,MAAM,CAAC,EACxD,OAAO,OAAO,CACnB;GAEA,IACE,MAAM,QAAQ,OAAO,KACrB,QAAQ,WAAW,KACnB,MAA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AO,OAAO;MAGhB,OAAO,CACL,OAAO,IACP,OAAO,SAAS,KAAK,OAAO,KAAK,OAAO,KAAK,CAAC,CAChD;KACF;KAEA,OAAO;IACT,CAAC,EACA,OAAO,OAAO;IACjB,SAAS,QACN,KAAI,WAAU;KACb,IAAI,MAAM,QAAQ,MAAM,KAAK,OAAO,UAAU,GAAG;MAC/C,IACE,OACG,MAAM,CAAC,EACP,OAAM,SAAQ,CAAC,MAAM,IAAI,KAAK,cAAc,IAAI,CAAC,GAEpD,OAAO,OAAO;MAGhB,OAAO,CACL,OAAO,IACP,OAAO,SAAS,KAAK,OAAO,KAAK,OAAO,KAAK,CAAC,CAChD;KACF;KAEA,OAAO;IACT,CAAC,EACA,OAAO,OAAO;GACnB,CAAC;GACD,IAAI,CAAC,QAAQ,MACX,MAAM,IAAI,MAAM,+CAA+C,IAAI;GAGrE,KAAK,MAAM,6CAA6C,IAAI;GAE5D,OAAO;IACL,MAAM,KAAK,OAAO,OAAO,gBAAgB,OAAO,OAAO;IACvD;GACF;EACF;CACF;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\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/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { PluginItem, PresetItem } from \"@babel/core\";\nimport { transformAsync } from \"@babel/core\";\nimport { omit } from \"@stryke/helpers/omit\";\nimport { findFileExtensionSafe } from \"@stryke/path/file-path-fns\";\nimport { isEmptyObject } from \"@stryke/type-checks/is-empty-object\";\nimport { isSetObject } from \"@stryke/type-checks/is-set-object\";\nimport defu from \"defu\";\nimport { isSet } from \"node:util/types\";\nimport type { Plugin } from \"powerlines\";\nimport { removeVirtualPrefix } from \"powerlines/plugin-utils\";\nimport { includesPlugin, includesPreset } from \"./helpers/filters\";\nimport {\n getUniquePlugins,\n getUniquePresets,\n resolveBabelPlugin,\n resolveBabelPreset\n} from \"./helpers/options\";\nimport type {\n ResolvedBabelTransformPluginOptions,\n ResolvedBabelTransformPresetOptions\n} from \"./types/config\";\nimport type { BabelPluginContext, BabelPluginOptions } from \"./types/plugin\";\n\nexport * from \"./helpers\";\nexport type * from \"./types\";\n\ndeclare module \"powerlines\" {\n interface Config {\n babel?: BabelPluginOptions;\n }\n}\n\n/**\n * Babel plugin for Powerlines.\n *\n * @param options - The Babel plugin user configuration options.\n * @returns A Powerlines plugin that integrates Babel transformations.\n */\nexport const plugin = <\n TContext extends BabelPluginContext = BabelPluginContext\n>(\n options: BabelPluginOptions = {}\n): Plugin<TContext> => {\n return {\n name: \"babel\",\n config() {\n if (!isSetObject(options)) {\n return undefined;\n }\n\n return {\n babel: options\n };\n },\n configResolved: {\n order: \"pre\",\n handler() {\n this.config.babel = defu(this.config.babel ?? {}, {\n plugins: [],\n presets: []\n });\n\n this.config.babel.plugins = getUniquePlugins(this.config.babel.plugins);\n this.config.babel.presets = getUniquePresets(this.config.babel.presets);\n }\n },\n async transform(code: string, id: string) {\n if (\n // isParentPath(id, this.powerlinesPath) ||\n code.includes(\"/* @powerlines-ignore */\") ||\n code.includes(\"/* @powerlines-disable */\")\n ) {\n this.trace(`Skipping Babel transformation for: ${id}`);\n\n return { code, id };\n }\n\n const plugins = getUniquePlugins(\n this.config.babel.plugins\n .map(plugin => resolveBabelPlugin(this, code, id, plugin))\n .filter(Boolean) as ResolvedBabelTransformPluginOptions[]\n );\n const presets = getUniquePresets(\n this.config.babel.presets\n .map(preset => resolveBabelPreset(this, code, id, preset))\n .filter(Boolean) as ResolvedBabelTransformPresetOptions[]\n );\n\n if (\n Array.isArray(plugins) &&\n plugins.length === 0 &&\n Array.isArray(presets) &&\n presets.length === 0\n ) {\n return { code, id };\n }\n\n if (!this.config.babel?.skipConfigResolution) {\n if (!includesPlugin(plugins, \"@babel/plugin-transform-json-modules\")) {\n plugins.push(\"@babel/plugin-transform-json-modules\");\n }\n\n if (\n /^(?:m|c)?tsx?$/.test(\n findFileExtensionSafe(id, {\n fullExtension: true\n })\n ) &&\n !includesPlugin(plugins, \"@babel/plugin-syntax-typescript\") &&\n !includesPreset(presets, \"@babel/preset-typescript\")\n ) {\n plugins.unshift(\"@babel/plugin-syntax-typescript\");\n }\n\n if (\n /^(?:t|j)sx$/.test(\n findFileExtensionSafe(id, {\n fullExtension: true\n })\n ) &&\n !includesPlugin(plugins, \"@babel/plugin-syntax-jsx\") &&\n !includesPreset(presets, \"@babel/preset-react\")\n ) {\n plugins.unshift(\"@babel/plugin-syntax-jsx\");\n }\n }\n\n if (\n !includesPlugin(plugins, \"@babel/plugin-transform-json-modules\") &&\n !includesPreset(presets, \"@babel/preset-env\") &&\n findFileExtensionSafe(id).startsWith(\"json\")\n ) {\n // Skipping transformation for JSON files if the transform-json-modules plugin is not included, as Babel cannot process JSON files without it.\n return { code, id };\n }\n\n this.trace(\n `Running babel transformations with ${plugins.length} plugins and ${\n presets.length\n } presets for file: ${id}`\n );\n\n const result = await transformAsync(code, {\n cwd: this.config.cwd,\n highlightCode: true,\n code: true,\n ast: false,\n cloneInputAst: false,\n comments: true,\n sourceType: \"module\",\n configFile: false,\n babelrc: false,\n envName: this.config.mode,\n caller: {\n name: this.config.framework?.name || \"powerlines\"\n },\n ...omit(this.config.babel ?? {}, [\n \"skipConfigResolution\",\n \"skipTransform\"\n ]),\n filename: removeVirtualPrefix(id),\n plugins: plugins\n .map(plugin => {\n if (Array.isArray(plugin) && plugin.length >= 2) {\n if (\n plugin\n .slice(1)\n .every(item => !isSet(item) || isEmptyObject(item))\n ) {\n return plugin[0];\n }\n\n return [\n plugin[0],\n plugin.length > 1 && plugin[1] ? plugin[1] : {}\n ];\n }\n\n return plugin;\n })\n .filter(Boolean) as PluginItem<object>[],\n presets: presets\n .map(preset => {\n if (Array.isArray(preset) && preset.length >= 2) {\n if (\n preset\n .slice(1)\n .every(item => !isSet(item) || isEmptyObject(item))\n ) {\n return preset[0];\n }\n\n return [\n preset[0],\n preset.length > 1 && preset[1] ? preset[1] : {}\n ];\n }\n\n return preset;\n })\n .filter(Boolean) as PresetItem<object>[]\n });\n if (!result?.code) {\n throw new Error(`Powerlines - Babel plugin failed to compile ${id}`);\n }\n\n this.trace(`Completed babel transformations for file: ${id}`);\n\n return {\n code: this.config.babel?.skipTransform ? code : result.code,\n id\n };\n }\n } as Plugin<TContext>;\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAwDA,MAAa,UAGX,UAA8B,CAAC,MACV;CACrB,OAAO;EACL,MAAM;EACN,SAAS;GACP,IAAI,CAAC,YAAY,OAAO,GACtB;GAGF,OAAO,EACL,OAAO,QACT;EACF;EACA,gBAAgB;GACd,OAAO;GACP,UAAU;IACR,KAAK,OAAO,QAAQ,KAAK,KAAK,OAAO,SAAS,CAAC,GAAG;KAChD,SAAS,CAAC;KACV,SAAS,CAAC;IACZ,CAAC;IAED,KAAK,OAAO,MAAM,UAAU,iBAAiB,KAAK,OAAO,MAAM,OAAO;IACtE,KAAK,OAAO,MAAM,UAAU,iBAAiB,KAAK,OAAO,MAAM,OAAO;GACxE;EACF;EACA,MAAM,UAAU,MAAc,IAAY;GACxC,IAEE,KAAK,SAAS,0BAA0B,KACxC,KAAK,SAAS,2BAA2B,GACzC;IACA,KAAK,MAAM,sCAAsC,IAAI;IAErD,OAAO;KAAE;KAAM;IAAG;GACpB;GAEA,MAAM,UAAU,iBACd,KAAK,OAAO,MAAM,QACf,KAAI,WAAU,mBAAmB,MAAM,MAAM,IAAI,MAAM,CAAC,CAAC,CACzD,OAAO,OAAO,CACnB;GACA,MAAM,UAAU,iBACd,KAAK,OAAO,MAAM,QACf,KAAI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AAM,QAAQ,MAAM,KAAK,OAAO,UAAU,GAAG;MAC/C,IACE,OACG,MAAM,CAAC,CAAC,CACR,OAAM,SAAQ,CAAC,MAAM,IAAI,KAAK,cAAc,IAAI,CAAC,GAEpD,OAAO,OAAO;MAGhB,OAAO,CACL,OAAO,IACP,OAAO,SAAS,KAAK,OAAO,KAAK,OAAO,KAAK,CAAC,CAChD;KACF;KAEA,OAAO;IACT,CAAC,CAAC,CACD,OAAO,OAAO;IACjB,SAAS,QACN,KAAI,WAAU;KACb,IAAI,MAAM,QAAQ,MAAM,KAAK,OAAO,UAAU,GAAG;MAC/C,IACE,OACG,MAAM,CAAC,CAAC,CACR,OAAM,SAAQ,CAAC,MAAM,IAAI,KAAK,cAAc,IAAI,CAAC,GAEpD,OAAO,OAAO;MAGhB,OAAO,CACL,OAAO,IACP,OAAO,SAAS,KAAK,OAAO,KAAK,OAAO,KAAK,CAAC,CAChD;KACF;KAEA,OAAO;IACT,CAAC,CAAC,CACD,OAAO,OAAO;GACnB,CAAC;GACD,IAAI,CAAC,QAAQ,MACX,MAAM,IAAI,MAAM,+CAA+C,IAAI;GAGrE,KAAK,MAAM,6CAA6C,IAAI;GAE5D,OAAO;IACL,MAAM,KAAK,OAAO,OAAO,gBAAgB,OAAO,OAAO;IACvD;GACF;EACF;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powerlines/plugin-babel",
3
- "version": "0.13.119",
3
+ "version": "0.13.121",
4
4
  "private": false,
5
5
  "description": "A package containing a Powerlines plugin to assist in developing other Powerlines plugins.",
6
6
  "keywords": ["babel", "powerlines", "storm-software", "powerlines-plugin"],
@@ -90,7 +90,7 @@
90
90
  "@babel/helper-plugin-utils": "8.0.0-rc.6",
91
91
  "@babel/parser": "8.0.0-rc.6",
92
92
  "@babel/types": "8.0.0-rc.6",
93
- "@powerlines/core": "^0.48.45",
93
+ "@powerlines/core": "^0.48.47",
94
94
  "@storm-software/config-tools": "^1.190.40",
95
95
  "@stryke/fs": "^0.33.85",
96
96
  "@stryke/path": "^0.29.11",
@@ -99,7 +99,7 @@
99
99
  "chalk": "5.6.2",
100
100
  "defu": "^6.1.7",
101
101
  "jiti": "^2.7.0",
102
- "powerlines": "^0.47.123"
102
+ "powerlines": "^0.47.125"
103
103
  },
104
104
  "devDependencies": {
105
105
  "@types/babel__helper-plugin-utils": "^7.10.3",
@@ -116,5 +116,5 @@
116
116
  "@babel/plugin-transform-json-modules": { "optional": false }
117
117
  },
118
118
  "publishConfig": { "access": "public" },
119
- "gitHead": "f8de1000ffda8a49be7fccbb6f53890449f7bdd3"
119
+ "gitHead": "30ba301952db7af53c07d369bd22f650e5ca9c45"
120
120
  }