forked from thgh/rollup-plugin-scss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.es.js
168 lines (147 loc) · 4.45 KB
/
index.es.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { existsSync, mkdirSync, writeFile } from 'fs'
import { dirname } from 'path'
import { createFilter } from 'rollup-pluginutils'
export default function css (options = {}) {
const filter = createFilter(options.include || ['/**/*.css', '/**/*.scss', '/**/*.sass'], options.exclude)
let dest = options.output
const compiler = options.compiler || require('node-sass')
const styles = {}
const prefix = options.prefix ? options.prefix + '\n' : ''
let includePaths = options.includePaths || ['node_modules/']
includePaths.push(process.cwd())
const compileToCSS = function (scss) {
// Compile SASS to CSS
if (scss.length) {
includePaths = includePaths.filter((v, i, a) => a.indexOf(v) === i)
try {
const css = compiler.renderSync(Object.assign({
data: prefix + scss,
includePaths
}, options)).css.toString()
// Possibly process CSS (e.g. by PostCSS)
if (typeof options.processor === 'function') {
return options.processor(css, styles)
}
return css
} catch (e) {
if (options.failOnError) {
throw e
}
console.log()
console.log(red('Error:\n\t' + e.message))
if (e.message.includes('Invalid CSS')) {
console.log(green('Solution:\n\t' + 'fix your Sass code'))
console.log('Line: ' + e.line)
console.log('Column: ' + e.column)
}
if (e.message.includes('node-sass') && e.message.includes('find module')) {
console.log(green('Solution:\n\t' + 'npm install --save node-sass'))
}
if (e.message.includes('node-sass') && e.message.includes('bindigs')) {
console.log(green('Solution:\n\t' + 'npm rebuild node-sass --force'))
}
console.log()
}
}
}
return {
name: 'css',
transform (code, id) {
if (!filter(id)) {
return
}
// When output is disabled, the stylesheet is exported as a string
if (options.output === false) {
const css = compileToCSS(code)
return {
code: 'export default ' + JSON.stringify(css),
map: { mappings: '' }
}
}
// Map of every stylesheet
styles[id] = code
includePaths.push(dirname(id))
// In the case of watching path, watch all files there
if ('watchDir' in options) {
this.addWatchFile(options.watchDir);
}
return ''
},
watchChange(id) {
console.log('Change found in: ', id);
},
generateBundle (opts) {
// No stylesheet needed
if (options.output === false) {
return
}
// Combine all stylesheets
let scss = ''
for (const id in styles) {
scss += styles[id] || ''
}
const css = compileToCSS(scss)
// Resolve if porcessor returned a Promise
Promise.resolve(css).then(css => {
// Emit styles through callback
if (typeof options.output === 'function') {
options.output(css, styles)
return
}
if (typeof css !== 'string') {
return
}
if (typeof dest !== 'string') {
// Don't create unwanted empty stylesheets
if (!css.length) {
return
}
// Guess destination filename
dest = opts.dest || opts.file || 'bundle.js'
if (dest.endsWith('.js')) {
dest = dest.slice(0, -3)
}
dest = dest + '.css'
}
// Ensure that dest parent folders exist (create the missing ones)
ensureParentDirsSync(dirname(dest))
// Emit styles to file
writeFile(dest, css, (err) => {
if (opts.verbose !== false) {
if (err) {
console.error(red(err))
} else if (css) {
console.log(green(dest), getSize(css.length))
}
}
})
})
}
}
}
function red (text) {
return '\x1b[1m\x1b[31m' + text + '\x1b[0m'
}
function green (text) {
return '\x1b[1m\x1b[32m' + text + '\x1b[0m'
}
function getSize (bytes) {
return bytes < 10000
? bytes.toFixed(0) + ' B'
: bytes < 1024000
? (bytes / 1024).toPrecision(3) + ' kB'
: (bytes / 1024 / 1024).toPrecision(4) + ' MB'
}
function ensureParentDirsSync (dir) {
if (existsSync(dir)) {
return
}
try {
mkdirSync(dir)
} catch (err) {
if (err.code === 'ENOENT') {
ensureParentDirsSync(dirname(dir))
ensureParentDirsSync(dir)
}
}
}