UNPKG

babel-plugin-sfcc-modules

Version:

Babel plugin to handle non-standard module paths used by Salesforce Commerce Cloud (SFCC)

78 lines (77 loc) 3.95 kB
//#region \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let _babel_types = require("@babel/types"); _babel_types = __toESM(_babel_types, 1); let imports_visitor = require("imports-visitor"); imports_visitor = __toESM(imports_visitor, 1); let node_fs = require("node:fs"); node_fs = __toESM(node_fs, 1); let node_path = require("node:path"); node_path = __toESM(node_path, 1); //#region src/index.ts const SUPPORTED_EXTENSIONS = [ "js", "ds", "json" ]; const getModulePath = (moduleName, basePath, cartridge, target) => { const relativePath = node_path.default.relative(node_path.default.dirname(moduleName), `${basePath}/${cartridge}${target}`); return (relativePath.includes(".") ? "" : "./") + relativePath; }; const plugin = (_babel, { cartridgePath, basePath }) => ({ visitor: { Program(thePath, state) { const imports = []; thePath.traverse(imports_visitor.default, { imports }); for (const imp of imports) { /** * Finds and sets the rewrittten module path. * * @param findCartridge a function to find the cartridge */ const resolve = (findCartridge) => { const target = imp.source.slice(1); const foundCartridge = findCartridge(target); if (foundCartridge) imp.source = getModulePath(state.file.opts.filename, basePath, foundCartridge, target); }; if (imp.source.indexOf("*/") === 0) resolve((target) => cartridgePath.find((cartridge) => SUPPORTED_EXTENSIONS.find((extension) => node_fs.default.existsSync(`${basePath}/${cartridge}${target}.${extension}`)))); if (imp.source.indexOf("~/") === 0) resolve(() => node_path.default.relative(basePath, node_path.default.dirname(state.file.opts.filename)).split(node_path.default.sep)[0]); } }, MemberExpression(thePath, state) { if (thePath.node.object.type === "Identifier" && thePath.node.object.name === "module" && thePath.node.property.name === "superModule") { const pathToModule = node_path.default.relative(basePath, state.file.opts.filename); const shortenedPathParts = pathToModule.split(node_path.default.sep).slice(1); const lastPart = shortenedPathParts.at(-1); if (!lastPart) return; shortenedPathParts[shortenedPathParts.length - 1] = lastPart.replace(/\.[^.]+$/, ""); const shortenedPathToModule = `/${shortenedPathParts.join(node_path.default.sep)}`; const cartridge = pathToModule.split(node_path.default.sep)[0]; const foundCartridge = cartridgePath.slice(cartridgePath.indexOf(cartridge) + 1).find((theCartridge) => SUPPORTED_EXTENSIONS.find((extension) => node_fs.default.existsSync(`${basePath}/${theCartridge}${shortenedPathToModule}.${extension}`))); let foundRequire; if (foundCartridge) foundRequire = getModulePath(state.file.opts.filename, basePath, foundCartridge, shortenedPathToModule); thePath.replaceWith(foundRequire ? _babel_types.default.callExpression(_babel_types.default.identifier("require"), [_babel_types.default.stringLiteral(foundRequire)]) : _babel_types.default.identifier("undefined")); } } } }); //#endregion module.exports = plugin;