UNPKG

rollup-plugin-react-scoped-css

Version:

A rollup plugin designed to allow scoped css to be run in react (Compatible with vite and rollup)

41 lines (40 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addHashAttributesToJsxTagsAst = addHashAttributesToJsxTagsAst; const ast_iterator_1 = require("./ast-iterator"); const ast_transformer_1 = require("./ast-transformer"); const ast_utils_1 = require("./ast.utils"); const legacy_jsx_parser_1 = require("./parsers/legacy-jsx-parser"); const modern_jsx_parser_1 = require("./parsers/modern-jsx-parser"); const jsx_parser_model_1 = require("./parsers/jsx-parser.model"); global.implementation = null; const legacyJsxParser = new legacy_jsx_parser_1.LegacyJsxParser(); const modernJsxParser = new modern_jsx_parser_1.ModernJsxParser(); let parser = legacyJsxParser; const findImplementation = (program) => { const implementation = jsx_parser_model_1.ParserImplementations.legacy; for (const node of (0, ast_iterator_1.astIterator)(program)) { if (!(0, ast_utils_1.isNodeImportOrRequire)(node)) { continue; } const src = (0, ast_utils_1.getSrcFromImportOrRequire)(node); if (["react/jsx-dev-runtime", "react/jsx-runtime"].includes(src)) { return jsx_parser_model_1.ParserImplementations.modern; } } return implementation; }; function addHashAttributesToJsxTagsAst(program, attr) { // Once in the program, we can determine which parser to use if (global.implementation === null) { global.implementation = findImplementation(program); if (global.implementation === jsx_parser_model_1.ParserImplementations.modern) { parser = modernJsxParser; } } return (0, ast_transformer_1.astTransformer)(program, (node) => { if (parser.isNodeReactElement(node) && !parser.isNodeReactFragment(node)) { return parser.extendNodeWithAttributes(node, attr); } }); }