0.9.2 • Published 1 year ago

parcel-transformer-css-to-string v0.9.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

0.9.2

1 year ago

0.9.0-rc

3 years ago

0.9.0

3 years ago

0.9.1

3 years ago

0.8.0

3 years ago

0.7.0-rc.0

3 years ago

0.7.0

3 years ago

0.6.0-rc.0

3 years ago

0.6.0

3 years ago

0.3.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.5.0-rc.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago