项目原始demo,不改动
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # postcss-import
  2. [![Unix Build status](https://img.shields.io/travis/postcss/postcss-import/master.svg?branch=master&label=unix%20build)](https://travis-ci.org/postcss/postcss-import)
  3. [![Windows Build status](https://img.shields.io/appveyor/ci/MoOx/postcss-import/master.svg?label=window%20build)](https://ci.appveyor.com/project/MoOx/postcss-import/branch/master)
  4. [![Version](https://img.shields.io/npm/v/postcss-import.svg)](https://github.com/postcss/postcss-import/blob/master/CHANGELOG.md)
  5. [![Greenkeeper badge](https://badges.greenkeeper.io/postcss/postcss-import.svg)](https://greenkeeper.io/)
  6. > [PostCSS](https://github.com/postcss/postcss) plugin to transform `@import`
  7. rules by inlining content.
  8. This plugin can consume local files, node modules or web_modules.
  9. To resolve path of an `@import` rule, it can look into root directory
  10. (by default `process.cwd()`), `web_modules`, `node_modules`
  11. or local modules.
  12. _When importing a module, it will look for `index.css` or file referenced in
  13. `package.json` in the `style` or `main` fields._
  14. You can also provide manually multiples paths where to look at.
  15. **Notes:**
  16. - **This plugin should probably be used as the first plugin of your list.
  17. This way, other plugins will work on the AST as if there were only a single file
  18. to process, and will probably work as you can expect**.
  19. - This plugin works great with
  20. [postcss-url](https://github.com/postcss/postcss-url) plugin,
  21. which will allow you to adjust assets `url()` (or even inline them) after
  22. inlining imported files.
  23. - In order to optimize output, **this plugin will only import a file once** on
  24. a given scope (root, media query...).
  25. Tests are made from the path & the content of imported files (using a hash
  26. table).
  27. If this behavior is not what you want, look at `skipDuplicates` option
  28. - **If you are looking for glob, or sass like imports (prefixed partials)**,
  29. please look at
  30. [postcss-easy-import](https://github.com/trysound/postcss-easy-import)
  31. (which use this plugin under the hood).
  32. - Imports which are not modified (by `options.filter` or because they are remote
  33. imports) are moved to the top of the output.
  34. - **This plugin attempts to follow the CSS `@import` spec**; `@import`
  35. statements must precede all other statements (besides `@charset`).
  36. ## Installation
  37. ```console
  38. $ npm install postcss-import
  39. ```
  40. ## Usage
  41. Unless your stylesheet is in the same place where you run postcss
  42. (`process.cwd()`), you will need to use `from` option to make relative imports
  43. work.
  44. ```js
  45. // dependencies
  46. var fs = require("fs")
  47. var postcss = require("postcss")
  48. var atImport = require("postcss-import")
  49. // css to be processed
  50. var css = fs.readFileSync("css/input.css", "utf8")
  51. // process css
  52. postcss()
  53. .use(atImport())
  54. .process(css, {
  55. // `from` option is needed here
  56. from: "css/input.css"
  57. })
  58. .then(function (result) {
  59. var output = result.css
  60. console.log(output)
  61. })
  62. ```
  63. `css/input.css`:
  64. ```css
  65. /* can consume `node_modules`, `web_modules` or local modules */
  66. @import "cssrecipes-defaults"; /* == @import "../node_modules/cssrecipes-defaults/index.css"; */
  67. @import "normalize.css"; /* == @import "../node_modules/normalize.css/normalize.css"; */
  68. @import "foo.css"; /* relative to css/ according to `from` option above */
  69. @import "bar.css" (min-width: 25em);
  70. body {
  71. background: black;
  72. }
  73. ```
  74. will give you:
  75. ```css
  76. /* ... content of ../node_modules/cssrecipes-defaults/index.css */
  77. /* ... content of ../node_modules/normalize.css/normalize.css */
  78. /* ... content of css/foo.css */
  79. @media (min-width: 25em) {
  80. /* ... content of css/bar.css */
  81. }
  82. body {
  83. background: black;
  84. }
  85. ```
  86. Checkout the [tests](test) for more examples.
  87. ### Options
  88. ### `filter`
  89. Type: `Function`
  90. Default: `() => true`
  91. Only transform imports for which the test function returns `true`. Imports for
  92. which the test function returns `false` will be left as is. The function gets
  93. the path to import as an argument and should return a boolean.
  94. #### `root`
  95. Type: `String`
  96. Default: `process.cwd()` or _dirname of
  97. [the postcss `from`](https://github.com/postcss/postcss#node-source)_
  98. Define the root where to resolve path (eg: place where `node_modules` are).
  99. Should not be used that much.
  100. _Note: nested `@import` will additionally benefit of the relative dirname of
  101. imported files._
  102. #### `path`
  103. Type: `String|Array`
  104. Default: `[]`
  105. A string or an array of paths in where to look for files.
  106. #### `plugins`
  107. Type: `Array`
  108. Default: `undefined`
  109. An array of plugins to be applied on each imported files.
  110. #### `resolve`
  111. Type: `Function`
  112. Default: `null`
  113. You can provide a custom path resolver with this option. This function gets
  114. `(id, basedir, importOptions)` arguments and should return a path, an array of
  115. paths or a promise resolving to the path(s). If you do not return an absolute
  116. path, your path will be resolved to an absolute path using the default
  117. resolver.
  118. You can use [resolve](https://github.com/substack/node-resolve) for this.
  119. #### `load`
  120. Type: `Function`
  121. Default: null
  122. You can overwrite the default loading way by setting this option.
  123. This function gets `(filename, importOptions)` arguments and returns content or
  124. promised content.
  125. #### `skipDuplicates`
  126. Type: `Boolean`
  127. Default: `true`
  128. By default, similar files (based on the same content) are being skipped.
  129. It's to optimize output and skip similar files like `normalize.css` for example.
  130. If this behavior is not what you want, just set this option to `false` to
  131. disable it.
  132. #### `addModulesDirectories`
  133. Type: `Array`
  134. Default: `[]`
  135. An array of folder names to add to [Node's resolver](https://github.com/substack/node-resolve).
  136. Values will be appended to the default resolve directories:
  137. `["node_modules", "web_modules"]`.
  138. This option is only for adding additional directories to default resolver. If
  139. you provide your own resolver via the `resolve` configuration option above, then
  140. this value will be ignored.
  141. #### Example with some options
  142. ```js
  143. var postcss = require("postcss")
  144. var atImport = require("postcss-import")
  145. postcss()
  146. .use(atImport({
  147. path: ["src/css"],
  148. }))
  149. .process(cssString)
  150. .then(function (result) {
  151. var css = result.css
  152. })
  153. ```
  154. ## `dependency` Message Support
  155. `postcss-import` adds a message to `result.messages` for each `@import`. Messages are in the following format:
  156. ```
  157. {
  158. type: 'dependency',
  159. file: absoluteFilePath,
  160. parent: fileContainingTheImport
  161. }
  162. ```
  163. This is mainly for use by postcss runners that implement file watching.
  164. ---
  165. ## CONTRIBUTING
  166. * ⇄ Pull requests and ★ Stars are always welcome.
  167. * For bugs and feature requests, please create an issue.
  168. * Pull requests must be accompanied by passing automated tests (`$ npm test`).
  169. ## [Changelog](CHANGELOG.md)
  170. ## [License](LICENSE)