2021-01-13 18:20:47 +08:00
|
|
|
const parser = require('@babel/parser')
|
|
|
|
const { default: traverse } = require('@babel/traverse')
|
|
|
|
const { default: generate } = require('@babel/generator')
|
|
|
|
|
|
|
|
module.exports = function terseCssr (code) {
|
2021-02-03 16:39:00 +08:00
|
|
|
const patternSpace = / +/g
|
|
|
|
const patternEnter = /\n+/g
|
2021-01-13 18:20:47 +08:00
|
|
|
|
2021-02-03 01:45:56 +08:00
|
|
|
const ast = parser.parse(code, {
|
|
|
|
sourceType: 'module'
|
|
|
|
})
|
2021-01-13 18:20:47 +08:00
|
|
|
|
2021-02-03 01:45:56 +08:00
|
|
|
traverse(ast, {
|
|
|
|
TemplateElement (path) {
|
|
|
|
;['raw', 'cooked'].forEach((type) => {
|
|
|
|
path.node.value[type] = path.node.value[type]
|
2021-02-03 16:39:00 +08:00
|
|
|
.replace(patternSpace, ' ')
|
|
|
|
.replace(patternEnter, '\n')
|
2021-02-03 01:45:56 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-01-13 18:20:47 +08:00
|
|
|
|
2021-02-03 01:45:56 +08:00
|
|
|
return generate(ast).code
|
2021-01-13 18:20:47 +08:00
|
|
|
}
|