2017-12-04 02:02:18 +08:00
|
|
|
#! /usr/bin/env node
|
|
|
|
|
2022-04-25 18:22:38 +08:00
|
|
|
import 'es.shim'
|
|
|
|
import fs from 'iofs'
|
|
|
|
import { parse, resolve } from 'path'
|
2023-02-27 17:53:27 +08:00
|
|
|
import scss from '@bytedo/sass'
|
2022-04-25 18:22:38 +08:00
|
|
|
import chalk from 'chalk'
|
|
|
|
import { transform } from 'esbuild'
|
|
|
|
import chokidar from 'chokidar'
|
|
|
|
|
|
|
|
const { version } = JSON.parse(fs.cat('./package.json'))
|
|
|
|
const sourceDir = resolve('./src')
|
|
|
|
const buildDir = resolve('./dist')
|
2017-12-04 02:02:18 +08:00
|
|
|
|
2022-04-25 18:22:38 +08:00
|
|
|
const log = console.log
|
2019-07-22 10:11:52 +08:00
|
|
|
const BUILD_DATE = new Date().format()
|
|
|
|
|
2021-11-24 11:09:28 +08:00
|
|
|
const ESBUILD_OPTIONS = {
|
|
|
|
sourcemap: false,
|
|
|
|
target: 'es2017',
|
|
|
|
format: 'esm'
|
|
|
|
}
|
2019-07-22 10:11:52 +08:00
|
|
|
const BASE_SCSS = `
|
2022-01-20 15:16:57 +08:00
|
|
|
@use "sass:math";
|
2019-07-22 10:11:52 +08:00
|
|
|
* {
|
|
|
|
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(/^\-/, '')
|
|
|
|
}
|
|
|
|
|
2019-08-08 16:59:37 +08:00
|
|
|
function fixImport(str) {
|
|
|
|
return str
|
|
|
|
.replace(/import '([\w-/_.]*)'/g, 'import "$1.js"')
|
2023-02-27 17:53:27 +08:00
|
|
|
.replace(
|
|
|
|
/import ([\w\s,{}$]*) from '([a-z0-9\/\.\-_]*)'/g,
|
|
|
|
'import $1 from "$2.js"'
|
|
|
|
)
|
2019-08-08 16:59:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function compileJs(entry, output) {
|
2021-11-24 11:09:28 +08:00
|
|
|
var t1 = Date.now()
|
2021-05-07 22:46:20 +08:00
|
|
|
var buf = fs.cat(entry).toString()
|
2019-02-20 15:34:02 +08:00
|
|
|
|
2021-11-24 11:09:28 +08:00
|
|
|
buf = fixImport(buf)
|
|
|
|
|
|
|
|
transform(buf, ESBUILD_OPTIONS).then(res => {
|
2023-02-27 17:53:27 +08:00
|
|
|
log(
|
|
|
|
'编译JS: %s, 耗时 %s ms',
|
|
|
|
chalk.green(entry),
|
|
|
|
chalk.yellow(Date.now() - t1)
|
|
|
|
)
|
2021-11-24 11:09:28 +08:00
|
|
|
fs.echo(res.code, output)
|
|
|
|
})
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 10:11:52 +08:00
|
|
|
// 编译样式
|
|
|
|
function compileScss(code = '') {
|
2018-08-04 21:55:37 +08:00
|
|
|
try {
|
2023-02-27 17:53:27 +08:00
|
|
|
return scss.compileString(BASE_SCSS + code).css
|
2018-08-04 21:55:37 +08:00
|
|
|
} catch (err) {
|
|
|
|
log(err)
|
|
|
|
}
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 10:11:52 +08:00
|
|
|
function mkWCFile({ style, html, js }) {
|
|
|
|
style = compileScss(style)
|
|
|
|
|
2019-07-22 21:18:33 +08:00
|
|
|
let name = ''
|
2020-12-14 14:44:05 +08:00
|
|
|
|
2022-04-25 18:22:38 +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 => {
|
|
|
|
var tmp = it.split(':')
|
2021-03-09 12:10:50 +08:00
|
|
|
return tmp[0].trim().replace(/^['"]|['"]$/g, '')
|
2020-12-14 14:44:05 +08:00
|
|
|
})
|
|
|
|
return `
|
|
|
|
|
|
|
|
static get observedAttributes() {
|
|
|
|
return ${JSON.stringify(attr)}
|
|
|
|
}
|
|
|
|
|
|
|
|
${str}
|
|
|
|
`
|
2019-07-22 10:11:52 +08:00
|
|
|
})
|
|
|
|
|
2019-08-08 16:59:37 +08:00
|
|
|
js = fixImport(js)
|
2022-04-25 18:22:38 +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 */',
|
|
|
|
`
|
2020-12-14 14:44:05 +08:00
|
|
|
Object.defineProperty(this, 'root', {
|
|
|
|
value: this.attachShadow({ mode: 'open' }),
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true
|
|
|
|
})
|
2019-07-22 10:11:52 +08:00
|
|
|
|
2020-12-14 14:44:05 +08:00
|
|
|
this.root.innerHTML = \`<style>${style}</style>${html}\`
|
2019-07-22 10:11:52 +08:00
|
|
|
`
|
|
|
|
)
|
2019-08-26 17:34:07 +08:00
|
|
|
.replace('mounted()', 'connectedCallback()')
|
2021-05-07 16:06:33 +08:00
|
|
|
.replace('unmounted()', 'disconnectedCallback()')
|
2023-02-27 17:53:27 +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 => {
|
|
|
|
return `/**
|
2019-07-22 10:11:52 +08:00
|
|
|
*
|
2020-07-22 19:56:46 +08:00
|
|
|
* @authors yutent (yutent.io@gmail.com)
|
2019-07-22 10:11:52 +08:00
|
|
|
* @date ${BUILD_DATE}
|
2022-04-25 18:22:38 +08:00
|
|
|
* @version v${version}
|
2019-07-22 10:11:52 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-11-24 11:09:28 +08:00
|
|
|
${res.code}
|
2019-07-22 10:11:52 +08:00
|
|
|
`
|
2021-11-24 11:09:28 +08:00
|
|
|
})
|
2019-07-22 10:11:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const compileWC = (entry, output) => {
|
2021-11-24 11:09:28 +08:00
|
|
|
var 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] : ''
|
|
|
|
|
2021-11-24 11:09:28 +08:00
|
|
|
mkWCFile({ style, html, js }).then(txt => {
|
2023-02-27 17:53:27 +08:00
|
|
|
log(
|
|
|
|
'编译WC: %s, 耗时 %s ms',
|
|
|
|
chalk.green(entry),
|
|
|
|
chalk.yellow(Date.now() - t1)
|
|
|
|
)
|
2021-11-24 11:09:28 +08:00
|
|
|
fs.echo(txt, output)
|
|
|
|
})
|
2019-07-22 10:11:52 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 02:02:18 +08:00
|
|
|
/*=======================================================*/
|
|
|
|
/*===== ===*/
|
|
|
|
/*=======================================================*/
|
|
|
|
|
|
|
|
chokidar
|
2018-08-04 21:55:37 +08:00
|
|
|
.watch(sourceDir)
|
2017-12-04 02:02:18 +08:00
|
|
|
.on('all', (act, file) => {
|
|
|
|
if (act === 'add' || act === 'change') {
|
2018-08-04 21:55:37 +08:00
|
|
|
let entry = file
|
|
|
|
let output = file.replace('src/', 'dist/')
|
|
|
|
|
2022-04-25 18:22:38 +08:00
|
|
|
file = parse(entry)
|
2018-08-04 21:55:37 +08:00
|
|
|
if (!file.ext || file.base === '.DS_Store' || file.base === 'var.scss') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-07 21:29:40 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
switch (file.ext) {
|
|
|
|
case '.js':
|
|
|
|
compileJs(entry, output)
|
|
|
|
break
|
|
|
|
case '.wc':
|
|
|
|
output = output.replace(/\.wc$/, '.js')
|
|
|
|
compileWC(entry, output)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
fs.cp(entry, output)
|
|
|
|
}
|
|
|
|
}, 100)
|
2017-12-04 02:02:18 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('ready', () => {
|
2017-12-21 22:39:45 +08:00
|
|
|
log(chalk.red('预处理完成,监听文件变化中,请勿关闭本窗口...'))
|
2017-12-04 02:02:18 +08:00
|
|
|
})
|