2017-12-04 02:02:18 +08:00
|
|
|
#! /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
|
2017-12-04 02:02:18 +08:00
|
|
|
const fs = require('iofs')
|
|
|
|
const path = require('path')
|
|
|
|
const scss = require('node-sass')
|
2017-12-21 22:39:45 +08:00
|
|
|
const chalk = require('chalk')
|
2018-03-25 15:45:11 +08:00
|
|
|
const uglify = require('uglify-es')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
const BASE_SCSS = `
|
|
|
|
$ct: #3fc2a7 #19b491 #16967a;
|
|
|
|
$cg: #58d68d #2ecc71 #27ae60;
|
|
|
|
$cpp: #ac61ce #9b59b6 #8e44ad;
|
2019-07-22 15:21:43 +08:00
|
|
|
$cb: #66b1ff #409eff #3a8ee6;
|
2019-07-22 10:11:52 +08:00
|
|
|
$cr: #ff5061 #eb3b48 #ce3742;
|
|
|
|
$co: #ffb618 #f39c12 #e67e22;
|
|
|
|
$cp: #f3f5fb #e8ebf4 #dae1e9;
|
|
|
|
$cgr: #aabac3 #90a3ae #7e909a;
|
|
|
|
$cd: #62778d #526273 #425064;
|
|
|
|
|
2019-09-11 18:35:14 +08:00
|
|
|
@mixin ts($c: all, $t: .1s, $m: ease-in-out){
|
2019-07-22 10:11:52 +08:00
|
|
|
transition:$c $t $m;
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
2019-09-04 20:35:00 +08:00
|
|
|
@mixin focus1(){
|
|
|
|
box-shadow: 0 0 2px #88f7df;
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin focus2(){
|
|
|
|
box-shadow: 0 0 2px #f3be4d;
|
|
|
|
}
|
|
|
|
|
2019-07-22 10:11:52 +08:00
|
|
|
* {
|
|
|
|
box-sizing: border-box;
|
|
|
|
margin: 0;padding: 0;
|
|
|
|
}
|
|
|
|
::before,
|
|
|
|
::after{box-sizing:border-box;}
|
|
|
|
`
|
|
|
|
|
2019-08-08 16:59:37 +08:00
|
|
|
function fixImport(str) {
|
|
|
|
return str
|
|
|
|
.replace(/import '([\w-/_.]*)'/g, 'import "$1.js"')
|
|
|
|
.replace(
|
2019-12-19 01:04:15 +08:00
|
|
|
/import ([\w\s,{}$]*) from '([a-z0-9\/\.\-_]*)'/g,
|
2019-08-08 16:59:37 +08:00
|
|
|
'import $1 from "$2.js"'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-12-04 02:02:18 +08:00
|
|
|
const compileJs = (entry, output) => {
|
|
|
|
let t1 = Date.now()
|
2019-02-20 15:34:02 +08:00
|
|
|
let buf = fs.cat(entry).toString()
|
2019-08-09 19:40:36 +08:00
|
|
|
buf = fixImport(buf)
|
2019-02-20 15:34:02 +08:00
|
|
|
let { code } = uglify.minify(buf)
|
2019-08-08 16:59:37 +08:00
|
|
|
|
2019-02-20 15:34:02 +08:00
|
|
|
log(
|
|
|
|
'编译JS: %s, 耗时 %s ms',
|
|
|
|
chalk.green(entry),
|
|
|
|
chalk.yellow(Date.now() - t1)
|
|
|
|
)
|
|
|
|
fs.echo(code, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 10:11:52 +08:00
|
|
|
// 编译样式
|
|
|
|
function compileScss(code = '') {
|
|
|
|
try {
|
|
|
|
return scss.renderSync({
|
|
|
|
data: BASE_SCSS + code,
|
|
|
|
outputStyle: 'compressed'
|
|
|
|
}).css
|
|
|
|
} 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 }) {
|
|
|
|
style = compileScss(style)
|
|
|
|
|
|
|
|
html = html.replace(/[\n\r]+/g, ' ')
|
|
|
|
html = html.replace(/\s+/g, ' ')
|
|
|
|
|
2019-07-30 16:32:14 +08:00
|
|
|
let name = ''
|
|
|
|
let props = ''
|
2019-07-22 10:11:52 +08:00
|
|
|
|
2019-08-12 21:26:08 +08:00
|
|
|
js = js.replace(/props = (\{\}|\{[\w\W]*?\n\s{2}?\})/, function(s, m) {
|
2019-07-22 10:11:52 +08:00
|
|
|
props = m
|
|
|
|
var attr = new Function(
|
|
|
|
`var props = ${m}, attr = []; for(var i in props){attr.push(i)}; return attr`
|
|
|
|
)()
|
|
|
|
return `static get observedAttributes() {
|
|
|
|
return ${JSON.stringify(attr)}
|
|
|
|
}
|
|
|
|
`
|
2018-01-22 02:01:44 +08:00
|
|
|
})
|
2019-07-22 10:11:52 +08:00
|
|
|
|
2019-08-08 16:59:37 +08:00
|
|
|
js = fixImport(js)
|
2019-08-21 21:03:41 +08:00
|
|
|
.replace(/class ([a-zA-Z0-9]+)/, function(s, m) {
|
2019-07-22 10:11:52 +08:00
|
|
|
name = m
|
|
|
|
return `${s} extends HTMLElement `
|
|
|
|
})
|
2019-08-21 21:03:41 +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
|
|
|
|
})
|
|
|
|
Object.defineProperty(this, 'props', {
|
|
|
|
value: ${props},
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true
|
|
|
|
})
|
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()')
|
|
|
|
.replace('unmount()', 'disconnectedCallback()')
|
2019-08-26 17:53:15 +08:00
|
|
|
.replace(
|
|
|
|
'watch() {',
|
2019-09-04 20:35:00 +08:00
|
|
|
'attributeChangedCallback(name, old, val) {\nif (val === null || old === val) {return}'
|
2019-08-26 17:53:15 +08:00
|
|
|
)
|
2019-08-26 17:34:07 +08:00
|
|
|
.replace('adopted()', 'adoptedCallback()')
|
2019-07-22 10:11:52 +08:00
|
|
|
|
|
|
|
let res = uglify.minify(js)
|
|
|
|
|
|
|
|
return `/**
|
|
|
|
*
|
|
|
|
* @authors yutent (yutent@doui.cc)
|
|
|
|
* @date ${BUILD_DATE}
|
|
|
|
* @version v${VERSION}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
${res.code}
|
|
|
|
|
2019-08-21 21:03:41 +08:00
|
|
|
if(!customElements.get('wc-${name.toLowerCase()}')){
|
|
|
|
customElements.define('wc-${name.toLowerCase()}', ${name})
|
|
|
|
}
|
2019-07-22 10:11:52 +08:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
const compileWC = (entry, output) => {
|
|
|
|
log('编译wc: %s', chalk.green(entry))
|
|
|
|
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] : ''
|
|
|
|
|
|
|
|
let result = mkWCFile({ style, html, js })
|
|
|
|
fs.echo(result, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*=======================================================*/
|
|
|
|
/*===== ===*/
|
|
|
|
/*=======================================================*/
|
|
|
|
|
|
|
|
if (fs.isdir(buildDir)) {
|
2018-03-25 15:45:11 +08:00
|
|
|
fs.rm(buildDir, true)
|
2017-12-21 22:39:45 +08:00
|
|
|
log(chalk.cyan('清除旧目录 dist/'))
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
2018-08-04 21:55:37 +08:00
|
|
|
fs.mkdir(buildDir)
|
|
|
|
|
|
|
|
let list = fs.ls('./node_modules/anot/dist/')
|
|
|
|
list.forEach(it => {
|
|
|
|
fs.cp(it, path.resolve(buildDir, path.parse(it).base))
|
|
|
|
})
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
log('复制anot框架文件完成...')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
/*----------------------------------------------*/
|
|
|
|
/*----------------------------------------------*/
|
|
|
|
/*----------------------------------------------*/
|
|
|
|
|
|
|
|
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
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
2018-08-04 21:55:37 +08:00
|
|
|
return { path: it, ext: file.ext, name: file.base }
|
2017-12-04 02:02:18 +08:00
|
|
|
})
|
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
files.forEach(file => {
|
|
|
|
if (!file) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let entry = file.path
|
|
|
|
let output = file.path.replace('src/', 'dist/')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2018-08-04 21:55:37 +08:00
|
|
|
switch (file.ext) {
|
2019-07-22 10:11:52 +08:00
|
|
|
case '.wc':
|
|
|
|
output = output.replace(/\.wc$/, '.js')
|
|
|
|
compileWC(entry, output)
|
|
|
|
break
|
2018-08-04 21:55:37 +08:00
|
|
|
case '.js':
|
2017-12-04 02:02:18 +08:00
|
|
|
compileJs(entry, output)
|
|
|
|
break
|
|
|
|
default:
|
2018-08-04 21:55:37 +08:00
|
|
|
fs.cp(entry, output)
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
})
|