Skip to content

Instantly share code, notes, and snippets.

const {htmlTagNames} = require('html-tag-names')
// get a list of all known html tag names and convert it into a map example:
// {body: true, head: true, li: true, h1: true, p: true} and so on
const tags = htmlTagNames.reduce((res, tag) => {
res[tag] = true
return res
}, {})
@sachin-hg
sachin-hg / cx.js
Last active January 20, 2023 12:14
const cache = {}
const Obj = function (js, css, nonAtomic) {
this.__isAtomic = true
this.js = js
this.css = css
this.nonAtomic = nonAtomic
}
// inspired from https://github.com/callstack/linaria/blob/master/packages/server/src/collect.ts
const postcss = require('postcss')
const htmlTagNames = require('./htmlTagNames')
module.exports = function collect (css) {
const basic = postcss.root()
const map = {}
const stylesheet = postcss.parse(css)
const getClassNameRoot = rule => {
const regex = /(style|Style|Style.jsx|style.jsx)$/
function plugin1 (state) {
const { variables, modules, nodes, moduleVsImport } = state
return {
Program (path) {
state.programPath = path
},
ImportDeclaration (path) {
if (regex.test(path.node.source.value)) {
const parser = require('@babel/parser')
const { default: traverse } = require('@babel/traverse')
const { plugin1, plugin2 } = require('./find-cx-babel-plugin')
const t = require('@babel/types')
const cx = require('./cx.cjs')
const { default: generate } = require('@babel/generator')
const parse = (code, traverser) => {
let ast = parser.parse(code, {
sourceType: 'module',
// read this: https://github.com/reactwg/react-18/discussions/110
import React, { useMemo, useInsertionEffect, forwardRef } from 'react'
const insertedRules =
typeof window !== 'undefined' && window.__SSR_STYLES__
? window.__SSR_STYLES__
: {}
function getStyleForRule (url, content) {
if (content) {
import React, { createContext, forwardRef, useContext } from 'react'
import { useStream } from 'react-streaming'
export const StylesContext = createContext({ set: new Set() })
function assetToStyleString (asset) {
const { url, content } = asset
if (content) {
return `<style data-href="${url}">${content}</style>`
// const { default: generate } = require('@babel/generator')
module.exports = function ({ types: t }) {
let programPath
function getUniqueName (key = '__style') {
return programPath.scope.generateUidIdentifier(key).name
}
function getStyles (body, _this) {
let namedImports = []
let unnamedImports = []
@sachin-hg
sachin-hg / requestContext.js
Created November 24, 2022 09:02
Request Context creation example for module federation request context cache
// utils/requestContext.js
import { createNamespace } from 'cls-hooked'
// context variable here may be global, but the data stored is
// not shared among different requests
const context = createNamespace(`app:requestData`)
// use this middleware in your express middleware chain, as early as possible
export function expressMiddleware (req, res, next) {
context.run(function () {
// output: component.linaria.css
.some_unique_classname {
font-size: 14px;
}