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

rollup-plugin-postcss-inject-to-css

v1.0.2

Published

rollup postcss 把inline的js文件转换成以css文件的方式引入

Downloads

593

Readme

rollup-plugin-postcss-inject-to-css

NPM version NPM downloads

rollup-plugin-postcss的 inject 模式下,把导出后组件引用的xxx.scss.js转换为xxx.css进行引入

本插件依赖 rollup-plugin-postcss ,主要是针对组件按需加载的场景下进行的优化适配

Install

npm:

npm install -D rollup-plugin-postcss-inject-to-css

yarn:

yarn add -D rollup-plugin-postcss-inject-to-css

Usage

// rollup.config.js

import RollPostcss from 'rollup-plugin-postcss'
import RollPostcssInject2Css from 'rollup-plugin-postcss-inject-to-css'

export default {
  input,
  output,
  plugins: [
    RollPostcss({
      extract: false, // 非导出模式
      inject: true,  // 内联模式
      plugins: []
    }),
    RollPostcssInject2Css()
  ]
}

Demo

痛点:

rollup-plugin-postcss在打包的时候,打包出来的css如果选择extract模式,会把所有样式都打包到一个入口文件下,若选择inject模式,则是通过把组件样式都嵌入到<style>标签里。

extract的问题:不方便进行组件模块的css按需加载 inject的问题:样式内联在<style>标签下,权重会比<link>引入的高,不符合组件的设计逻辑

解决问题:

在实现按需加载的时候,除了组件的js代码需要按需加载,样式文件同样需要,采用rollup的output.preserveModules搭配babel-plugin-import插件即可实现组件按需加载

实例:

├── packages (组件目录)
│   ├── componentA (组件A)
│   │   ├── index.js 
│   │   └── index.scss 
│   ├── componentB (组件B)
│   │   ├── index.js 
│   │   └── index.scss 
  • extract 模式下 编译后:====>
├── packages (组件目录)
│   ├── componentA (组件A)
│   │   ├── index.js 
│   ├── componentB (组件B)
│   │   ├── index.js 
│   ├── index.css (入口样式文件 包括A和B)
  • inject 模式下 编译后:====>
├── packages (组件目录)
│   ├── componentA (组件A)
│   │   ├── index.js (组件引入样式import './index.scss.js')
│   │   └── index.scss.js
│   ├── componentB (组件B)
│   │   ├── index.js 
│   │   └── index.scss.js
  • 使用了rollup-plugin-postcss-inject-to-cssinject模式 编译后:====>
├── packages (组件目录)
│   ├── componentA (组件A)
│   │   ├── index.js  (组件引入样式import './index.css')
│   │   └── index.css
│   ├── componentB (组件B)
│   │   ├── index.js 
│   │   └── index.css