|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +'use strict'; |
| 8 | + |
| 9 | +const path = require('path'); |
| 10 | +const fs = require('fs'); |
| 11 | +const { XMLParser } = require('fast-xml-parser'); |
| 12 | + |
| 13 | +const SSR_CAPABILITIES = [ |
| 14 | + 'lightning__ServerRenderable', |
| 15 | + 'lightning__ServerRenderableWithHydration', |
| 16 | +]; |
| 17 | + |
| 18 | +const metaCache = new Map(); // Cache to store processed directories and their SSR status |
| 19 | + |
| 20 | +function hasSSRCapabilities(dir) { |
| 21 | + // Check if the result for this directory is cached |
| 22 | + if (metaCache.has(dir)) { |
| 23 | + return metaCache.get(dir); |
| 24 | + } |
| 25 | + |
| 26 | + const JS_META_REGEX = /\.js-meta\.xml$/i; // Regular expression to match meta.xml files |
| 27 | + const metaFile = fs.readdirSync(dir).find((file) => JS_META_REGEX.test(file)); |
| 28 | + |
| 29 | + if (!metaFile) { |
| 30 | + metaCache.set(dir, false); // Cache the result for this directory |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + const metaFilePath = path.join(dir, metaFile); |
| 35 | + const content = fs.readFileSync(metaFilePath, 'utf8'); |
| 36 | + let xmlRoot; |
| 37 | + try { |
| 38 | + xmlRoot = new XMLParser({ |
| 39 | + isArray: (_name, jPath) => jPath === 'LightningComponentBundle.capabilities', |
| 40 | + }).parse(content); |
| 41 | + } catch (error) { |
| 42 | + // If XML parsing fails, return false to indicate no SSR capabilities |
| 43 | + console.warn(`Failed to parse XML for ${metaFilePath}: ${error.message}`); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + const bundle = xmlRoot.LightningComponentBundle; |
| 48 | + const hasSSR = |
| 49 | + bundle && bundle.capabilities |
| 50 | + ? bundle.capabilities.some((capabilityObj) => |
| 51 | + Array.isArray(capabilityObj.capability) |
| 52 | + ? capabilityObj.capability.some((cap) => SSR_CAPABILITIES.includes(cap)) |
| 53 | + : SSR_CAPABILITIES.includes(capabilityObj.capability), |
| 54 | + ) |
| 55 | + : false; |
| 56 | + |
| 57 | + // Cache the result for future use |
| 58 | + metaCache.set(dir, hasSSR); |
| 59 | + return hasSSR; |
| 60 | +} |
| 61 | + |
| 62 | +// processor to only lint js files for components marked as ssrable in their .js-meta.xml capabilities |
| 63 | +module.exports = { |
| 64 | + preprocess(text, filename) { |
| 65 | + // If the file already has the .ssrjs return same file |
| 66 | + if (filename.endsWith('.ssrjs')) { |
| 67 | + return [text]; |
| 68 | + } |
| 69 | + const dirName = path.dirname(filename); // Extract the directory name from the full file path |
| 70 | + const baseFileName = path.basename(filename); |
| 71 | + const updatedFileName = baseFileName.replace(/(js|ts)$/, 'ssr$1'); |
| 72 | + // Check SSR capabilities before processing |
| 73 | + if (!hasSSRCapabilities(dirName)) { |
| 74 | + return [text]; |
| 75 | + } |
| 76 | + // Creates a copy file for every ssr component js file with `${filePath}/${fileName}.ssrjs` path |
| 77 | + return [text, { text, filename: updatedFileName }]; |
| 78 | + }, |
| 79 | + |
| 80 | + postprocess(messages) { |
| 81 | + // Return all messages for files that were processed |
| 82 | + return messages.flat(); |
| 83 | + }, |
| 84 | +}; |
0 commit comments