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

@hnrchrdl/babel-plugin-import-sideeffects

v1.0.2

Published

Transform import statements to include side effects.

Downloads

4

Readme

@hnrchrdl/babel-plugin-import-sideeffects

For any import statement, import one or more sideeffects.

Why?

This babel plugin enables you to import any sideeffects. This can be useful for example when you import components that need css, but don't have them bundled. Of course it is not limited to css imports, you can import any code with sideeffects.

This Solution

In your code, you have an import statement like this

import MyComponent from '@my/library'

This plugin would allow you to change these imports to something like this:

import '@my/library/styles.css'
import MyComponent from '@my/library'

See examples below on how to configure the plugin to achieve this.

Installation

# yarn
yarn add -D @hnrchrdl/babel-plugin-import-sideeffects
# npm
npm install --save-dev @hnrchrdl/babel-plugin-import-sideeffects

Basic Usage

// in babel.config.json
{
  plugins: [
    ["@hnrchrdl/babel-plugin-import-sideeffects", {
      sideEffects: {
        "^@some/package$": "[*]/style.css"
      }
    }]
  ]
}

Options

sideEffects

object

default: {}

This is an object that declares the patterns for the sideeffects. The object's keys are a regexp pattern to match import sources, the value is a string or an array of strings that hold the import source(s) of sideeffects. For example:

sideEffects: {
  "^@my/package$": "[*]/style.css"
}

For full control, the value can also be a function, that takes an object as argument, holding the relevant placeholders. For example:

sideEffects: {
  "@scope/components$": ({ source, scope, package: pkgName, specifier }) => {
    return `${scope}/${pkgName}/dist/${specifier}/styles.css`;
  },
}

insert

'before' | 'after'

default 'before'

Option to prepend or append sideffects. Default is to prepend.

Placeholders

You can use placeholders to map to some specific import source.

[*]: The full original import source, e.g. '@reach/accordion' if you import from @reach/accordion. It will include all paths from the import statement as well.

[scope]: The scope of the package, e.g. '@reach' if you import from @reach/accordion.

[package]: The package name of the package, e.g. 'accordion' if you import from @reach/accordion. If the package is not scoped, this will simply be the package name.

[specifier]: The specfier for named imports, e.g. for an import statement like import MyComponent from "my-package" this will be replace with MyComponent.

Examples

// plugin config
["@hnrchrdl/babel-plugin-import-sideeffects", {
  sideEffects: {
    "^@reach/(accordion|checkbox)$": "[*]/style.css"
  }
}]
// code
import { Accordion,  AccordionItem,  AccordionButton,  AccordionPanel } from "@reach/accordion";
// output
import '@reach/accordion/styles.css'
import { Accordion,  AccordionItem,  AccordionButton,  AccordionPanel,} from "@reach/accordion";
// plugin config
["@hnrchrdl/babel-plugin-import-sideeffects", {
  sideEffects: {
    "^my-awesome-library$": ["[package]/global.css", "[package]/[specifier]/style.css"]
  }
}]
// code
import { Component1, Component2 } from 'my-awesome-library'
// output
import 'my-awesome-library/global.css'
import 'my-awesome-library/Component1/styles.css'
import 'my-awesome-library/Component2/styles.css'
import { Component1, Component2 } from 'my-awesome-library'
// plugin config
["@hnrchrdl/babel-plugin-import-sideeffects", {
  sideEffects: {
    "^my-awesome-library": ["[*]/style.css"]
  }
}]
// code
import Component1 from 'my-awesome-library/component1'
import Component2 from 'my-awesome-library/component2'
// output
import 'my-awesome-library/component1/styles.css'
import 'my-awesome-library/component2/styles.css'
import Component1 from 'my-awesome-library/component1'
import Component2 from 'my-awesome-library/component2'
// plugin config
["@hnrchrdl/babel-plugin-import-sideeffects", {
  sideEffects: {
    "^@my-scope/my-awesome-library$": ({ scope, pkgName, specifier }) =>
      `@${scope}/${pkgName}/dist/${specifier.toLowerCase()}/styles.css`
  },
  insert: 'after'
}]
// code
import { Component } from '@my-scope/my-awesome-library'
// output
import { Component } from '@my-scope/my-awesome-library'
import '@my-scope/my-awesome-library/dist/component/styles.css'