diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 35b9c3c..f061f39 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -115,7 +115,7 @@ export function compileScss(file, inject = '') { export function parseJs( code = '', imports, - { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {}, + { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE, define } = {}, filename, linePatch = 1 ) { @@ -287,6 +287,10 @@ export function parseJs( } }) + for (let key in define) { + code = code.replaceAll(key, define[key]) + } + if (fixedStyle) { code += '\n\n' + (IS_ENTRY ? SHEETS_DEF : '') + fixedStyle @@ -433,9 +437,6 @@ document.adoptedStyleSheets = __sheets__ } output += `__sfc__.__file = '${filename}'\nexport default __sfc__` - for (let fn of options.plugin) { - output = await fn('js', output) - } return output } diff --git a/lib/compile.js b/lib/compile.js index 0208dca..fd81618 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -46,9 +46,12 @@ export async function compileFiles( { 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( - r.code, + code, join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) ) }) diff --git a/lib/dev.js b/lib/dev.js index 59696c7..9bc8f63 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -34,6 +34,7 @@ export default async function createServer(root = '', conf = {}) { const LEGACY_MODE = !!conf.legacy const ENABLE_GZIP = !!conf.devServer.gzip const { isCustomElement = defaultCustomElement } = conf.compileOptions || {} + const { plugin = [], define = {} } = conf if (conf.imports['vue-dev']) { conf.imports.vue = conf.imports['vue-dev'] @@ -73,7 +74,7 @@ export default async function createServer(root = '', conf = {}) { currentPage = '' server - .on('request', function (req, res) { + .on('request', async function (req, res) { let prefix = DEPLOY_PATH ? DEPLOY_PATH.replace(/\/$/, '') : '' let url = prefix && req.url.startsWith(prefix) @@ -183,6 +184,10 @@ export default async function createServer(root = '', conf = {}) { page.entry ) + for (let fn of plugin) { + entry = await fn('js', entry) + } + code = parseHtml(html, { page, imports: conf.imports, @@ -215,7 +220,7 @@ export default async function createServer(root = '', conf = {}) { return } - code = compileVue(file, conf.imports, { + code = await compileVue(file, conf.imports, { IS_MPA, currentPage, SOURCE_DIR, @@ -223,9 +228,15 @@ export default async function createServer(root = '', conf = {}) { DEPLOY_PATH, INJECT_SCSS, LEGACY_MODE, - isCustomElement + isCustomElement, + plugin, + define }) + for (let fn of plugin) { + code = await fn('js', code) + } + res.setHeader('content-type', MIME_TYPES.js) } break @@ -251,6 +262,9 @@ export default async function createServer(root = '', conf = {}) { return } code = compileScss(file) + for (let fn of plugin) { + code = await fn('css', code) + } res.setHeader('content-type', MIME_TYPES.css) } break @@ -307,6 +321,9 @@ export default async function createServer(root = '', conf = {}) { }, file ) + for (let fn of plugin) { + code = await fn('js', code) + } } res.setHeader('content-type', MIME_TYPES[ext]) } @@ -387,7 +404,7 @@ export default async function createServer(root = '', conf = {}) { ) chokidar .watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')]) - .on('all', (act, filePath) => { + .on('all', async (act, filePath) => { if (ready) { let file = filePath.slice(SOURCE_DIR.length) @@ -407,6 +424,9 @@ export default async function createServer(root = '', conf = {}) { } else { content = fs.cat(filePath).toString() } + for (let fn of plugin) { + content = await fn('css', content) + } ws.send({ action: 'render', data: { path: file.replace(/\\/g, '/'), content } @@ -416,7 +436,7 @@ export default async function createServer(root = '', conf = {}) { case 'vue': { - let content = compileVue(filePath, conf.imports, { + let content = await compileVue(filePath, conf.imports, { IS_MPA, currentPage, SOURCE_DIR, @@ -424,7 +444,9 @@ export default async function createServer(root = '', conf = {}) { DEPLOY_PATH, INJECT_SCSS, LEGACY_MODE, - isCustomElement + isCustomElement, + plugin, + define }) let tmp = CACHE[filePath] if (tmp.changed) { diff --git a/lib/prod.js b/lib/prod.js index 2027912..97b82c7 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -52,7 +52,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { ) const INJECT_SCSS = readFile(conf.inject?.scss) 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: '' } @@ -65,7 +66,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { DEPLOY_PATH, INJECT_SCSS, LEGACY_MODE, - ABS_CONFIG_FILEPATH + ABS_CONFIG_FILEPATH, + define } fs.ls(SOURCE_DIR, true).forEach(path => { @@ -110,6 +112,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { }) ) } + } else { + options.isCustomElement = isCustomElement } // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 diff --git a/package.json b/package.json index 5628368..70acbf4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fite", "type": "module", - "version": "1.3.1", + "version": "1.4.0", "bin": { "fite": "index.js" },