|
1 | 1 | // rollup-plugin-inline-postcss.js |
2 | | -import * as cssesc from "cssesc"; |
3 | | -import * as findup from "findup"; |
4 | | -import * as path from "path"; |
5 | | -import * as postcss from "postcss"; |
6 | | -import { createFilter } from "rollup-pluginutils"; |
| 2 | +import * as findup from 'findup'; |
| 3 | +import * as path from 'path'; |
| 4 | +import postcss from 'postcss'; |
| 5 | +import { createFilter } from 'rollup-pluginutils'; |
7 | 6 |
|
8 | 7 | export default function inlinePostCSS(options: any = {}) { |
9 | 8 | const filter = createFilter(options.include, options.exclude); |
10 | | - const styleRegex = options.styleRegex ? options.styleRegex : /(css\`((.|\n)*)\`)/g; |
| 9 | + const styleRegex = options.styleRegex |
| 10 | + ? options.styleRegex |
| 11 | + : /(css\`((.|\n)*)\`)/g; |
11 | 12 | return { |
12 | | - name: "inline-postcss", |
| 13 | + name: 'inline-postcss', |
13 | 14 | transform(code, id) { |
14 | 15 | if (!filter(id)) { |
15 | | - return; |
| 16 | + return; |
16 | 17 | } |
17 | 18 | if (!code.match(styleRegex)) { |
18 | | - return; |
| 19 | + return; |
19 | 20 | } |
20 | | - let punc = code.match(styleRegex)[0][code.match(styleRegex)[0].length - 1]; |
21 | | - if (punc !== "," && punc !== ";") { |
| 21 | + let punc = code.match(styleRegex)[0][ |
| 22 | + code.match(styleRegex)[0].length - 1 |
| 23 | + ]; |
| 24 | + if (punc !== ',' && punc !== ';') { |
22 | 25 | punc = null; |
23 | 26 | } |
24 | 27 | try { |
25 | 28 | let configFolder; |
26 | 29 | if (!options.plugins) { |
27 | | - configFolder = findup.sync(__dirname, "postcss.config.js"); |
| 30 | + configFolder = findup.sync(__dirname, 'postcss.config.js'); |
28 | 31 | } else { |
29 | | - configFolder = ""; |
| 32 | + configFolder = ''; |
30 | 33 | } |
31 | | - const config = options.plugins ? options.plugins : require(path.join(configFolder, "postcss.config.js"))({ |
32 | | - env: process.env.NODE_ENV, |
33 | | - }); |
34 | | - const css = code.match(styleRegex)[0].split("`")[1]; |
35 | | - const opts = { from: options.from ? path.join(process.cwd(), options.from) : id, |
| 34 | + const config = options.plugins |
| 35 | + ? options.plugins |
| 36 | + : require(path.join(configFolder, 'postcss.config.js'))({ |
| 37 | + env: process.env.NODE_ENV, |
| 38 | + }); |
| 39 | + const css = code.match(styleRegex)[0].split('`')[1]; |
| 40 | + const opts = { |
| 41 | + from: options.from ? path.join(process.cwd(), options.from) : id, |
36 | 42 | to: options.to ? path.join(process.cwd(), options.to) : id, |
37 | 43 | map: { |
38 | 44 | inline: false, |
39 | 45 | annotation: false, |
40 | 46 | }, |
41 | 47 | }; |
42 | | - const outputConfig = options.plugins ? options.plugins : Object.keys(config.plugins) |
43 | | - .filter((key) => config.plugins[key]) |
44 | | - .map((key) => require(key)); |
| 48 | + const outputConfig = options.plugins |
| 49 | + ? options.plugins |
| 50 | + : Object.keys(config.plugins) |
| 51 | + .filter((key) => config.plugins[key]) |
| 52 | + .map((key) => require(key)); |
45 | 53 | return postcss(outputConfig) |
46 | 54 | .process(css, opts) |
47 | 55 | .then((result) => { |
48 | | - code = code.replace(styleRegex, `\`${result.css}\`${punc ? punc : ""}`); |
| 56 | + code = code.replace( |
| 57 | + styleRegex, |
| 58 | + `\`${result.css}\`${punc ? punc : ''}`, |
| 59 | + ); |
49 | 60 | const map = result.map |
50 | 61 | ? JSON.parse((result as any).map) |
51 | | - : { mappings: "" }; |
| 62 | + : { mappings: '' }; |
52 | 63 | return { |
53 | 64 | code, |
54 | 65 | map, |
|
0 commit comments