UNPKG

12.6 kBTypeScriptView Raw
1
2/// <reference types="./shims/icss-utils" />
3/// <reference types="./shims/less" />
4/// <reference types="./shims/postcss-modules" />
5/// <reference types="./shims/sass" />
6/// <reference types="./shims/stylus" />
7import * as rollup from 'rollup';
8import { Plugin } from 'rollup';
9import * as postcss from 'postcss';
10import cssnano from 'cssnano';
11import { RawSourceMap } from 'source-map-js';
12
13/** File resolved by `@import` resolver */
14interface ImportFile {
15 /** Absolute path to file */
16 from: string;
17 /** File source */
18 source: Uint8Array;
19}
20/** `@import` resolver */
21declare type ImportResolve = (url: string, basedir: string, extensions: string[]) => Promise<ImportFile>;
22
23/** `@import` handler options */
24interface ImportOptions {
25 /**
26 * Provide custom resolver for imports
27 * in place of the default one
28 */
29 resolve?: ImportResolve;
30 /**
31 * Aliases for import paths.
32 * Overrides the global `alias` option.
33 * - ex.: `{"foo":"bar"}`
34 */
35 alias?: Record<string, string>;
36 /**
37 * Import files ending with these extensions.
38 * Overrides the global `extensions` option.
39 * @default [".css", ".pcss", ".postcss", ".sss"]
40 */
41 extensions?: string[];
42}
43
44/** File resolved by URL resolver */
45interface UrlFile {
46 /** Absolute path to file */
47 from: string;
48 /** File source */
49 source: Uint8Array;
50 /** Original query extracted from the input path */
51 urlQuery?: string;
52}
53/** URL resolver */
54declare type UrlResolve = (inputUrl: string, basedir: string) => Promise<UrlFile>;
55
56/** URL handler options */
57interface UrlOptions {
58 /**
59 * Inline files instead of copying
60 * @default true for `inject` mode, otherwise false
61 */
62 inline?: boolean;
63 /**
64 * Public Path for URLs in CSS files
65 * @default "./"
66 */
67 publicPath?: string;
68 /**
69 * Directory path for outputted CSS assets,
70 * which is not included into resulting URL
71 * @default "."
72 */
73 assetDir?: string;
74 /**
75 * Enable/disable name generation with hash for outputted CSS assets
76 * or provide your own placeholder with the following blocks:
77 * - `[extname]`: The file extension of the asset including a leading dot, e.g. `.png`.
78 * - `[ext]`: The file extension without a leading dot, e.g. `png`.
79 * - `[hash(:<num>)]`: A hash based on the name and content of the asset (with optional length).
80 * - `[name]`: The file name of the asset excluding any extension.
81 *
82 * Forward slashes / can be used to place files in sub-directories.
83 * @default "assets/[name]-[hash][extname]" ("assets/[name][extname]" if false)
84 */
85 hash?: boolean | string;
86 /**
87 * Provide custom resolver for URLs
88 * in place of the default one
89 */
90 resolve?: UrlResolve;
91 /**
92 * Aliases for URL paths.
93 * Overrides the global `alias` option.
94 * - ex.: `{"foo":"bar"}`
95 */
96 alias?: Record<string, string>;
97}
98
99/** Options for [CSS Modules](https://github.com/css-modules/css-modules) */
100interface ModulesOptions {
101 /**
102 * Default mode for classes
103 * @default "local"
104 */
105 mode?: "local" | "global" | "pure";
106 /** Fail on wrong order of composition */
107 failOnWrongOrder?: boolean;
108 /** Export global classes */
109 exportGlobals?: boolean;
110 /**
111 * Placeholder or function for scoped name generation.
112 * Allowed blocks for placeholder:
113 * - `[dir]`: The directory name of the asset.
114 * - `[name]`: The file name of the asset excluding any extension.
115 * - `[local]`: The original value of the selector.
116 * - `[hash(:<num>)]`: A hash based on the name and content of the asset (with optional length).
117 * @default "[name]_[local]__[hash:8]"
118 */
119 generateScopedName?: string | ((name: string, file: string, css: string) => string);
120}
121
122/**
123 * Loader
124 * @param T type of loader's options
125 */
126interface Loader<T = Record<string, unknown>> {
127 /** Name */
128 name: string;
129 /**
130 * Test to control if file should be processed.
131 * Also used for plugin's supported files test.
132 */
133 test?: RegExp | ((file: string) => boolean);
134 /** Skip testing, always process the file */
135 alwaysProcess?: boolean;
136 /** Function for processing */
137 process: (this: LoaderContext<T>, payload: Payload) => Promise<Payload> | Payload;
138}
139/**
140 * Loader's context
141 * @param T type of loader's options
142 */
143interface LoaderContext<T = Record<string, unknown>> {
144 /**
145 * Loader's options
146 * @default {}
147 */
148 readonly options: T;
149 /** @see {@link Options.sourceMap} */
150 readonly sourceMap: false | ({
151 inline: boolean;
152 } & SourceMapOptions);
153 /** Resource path */
154 readonly id: string;
155 /** Files to watch */
156 readonly deps: Set<string>;
157 /** Assets to emit */
158 readonly assets: Map<string, Uint8Array>;
159 /** [Plugin's context](https://rollupjs.org/guide/en#plugin-context) */
160 readonly plugin: rollup.PluginContext;
161 /** [Function for emitting a warning](https://rollupjs.org/guide/en/#thiswarnwarning-string--rollupwarning-position-number---column-number-line-number---void) */
162 readonly warn: rollup.PluginContext["warn"];
163}
164/** Extracted data */
165interface Extracted {
166 /** Source file path */
167 id: string;
168 /** CSS */
169 css: string;
170 /** Sourcemap */
171 map?: string;
172}
173/** Loader's payload */
174interface Payload {
175 /** File content */
176 code: string;
177 /** Sourcemap */
178 map?: string;
179 /** Extracted data */
180 extracted?: Extracted;
181}
182/** Options for sourcemaps */
183interface SourceMapOptions {
184 /**
185 * Include sources content
186 * @default true
187 */
188 content?: boolean;
189 /** Function for transforming resulting sourcemap */
190 transform?: (map: RawSourceMap, name?: string) => void;
191}
192
193/** Options for Sass loader */
194interface SASSLoaderOptions extends Record<string, unknown>, sass.PublicOptions {
195 /** Force Sass implementation */
196 impl?: string;
197 /** Forcefully enable/disable sync mode */
198 sync?: boolean;
199}
200
201/** Options for Less loader */
202interface LESSLoaderOptions extends Record<string, unknown>, less.PublicOptions {
203}
204
205/** Options for Stylus loader */
206interface StylusLoaderOptions extends Record<string, unknown>, stylus.PublicOptions {
207}
208
209/** Options for PostCSS config loader */
210interface PostCSSConfigLoaderOptions {
211 /** Path to PostCSS config file directory */
212 path?: string;
213 /**
214 * Context object passed to PostCSS config file
215 * @default {}
216 */
217 ctx?: Record<string, unknown>;
218}
219/** CSS data for extraction */
220interface ExtractedData {
221 /** CSS */
222 css: string;
223 /** Sourcemap */
224 map?: string;
225 /** Output name for CSS */
226 name: string;
227}
228/** Options for CSS injection */
229interface InjectOptions {
230 /**
231 * Insert `<style>` tag(s) to the beginning of the container
232 * @default false
233 */
234 prepend?: boolean;
235 /**
236 * Inject CSS into single `<style>` tag only
237 * @default false
238 */
239 singleTag?: boolean;
240 /**
241 * Container for `<style>` tag(s) injection
242 * @default "head"
243 */
244 container?: string;
245 /**
246 * Set attributes of injected `<style>` tag(s)
247 * - ex.: `{"id":"global"}`
248 */
249 attributes?: Record<string, string>;
250 /**
251 * Makes injector treeshakeable,
252 * as it is only called when either classes are referenced directly,
253 * or `inject` function is called from the default export.
254 *
255 * Incompatible with `namedExports` option.
256 */
257 treeshakeable?: boolean;
258}
259/** `rollup-plugin-styles`'s full option list */
260interface Options {
261 /** Files to include for processing */
262 include?: ReadonlyArray<string | RegExp> | string | RegExp | null;
263 /** Files to exclude from processing */
264 exclude?: ReadonlyArray<string | RegExp> | string | RegExp | null;
265 /**
266 * PostCSS will process files ending with these extensions
267 * @default [".css", ".pcss", ".postcss", ".sss"]
268 */
269 extensions?: string[];
270 /**
271 * A list of plugins for PostCSS,
272 * which are used before plugins loaded from PostCSS config file, if any
273 */
274 plugins?: Record<string, unknown> | (postcss.AcceptedPlugin | string | [string | postcss.PluginCreator<unknown>] | [string | postcss.PluginCreator<unknown>, Record<string, unknown>] | null | undefined)[];
275 /**
276 * Select mode for this plugin:
277 * - `"inject"` *(default)* - Embeds CSS inside JS and injects it into `<head>` at runtime.
278 * You can also pass options for CSS injection.
279 * Alternatively, you can pass your own CSS injector.
280 * - `"extract"` - Extract CSS to the same location where JS file is generated but with `.css` extension.
281 * You can also set extraction path manually,
282 * relative to output dir/output file's basedir,
283 * but not outside of it.
284 * - `"emit"` - Emit pure processed CSS and pass it along the build pipeline.
285 * Useful if you want to preprocess CSS before using it with CSS consuming plugins.
286 * @default "inject"
287 */
288 mode?: "inject" | ["inject"] | ["inject", InjectOptions | ((varname: string, id: string) => string)] | "extract" | ["extract"] | ["extract", string] | "emit" | ["emit"];
289 /** `to` option for PostCSS, required for some plugins */
290 to?: string;
291 /**
292 * Generate TypeScript declarations files for input style files
293 * @default false
294 */
295 dts?: boolean;
296 /**
297 * Enable/disable or pass options for CSS `@import` resolver
298 * @default true
299 */
300 import?: ImportOptions | boolean;
301 /**
302 * Enable/disable or pass options for CSS URL resolver
303 * @default true
304 */
305 url?: UrlOptions | boolean;
306 /**
307 * Aliases for URL and import paths
308 * - ex.: `{"foo":"bar"}`
309 */
310 alias?: Record<string, string>;
311 /**
312 * Enable/disable or pass options for
313 * [CSS Modules](https://github.com/css-modules/css-modules)
314 * @default false
315 */
316 modules?: boolean | ModulesOptions;
317 /**
318 * Automatically enable
319 * [CSS Modules](https://github.com/css-modules/css-modules)
320 * for files named `[name].module.[ext]`
321 * (e.g. `foo.module.css`, `bar.module.stylus`),
322 * or pass your own function or regular expression
323 * @default false
324 */
325 autoModules?: boolean | RegExp | ((id: string) => boolean);
326 /**
327 * Use named exports alongside default export.
328 * You can pass a function to control how exported name is generated.
329 * @default false
330 */
331 namedExports?: boolean | ((name: string) => string);
332 /**
333 * Enable/disable or pass options for
334 * [cssnano](https://github.com/cssnano/cssnano)
335 * @default false
336 */
337 minimize?: boolean | cssnano.CssNanoOptions;
338 /**
339 * Enable/disable or configure sourcemaps
340 * @default false
341 */
342 sourceMap?: boolean | "inline" | [boolean | "inline"] | [boolean | "inline", SourceMapOptions];
343 /**
344 * Set PostCSS parser, e.g. `sugarss`.
345 * Overrides the one loaded from PostCSS config file, if any.
346 */
347 parser?: string | postcss.Parser;
348 /**
349 * Set PostCSS stringifier.
350 * Overrides the one loaded from PostCSS config file, if any.
351 */
352 stringifier?: string | postcss.Stringifier;
353 /**
354 * Set PostCSS syntax.
355 * Overrides the one loaded from PostCSS config file, if any.
356 */
357 syntax?: string | postcss.Syntax;
358 /**
359 * Enable/disable or pass options for PostCSS config loader
360 * @default true
361 */
362 config?: boolean | PostCSSConfigLoaderOptions;
363 /**
364 * Array of loaders to use, executed from right to left.
365 * Currently built-in loaders are:
366 * - `sass` (Supports `.scss` and `.sass` files)
367 * - `less` (Supports `.less` files)
368 * - `stylus` (Supports `.styl` and `.stylus` files)
369 * @default ["sass", "less", "stylus"]
370 */
371 use?: string[];
372 /** Options for Sass loader */
373 sass?: SASSLoaderOptions;
374 /** Options for Less loader */
375 less?: LESSLoaderOptions;
376 /** Options for Stylus loader */
377 stylus?: StylusLoaderOptions;
378 /** Array of custom loaders */
379 loaders?: Loader[];
380 /**
381 * Function which is invoked on CSS file import,
382 * before any transformations are applied
383 */
384 onImport?: (code: string, id: string) => void;
385 /**
386 * Function which is invoked on CSS file extraction.
387 * Return `boolean` to control if file should be extracted or not.
388 */
389 onExtract?: (data: ExtractedData) => boolean;
390}
391
392declare const _default: (options?: Options) => Plugin;
393
394export { _default as default };
395
\No newline at end of file