This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0
wcui/build.prod.js

194 lines
4.6 KiB
JavaScript
Raw Normal View History

#! /usr/bin/env node
2019-07-22 10:11:52 +08:00
require('es.shim')
2018-01-22 02:01:44 +08:00
const log = console.log
const fs = require('iofs')
const path = require('path')
2021-01-29 11:05:59 +08:00
const scss = require('sass')
2017-12-21 22:39:45 +08:00
const chalk = require('chalk')
2021-11-24 11:09:28 +08:00
const { transform } = require('esbuild')
const sourceDir = path.resolve(__dirname, 'src')
const buildDir = path.resolve(__dirname, 'dist')
2019-02-20 15:34:02 +08:00
2019-07-22 10:11:52 +08:00
const VERSION = require('./package.json').version
const BUILD_DATE = new Date().format()
2021-11-24 11:09:28 +08:00
const ESBUILD_OPTIONS = {
sourcemap: false,
target: 'es2017',
format: 'esm',
minify: true
}
2019-07-22 10:11:52 +08:00
const BASE_SCSS = `
* {
box-sizing: border-box;
margin: 0;padding: 0;
}
::before,
::after{box-sizing:border-box;}
`
2020-05-06 21:10:08 +08:00
function parseName(str) {
return str.replace(/[A-Z]/g, m => `-${m.toLowerCase()}`).replace(/^\-/, '')
}
function fixImport(str) {
return str
.replace(/import '([\w-/_.]*)'/g, 'import "$1.js"')
2021-11-24 11:09:28 +08:00
.replace(/import ([\w\s,{}$]*) from '([a-z0-9\/\.\-_]*)'/g, 'import $1 from "$2.js"')
}
2021-05-07 22:46:20 +08:00
function compileJs(entry, output) {
var t1 = Date.now()
var buf = fs.cat(entry).toString()
buf = fixImport(buf)
2021-05-07 22:46:20 +08:00
2021-11-24 11:09:28 +08:00
transform(buf, ESBUILD_OPTIONS).then(res => {
log('编译JS: %s, 耗时 %s ms', chalk.green(entry), chalk.yellow(Date.now() - t1))
2020-12-14 14:44:05 +08:00
fs.echo(res.code, output)
})
}
2019-07-22 10:11:52 +08:00
// 编译样式
function compileScss(code = '') {
try {
2020-02-11 00:15:13 +08:00
return (
scss.renderSync({
data: BASE_SCSS + code,
outputStyle: 'compressed'
}).css + ''
).trim()
2019-07-22 10:11:52 +08:00
} catch (err) {
log(err)
}
}
2019-02-20 15:34:02 +08:00
2019-07-22 10:11:52 +08:00
function mkWCFile({ style, html, js }) {
2021-05-07 22:46:20 +08:00
let name = ''
2019-07-22 10:11:52 +08:00
style = compileScss(style)
html = html.replace(/[\n\r]+/g, ' ')
html = html.replace(/\s+/g, ' ')
2021-11-24 11:09:28 +08:00
js = js.replace(/props = (\{\}|\{[\w\W]*?\n\s{2}?\})/, function(str) {
2020-12-14 14:44:05 +08:00
var attr = str
.split(/\n+/)
.slice(1, -1)
.map(it => {
2021-05-07 22:46:20 +08:00
let tmp = it.split(':')
return tmp[0].trim().replace(/^['"]|['"]$/g, '')
2020-12-14 14:44:05 +08:00
})
return `
static get observedAttributes() {
return ${JSON.stringify(attr)}
}
${str}
`
2018-01-22 02:01:44 +08:00
})
2019-07-22 10:11:52 +08:00
js = fixImport(js)
2021-11-24 11:09:28 +08:00
.replace(/export default class ([a-zA-Z0-9]+)/, function(s, m) {
2019-07-22 10:11:52 +08:00
name = m
return `${s} extends HTMLElement `
})
2020-12-14 14:44:05 +08:00
.replace(/__init__\(\)\s+\{/, 'constructor() {\n super();')
2019-07-22 10:11:52 +08:00
.replace(
'/* render */',
`
2019-07-23 23:16:30 +08:00
Object.defineProperty(this, 'root', {
value: this.attachShadow({ mode: 'open' }),
writable: true,
enumerable: false,
configurable: true
})
2020-12-14 14:44:05 +08:00
2019-07-22 10:11:52 +08:00
this.root.innerHTML = \`<style>${style}</style>${html}\`
`
)
2019-08-26 17:34:07 +08:00
.replace('mounted()', 'connectedCallback()')
2021-05-07 16:06:33 +08:00
.replace('unmounted()', 'disconnectedCallback()')
2021-11-24 11:09:28 +08:00
.replace('watch() {', 'attributeChangedCallback(name, old, val) {\nif (old === val) {return}')
2019-08-26 17:34:07 +08:00
.replace('adopted()', 'adoptedCallback()')
2019-07-22 10:11:52 +08:00
2021-11-24 11:32:04 +08:00
js += `if(!customElements.get('wc-${parseName(name)}')){
customElements.define('wc-${parseName(name)}', ${name})
}`
2021-11-24 11:09:28 +08:00
return transform(js, ESBUILD_OPTIONS).then(res => {
2020-12-14 14:44:05 +08:00
return `/**
*
* @authors yutent (yutent.io@gmail.com)
* @date ${BUILD_DATE}
* @version v${VERSION}
*
*/
2019-07-22 10:11:52 +08:00
${res.code}
`
2020-12-14 14:44:05 +08:00
})
2019-07-22 10:11:52 +08:00
}
const compileWC = (entry, output) => {
2021-05-07 22:46:20 +08:00
let t1 = Date.now()
2019-07-22 10:11:52 +08:00
let code = fs.cat(entry).toString()
let style = code.match(/<style[^>]*?>([\w\W]*?)<\/style>/)
let html = code.match(/<template>([\w\W]*?)<\/template>/)
let js = code.match(/<script>([\w\W]*?)<\/script>/)
style = style ? style[1] : ''
html = html ? html[1] : ''
js = js ? js[1] : ''
2020-12-14 14:44:05 +08:00
mkWCFile({ style, html, js }).then(txt => {
2021-11-24 11:09:28 +08:00
log('编译WC: %s, 耗时 %s ms', chalk.green(entry), chalk.yellow(Date.now() - t1))
2020-12-14 14:44:05 +08:00
fs.echo(txt, output)
})
}
/*=======================================================*/
/*===== ===*/
/*=======================================================*/
if (fs.isdir(buildDir)) {
fs.rm(buildDir, true)
2017-12-21 22:39:45 +08:00
log(chalk.cyan('清除旧目录 dist/'))
}
fs.mkdir(buildDir)
/*----------------------------------------------*/
/*----------------------------------------------*/
/*----------------------------------------------*/
let files = fs.ls(sourceDir, true)
files = files.map(it => {
let file = path.parse(it)
if (!file.ext || file.base === '.DS_Store' || file.base === 'var.scss') {
return null
}
return { path: it, ext: file.ext, name: file.base }
})
files.forEach(file => {
if (!file) {
return
}
let entry = file.path
let output = file.path.replace('src/', 'dist/')
switch (file.ext) {
2019-07-22 10:11:52 +08:00
case '.wc':
output = output.replace(/\.wc$/, '.js')
compileWC(entry, output)
break
case '.js':
compileJs(entry, output)
break
default:
fs.cp(entry, output)
}
})