UNPKG

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