Giter VIP home page Giter VIP logo

parcel-transformer-css-to-string's Introduction

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

parcel-transformer-css-to-string's People

Contributors

dependabot[bot] avatar shoonia avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

parcel-transformer-css-to-string's Issues

CSS imports are not processed when using the plugin

I have the following css file that I want to inline:

@import 'npm:tippy.js/dist/tippy.css';
@import 'npm:tippy.js/themes/light.css';

.iframe-preview,
.wikipedia-preview {
	width: 600px;
	height: 500px;
	border: none;
	padding: 0.4em 2px 2px;
}
...

When processed by the plugin it becomes the following string:

exports.default = "@import 'npm:tippy.js/dist/tippy.css';\n@import 'npm:tippy.js/themes/light.css';\n\n.iframe-preview,\n.wikipedia-preview {\n\twidth: 600px;\n\theight: 500px;\n\tborder: none;\n\tpadding: 0.4em 2px 2px;\n}\n\n.tippy-content {\n\tpadding: 0;\n}\n\n.text-fragment-preview {\n\tpadding: 1px 0.5em;\n}\n";

Expected behavior: Imports are processed and the results are also inlined

parcel-transformer-css-to-string: true is not a PostCSS plugin

The plugin seems to work fine with parcel in watch mode. But when I try to run parcel build, : get the following error:

parcel-transformer-css-to-string: true is not a PostCSS plugin

  Error: true is not a PostCSS plugin
      at Processor.normalize (\node_modules\postcss\lib\processor.js:145:15)
      at new Processor (\node_modules\postcss\lib\processor.js:51:25)
      at postcss (\node_modules\postcss\lib\postcss.js:73:10)
      at Object.transform (\node_modules\parcel-transformer-css-to-string\dist\index.cjs:87:15)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Transformation.runTransformer (\node_modules\@parcel\core\lib\Transformation.js:653:5)
      at async Transformation.runPipeline (\node_modules\@parcel\core\lib\Transformation.js:402:36)
      at async Transformation.runPipelines (\node_modules\@parcel\core\lib\Transformation.js:265:40)
      at async Transformation.runPipelines (\node_modules\@parcel\core\lib\Transformation.js:309:34)
      at async Transformation.run (\node_modules\@parcel\core\lib\Transformation.js:183:21)

I'm not using PostCSS, or at least it's not configured to be used anyway. Is there a way to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.