Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import minify from './visitors/minify'
import displayNameAndId from './visitors/displayNameAndId'
import displayNameIdAndNamespace from './visitors/displayNameIdAndNamespace'
import templateLiterals from './visitors/templateLiterals'
import assignStyledRequired from './visitors/assignStyledRequired'
import { noParserImportDeclaration, noParserRequireCallExpression } from './visitors/noParserImport'
Expand All @@ -15,7 +15,7 @@ export default function({ types: t }) {
},
TaggedTemplateExpression(path, state) {
minify(path, state)
displayNameAndId(path, state)
displayNameIdAndNamespace(path, state)
templateLiterals(path, state)
},
VariableDeclarator(path, state) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ function getOption({ opts }, name, defaultValue = true) {
}

export const useDisplayName = (state) => getOption(state, 'displayName')
export const useSSR = (state) => getOption(state, 'ssr', false)
export const useFileName = (state) =>getOption(state, 'fileName')
export const useSSR = (state) => getOption(state, 'ssr', false)
export const useFileName = (state) => getOption(state, 'fileName')
export const useMinify = (state) => getOption(state, 'minify')
export const useCSSPreprocessor = (state) => getOption(state, 'preprocess', false) // EXPERIMENTAL
export const useTranspileTemplateLiterals = (state) => getOption(state, 'transpileTemplateLiterals')
export const useNamespaceClasses = state => getOption(state, 'namespaceClasses', null);
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as t from 'babel-types'
import { useFileName, useDisplayName, useSSR } from '../utils/options'
import { useFileName, useDisplayName, useSSR, useNamespaceClasses } from '../utils/options'
import getName from '../utils/getName'
import path from 'path'
import fs from 'fs'
import hash from '../utils/hash'
import { isStyled } from '../utils/detectors'

const addConfig = (path, displayName, componentId) => {
const addConfig = (path, displayName, componentId, namespaceClasses) => {
if (!displayName && !componentId) {
return
}
Expand All @@ -18,11 +18,17 @@ const addConfig = (path, displayName, componentId) => {
if (componentId) {
withConfigProps.push(t.objectProperty(t.identifier('componentId'), t.stringLiteral(componentId)))
}
if (namespaceClasses) {
const namespaceClassOption = Array.isArray(namespaceClasses) ?
t.arrayExpression(namespaceClasses.map(value => t.stringLiteral(value)))
: t.stringLiteral(namespaceClasses);
withConfigProps.push(t.objectProperty(t.identifier('namespaceClasses'), namespaceClassOption))
}

// Replace x`...` with x.withConfig({ })`...`
path.node.tag = t.callExpression(
t.memberExpression(path.node.tag, t.identifier('withConfig')),
[ t.objectExpression(withConfigProps) ]
[t.objectExpression(withConfigProps)]
)
}

Expand Down Expand Up @@ -99,7 +105,8 @@ export default (path, state) => {
addConfig(
path,
displayName && displayName.replace(/[^_a-zA-Z0-9-]/g, ''),
useSSR(state) && getComponentId(state)
useSSR(state) && getComponentId(state),
useNamespaceClasses(state)
)
}
}