forked from matyunya/smelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-plugin-smelte.js
101 lines (89 loc) · 2.83 KB
/
rollup-plugin-smelte.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const postcss = require("rollup-plugin-postcss");
const path = require("path");
const extractor = require("./src/utils/css-extractor.js");
const defaultWhitelist = ["html", "body", "stroke-primary", "mode-dark"];
const defaultWhitelistPatterns = [
// for JS ripple
/ripple/,
// date picker
/w\-.\/7/
];
const postcssProcessor = ({
tailwind = {},
postcss = [],
whitelist = defaultWhitelist,
whitelistPatterns = defaultWhitelistPatterns,
whitelistPatternsChildren = defaultWhitelistPatterns,
purge = false
}) => {
const tailwindConfig = require("./tailwind.config.js")(tailwind);
return [
require("postcss-import")(),
require("postcss-url")(),
require("postcss-input-range")(),
require("autoprefixer")(),
require("tailwindcss")(tailwindConfig),
...postcss,
purge &&
require("cssnano")({
preset: "default"
}),
purge &&
require("@fullhuman/postcss-purgecss")({
content: ["./**/*.svelte"],
extractors: [
{
extractor,
extensions: ["svelte"]
}
],
whitelist: whitelist.filter(Boolean),
whitelistPatterns: whitelistPatterns.filter(Boolean),
whitelistPatternsChildren: whitelistPatternsChildren.filter(Boolean)
})
].filter(Boolean);
};
function moveGlobalCssToStatic({ dir, file }, output) {
let directory;
if (dir && !file) { // single file asset
directory = dir;
} else if (!dir && file) { // chunks
directory = path.dirname(file);
}
if (directory === path.dirname(output)) {
return;
}
const fs = require('fs')
const cssFile = path.basename(output)
if (fs.existsSync(path.resolve(directory, cssFile))) {
fs.copyFileSync(path.resolve(directory, cssFile), path.resolve(output))
fs.unlinkSync(path.resolve(directory, cssFile))
console.log(
`rollup-plugin-smelte:
moved ${cssFile}
from ${path.resolve(directory, cssFile)}
to ${path.resolve(output)}`
)
}
}
const plugins = config => postcssProcessor(config || {});
module.exports = (config = {}) =>
{
const defaultOutput = "./static/global.css"
const pcss = postcss({
plugins: plugins(config),
extract: path.basename(config.output || defaultOutput)
});
// since rollup v2 we can't extract css outside of the defined output, so to keep the impact to this
// plugin minimal I injected some code to the "writeBundle" method (which should be called after "generateBundle")
// to move the global.css file located under rollup.config.js's rollup.output to config parameter's output
const old_writeBundle = pcss.writeBundle
pcss.writeBundle = function() {
if (old_writeBundle) {
old_writeBundle.apply(this, arguments)
}
// arguments[0]: OutputOptions
moveGlobalCssToStatic(arguments[0], config.output || defaultOutput)
}
return pcss;
}