postcss-import

PostCSS plugin to inline @import rules content
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 秦景福
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

postcss-import

PostCSS plugin to transform @importrules by inlining content.

This plugin can consume local files, node modules or web_modules.To resolve path of an @import rule, it can look into root directory(by default process.cwd()), web_modules, node_modulesor local modules.When importing a module, it will look for index.css or file referenced inpackage.json in the style or main fields.You can also provide manually multiples paths where to look at.

Notes:

  • This plugin should probably be used as the first plugin of your list.This way, other plugins will work on the AST as if there were only a single fileto process, and will probably work as you can expect.
  • This plugin works great withpostcss-url plugin,which will allow you to adjust assets url() (or even inline them) afterinlining imported files.
  • In order to optimize output, this plugin will only import a file once ona given scope (root, media query...).Tests are made from the path & the content of imported files (using a hashtable).If this behavior is not what you want, look at skipDuplicates option
  • If you are looking for glob, or sass like imports (prefixed partials),please look atpostcss-easy-import(which use this plugin under the hood).
  • Imports which are not modified (by options.filter or because they are remoteimports) are moved to the top of the output.
  • This plugin attempts to follow the CSS @import spec; @importstatements must precede all other statements (besides @charset).

Installation

$ npm install -D postcss-import

Usage

Unless your stylesheet is in the same place where you run postcss(process.cwd()), you will need to use from option to make relative importswork.

// dependencies
const fs = require("fs")
const postcss = require("postcss")
const atImport = require("postcss-import")

// css to be processed
const css = fs.readFileSync("css/input.css", "utf8")

// process css
postcss()
  .use(atImport())
  .process(css, {
    // `from` option is needed here
    from: "css/input.css"
  })
  .then((result) => {
    const output = result.css

    console.log(output)
  })

css/input.css:

/* can consume `node_modules`, `web_modules` or local modules */
@import "cssrecipes-defaults"; /* == @import "../node_modules/cssrecipes-defaults/index.css"; */
@import "normalize.css"; /* == @import "../node_modules/normalize.css/normalize.css"; */

@import "foo.css"; /* relative to css/ according to `from` option above */

@import "bar.css" (min-width: 25em);

body {
  background: black;
}

will give you:

/* ... content of ../node_modules/cssrecipes-defaults/index.css */
/* ... content of ../node_modules/normalize.css/normalize.css */

/* ... content of css/foo.css */

@media (min-width: 25em) {
/* ... content of css/bar.css */
}

body {
  background: black;
}

Checkout the tests for more examples.

Options

filter

Type: Function
Default: () => true

Only transform imports for which the test function returns true. Imports forwhich the test function returns false will be left as is. The function getsthe path to import as an argument and should return a boolean.

root

Type: String
Default: process.cwd() or dirname ofthe postcss from

Define the root where to resolve path (eg: place where node_modules are).Should not be used that much.
Note: nested @import will additionally benefit of the relative dirname ofimported files.

path

Type: String|Array
Default: []

A string or an array of paths in where to look for files.

plugins

Type: Array
Default: undefined

An array of plugins to be applied on each imported files.

resolve

Type: Function
Default: null

You can provide a custom path resolver with this option. This function gets(id, basedir, importOptions) arguments and should return a path, an array ofpaths or a promise resolving to the path(s). If you do not return an absolutepath, your path will be resolved to an absolute path using the defaultresolver.You can use resolve for this.

load

Type: Function
Default: null

You can overwrite the default loading way by setting this option.This function gets (filename, importOptions) arguments and returns content orpromised content.

skipDuplicates

Type: Boolean
Default: true

By default, similar files (based on the same content) are being skipped.It's to optimize output and skip similar files like normalize.css for example.If this behavior is not what you want, just set this option to false todisable it.

addModulesDirectories

Type: Array
Default: []

An array of folder names to add to Node's resolver.Values will be appended to the default resolve directories:["node_modules", "web_modules"].

This option is only for adding additional directories to default resolver. Ifyou provide your own resolver via the resolve configuration option above, thenthis value will be ignored.

Example with some options

const postcss = require("postcss")
const atImport = require("postcss-import")

postcss()
  .use(atImport({
    path: ["src/css"],
  }))
  .process(cssString)
  .then((result) => {
    const { css } = result
  })

dependency Message Support

postcss-import adds a message to result.messages for each @import. Messages are in the following format:

{
  type: 'dependency',
  file: absoluteFilePath,
  parent: fileContainingTheImport
}

This is mainly for use by postcss runners that implement file watching.


CONTRIBUTING

  • ⇄ Pull requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
  • Pull requests must be accompanied by passing automated tests ($ npm test).

Changelog

License

 相关资料
  • 由vue-loader 处理的 CSS 输出,都是通过 PostCSS 进行作用域重写,你还可以为 PostCSS 添加自定义插件,例如 autoprefixer 或者 CSSNext。 使用配置文件 vue-loader 支持通过 postcss-loader 自动加载同一个配置文件: postcss.config.js .postcssrc package.json 中的 postcss 使用

  • vue-loader处理的任何CSS输出都通过PostCSS进行管道传输以进行作用域CSS重写。您还可以向过程添加自定义PostCSS插件,例如autoprefixer 或 CSSNext。 使用配置文件 从11.0开始vue-loader支持自动加载postcss-loader支持的相同PostCss配置文件: postcss.config.js .postcssrc postcss field

  • PostCSS 是使用 JS 插件来转换 CSS 的工具,支持变量,混入,未来 CSS 语法,内联图像等等。 PostCSS 已经被许多大公司使用:谷歌,推特,阿里巴巴和 Shopify。 Autoprefixer PostCSS 插件是最流行的 CSS 处理器插件之一。 PostCSS 可以作为预处理器使用,类似:Sass, Less 和 Stylus。但是 PostCSS 是模块化的工具,比之

  • PostCSS plugin for wxss. 专门为 wxss 格式化处理的的一个 postcss 插件,特别是在做 css 转 wxss 的时候好用到爆。 实现的功能 清理 wxss 不支持的选择器。 清理 wxss 不支持的注释。 转换 rem 单位到 rpx。 转换 Web 的标签选择器到小程序的 class 选择器。 style scoped(postcss插件部分)。 例子 /* 被

  • postcss-each 是一个 postcss 的插件,用来迭代数值。 迭代数值: @each $icon in foo, bar, baz {  .icon-$(icon) {    background: url('icons/$(icon).png');  }} .icon-foo {  background: url('icons/foo.png');}.icon-bar {  back

  • postcss-less 是一个用 PostCSS 语法解析 LESS 的模块 注意: 这个模块需要 Node v6.14.4 以上版本。poscss-less 不是 LESS 编译器。对于 LESS 的编译,请使用 LESS 的官方工具集。 安装 使用 npm: npm install postcss-less --save-dev 使用方法 postcss-less 最常见的用途是将 Post