diff --git a/build.dev.js b/build.dev.js index 02cd33d..5a5e65a 100644 --- a/build.dev.js +++ b/build.dev.js @@ -7,6 +7,7 @@ const path = require('path') const scss = require('sass') const chokidar = require('chokidar') const chalk = require('chalk') +const { transform } = require('esbuild') const sourceDir = path.resolve(__dirname, 'src') const buildDir = path.resolve(__dirname, 'dist') @@ -14,6 +15,11 @@ const buildDir = path.resolve(__dirname, 'dist') const VERSION = require('./package.json').version const BUILD_DATE = new Date().format() +const ESBUILD_OPTIONS = { + sourcemap: false, + target: 'es2017', + format: 'esm' +} const BASE_SCSS = ` * { box-sizing: border-box; @@ -30,18 +36,19 @@ function parseName(str) { function fixImport(str) { return str .replace(/import '([\w-/_.]*)'/g, 'import "$1.js"') - .replace( - /import ([\w\s,{}$]*) from '([a-z0-9\/\.\-_]*)'/g, - 'import $1 from "$2.js"' - ) + .replace(/import ([\w\s,{}$]*) from '([a-z0-9\/\.\-_]*)'/g, 'import $1 from "$2.js"') } function compileJs(entry, output) { + var t1 = Date.now() var buf = fs.cat(entry).toString() - var code = fixImport(buf) - log('编译JS: %s', chalk.green(entry)) - fs.echo(code, output) + buf = fixImport(buf) + + transform(buf, ESBUILD_OPTIONS).then(res => { + log('编译JS: %s, 耗时 %s ms', chalk.green(entry), chalk.yellow(Date.now() - t1)) + fs.echo(res.code, output) + }) } // 编译样式 @@ -58,7 +65,7 @@ function mkWCFile({ style, html, js }) { let name = '' - js = js.replace(/props = (\{\}|\{[\w\W]*?\n\s{2}?\})/, function (str) { + js = js.replace(/props = (\{\}|\{[\w\W]*?\n\s{2}?\})/, function(str) { var attr = str .split(/\n+/) .slice(1, -1) @@ -77,7 +84,7 @@ function mkWCFile({ style, html, js }) { }) js = fixImport(js) - .replace(/export default class ([a-zA-Z0-9]+)/, function (s, m) { + .replace(/export default class ([a-zA-Z0-9]+)/, function(s, m) { name = m return `${s} extends HTMLElement ` }) @@ -97,13 +104,11 @@ function mkWCFile({ style, html, js }) { ) .replace('mounted()', 'connectedCallback()') .replace('unmounted()', 'disconnectedCallback()') - .replace( - 'watch() {', - 'attributeChangedCallback(name, old, val) {\nif (old === val) {return}' - ) + .replace('watch() {', 'attributeChangedCallback(name, old, val) {\nif (old === val) {return}') .replace('adopted()', 'adoptedCallback()') - return `/** + return transform(js, ESBUILD_OPTIONS).then(res => { + return `/** * * @authors yutent (yutent.io@gmail.com) * @date ${BUILD_DATE} @@ -111,16 +116,17 @@ function mkWCFile({ style, html, js }) { * */ -${js} +${res.code} if(!customElements.get('wc-${parseName(name)}')){ customElements.define('wc-${parseName(name)}', ${name}) } ` + }) } const compileWC = (entry, output) => { - log('编译wc: %s', chalk.green(entry)) + var t1 = Date.now() let code = fs.cat(entry).toString() let style = code.match(/]*?>([\w\W]*?)<\/style>/) let html = code.match(/