Socket
Socket
Sign inDemoInstall

parcel-transformer-css-to-string

Package Overview
Dependencies
104
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    parcel-transformer-css-to-string

Importing CSS files as a string to JavaScript


Version published
Weekly downloads
15
increased by650%
Maintainers
1
Install size
702 kB
Created
Weekly downloads
 

Readme

Source

parcel-transformer-css-to-string

support parcel v2 ci status npm version

Importing CSS files as a string to JavaScript.

Transform plugin for Parcel v2

Support Parcel v1: parcel-plugin-css-to-string

Example

styles.inline.css

.text {
  color: #000000;
}

index.js

import styles from './styles.inline.css';

document.body.insertAdjacentHTML('beforeend', `<style>${styles}</style>`);

Result:

document.body.insertAdjacentHTML("beforeend","<style>.text{color:#000}</style>");
//# sourceMappingURL=index.js.map

Install

npm i parcel-transformer-css-to-string
# or
yarn add -D parcel-transformer-css-to-string

How to use

Add the plugin to transformers section in your .parcelrc.

In example use a Glob pattern for *.inline.css files that will be inlined as a string into JavaScript.

.parcelrc

{
  "extends": "@parcel/config-default",
  "transformers": {
    "*.inline.css": [
      "parcel-transformer-css-to-string"
    ]
  }
}

Minify config

You can configure minify CSS in production build, where custom configuration can be set by creating cssnano.config.js file

cssnano.config.js

module.exports = {
  preset: [
    "default",
    {
      calc: false,
      discardComments: {
        removeAll: true,
      },
    },
  ],
}

PostCSS

You can configure CSS transforming with PostCSS creating a configuration file using one of these names (in that priority): .postcssrc (JSON), .postcssrc.json, .postcssrc.js, or postcss.config.js.

If you use PostCSS config then you need added cssnano plugin to minify production build.

.postcssrc

{
  "plugins": {
    "postcss-import": {},
    "autoprefixer": {},
    "cssnano": {}
  }
}

Alternative

You can use official build-in named pipelines bundle-text:. In this case Parcel create a JavaScript module.

style.css

.text {
  color: #000000;
}

index.js

import styles from 'bundle-text:./styles.css';

document.body.insertAdjacentHTML('beforeend', `<style>${styles}</style>`);

Reslut:

function e(e){return e&&e.__esModule?e.default:e}document.body.insertAdjacentHTML("beforeend",`<style>${e(".text{color:#000}")}</style>`);
//# sourceMappingURL=index.js.map

License

MIT

Keywords

FAQs

Last updated on 14 Apr 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc