UNPKG

5.99 kBMarkdownView Raw
1# postcss-import
2
3[![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)
4[![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)
5[![Version](https://img.shields.io/npm/v/postcss-import.svg)](https://github.com/postcss/postcss-import/blob/master/CHANGELOG.md)
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 looks 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- This plugin works great with
24[postcss-url](https://github.com/postcss/postcss-url) plugin,
25which will allow you to adjust assets `url()` (or even inline them) after
26inlining imported files.
27- In order to optimize output, **this plugin will only import a file once** on
28a given scope (root, media query...).
29Tests are made from the path & the content of imported files (using a hash
30table).
31If this behavior is not what you want, look at `skipDuplicates` option
32
33## Installation
34
35```console
36$ npm install postcss-import
37```
38
39## Usage
40
41If your stylesheets are not in the same place where you run postcss
42(`process.cwd()`), you will need to use `from` option to make relative imports
43work from input dirname.
44
45```js
46// dependencies
47var fs = require("fs")
48var postcss = require("postcss")
49var atImport = require("postcss-import")
50
51// css to be processed
52var css = fs.readFileSync("css/input.css", "utf8")
53
54// process css
55postcss()
56 .use(atImport())
57 .process(css, {
58 // `from` option is required so relative import can work from input dirname
59 from: "css/input.css"
60 })
61 .then(function (result) {
62 var output = result.css
63
64 console.log(output)
65 })
66```
67
68Using this `input.css`:
69
70```css
71/* can consume `node_modules`, `web_modules` or local modules */
72@import "cssrecipes-defaults"; /* == @import "./node_modules/cssrecipes-defaults/index.css"; */
73@import "normalize.css"; /* == @import "./node_modules/normalize.css/normalize.css"; */
74
75@import "css/foo.css"; /* relative to stylesheets/ according to `from` option above */
76
77@import "css/bar.css" (min-width: 25em);
78
79body {
80 background: black;
81}
82```
83
84will give you:
85
86```css
87/* ... content of ./node_modules/cssrecipes-defaults/index.css */
88/* ... content of ./node_modules/normalize.css/normalize.css */
89
90/* ... content of foo.css */
91
92@media (min-width: 25em) {
93/* ... content of bar.css */
94}
95
96body {
97 background: black;
98}
99```
100
101Checkout [tests](test) for more examples.
102
103### Options
104
105#### `root`
106
107Type: `String`
108Default: `process.cwd()` or _dirname of
109[the postcss `from`](https://github.com/postcss/postcss#node-source)_
110
111Define the root where to resolve path (eg: place where `node_modules` are).
112Should not be used that much.
113_Note: nested `@import` will additionally benefit of the relative dirname of
114imported files._
115
116#### `path`
117
118Type: `String|Array`
119Default: `[]`
120
121A string or an array of paths in where to look for files.
122
123#### `transform`
124
125Type: `Function`
126Default: `null`
127
128A function to transform the content of imported files. Take one argument (file
129 content) and should return the modified content or promise with it.
130`undefined` result will be skipped.
131
132#### `plugins`
133
134Type: `Array`
135Default: `undefined`
136
137An array of plugins to be applied on each imported files.
138
139#### `onImport`
140
141Type: `Function`
142Default: `null`
143
144Function called after the import process. Take one argument (array of imported
145files).
146
147#### `resolve`
148
149Type: `Function`
150Default: `null`
151
152You can overwrite the default path resolving way by setting this option.
153This function gets `(id, basedir, importOptions)` arguments and returns full
154path, array of paths or promise resolving paths.
155You can use [resolve](https://github.com/substack/node-resolve) for that.
156
157#### `load`
158
159Type: `Function`
160Default: null
161
162You can overwrite the default loading way by setting this option.
163This function gets `(filename, importOptions)` arguments and returns content or
164promised content.
165
166#### `skipDuplicates`
167
168Type: `Boolean`
169Default: `true`
170
171By default, similar files (based on the same content) are being skipped.
172It's to optimize output and skip similar files like `normalize.css` for example.
173If this behavior is not what you want, just set this option to `false` to
174disable it.
175
176#### `addDependencyTo`
177
178Type: `Function`
179Default: null
180
181Allow to generate and call a callback that take one argument, the object from
182which you need to call `addDependency` from.
183Called whenever a file is imported, handy in a webpack workflow.
184It's equivalent to `onImport` with the following code:
185
186```js
187{
188 onImport: function (files) {
189 files.forEach(this.addDependency)
190 }.bind(obj) // obj = the argument you should pass to `addDependencyTo()`
191}
192```
193
194#### Example with some options
195
196```js
197var postcss = require("postcss")
198var atImport = require("postcss-import")
199
200postcss()
201 .use(atImport({
202 path: ["src/css"]
203 transform: require("css-whitespace")
204 }))
205 .process(cssString)
206 .then(function (result) {
207 var css = result.css
208 })
209```
210
211---
212
213## CONTRIBUTING
214
215* ⇄ Pull requests and ★ Stars are always welcome.
216* For bugs and feature requests, please create an issue.
217* Pull requests must be accompanied by passing automated tests (`$ npm test`).
218
219## [Changelog](CHANGELOG.md)
220
221## [License](LICENSE)