代码中不再注入环境变量; 模板文件支持条件判断

pull/1/head
yutent 2023-05-11 12:25:07 +08:00
parent 3e6ecc5e5f
commit 0af6b36027
3 changed files with 25 additions and 34 deletions

View File

@ -37,6 +37,8 @@ if (NODE_VERSION < 16.6) {
switch (mode) { switch (mode) {
case 'dev': case 'dev':
process.env.NODE_ENV = 'development'
import(PROTOCOL + CONFIG_FILE) import(PROTOCOL + CONFIG_FILE)
.then(function (conf) { .then(function (conf) {
createServer(WORK_SPACE, conf.default) createServer(WORK_SPACE, conf.default)
@ -47,6 +49,8 @@ switch (mode) {
break break
case 'build': case 'build':
process.env.NODE_ENV = 'production'
import(PROTOCOL + CONFIG_FILE) import(PROTOCOL + CONFIG_FILE)
.then(function (conf) { .then(function (conf) {
let dist = conf.buildDir || 'dist' let dist = conf.buildDir || 'dist'

View File

@ -127,14 +127,14 @@ export function parseJs(
code = '', code = '',
imports, imports,
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {}, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {},
isBuild,
filename filename
) { ) {
let fixedStyle = '\n\n' let fixedStyle = '\n\n'
let ASSETS_DIR = '/@/' let ASSETS_DIR = '/@/'
let isBuild = process.env.NODE_ENV === 'production'
if (isBuild) { if (isBuild) {
ASSETS_DIR = '/assets/' // + (IS_MPA ? 'pages/' : '') ASSETS_DIR = '/assets/'
} }
try { try {
code = Es.transformSync(code).code || '' code = Es.transformSync(code).code || ''
@ -157,6 +157,7 @@ export function parseJs(
return ( return (
code code
.replace(/\r\n/g, '\n') .replace(/\r\n/g, '\n')
.replace(/process\.env\.NODE_ENV/g, `'${process.env.NODE_ENV}'`)
.replace( .replace(
/import ([\w\W]*?) from (["'])(.*?)\2/g, /import ([\w\W]*?) from (["'])(.*?)\2/g,
function (m, alias, q, name) { function (m, alias, q, name) {
@ -257,7 +258,7 @@ export function parseJs(
* @param file <String> 文件路径 * @param file <String> 文件路径
* @return <String> 返回转换后的js代码 * @return <String> 返回转换后的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 filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/')
let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n') let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n')
let CACHE = options.CACHE || {} let CACHE = options.CACHE || {}
@ -317,7 +318,7 @@ export function compileVue(file, imports, options = {}, isBuild) {
CACHE[file] = { changed: false, js, html } CACHE[file] = { changed: false, js, html }
} }
output += parseJs(js, imports, options, isBuild, file).replace( output += parseJs(js, imports, options, file).replace(
'export default {', 'export default {',
`\nconst __sheets__ = [...document.adoptedStyleSheets]\n${html}\n\nconst __sfc__ = {\n render,\n` `\nconst __sheets__ = [...document.adoptedStyleSheets]\n${html}\n\nconst __sfc__ = {\n render,\n`
) )
@ -347,19 +348,17 @@ document.adoptedStyleSheets = __sheets__
/** /**
* 解析模板html * 解析模板html
*/ */
export function parseHtml(html, { page, imports, entry }, isBuild = false) { export function parseHtml(html, { page, imports, entry }) {
return html return html
.replace(/\r\n/g, '\n') .replace(/\r\n/g, '\n')
.replace( .replace(
'</head>', '</head>',
` <script>window.process = {env: {NODE_ENV: '${ `${
isBuild ? 'production' : 'development' process.env.NODE_ENV === 'development'
}'}}</script>\n${ ? ` <script>${
isBuild
? ''
: ` <script>${
Es.transformSync(HMR_SCRIPT, { minify: true }).code Es.transformSync(HMR_SCRIPT, { minify: true }).code
}</script>` }</script>`
: ''
}</head>` }</head>`
) )
.replace('{{title}}', page.title || '') .replace('{{title}}', page.title || '')
@ -370,4 +369,9 @@ export function parseHtml(html, { page, imports, entry }, isBuild = false) {
'<script src="main.js"></script>', '<script src="main.js"></script>',
`<script type="module">\n${entry}\n</script>` `<script type="module">\n${entry}\n</script>`
) )
.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}'`)
} }

View File

@ -23,23 +23,16 @@ export default function compile(root = '', dist = '', conf = {}) {
.filter(it => fs.isfile(it.path) && it.ext !== '') .filter(it => fs.isfile(it.path) && it.ext !== '')
let compileFiles = function (currentPage, page, files) { let compileFiles = function (currentPage, page, files) {
let options = { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH }
for (let it of files) { for (let it of files) {
// 入口文件, 特殊处理 // 入口文件, 特殊处理
if (page && it.path === page.entry) { if (page && it.path === page.entry) {
let entry = fs.cat(page.entry).toString() let entry = fs.cat(page.entry).toString()
entry = parseJs( entry = parseJs(entry, conf.imports, { ...options, IS_ENTRY: true })
entry,
conf.imports,
{ IS_MPA, currentPage, IS_ENTRY: true, DEPLOY_PATH },
true
)
let code = parseHtml( let code = parseHtml(template, { page, imports: conf.imports, entry })
template,
{ page, imports: conf.imports, entry },
true
)
fs.echo(code, join(dist, `${currentPage}.html`)) fs.echo(code, join(dist, `${currentPage}.html`))
continue continue
@ -52,12 +45,7 @@ export default function compile(root = '', dist = '', conf = {}) {
switch (it.ext) { switch (it.ext) {
case '.vue': case '.vue':
{ {
let code = compileVue( let code = compileVue(it.path, conf.imports, options)
it.path,
conf.imports,
{ IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH },
true
)
Es.transform(code, { minify: true }).then(r => { Es.transform(code, { minify: true }).then(r => {
fs.echo( fs.echo(
@ -72,12 +60,7 @@ export default function compile(root = '', dist = '', conf = {}) {
{ {
let code = fs.cat(it.path) let code = fs.cat(it.path)
code = parseJs( code = parseJs(code + '', conf.imports, options)
code + '',
conf.imports,
{ IS_MPA, currentPage, DEPLOY_PATH },
true
)
Es.transform(code, { minify: true }).then(r => { Es.transform(code, { minify: true }).then(r => {
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) fs.echo(r.code, join(dist, 'assets/', pageDir, it.name))
}) })