UNPKG

tpa-style-webpack-plugin

Version:

A Webpack plugin that handles wix tpa styles, it separates static css file that injects dynamic style at runtime.

56 lines (55 loc) 2.36 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractTPACustomSyntax = void 0; const replacers = __importStar(require("./replacers")); const postcss_1 = __importDefault(require("postcss")); function isCssVar(key) { return key.indexOf('--') === 0; } const customSyntaxRegex = /"\w+\([^"]*\)"/g; function collectCustomSyntaxFrom(value, customSyntaxStrs) { let match; if ((match = value.match(customSyntaxRegex))) { customSyntaxStrs.push(...match); } } exports.extractTPACustomSyntax = postcss_1.default.plugin('postcss-wix-tpa-style', (opts = {}) => { const cssVars = {}; const customSyntaxStrs = []; return (css) => { css.walkDecls((decl) => { Object.keys(replacers).forEach(replacerName => (decl = replacers[replacerName](decl))); if (isCssVar(decl.prop)) { cssVars[decl.prop] = decl.value; } collectCustomSyntaxFrom(decl.prop, customSyntaxStrs); collectCustomSyntaxFrom(decl.value, customSyntaxStrs); }); if (typeof opts.onFinish === 'function') { const uniqueCustomSyntaxStrs = customSyntaxStrs.filter((value, index, self) => self.indexOf(value) === index); opts.onFinish({ cssVars, customSyntaxStrs: uniqueCustomSyntaxStrs, css: css.toString() }); } }; });