naive-ui/build/utils/terse-cssr.js

25 lines
606 B
JavaScript
Raw Normal View History

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) {
if (process.env.NODE_ENV === 'production') {
const pattern = /[\n\t\s]+/g
const ast = parser.parse(code, {
sourceType: 'module'
})
traverse(ast, {
TemplateElement (path) {
;['raw', 'cooked'].forEach((type) => {
path.node.value[type] = path.node.value[type].replace(pattern, '')
})
}
})
return generate(ast).code
}
return code
}