From 0af6b36027483448050bcaf16a872d7948bcbaaf Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 11 May 2023 12:25:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B8=AD=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F;=20?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E6=96=87=E4=BB=B6=E6=94=AF=E6=8C=81=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 4 ++++ lib/compile-vue.js | 26 +++++++++++++++----------- lib/prod.js | 29 ++++++----------------------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 948622d..5ff1900 100755 --- a/index.js +++ b/index.js @@ -37,6 +37,8 @@ if (NODE_VERSION < 16.6) { switch (mode) { case 'dev': + process.env.NODE_ENV = 'development' + import(PROTOCOL + CONFIG_FILE) .then(function (conf) { createServer(WORK_SPACE, conf.default) @@ -47,6 +49,8 @@ switch (mode) { break case 'build': + process.env.NODE_ENV = 'production' + import(PROTOCOL + CONFIG_FILE) .then(function (conf) { let dist = conf.buildDir || 'dist' diff --git a/lib/compile-vue.js b/lib/compile-vue.js index a1eee3b..7cdd547 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -127,14 +127,14 @@ export function parseJs( code = '', imports, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {}, - isBuild, filename ) { let fixedStyle = '\n\n' let ASSETS_DIR = '/@/' + let isBuild = process.env.NODE_ENV === 'production' if (isBuild) { - ASSETS_DIR = '/assets/' // + (IS_MPA ? 'pages/' : '') + ASSETS_DIR = '/assets/' } try { code = Es.transformSync(code).code || '' @@ -157,6 +157,7 @@ export function parseJs( return ( code .replace(/\r\n/g, '\n') + .replace(/process\.env\.NODE_ENV/g, `'${process.env.NODE_ENV}'`) .replace( /import ([\w\W]*?) from (["'])(.*?)\2/g, function (m, alias, q, name) { @@ -257,7 +258,7 @@ export function parseJs( * @param file 文件路径 * @return 返回转换后的js代码 */ -export function compileVue(file, imports, options = {}, isBuild) { +export function compileVue(file, imports, options = {}) { let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/') let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n') let CACHE = options.CACHE || {} @@ -317,7 +318,7 @@ export function compileVue(file, imports, options = {}, isBuild) { CACHE[file] = { changed: false, js, html } } - output += parseJs(js, imports, options, isBuild, file).replace( + output += parseJs(js, imports, options, file).replace( 'export default {', `\nconst __sheets__ = [...document.adoptedStyleSheets]\n${html}\n\nconst __sfc__ = {\n render,\n` ) @@ -347,19 +348,17 @@ document.adoptedStyleSheets = __sheets__ /** * 解析模板html */ -export function parseHtml(html, { page, imports, entry }, isBuild = false) { +export function parseHtml(html, { page, imports, entry }) { return html .replace(/\r\n/g, '\n') .replace( '', - ` \n${ - isBuild - ? '' - : ` ` + : '' }` ) .replace('{{title}}', page.title || '') @@ -370,4 +369,9 @@ export function parseHtml(html, { page, imports, entry }, isBuild = false) { '', `` ) + .replace(/\{\{#if(.*?)\}\}([\w\W]*?)\{\{#\/if\}\}/g, (m, c, code) => { + let res = Function('return ' + c)() + return res ? code : '' + }) + .replace(/process\.env\.NODE_ENV/g, `'${process.env.NODE_ENV}'`) } diff --git a/lib/prod.js b/lib/prod.js index b07d7fc..6c70955 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -23,23 +23,16 @@ export default function compile(root = '', dist = '', conf = {}) { .filter(it => fs.isfile(it.path) && it.ext !== '') let compileFiles = function (currentPage, page, files) { + let options = { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH } + for (let it of files) { // 入口文件, 特殊处理 if (page && it.path === page.entry) { let entry = fs.cat(page.entry).toString() - entry = parseJs( - entry, - conf.imports, - { IS_MPA, currentPage, IS_ENTRY: true, DEPLOY_PATH }, - true - ) + entry = parseJs(entry, conf.imports, { ...options, IS_ENTRY: true }) - let code = parseHtml( - template, - { page, imports: conf.imports, entry }, - true - ) + let code = parseHtml(template, { page, imports: conf.imports, entry }) fs.echo(code, join(dist, `${currentPage}.html`)) continue @@ -52,12 +45,7 @@ export default function compile(root = '', dist = '', conf = {}) { switch (it.ext) { case '.vue': { - let code = compileVue( - it.path, - conf.imports, - { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH }, - true - ) + let code = compileVue(it.path, conf.imports, options) Es.transform(code, { minify: true }).then(r => { fs.echo( @@ -72,12 +60,7 @@ export default function compile(root = '', dist = '', conf = {}) { { let code = fs.cat(it.path) - code = parseJs( - code + '', - conf.imports, - { IS_MPA, currentPage, DEPLOY_PATH }, - true - ) + code = parseJs(code + '', conf.imports, options) Es.transform(code, { minify: true }).then(r => { fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) })