You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.4 KiB

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