UNPKG

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