forked from quasarframework/quasar
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.build.css.js
149 lines (130 loc) · 3.72 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const path = require('path')
const stylus = require('stylus')
const sass = require('sass')
const rtl = require('postcss-rtl')
const postcss = require('postcss')
const cssnano = require('cssnano')
const autoprefixer = require('autoprefixer')
const buildConf = require('./build.conf')
const buildUtils = require('./build.utils')
const pathList = [ path.join(__dirname, '../src/css/') ]
const nano = postcss([
cssnano({
preset: ['default', {
mergeLonghand: false,
convertValues: false,
cssDeclarationSorter: false,
reduceTransforms: false
}]
})
])
function generateSassFile (source, destination) {
const src = path.join(__dirname, '..', source)
const dest = path.join(__dirname, '..', destination)
// We do 2 things here: validate and build import graph
const result = sass.renderSync({ file: src })
return getConcatenatedContent(result.stats.includedFiles)
.then(code => buildUtils.writeFile(dest, code))
.then(() => validateSassFile(destination))
}
function validateSassFile (src) {
const file = path.join(__dirname, '..', src)
return new Promise((resolve, reject) => {
sass.render({ file }, (err) => {
if (err) {
reject(err)
return
}
resolve(true)
})
})
}
function generateStylusBase (src) {
// We do 2 things here: validate and get import graph
const deps = stylus(buildUtils.readFile(src))
.set('paths', pathList)
.deps()
return generateStylusFiles({
sources: [src].concat(deps),
styl: true
})
}
function generateStylusAddon () {
return generateStylusFiles({
sources: [
'src/css/variables.styl',
'src/css/flex-addon.styl'
],
name: '.addon'
})
}
function generateStylusFiles ({ sources, name = '', styl }) {
return getConcatenatedContent(sources)
.then(code => {
if (styl) { return buildUtils.writeFile(`dist/quasar${name}.styl`, code) }
else { return code }
})
.then(code => compileStylus(code))
.then(code => postcss([ autoprefixer ]).process(code, { from: void 0 }))
.then(code => {
code.warnings().forEach(warn => {
console.warn(warn.toString())
})
return code.css
})
.then(code => Promise.all([
generateUMD(name, code),
postcss([ rtl({}) ]).process(code, { from: void 0 }).then(code => generateUMD(name, code.css, '.rtl'))
]))
}
function generateUMD (name, code, ext = '') {
return buildUtils.writeFile(`dist/quasar${name}${ext}.css`, code, true)
.then(code => nano.process(code, { from: void 0 }))
.then(code => buildUtils.writeFile(`dist/quasar${name}${ext}.min.css`, code.css, true))
}
function getConcatenatedContent (src, noBanner) {
return new Promise((resolve, reject) => {
let code = noBanner !== true
? buildConf.banner
: ''
src.forEach(function (file) {
code += buildUtils.readFile(file) + '\n'
})
code = code
// remove imports
.replace(/@import\s+'[^']+'[\s\r\n]+/g, '')
// remove comments
.replace(/(\/\*[\w'-.,`\s\r\n*@]*\*\/)|(\/\/[^\r\n]*)/g, '')
// remove unnecessary newlines
.replace(/[\r\n]+/g, '\r\n')
resolve(code)
})
}
function compileStylus (code) {
return new Promise((resolve, reject) => {
stylus(code)
.set('paths', pathList)
.render((err, code) => {
if (err) {
console.log()
reject(err)
}
else {
resolve(code)
}
})
})
}
module.exports = function () {
Promise
.all([
generateStylusBase('src/css/index.styl'),
generateStylusAddon(),
generateSassFile('src/css/index.sass', 'dist/quasar.sass'),
validateSassFile('src/css/flex-addon.sass')
])
.catch(e => {
console.error(e)
process.exit(1)
})
}