From 4fd49438aae3ecad45933795fb4af5df315688c8 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 31 Jul 2021 18:16:17 +0800 Subject: [PATCH] wip(app): nvue --- .../uni-cli-shared/src/json/app/pages/nvue.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/packages/uni-cli-shared/src/json/app/pages/nvue.ts b/packages/uni-cli-shared/src/json/app/pages/nvue.ts index bdecd1d4f..fe7f97425 100644 --- a/packages/uni-cli-shared/src/json/app/pages/nvue.ts +++ b/packages/uni-cli-shared/src/json/app/pages/nvue.ts @@ -1,3 +1,4 @@ +import fs from 'fs' import path from 'path' import { normalizePath } from '../../../utils' @@ -20,6 +21,7 @@ function genNVueEntryCode(route: string) { return `import App from '${normalizePath( path.resolve(process.env.UNI_INPUT_DIR, route) )}.nvue?mpType=page' +${genNVueAppStyle()} if (typeof Promise !== 'undefined' && !Promise.prototype.finally) { Promise.prototype.finally = function(callback) { var promise = this.constructor @@ -40,3 +42,56 @@ App.el = '#root' new Vue(App) ` } + +interface VueSfcStyle { + src?: string + attrs: Record +} + +function genNVueAppStyle() { + if (process.env.UNI_NVUE_COMPILER !== 'uni-app') { + return '' + } + const appVuePath = path.resolve(process.env.UNI_INPUT_DIR, 'App.vue') + let code = 'Vue.prototype.__$appStyle__ = {}\n' + let styles = [] + try { + if (fs.existsSync(appVuePath)) { + const { + parseComponent, + } = require('@dcloudio/uni-cli-nvue/lib/weex-template-compiler') + styles = parseComponent(fs.readFileSync(appVuePath, 'utf8')).styles + } + } catch (e) {} + const loaderUtils = require('loader-utils') + const stringifyRequest = (r: string) => loaderUtils.stringifyRequest({}, r) + styles.forEach((style: VueSfcStyle, index: number) => { + if (!style.src) { + style.src = normalizePath(appVuePath) + } + code = + code + + `import __style${index} from ${genStyleRequest( + style, + index, + stringifyRequest + )} +Vue.prototype.__merge_style(__style${index},Vue.prototype.__$appStyle__)\n` + }) + return code +} + +function genStyleRequest( + style: VueSfcStyle, + i: number, + stringifyRequest: Function +) { + const { + attrsToQuery, + } = require('@dcloudio/uni-cli-nvue/lib/vue-loader/lib/codegen/utils') + + const src = style.src + const attrsQuery = attrsToQuery(style.attrs, 'css') + const query = `?vue&type=style&index=${i}${attrsQuery}` + return stringifyRequest(src + query) +} -- GitLab