-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathscript.build.css.js
126 lines (103 loc) · 3.1 KB
/
script.build.css.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
import { compileAsync } from 'sass-embedded'
import rtl from 'postcss-rtlcss'
import postcss from 'postcss'
import cssnano from 'cssnano'
import autoprefixer from 'autoprefixer'
import { banner, resolveToRoot, readFile, writeFile } from './build.utils.js'
import prepareDiff from './prepare-diff.js'
const nano = postcss([
cssnano({
preset: [ 'default', {
mergeLonghand: false,
convertValues: false,
cssDeclarationSorter: false,
reduceTransforms: false
} ]
})
])
const sassUseRE = /@use\s+['"][^'"]+['"]/g
function moveUseStatementsToTop (code) {
const useStatements = code.match(sassUseRE)
return useStatements === null
? code
: Array.from(new Set(useStatements)).join('\n')
+ '\n'
+ code.replace(sassUseRE, '')
}
function compileSass (src) {
return compileAsync(src, {
silenceDeprecations: [ 'import' ]
})
}
function getConcatenatedContent (src, noBanner) {
return new Promise(resolve => {
let code = ''
const localBanner = noBanner !== true
? banner
: ''
src.forEach(file => {
code += readFile(file) + '\n'
})
code = code
// remove imports
.replace(/@import\s+'[^']+'[\s\r\n]+/g, '')
// remove comments
.replace(/(\/\*[\w'-.,`\s\r\n*@]*\*\/)|(\/\/[^\r\n]*)/g, '')
code = moveUseStatementsToTop(code)
// remove unnecessary newlines
.replace(/[\r\n]+/g, '\r\n')
resolve(localBanner + code)
})
}
function generateUMD (code, middleName, ext = '') {
return writeFile(`dist/quasar${ middleName }${ ext }.css`, code, true)
.then(code => nano.process(code, { from: void 0 }))
.then(code => writeFile(`dist/quasar${ middleName }${ ext }.prod.css`, code.css, true))
}
function renderAsset (cssCode, middleName = '') {
return postcss([ autoprefixer ]).process(cssCode, { from: void 0 })
.then(code => {
code.warnings().forEach(warn => {
console.warn(warn.toString())
})
return code.css
})
.then(code => Promise.all([
generateUMD(code, middleName),
postcss([ rtl({}) ]).process(code, { from: void 0 })
.then(code => generateUMD(code.css, middleName, '.rtl'))
]))
}
async function generateBase (source) {
const src = resolveToRoot(source)
const sassDistDest = resolveToRoot('dist/quasar.sass')
const result = await compileSass(src)
// remove @charset declaration -- breaks Vite usage
const cssCode = result.css.toString().replace('@charset "UTF-8";', '')
const depsList = result.loadedUrls
return Promise.all([
renderAsset(cssCode),
getConcatenatedContent(depsList)
.then(code => writeFile(sassDistDest, code))
])
}
async function generateAddon (source) {
const src = resolveToRoot(source)
const result = await compileSass(src)
const cssCode = result.css.toString()
return renderAsset(cssCode, '.addon')
}
export function buildCss (withDiff) {
if (withDiff === true) {
prepareDiff('dist/quasar.sass')
}
Promise
.all([
generateBase('src/css/index.sass'),
generateAddon('src/css/flex-addon.sass')
])
.catch(e => {
console.error(e)
process.exit(1)
})
}