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