npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xstyled-theme/config

v1.4.2

Published

Tools to add xstyled-theme support to your app's tooling

Downloads

6

Readme

@xstyled-theme/config

This package helps your project link to a @xstyled-theme theme. It provides configuration hooks for Webpack and Jest, so that the import alias ~theme in your code is resolved to the location of your current theme.

Install

Install the dependencies needed to run this project. Note that @xstyled and styled-components should be installed as peer dependencies if your project is a library meant to be used in third-party apps.

In Apps

yarn add @xstyled/styled-components @xstyled/system styled-components
yarn add -D @xstyled-theme/config

In Libraries

yarn add -P @xstyled/styled-components @xstyled/system styled-components
yarn add -D @xstyled-theme/config

Usage

In a React App

@xstyled-theme/config can be used to inject a theme in a React App. Note that we only support apps generated with the typescript template.

Prerequisites

You'll need an extra dependency to customise your React App configuration. Install it with:

yarn add -D react-app-rewired

Next, ensure your package.json file uses the react-app-rewired wrapper to let us inject configuration into your React App. Replace the existing scripts with the following:

  "scripts": {
    "build": "react-app-rewired build",
    "start": "react-app-rewired start",
    "test": "react-app-rewired test"

  },

Pick a Theme

The most common way to select a theme to use is via the @xstyled-theme/cli. The xstyled-theme set theme-squirrels would make the theme-squirrels package your app's current theme, for instance.

Themes are saved in the xstyledTheme.config.js file. With the above example, this file would contain:

module.exports = 'theme-squirrels'

Use @xstyled-theme/config

Create a file named config-overrides.js, containing:

const { withXstyledThemeWebpack, withXstyledThemeJest } = require('@xstyled-theme/config')
const theme = require('./xstyledTheme.config')

module.exports = {
  webpack: withXstyledThemeWebpack(theme),
  jest: withXstyledThemeJest(theme),
}

Or if you want to apply additional overrides:

const { withXstyledThemeWebpack, withXstyledThemeJest } = require('@xstyled-theme/config')
const theme = require('./xstyledTheme.config')

module.exports = {
  webpack: (config, env) => {
    const ownConfig = withXstyledThemeWebpack(theme)(config, env)

    // Your other overrides can be applied here to `ownConfig`.

    return ownConfig
  },
  jest: withXstyledThemeJest(theme),
}

In a UI Toolkit

Webpack

UI toolkits need to ensure they do not bundle React, styled-components or other packages that mustn't be duplicated in the dependency chain. @xstyled-theme/config comes with an option to declare such packages as externals. Here is what your webpack.config.js could look like:

const { withXstyledThemeWebpack } = require('@xstyled-theme/config')
const theme = require('./xstyledTheme.config')

module.exports = async (webpackEnv) => {
	const config = {
		entry: path.resolve(__dirname, 'src', 'index.ts'),
		output: {
			path: path.resolve(__dirname, 'build'),
			filename: 'index.js',
		},
		// etc
	}

	return withXstyledThemeWebpack(theme, { externals: true })(config)
}

You may even pass a null value to the theme parameter if you only want to get the Webpack externals configured without providing a default theme in your UI library code.

Jest

In your jest.config.js, you can even use a different theme against which you write your tests (make sure that theme is installed in your development dependencies, though):

const { withXstyledThemeJest } = require('@xstyled-theme/config')

module.exports = withXstyledThemeJest('my-test-theme')({
	roots: ['<rootDir>/src'],
	coverageReporters: ['json', 'lcov', 'text'],
	testMatch: ['**/__tests__/**/*.test.(js|ts|jsx|tsx)'],
})

In StoryBook

Below is a sample configuration for Storybook, to be written in .storybook/main.js, and tested on StoryBook 5.3:

const path = require('path')
const webpackConfig = require('../webpack.config')
const { withXstyledThemeWebpack } = require('@xstyled-theme/config')
const theme = require('../xstyledTheme.config')

module.exports = {
	stories: [ /* ... */ ],
	addons: [ /* ... */ ],
	webpackFinal: async config => withXstyledThemeWebpack(theme)(config)
}

Jest transformIgnorePatterns

It's very common to find, in Jest configs, a line that prevents transformation of dependencies found in node_modules, through the transformIgnorePatterns option. Unfortunately, @xstyled-theme provides Typescript files which must be transformed, so this approach is unsuitable. We automatically remove the following lines from transformIgnorePatterns to avoid a compatibility issue:

  • <rootDir>/node_modules/
  • <rootDir>/node_modules
  • /node_modules/
  • /node_modules
  • node_modules/
  • node_modules

We then inject a new line that filters all node_modules except the ones we want to have transformed.

Ignore Additional Patterns

If you also need other modules to be transformed, add their names to the modulesToTransform option. For example, if you used to ignore every module except foo and bar, as well as the vendors directory, your original jest.config.js file should look like this:

module.exports = {
	transformIgnorePatterns: ['<rootDir>/node_modules/(?!foo|bar)', '<rootDir>/vendors/'],
}

You should change it to:

const { withXstyledThemeJest } = require('@xstyled-theme/config')
const theme = require('./xstyledTheme.config')

module.exports = withXstyledThemeJest(theme, {
	modulesToTransform: ['foo', 'bar'],
})({
	transformIgnorePatterns: ['<rootDir>/vendors/'],
})

Other Options

forceLocalStyledComponents

This flag forces the styled-components dependency to be resolved to the one in the local project directory, using an alias.

When developing apps that use an @xstyled-theme UI toolkit, we noticed that our UI toolkit styled-components devDependency would be used rather than the app's runtime dependency. This caused a duplicate React instance and broke our app. This can happen in development mode when using symbolic links or NPM links to local packages, or if you accidentally publish your UI toolkit's dependencies folder to NPM.

The forceLocalStyledComponents flag is set to true when externals is unset, and to false when externals is set to true. This way, UI toolkits don't alias styled-components, but apps do and force UI toolkits, regardless of their node_modules content, to use the same unique instance of styled-components.

projectDir

By default, the project directory will be computed to be where your package.json resides. However, if you are calling your build scripts from outside your project root directory, we recommend you manually set the projectDir option to the absolute path to your package.json, as we cannot compute it as reliably as you in advance.

This is used to compute the styled-components alias used when forceLocalStyledComponents is set to true.

Typescript – Coming Soon

Some aspects are not yet covered in this doc, including support for the tsconfig.json file. Please notify the developers if you need help running @xstyled-theme/config with Typescript until this part of the doc is written up.

We also don't explain yet how to import type declarations from the theme. This will also be documented in the future but is still subject to minor adjustments.

WARNING: it is now looking increasingly likely that we drop Typescript support, as we have dropped it in our UI toolkit due to a very long list of grievances, but also because Typescript is causing significant support overload in @xstyled-theme and bugs in enum generation based on your theme key names and your icon and font names.

Usage Example

The following code defines an API to style a component with an @xstyled-theme theme, loads the current theme, and provides the theme's variables into the styled function as props using styled-component attrs.

import styledFactory from '~theme/components/MyComponent.style'

const MyComponentApi = {
	textColor: null,
	textDecoration: null,
	textTransform: null,
}

const styled = styledFactory(MyComponentApi)

export const MyComponentRoot = styled('button')(
	(props) => css`
		color: ${props.textColor};
		text-decoration: ${props.textDecoration};
		text-transform: ${props.textTransform};
	`
)