增加define配置的支持

master 1.4.0
yutent 2024-07-25 16:30:30 +08:00
parent c3dd7a843c
commit 7167a8b467
5 changed files with 45 additions and 15 deletions

View File

@ -115,7 +115,7 @@ export function compileScss(file, inject = '') {
export function parseJs( export function parseJs(
code = '', code = '',
imports, imports,
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {}, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE, define } = {},
filename, filename,
linePatch = 1 linePatch = 1
) { ) {
@ -287,6 +287,10 @@ export function parseJs(
} }
}) })
for (let key in define) {
code = code.replaceAll(key, define[key])
}
if (fixedStyle) { if (fixedStyle) {
code += '\n\n' + (IS_ENTRY ? SHEETS_DEF : '') + fixedStyle code += '\n\n' + (IS_ENTRY ? SHEETS_DEF : '') + fixedStyle
@ -433,9 +437,6 @@ document.adoptedStyleSheets = __sheets__
} }
output += `__sfc__.__file = '${filename}'\nexport default __sfc__` output += `__sfc__.__file = '${filename}'\nexport default __sfc__`
for (let fn of options.plugin) {
output = await fn('js', output)
}
return output return output
} }

View File

@ -46,9 +46,12 @@ export async function compileFiles(
{ {
let code = await compileVue(path, imports, options) let code = await compileVue(path, imports, options)
await Es.transform(code, { minify: true }).then(r => { await Es.transform(code, { minify: true }).then(async ({ code }) => {
for (let fn of options.plugin) {
code = await fn('js', code)
}
fs.echo( fs.echo(
r.code, code,
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
) )
}) })

View File

@ -34,6 +34,7 @@ export default async function createServer(root = '', conf = {}) {
const LEGACY_MODE = !!conf.legacy const LEGACY_MODE = !!conf.legacy
const ENABLE_GZIP = !!conf.devServer.gzip const ENABLE_GZIP = !!conf.devServer.gzip
const { isCustomElement = defaultCustomElement } = conf.compileOptions || {} const { isCustomElement = defaultCustomElement } = conf.compileOptions || {}
const { plugin = [], define = {} } = conf
if (conf.imports['vue-dev']) { if (conf.imports['vue-dev']) {
conf.imports.vue = conf.imports['vue-dev'] conf.imports.vue = conf.imports['vue-dev']
@ -73,7 +74,7 @@ export default async function createServer(root = '', conf = {}) {
currentPage = '' currentPage = ''
server server
.on('request', function (req, res) { .on('request', async function (req, res) {
let prefix = DEPLOY_PATH ? DEPLOY_PATH.replace(/\/$/, '') : '' let prefix = DEPLOY_PATH ? DEPLOY_PATH.replace(/\/$/, '') : ''
let url = let url =
prefix && req.url.startsWith(prefix) prefix && req.url.startsWith(prefix)
@ -183,6 +184,10 @@ export default async function createServer(root = '', conf = {}) {
page.entry page.entry
) )
for (let fn of plugin) {
entry = await fn('js', entry)
}
code = parseHtml(html, { code = parseHtml(html, {
page, page,
imports: conf.imports, imports: conf.imports,
@ -215,7 +220,7 @@ export default async function createServer(root = '', conf = {}) {
return return
} }
code = compileVue(file, conf.imports, { code = await compileVue(file, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
@ -223,9 +228,15 @@ export default async function createServer(root = '', conf = {}) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
isCustomElement isCustomElement,
plugin,
define
}) })
for (let fn of plugin) {
code = await fn('js', code)
}
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
} }
break break
@ -251,6 +262,9 @@ export default async function createServer(root = '', conf = {}) {
return return
} }
code = compileScss(file) code = compileScss(file)
for (let fn of plugin) {
code = await fn('css', code)
}
res.setHeader('content-type', MIME_TYPES.css) res.setHeader('content-type', MIME_TYPES.css)
} }
break break
@ -307,6 +321,9 @@ export default async function createServer(root = '', conf = {}) {
}, },
file file
) )
for (let fn of plugin) {
code = await fn('js', code)
}
} }
res.setHeader('content-type', MIME_TYPES[ext]) res.setHeader('content-type', MIME_TYPES[ext])
} }
@ -387,7 +404,7 @@ export default async function createServer(root = '', conf = {}) {
) )
chokidar chokidar
.watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')]) .watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')])
.on('all', (act, filePath) => { .on('all', async (act, filePath) => {
if (ready) { if (ready) {
let file = filePath.slice(SOURCE_DIR.length) let file = filePath.slice(SOURCE_DIR.length)
@ -407,6 +424,9 @@ export default async function createServer(root = '', conf = {}) {
} else { } else {
content = fs.cat(filePath).toString() content = fs.cat(filePath).toString()
} }
for (let fn of plugin) {
content = await fn('css', content)
}
ws.send({ ws.send({
action: 'render', action: 'render',
data: { path: file.replace(/\\/g, '/'), content } data: { path: file.replace(/\\/g, '/'), content }
@ -416,7 +436,7 @@ export default async function createServer(root = '', conf = {}) {
case 'vue': case 'vue':
{ {
let content = compileVue(filePath, conf.imports, { let content = await compileVue(filePath, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
@ -424,7 +444,9 @@ export default async function createServer(root = '', conf = {}) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
isCustomElement isCustomElement,
plugin,
define
}) })
let tmp = CACHE[filePath] let tmp = CACHE[filePath]
if (tmp.changed) { if (tmp.changed) {

View File

@ -52,7 +52,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
) )
const INJECT_SCSS = readFile(conf.inject?.scss) const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy const LEGACY_MODE = !!conf.legacy
const { ABS_CONFIG_FILEPATH } = conf const { ABS_CONFIG_FILEPATH, compileOptions = {}, define = {} } = conf
const { isCustomElement = defaultCustomElement } = compileOptions
conf.inject = conf.inject || { scss: '' } conf.inject = conf.inject || { scss: '' }
@ -65,7 +66,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
ABS_CONFIG_FILEPATH ABS_CONFIG_FILEPATH,
define
} }
fs.ls(SOURCE_DIR, true).forEach(path => { fs.ls(SOURCE_DIR, true).forEach(path => {
@ -110,6 +112,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
}) })
) )
} }
} else {
options.isCustomElement = isCustomElement
} }
// 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件

View File

@ -1,7 +1,7 @@
{ {
"name": "fite", "name": "fite",
"type": "module", "type": "module",
"version": "1.3.1", "version": "1.4.0",
"bin": { "bin": {
"fite": "index.js" "fite": "index.js"
}, },