@@ -49,22 +49,91 @@ console.log(output);
49
49
50
50
Creates a new plugin for rework that will import files from NPM.
51
51
52
- Valid options:
53
-
54
- * ` root ` : The root directory for the source files. This is used for source maps
55
- to make imported file names relative to this directory, and for finding the
56
- absolute path for the top level source file. Example: ` root: 'src/client' `
57
- * ` shim ` : If you need to import packages that do not specify a ` style `
58
- property in their ` package.json ` or provide their styles in ` index.css ` ,
59
- you can provide a shim config option to access them. This is specified as a
60
- hash whose keys are the names of packages to shim and whose values are the
61
- path, relative to that package's ` package.json ` file, where styles can be
62
- found. Example: ` shim: { 'leaflet': 'dist/leaflet.css' } `
63
- * ` alias ` : You can provide aliases for arbitrary file paths using the same
64
- format as the ` shim ` option. These files must be complete file paths,
65
- relative to the ` root ` directory. Example:
66
- ` alias: { 'tree': './deep/tree/index.css' } `
67
- * ` prefilter ` : A function that will be called before an imported file is
68
- parsed. This function will be called with the file contents and the full file
69
- path. This option can be used to convert other languages such as SCSS to CSS
70
- before importing. Example: ` prefilter: function(src, file) { return src; } `
52
+ ## Options
53
+
54
+ ### root
55
+ The root directory for the source files. This is used for source maps to make
56
+ imported file names relative to this directory, and for finding the absolute
57
+ path for the top level source file.
58
+
59
+ Example:
60
+
61
+ ``` js
62
+ // Uses `<dir>/src/index.css` as the file path for the top level file. Also all
63
+ // file paths in the source map will be relative to the `<dir>/src` folder.
64
+ rework (' @import "./abc";' , { source: ' index.css' })
65
+ .use (reworkNPM ({ root: path .join (__dirname , ' src' ) }))
66
+ .toString ();
67
+ ```
68
+
69
+ ### shim
70
+ If you need to import packages that do not specify a ` style ` property in their
71
+ ` package.json ` or provide their styles in ` index.css ` , you can provide a shim
72
+ config option to access them. This is specified as a hash whose keys are the
73
+ names of packages to shim and whose values are the path, relative to that
74
+ package's ` package.json ` file, where styles can be found.
75
+
76
+ Example:
77
+
78
+ ``` js
79
+ // Imports the `dist/leaflet.css` file from the `leaflet` package
80
+ rework (' @import "leaflet";' , { source: ' index.css' })
81
+ .use (reworkNPM ({ shim: { ' leaflet' : ' dist/leaflet.css' } }))
82
+ .toString ();
83
+ ```
84
+
85
+ ### alias
86
+
87
+ You can provide aliases for arbitrary import paths, including files and
88
+ directories. When importing a file, it will search all directories in the path
89
+ for aliases also. Note that relative imports are never aliased.
90
+
91
+ This is specified as an object where the keys are the name of the import path to
92
+ alias, and the values are the file or directory path for the destination,
93
+ relative to the ` root ` option.
94
+
95
+ Example:
96
+
97
+ ``` js
98
+ // Imports the `styles/util.css` file
99
+ rework (' @import "util";' , { source: ' index.css' })
100
+ .use (reworkNPM ({ alias: { ' util' : ' styles/util.css' } }))
101
+ .toString ();
102
+ ```
103
+
104
+ ``` js
105
+ // Imports the `styles/index.css` file if there is a `styles` directory,
106
+ // otherwise the `styles.css` file.
107
+ rework (' @import "util";' , { source: ' index.css' })
108
+ .use (reworkNPM ({ alias: { ' util' : ' styles' } }))
109
+ .toString ();
110
+ ```
111
+
112
+ ``` js
113
+ // Imports the `styles/other.css` file
114
+ rework (' @import "util/other";' , { source: ' index.css' })
115
+ .use (reworkNPM ({ alias: { ' util' : ' styles' } }))
116
+ .toString ();
117
+ ```
118
+
119
+ ### prefilter
120
+ A function that will be called before an imported file is parsed. This function
121
+ will be called with the file contents and the full file path. This option can be
122
+ used to convert other languages such as SCSS to CSS before importing.
123
+
124
+ Example:
125
+
126
+ ``` js
127
+ // Process SCSS files
128
+ rework (' @import "./some-file.scss";' , { source: ' index.css' })
129
+ .use (reworkNPM ({ prefilter: compile }))
130
+ .toString ();
131
+
132
+ function compile (src , file ) {
133
+ if (path .extname (file) === ' .scss' ) {
134
+ return compileScss (src);
135
+ }
136
+
137
+ return src;
138
+ }
139
+ ```
0 commit comments