From aed4186bcfbadbe4bd7baa95b236dff67c92a452 Mon Sep 17 00:00:00 2001 From: xuegan Date: Wed, 22 Mar 2023 17:22:34 +0800 Subject: [PATCH 01/21] =?UTF-8?q?feat:=20native-loader=20=E6=94=BE?= =?UTF-8?q?=E5=BC=80=E5=A4=84=E7=90=86=E5=8E=9F=E7=94=9F=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E8=BD=ACH5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/json-compiler/helper.js | 2 +- packages/webpack-plugin/lib/native-loader.js | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index 19d8a7b386..e46531cc87 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -79,7 +79,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom outputPath = getOutputPath(resourcePath, 'component') } } - if (ext === '.js' && mode !== 'web') { + if (ext === '.js') { resource = `!!${nativeLoaderPath}!${resource}` } diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index 15e9c92593..c6095c2460 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -1,5 +1,6 @@ const path = require('path') const JSON5 = require('json5') +const fs = require('fs') const parseRequest = require('./utils/parse-request') const config = require('./config') const createHelpers = require('./helpers') @@ -8,6 +9,11 @@ const async = require('async') const { matchCondition } = require('./utils/match-condition') const fixUsingComponent = require('./utils/fix-using-component') const { JSON_JS_EXT } = require('./utils/const') +const processTemplate = require('./web/processTemplate') +const processStyles = require('./web/processStyles') +const processJSON = require('./web/processJSON') +const processScript = require('./web/processScript') +const RecordVueContentDependency = require("./dependencies/RecordVueContentDependency") module.exports = function (content) { this.cacheable() @@ -49,6 +55,9 @@ module.exports = function (content) { const hasScoped = (queryObj.scoped || autoScope) && mode === 'ali' const hasComment = false const isNative = true + const parts = {} + let ctorType = 'component' + const checkFileExists = (extName, callback) => { this.resolve(parsed.dir, resourceName + extName, callback) @@ -113,6 +122,100 @@ module.exports = function (content) { }) }, callback) }, + (callback) => { + if (mode === 'web') { + async.forEachOf(typeExtMap, (ext, key, callback) => { + // 检测到jsonjs或cssLang时跳过对应类型文件检测 + if (typeResourceMap[key]) { + fs.readFile(typeResourceMap[key], (err, content) => { + if (err) return callback(err) + parts[key] = { + content: content.toString('utf-8'), + tag: key, + attrs: {} + } + callback() + }) + } else { + callback() + } + }, callback) + } else { + callback() + } + }, + (callback) => { + if (mode === 'web') { + async.waterfall([ + (callback) => { + async.parallel([ + (callback) => { + processTemplate(parts.template, { + loaderContext, + hasScoped, + hasComment, + isNative, + srcMode, + moduleId, + ctorType, + usingComponents: [], + componentGenerics: {} + }, callback) + }, + (callback) => { + processStyles(parts.styles, { + ctorType, + autoScope, + moduleId + }, callback) + }, + (callback) => { + processJSON(parts.json, { + loaderContext, + pagesMap, + componentsMap + }, callback) + } + ], (err, res) => { + callback(err, res) + }) + }, + ([templateRes, stylesRes, jsonRes], callback) => { + output += templateRes.output + output += stylesRes.output + output += jsonRes.output + if (ctorType === 'app' && jsonRes.jsonObj.window && jsonRes.jsonObj.window.navigationBarTitleText) { + mpx.appTitle = jsonRes.jsonObj.window.navigationBarTitleText + } + + processScript(parts.script, { + loaderContext, + ctorType, + srcMode, + moduleId, + isProduction, + componentGenerics, + jsonConfig: jsonRes.jsonObj, + outputPath: queryObj.outputPath || '', + tabBarMap: jsonRes.tabBarMap, + tabBarStr: jsonRes.tabBarStr, + builtInComponentsMap: templateRes.builtInComponentsMap, + genericsInfo: templateRes.genericsInfo, + wxsModuleMap: templateRes.wxsModuleMap, + localComponentsMap: jsonRes.localComponentsMap, + localPagesMap: jsonRes.localPagesMap + }, callback) + } + ], (err, scriptRes) => { + if (err) return callback(err) + output += scriptRes.output + this._module.addPresentationalDependency(new RecordVueContentDependency(filePath, output)) + callback(null, output) + }) + } else { + callback() + } + }, (callback) => { getJSONContent({ src: typeResourceMap.json, From 28f71bfb2c5aea8335d584f3afffb095868e5139 Mon Sep 17 00:00:00 2001 From: xuegan Date: Thu, 23 Mar 2023 14:43:01 +0800 Subject: [PATCH 02/21] =?UTF-8?q?feat:=E8=B0=83=E6=95=B4=E5=8E=9F=E7=94=9F?= =?UTF-8?q?=E7=BB=84=E4=BB=B6rules=20loader=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/index.js | 12 +++ .../lib/json-compiler/helper.js | 4 +- packages/webpack-plugin/lib/native-loader.js | 75 +++++++++---------- .../webpack-plugin/lib/web/processScript.js | 4 +- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index b9cf35d70f..6b629b389b 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -49,6 +49,7 @@ const wxssLoaderPath = normalize.lib('wxss/index') const wxmlLoaderPath = normalize.lib('wxml/loader') const wxsLoaderPath = normalize.lib('wxs/loader') const styleCompilerPath = normalize.lib('style-compiler/index') +const nativeLoaderPath = normalize.lib('native-loader') const templateCompilerPath = normalize.lib('template-compiler/index') const jsonCompilerPath = normalize.lib('json-compiler/index') const jsonThemeCompilerPath = normalize.lib('json-compiler/theme') @@ -1439,6 +1440,7 @@ try { let cssLoaderIndex = -1 let vueStyleLoaderIndex = -1 let mpxStyleLoaderIndex = -1 + let babelLoaderIndex = -1 loaders.forEach((loader, index) => { const currentLoader = toPosix(loader.loader) if (currentLoader.includes('css-loader') && cssLoaderIndex === -1) { @@ -1447,6 +1449,8 @@ try { vueStyleLoaderIndex = index } else if (currentLoader.includes(styleCompilerPath) && mpxStyleLoaderIndex === -1) { mpxStyleLoaderIndex = index + } else if (currentLoader.includes('babel-loader')) { + babelLoaderIndex = index } }) if (mpxStyleLoaderIndex === -1) { @@ -1463,6 +1467,14 @@ try { }) } } + if (babelLoaderIndex === loaders.length - 1 && queryObj.vue) { + loaders.splice(loaders.length - 1, 1, { + "loader": "/Users/didi/blackdir/mpx-native-to-h5/node_modules/@vue/vue-loader-v15/lib/index.js" + }) + loaders.splice(loaders.length, 0, { + "loader": "/Users/didi/blackdir/mpx-native-to-h5/node_modules/@mpxjs/webpack-plugin/lib/native-loader.js" + }) + } } createData.request = stringifyLoadersAndResource(loaders, createData.resource) diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index e46531cc87..e365b7d33a 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -80,7 +80,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom } } if (ext === '.js') { - resource = `!!${nativeLoaderPath}!${resource}` + resource = `!!@vue/vue-loader-v15!${nativeLoaderPath}!${resource}` } const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath) @@ -117,7 +117,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1] } } - if (ext === '.js' && mode !== 'web') { + if (ext === '.js') { resource = `!!${nativeLoaderPath}!${resource}` } const entry = getDynamicEntry(resource, 'page', outputPath, tarRoot, publicPath + tarRoot) diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index c6095c2460..7636b4a456 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -57,7 +57,7 @@ module.exports = function (content) { const isNative = true const parts = {} let ctorType = 'component' - + let output = '' const checkFileExists = (extName, callback) => { this.resolve(parsed.dir, resourceName + extName, callback) @@ -145,8 +145,40 @@ module.exports = function (content) { } }, (callback) => { + getJSONContent({ + src: typeResourceMap.json, + useJSONJS + }, null, this, callback) + }, (content, callback) => { + let json + try { + json = JSON5.parse(content) + } catch (e) { + return callback(e) + } + let usingComponents = Object.keys(mpx.usingComponents) + if (json.usingComponents) { + fixUsingComponent(json.usingComponents, mode) + usingComponents = usingComponents.concat(Object.keys(json.usingComponents)) + } + + // 注入构造函数 + let ctor = 'App' + let ctorType = 'app' + if (pagesMap[resourcePath]) { + ctorType = 'page' + if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') { + ctor = 'Page' + } else { + ctor = 'Component' + } + } else if (componentsMap[resourcePath]) { + ctor = 'Component' + ctorType = 'component' + } + if (mode === 'web') { - async.waterfall([ + return async.waterfall([ (callback) => { async.parallel([ (callback) => { @@ -194,7 +226,7 @@ module.exports = function (content) { srcMode, moduleId, isProduction, - componentGenerics, + componentGenerics: {}, jsonConfig: jsonRes.jsonObj, outputPath: queryObj.outputPath || '', tabBarMap: jsonRes.tabBarMap, @@ -212,27 +244,8 @@ module.exports = function (content) { this._module.addPresentationalDependency(new RecordVueContentDependency(filePath, output)) callback(null, output) }) - } else { - callback() - } - }, - (callback) => { - getJSONContent({ - src: typeResourceMap.json, - useJSONJS - }, null, this, callback) - }, (content, callback) => { - let json - try { - json = JSON5.parse(content) - } catch (e) { - return callback(e) - } - let usingComponents = Object.keys(mpx.usingComponents) - if (json.usingComponents) { - fixUsingComponent(json.usingComponents, mode) - usingComponents = usingComponents.concat(Object.keys(json.usingComponents)) } + const { getRequire } = createHelpers(loaderContext) @@ -271,25 +284,11 @@ module.exports = function (content) { } // 注入模块id及资源路径 - let output = `global.currentModuleId = ${JSON.stringify(moduleId)}\n` + output = `global.currentModuleId = ${JSON.stringify(moduleId)}\n` if (!isProduction) { output += `global.currentResource = ${JSON.stringify(filePath)}\n` } - // 注入构造函数 - let ctor = 'App' - let ctorType = 'app' - if (pagesMap[resourcePath]) { - ctorType = 'page' - if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') { - ctor = 'Page' - } else { - ctor = 'Component' - } - } else if (componentsMap[resourcePath]) { - ctor = 'Component' - ctorType = 'component' - } output += `global.currentCtor = ${ctor}\n` output += `global.currentCtorType = ${JSON.stringify(ctor.replace(/^./, (match) => { return match.toLowerCase() diff --git a/packages/webpack-plugin/lib/web/processScript.js b/packages/webpack-plugin/lib/web/processScript.js index 125fff3cc0..a3979b92fd 100644 --- a/packages/webpack-plugin/lib/web/processScript.js +++ b/packages/webpack-plugin/lib/web/processScript.js @@ -132,7 +132,7 @@ module.exports = function (script, { const i18nObj = Object.assign({}, i18n) content += ` import VueI18n from 'vue-i18n' import { createI18n } from 'vue-i18n-bridge' - + Vue.use(VueI18n , { bridge: true })\n` const requestObj = {} const i18nKeys = ['messages', 'dateTimeFormats', 'numberFormats'] @@ -221,7 +221,7 @@ module.exports = function (script, { content += ` ${getRequire('script', script, extraOptions)}\n` // createApp/Page/Component执行完成后立刻获取当前的option并暂存 - content += ` const currentOption = global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n` + content += ` const currentOption = global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n console.log('in this currentOption read')\n` // 获取pageConfig const pageConfig = {} if (ctorType === 'page') { From 3831ef0a057c43ea50e792ce98955b576cecb9b3 Mon Sep 17 00:00:00 2001 From: xuegan Date: Wed, 12 Apr 2023 22:35:50 +0800 Subject: [PATCH 03/21] =?UTF-8?q?core=E6=B7=BB=E5=8A=A0setData=20Proxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/platform/builtInMixins/index.js | 4 +++- .../builtInMixins/setDataMixin.web.js | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/platform/builtInMixins/setDataMixin.web.js diff --git a/packages/core/src/platform/builtInMixins/index.js b/packages/core/src/platform/builtInMixins/index.js index c7c54e63f5..e4736a2a83 100644 --- a/packages/core/src/platform/builtInMixins/index.js +++ b/packages/core/src/platform/builtInMixins/index.js @@ -10,6 +10,7 @@ import pageScrollMixin from './pageScrollMixin' import componentGenericsMixin from './componentGenericsMixin' import getTabBarMixin from './getTabBarMixin' import pageRouteMixin from './pageRouteMixin' +import setDataMixin from './setDataMixin' export default function getBuiltInMixins (options, type) { let bulitInMixins = [] @@ -24,7 +25,8 @@ export default function getBuiltInMixins (options, type) { getTabBarMixin(type), pageRouteMixin(type), // 由于relation可能是通过mixin注入的,不能通过当前的用户options中是否存在relations来简单判断是否注入该项mixin - relationsMixin(type) + relationsMixin(type), + setDataMixin(type) ] } else { // 此为差异抹平类mixins,原生模式下也需要注入也抹平平台差异 diff --git a/packages/core/src/platform/builtInMixins/setDataMixin.web.js b/packages/core/src/platform/builtInMixins/setDataMixin.web.js new file mode 100644 index 0000000000..a42a585e9c --- /dev/null +++ b/packages/core/src/platform/builtInMixins/setDataMixin.web.js @@ -0,0 +1,21 @@ +import {isObject, isFunction, error} from '@mpxjs/utils' + +export default function setDataMixin() { + return { + methods: { + setData(data, callback) { + if (!isObject(data)) { + error(`The data entry type of the setData method must be object, The type of data ${data} is incorrect`) + return + } + Object.keys(data).forEach(key => { + this[key] = data[key] + }) + if (callback && isFunction(callback)) { + this.$nextTick(callback.bind(this)) + } + } + } + } +} + From b182db0c248a64b18ac80d4bb38646ac9f46de9e Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 16 Jan 2024 15:46:02 +0800 Subject: [PATCH 04/21] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0parser=20hooks?= =?UTF-8?q?=20=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/index.js | 17 ++++++++++++++++- .../webpack-plugin/lib/json-compiler/helper.js | 2 +- .../webpack-plugin/lib/web/processScript.js | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 6b629b389b..48a559e20b 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1117,6 +1117,22 @@ class MpxWebpackPlugin { // 转换wx全局对象 parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transGlobalObject) + // 转换this.data等原生用法,仅在输出web时需要 + // parser.hooks.expression.for('this').tap('MpxWebpackPlugin', (expression) => { + // console.log('express ====', expression) + // }); + parser.hooks.evaluate.for('MemberExpression').tap('MpxWebpackPlugin', (expression) => { + if (expression.object && expression.object.type === 'ThisExpression' && expression.property.name === 'data') { + console.log('expression', expression) + const module = parser.state.module + const current = parser.state.current + const { queryObj, resourcePath } = parseRequest(module.resource) + if (!resourcePath.indexOf('@vant')) return + const replaceContent = 'this.$data' + const dep = new ReplaceDependency(replaceContent, expression.range) + current.addPresentationalDependency(dep) + } + }); // Proxy ctor for transMode if (!this.options.forceDisableProxyCtor) { parser.hooks.call.for('Page').tap('MpxWebpackPlugin', (expr) => { @@ -1135,7 +1151,6 @@ class MpxWebpackPlugin { }) } } - // 为跨平台api调用注入srcMode参数指导api运行时转换 const apiBlackListMap = [ 'createApp', diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index e365b7d33a..225a7004aa 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -79,7 +79,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom outputPath = getOutputPath(resourcePath, 'component') } } - if (ext === '.js') { + if (ext === '.js' && mode === 'web') { resource = `!!@vue/vue-loader-v15!${nativeLoaderPath}!${resource}` } diff --git a/packages/webpack-plugin/lib/web/processScript.js b/packages/webpack-plugin/lib/web/processScript.js index a3979b92fd..0a4a816016 100644 --- a/packages/webpack-plugin/lib/web/processScript.js +++ b/packages/webpack-plugin/lib/web/processScript.js @@ -221,7 +221,7 @@ module.exports = function (script, { content += ` ${getRequire('script', script, extraOptions)}\n` // createApp/Page/Component执行完成后立刻获取当前的option并暂存 - content += ` const currentOption = global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n console.log('in this currentOption read')\n` + content += ` const currentOption = global.__mpxOptionsMap[${JSON.stringify(moduleId)}]\n` // 获取pageConfig const pageConfig = {} if (ctorType === 'page') { From da72d930435f9d7e149925591216c43db011d146 Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 30 Jan 2024 15:28:03 +0800 Subject: [PATCH 05/21] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0this.data?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/builtInMixins/setDataMixin.web.js | 8 ++++++++ packages/webpack-plugin/lib/index.js | 16 ---------------- .../lib/platform/template/wx/index.js | 4 ++++ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/core/src/platform/builtInMixins/setDataMixin.web.js b/packages/core/src/platform/builtInMixins/setDataMixin.web.js index a42a585e9c..ac52658bd1 100644 --- a/packages/core/src/platform/builtInMixins/setDataMixin.web.js +++ b/packages/core/src/platform/builtInMixins/setDataMixin.web.js @@ -2,6 +2,14 @@ import {isObject, isFunction, error} from '@mpxjs/utils' export default function setDataMixin() { return { + beforeCreate() { + Object.defineProperty(this, 'data', { + get () { + return this.$data + }, + configurable: true + }) + }, methods: { setData(data, callback) { if (!isObject(data)) { diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 48a559e20b..b0e0d334fa 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1117,22 +1117,6 @@ class MpxWebpackPlugin { // 转换wx全局对象 parser.hooks.expression.for('wx').tap('MpxWebpackPlugin', transGlobalObject) - // 转换this.data等原生用法,仅在输出web时需要 - // parser.hooks.expression.for('this').tap('MpxWebpackPlugin', (expression) => { - // console.log('express ====', expression) - // }); - parser.hooks.evaluate.for('MemberExpression').tap('MpxWebpackPlugin', (expression) => { - if (expression.object && expression.object.type === 'ThisExpression' && expression.property.name === 'data') { - console.log('expression', expression) - const module = parser.state.module - const current = parser.state.current - const { queryObj, resourcePath } = parseRequest(module.resource) - if (!resourcePath.indexOf('@vant')) return - const replaceContent = 'this.$data' - const dep = new ReplaceDependency(replaceContent, expression.range) - current.addPresentationalDependency(dep) - } - }); // Proxy ctor for transMode if (!this.options.forceDisableProxyCtor) { parser.hooks.call.for('Page').tap('MpxWebpackPlugin', (expr) => { diff --git a/packages/webpack-plugin/lib/platform/template/wx/index.js b/packages/webpack-plugin/lib/platform/template/wx/index.js index ac0ae2cfae..eb031021de 100644 --- a/packages/webpack-plugin/lib/platform/template/wx/index.js +++ b/packages/webpack-plugin/lib/platform/template/wx/index.js @@ -338,6 +338,10 @@ module.exports = function getSpec ({ warn, error }) { const meta = { modifierStr } + const parsed = parseMustache(value) + if (parsed.hasBinding) { + value = parsed.result + } // 记录event监听信息用于后续判断是否需要使用内置基础组件 el.hasEvent = true const rPrefix = runRules(spec.event.prefix, prefix, { mode: 'web', meta }) From b86f6a51954dadc9a230782330cb72485b354167 Mon Sep 17 00:00:00 2001 From: xuegan Date: Wed, 31 Jan 2024 21:02:38 +0800 Subject: [PATCH 06/21] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20style=20?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E5=8E=9F=E7=94=9F=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/index.js | 2 +- packages/webpack-plugin/lib/json-compiler/helper.js | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index b0e0d334fa..84df2e8a73 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1431,7 +1431,7 @@ try { } createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true) } - + // TODO loader 顺序有待调整 if (mpx.mode === 'web') { const mpxStyleOptions = queryObj.mpxStyleOptions const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : '' diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index 225a7004aa..8426a595ac 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -80,7 +80,11 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom } } if (ext === '.js' && mode === 'web') { - resource = `!!@vue/vue-loader-v15!${nativeLoaderPath}!${resource}` + if (mode === 'web') { + resource = `!!@vue/vue-loader-v15!${nativeLoaderPath}!${resource}` + } else { + resource = `!!${nativeLoaderPath}!${resource}` + } } const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath) From ff637077d1e8a873a9bda22fa1838595e81a8d89 Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 20 Feb 2024 17:11:34 +0800 Subject: [PATCH 07/21] fix: save change --- .../webpack-plugin/lib/json-compiler/helper.js | 2 +- packages/webpack-plugin/lib/native-loader.js | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index 8426a595ac..e0ff0120bc 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -79,7 +79,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom outputPath = getOutputPath(resourcePath, 'component') } } - if (ext === '.js' && mode === 'web') { + if (ext === '.js') { if (mode === 'web') { resource = `!!@vue/vue-loader-v15!${nativeLoaderPath}!${resource}` } else { diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index 7636b4a456..ef2ef865ac 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -129,10 +129,18 @@ module.exports = function (content) { if (typeResourceMap[key]) { fs.readFile(typeResourceMap[key], (err, content) => { if (err) return callback(err) - parts[key] = { - content: content.toString('utf-8'), - tag: key, - attrs: {} + if (key === 'styles') { + parts[key] = [{ + content: content.toString('utf-8'), + tag: 'style', + attrs: {} + }] + } else { + parts[key] = { + content: content.toString('utf-8'), + tag: key, + attrs: {} + } } callback() }) From a5955574b66b15351efbe375163fc1287761739d Mon Sep 17 00:00:00 2001 From: xuegan Date: Tue, 20 Feb 2024 19:28:52 +0800 Subject: [PATCH 08/21] fix: merge master && fix eslint error --- .../builtInMixins/setDataMixin.web.js | 9 ++++----- packages/webpack-plugin/lib/index.js | 5 ++--- packages/webpack-plugin/lib/native-loader.js | 20 +------------------ .../lib/platform/template/wx/index.js | 2 +- 4 files changed, 8 insertions(+), 28 deletions(-) diff --git a/packages/core/src/platform/builtInMixins/setDataMixin.web.js b/packages/core/src/platform/builtInMixins/setDataMixin.web.js index ac52658bd1..04e785eb38 100644 --- a/packages/core/src/platform/builtInMixins/setDataMixin.web.js +++ b/packages/core/src/platform/builtInMixins/setDataMixin.web.js @@ -1,8 +1,8 @@ -import {isObject, isFunction, error} from '@mpxjs/utils' +import { isObject, isFunction, error } from '@mpxjs/utils' -export default function setDataMixin() { +export default function setDataMixin () { return { - beforeCreate() { + beforeCreate () { Object.defineProperty(this, 'data', { get () { return this.$data @@ -11,7 +11,7 @@ export default function setDataMixin() { }) }, methods: { - setData(data, callback) { + setData (data, callback) { if (!isObject(data)) { error(`The data entry type of the setData method must be object, The type of data ${data} is incorrect`) return @@ -26,4 +26,3 @@ export default function setDataMixin() { } } } - diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index c4fafbc710..5c413269fc 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -51,7 +51,6 @@ const wxssLoaderPath = normalize.lib('wxss/index') const wxmlLoaderPath = normalize.lib('wxml/loader') const wxsLoaderPath = normalize.lib('wxs/loader') const styleCompilerPath = normalize.lib('style-compiler/index') -const nativeLoaderPath = normalize.lib('native-loader') const templateCompilerPath = normalize.lib('template-compiler/index') const jsonCompilerPath = normalize.lib('json-compiler/index') const jsonThemeCompilerPath = normalize.lib('json-compiler/theme') @@ -1627,10 +1626,10 @@ try { } if (babelLoaderIndex === loaders.length - 1 && queryObj.vue) { loaders.splice(loaders.length - 1, 1, { - "loader": "/Users/didi/blackdir/mpx-native-to-h5/node_modules/@vue/vue-loader-v15/lib/index.js" + loader: '/Users/didi/blackdir/mpx-native-to-h5/node_modules/@vue/vue-loader-v15/lib/index.js' }) loaders.splice(loaders.length, 0, { - "loader": "/Users/didi/blackdir/mpx-native-to-h5/node_modules/@mpxjs/webpack-plugin/lib/native-loader.js" + loader: '/Users/didi/blackdir/mpx-native-to-h5/node_modules/@mpxjs/webpack-plugin/lib/native-loader.js' }) } } diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index d1bfbe17c9..17cfa25b82 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -12,13 +12,11 @@ const getRulesRunner = require('./platform') const getEntryName = require('./utils/get-entry-name') const AppEntryDependency = require('./dependencies/AppEntryDependency') const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency') -const fixUsingComponent = require('./utils/fix-using-component') -const { JSON_JS_EXT } = require('./utils/const') const processTemplate = require('./web/processTemplate') const processStyles = require('./web/processStyles') const processJSON = require('./web/processJSON') const processScript = require('./web/processScript') -const RecordVueContentDependency = require("./dependencies/RecordVueContentDependency") +const RecordVueContentDependency = require('./dependencies/RecordVueContentDependency') // todo native-loader考虑与mpx-loader或加强复用,原生组件约等于4个区块都为src的.mpx文件 module.exports = function (content) { @@ -63,7 +61,6 @@ module.exports = function (content) { const hasComment = false const isNative = true const parts = {} - let ctorType = 'component' let output = '' const checkFileExists = (extName, callback) => { @@ -243,21 +240,6 @@ module.exports = function (content) { componentPlaceholder = componentPlaceholder.concat(Object.values(json.componentPlaceholder)) } - // 注入构造函数 - let ctor = 'App' - let ctorType = 'app' - if (pagesMap[resourcePath]) { - ctorType = 'page' - if (mpx.forceUsePageCtor || mode === 'ali' || mode === 'swan') { - ctor = 'Page' - } else { - ctor = 'Component' - } - } else if (componentsMap[resourcePath]) { - ctor = 'Component' - ctorType = 'component' - } - if (mode === 'web') { return async.waterfall([ (callback) => { diff --git a/packages/webpack-plugin/lib/platform/template/wx/index.js b/packages/webpack-plugin/lib/platform/template/wx/index.js index a79147c1ba..78db13675b 100644 --- a/packages/webpack-plugin/lib/platform/template/wx/index.js +++ b/packages/webpack-plugin/lib/platform/template/wx/index.js @@ -387,7 +387,7 @@ module.exports = function getSpec ({ warn, error }) { modifierStr } const isComponent = usingComponents.indexOf(el.tag) !== -1 || el.tag === 'component' - const parsed = parseMustache(value) + const parsed = parseMustacheWithContext(value) if (parsed.hasBinding) { value = parsed.result } From a093cac663f3eb9df0f59f55236b3a5f484a210d Mon Sep 17 00:00:00 2001 From: xuegan Date: Fri, 23 Feb 2024 11:23:13 +0800 Subject: [PATCH 09/21] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4mpx-native-load?= =?UTF-8?q?er=E5=92=8Cvue-loader=E6=8F=92=E5=85=A5=E7=9A=84=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/index.js | 16 ++++++++-------- .../webpack-plugin/lib/json-compiler/helper.js | 7 ++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 5c413269fc..c8540d5200 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1597,7 +1597,7 @@ try { let cssLoaderIndex = -1 let vueStyleLoaderIndex = -1 let mpxStyleLoaderIndex = -1 - let babelLoaderIndex = -1 + let nativeLoaderIndex = -1 loaders.forEach((loader, index) => { const currentLoader = toPosix(loader.loader) if (currentLoader.includes('node_modules/css-loader') && cssLoaderIndex === -1) { @@ -1606,8 +1606,8 @@ try { vueStyleLoaderIndex = index } else if (currentLoader.includes(styleCompilerPath) && mpxStyleLoaderIndex === -1) { mpxStyleLoaderIndex = index - } else if (currentLoader.includes('babel-loader')) { - babelLoaderIndex = index + } else if (currentLoader.includes('native-loader')) { + nativeLoaderIndex = index } }) if (mpxStyleLoaderIndex === -1) { @@ -1624,12 +1624,12 @@ try { }) } } - if (babelLoaderIndex === loaders.length - 1 && queryObj.vue) { - loaders.splice(loaders.length - 1, 1, { - loader: '/Users/didi/blackdir/mpx-native-to-h5/node_modules/@vue/vue-loader-v15/lib/index.js' + if (queryObj.isNative && nativeLoaderIndex !== loaders.length - 1) { + loaders.push({ + loader: require.resolve('@vue/vue-loader-v15/lib/index.js') }) - loaders.splice(loaders.length, 0, { - loader: '/Users/didi/blackdir/mpx-native-to-h5/node_modules/@mpxjs/webpack-plugin/lib/native-loader.js' + loaders.push({ + loader: require.resolve(nativeLoaderPath) }) } } diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index 5392220a24..4aabd833f6 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -95,14 +95,11 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom // TODO 待调整 if (ext === '.js') { if (mode === 'web') { - resource = `!!@vue/vue-loader-v15!${nativeLoaderPath}!${resource}` + resource = `${resource}?isNative=true` } else { - resource = `!!${nativeLoaderPath}!${resource}` + resource = `!!${nativeLoaderPath}!${resource}?isNative=true` } } - if (isScript(ext) && mode !== 'web') { - resource = `!!${nativeLoaderPath}!${resource}` - } const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath) callback(null, entry, { From 22a53609fa17f3ba235862601f28f6bc00391e13 Mon Sep 17 00:00:00 2001 From: xuegan Date: Fri, 23 Feb 2024 11:36:40 +0800 Subject: [PATCH 10/21] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4selector?= =?UTF-8?q?=E4=B8=AD=E5=AF=B9=E4=BA=8E=E9=9D=9E.mpx=E5=90=8E=E7=BC=80?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/selector.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/webpack-plugin/lib/selector.js b/packages/webpack-plugin/lib/selector.js index a64c5ddb06..ac05adc1af 100644 --- a/packages/webpack-plugin/lib/selector.js +++ b/packages/webpack-plugin/lib/selector.js @@ -7,7 +7,8 @@ module.exports = function (content) { this.cacheable() // 兼容处理处理ts-loader中watch-run/updateFile逻辑,直接跳过当前loader及后续的loader返回内容 const pathExtname = path.extname(this.resourcePath) - if (!['.vue', '.mpx'].includes(pathExtname)) { + const { queryObj } = parseRequest(this.resource) + if (!['.vue', '.mpx'].includes(pathExtname) && !queryObj.isNative) { this.loaderIndex = tsWatchRunLoaderFilter(this.loaders, this.loaderIndex) return content } @@ -17,7 +18,6 @@ module.exports = function (content) { return content } - const { queryObj } = parseRequest(this.resource) const ctorType = queryObj.ctorType const type = queryObj.type const index = queryObj.index || 0 From 8f55d480d9572a67ab14b21fbf442e13e42989b1 Mon Sep 17 00:00:00 2001 From: xuegan Date: Fri, 23 Feb 2024 11:43:46 +0800 Subject: [PATCH 11/21] fix: delete some todo --- packages/webpack-plugin/lib/index.js | 1 - packages/webpack-plugin/lib/json-compiler/helper.js | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index c8540d5200..d24cd46b8a 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1589,7 +1589,6 @@ try { } createData.resource = addQuery(createData.resource, { mpx: MPX_PROCESSED_FLAG }, true) } - // TODO loader 顺序有待调整 if (mpx.mode === 'web') { const mpxStyleOptions = queryObj.mpxStyleOptions const firstLoader = loaders[0] ? toPosix(loaders[0].loader) : '' diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index 4aabd833f6..d696aadf7f 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -92,7 +92,6 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom outputPath = getOutputPath(resourcePath, 'component') } } - // TODO 待调整 if (ext === '.js') { if (mode === 'web') { resource = `${resource}?isNative=true` @@ -140,7 +139,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom outputPath = /^(.*?)(\.[^.]*)?$/.exec(relative)[1] } } - if (isScript(ext)) { + if (isScript(ext) && mode !== 'web') { resource = `!!${nativeLoaderPath}!${resource}` } const entry = getDynamicEntry(resource, 'page', outputPath, tarRoot, publicPath + tarRoot) From 0c5d041f921bc749c7f7600a22e409201dde29be Mon Sep 17 00:00:00 2001 From: xuegan Date: Fri, 22 Mar 2024 12:00:53 +0800 Subject: [PATCH 12/21] =?UTF-8?q?fix:=20=20=E4=B8=AD=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8C=85=E5=90=ABdata=E5=92=8Cprops?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/builtInMixins/setDataMixin.web.js | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/packages/core/src/platform/builtInMixins/setDataMixin.web.js b/packages/core/src/platform/builtInMixins/setDataMixin.web.js index 04e785eb38..c5b27d0f54 100644 --- a/packages/core/src/platform/builtInMixins/setDataMixin.web.js +++ b/packages/core/src/platform/builtInMixins/setDataMixin.web.js @@ -5,24 +5,10 @@ export default function setDataMixin () { beforeCreate () { Object.defineProperty(this, 'data', { get () { - return this.$data + return Object.assign({}, this.$data, this.$props) }, configurable: true }) - }, - methods: { - setData (data, callback) { - if (!isObject(data)) { - error(`The data entry type of the setData method must be object, The type of data ${data} is incorrect`) - return - } - Object.keys(data).forEach(key => { - this[key] = data[key] - }) - if (callback && isFunction(callback)) { - this.$nextTick(callback.bind(this)) - } - } } } } From 3ac037fe3c601b67a4591579b2f57c94bac9def1 Mon Sep 17 00:00:00 2001 From: xuegan Date: Sun, 24 Mar 2024 19:54:20 +0800 Subject: [PATCH 13/21] =?UTF-8?q?feat:=20vue=20=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20setData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/vuePlugin.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/core/src/vuePlugin.js b/packages/core/src/vuePlugin.js index 0e53a6971f..ab10b79090 100644 --- a/packages/core/src/vuePlugin.js +++ b/packages/core/src/vuePlugin.js @@ -1,4 +1,4 @@ -import { walkChildren, parseSelector, error, hasOwn } from '@mpxjs/utils' +import { walkChildren, parseSelector, error, hasOwn, isFunction } from '@mpxjs/utils' import { createSelectorQuery, createIntersectionObserver } from '@mpxjs/api-proxy' const datasetReg = /^data-(.+)$/ @@ -55,4 +55,9 @@ export default function install (Vue) { Vue.prototype.createIntersectionObserver = function (options) { return createIntersectionObserver(this, options) } + Vue.prototype.setData = function (newData, callback) { + if (callback && isFunction(callback)) { + this.$nextTick(callback.bind(this)) + } + } } From 7235ee87cb9d0de6ca4f72bb62dc099a309544f1 Mon Sep 17 00:00:00 2001 From: xuegan Date: Sun, 24 Mar 2024 20:02:52 +0800 Subject: [PATCH 14/21] =?UTF-8?q?feat:=20=E5=A4=84=E7=90=86=E6=AD=A3?= =?UTF-8?q?=E5=B8=B8=E9=A1=B6=E5=B1=82=E5=B1=9E=E6=80=A7=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/vuePlugin.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/vuePlugin.js b/packages/core/src/vuePlugin.js index ab10b79090..1325526f58 100644 --- a/packages/core/src/vuePlugin.js +++ b/packages/core/src/vuePlugin.js @@ -1,4 +1,4 @@ -import { walkChildren, parseSelector, error, hasOwn, isFunction } from '@mpxjs/utils' +import { walkChildren, parseSelector, error, isObject, hasOwn, isFunction } from '@mpxjs/utils' import { createSelectorQuery, createIntersectionObserver } from '@mpxjs/api-proxy' const datasetReg = /^data-(.+)$/ @@ -56,6 +56,27 @@ export default function install (Vue) { return createIntersectionObserver(this, options) } Vue.prototype.setData = function (newData, callback) { + if (!isObject(newData)) { + error(`The data entry type of the setData method must be object, The type of data ${data} is incorrect`) + return + } + const rawData = this.$data + Object.entries(newData).forEach(([key, value]) => { + + if (key.includes('[')) { + + } else if (key.includes('.')) { + + } else { + // key 为正常顶层属性 + if (hasOwn(rawData, key)) { + rawData[key] = value + } else { + // data 不存在属性,通过 $set 设置 + this.$set(rawData, key, value) + } + } + }) if (callback && isFunction(callback)) { this.$nextTick(callback.bind(this)) } From 6e3e56077cb9885aeacb801b3ed4894958c544f2 Mon Sep 17 00:00:00 2001 From: xuegan Date: Sun, 24 Mar 2024 20:14:04 +0800 Subject: [PATCH 15/21] =?UTF-8?q?feat:=20=E5=A4=84=E7=90=86=E5=B8=A6.?= =?UTF-8?q?=E7=9A=84=E8=B7=AF=E5=BE=84=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/vuePlugin.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/core/src/vuePlugin.js b/packages/core/src/vuePlugin.js index 1325526f58..e4f857f362 100644 --- a/packages/core/src/vuePlugin.js +++ b/packages/core/src/vuePlugin.js @@ -66,7 +66,18 @@ export default function install (Vue) { if (key.includes('[')) { } else if (key.includes('.')) { + // key 为索引的路径式设置 this.setData({'a.b': 'text'}) + const fullKeys = key.split('.'); + let target = this.$data; + const lastKey = fullKeys.pop(); + fullKeys.forEach((nestedKey) => { + if (!hasOwn(target, nestedKey)) { + this.$set(target, nestedKey, {}) + } + target = target[nestedKey] + }) + target[lastKey] = value; } else { // key 为正常顶层属性 if (hasOwn(rawData, key)) { From ed2f9752b3e57e222fce6dee1760fd61823e6987 Mon Sep 17 00:00:00 2001 From: xuegan Date: Sun, 24 Mar 2024 22:49:20 +0800 Subject: [PATCH 16/21] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84setData=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/vuePlugin.js | 52 +++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/packages/core/src/vuePlugin.js b/packages/core/src/vuePlugin.js index e4f857f362..3aa36cfe25 100644 --- a/packages/core/src/vuePlugin.js +++ b/packages/core/src/vuePlugin.js @@ -62,22 +62,28 @@ export default function install (Vue) { } const rawData = this.$data Object.entries(newData).forEach(([key, value]) => { - - if (key.includes('[')) { - - } else if (key.includes('.')) { - // key 为索引的路径式设置 this.setData({'a.b': 'text'}) - const fullKeys = key.split('.'); + if (key.includes('.') || key.includes('[')) { + // key 为索引的路径式设置 (如 'a.b', 'a[0].b.c') + const fullKeyItems = formatKey(key) let target = this.$data; - const lastKey = fullKeys.pop(); + const lastItem = fullKeyItems.pop() - fullKeys.forEach((nestedKey) => { - if (!hasOwn(target, nestedKey)) { - this.$set(target, nestedKey, {}) - } - target = target[nestedKey] - }) - target[lastKey] = value; + if (fullKeyItems.length > 0) { + fullKeyItems.forEach((item) => { + const nestedKey = item.name; + if (item.isArray) { + if (!Array.isArray(target[nestedKey])) { + this.$set(target, nestedKey, []) + } + } + if (!hasOwn(target, nestedKey)) { + this.$set(target, nestedKey, {}) + } + target = item.isArray? target[nestedKey][item.index]: target[nestedKey] + }) + } + + lastItem.isArray? this.$set(target[lastItem.name], lastItem.index, value) : target[lastItem.name] = value } else { // key 为正常顶层属性 if (hasOwn(rawData, key)) { @@ -88,6 +94,24 @@ export default function install (Vue) { } } }) + + function formatKey (key) { + const regex = /(\w+)(?:\[(\d+)\])?\.?/g + let match + const parsed = [] + while ((match = regex.exec(key)) !== null) { + const propertyName = match[1] + const index = match[2] + const property = { + name: propertyName, + isArray: !!index, + index: index ? parseInt(index, 10) : undefined, + } + parsed.push(property); + } + return parsed + } + if (callback && isFunction(callback)) { this.$nextTick(callback.bind(this)) } From 631cb7e7264712f474f68debe3af46859f77458f Mon Sep 17 00:00:00 2001 From: xuegan Date: Mon, 25 Mar 2024 11:08:20 +0800 Subject: [PATCH 17/21] =?UTF-8?q?feat:=20=E5=8E=9F=E7=94=9F=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E8=BE=93=E5=87=BAweb=E8=A1=A5=E9=BD=90mainScript?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/native-loader.js | 39 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index 17cfa25b82..8866623eb8 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -17,6 +17,7 @@ const processStyles = require('./web/processStyles') const processJSON = require('./web/processJSON') const processScript = require('./web/processScript') const RecordVueContentDependency = require('./dependencies/RecordVueContentDependency') +const processMainScript = require("./web/processMainScript"); // todo native-loader考虑与mpx-loader或加强复用,原生组件约等于4个区块都为src的.mpx文件 module.exports = function (content) { @@ -224,13 +225,11 @@ module.exports = function (content) { if (ctorType === 'app') { const appName = getEntryName(this) if (appName) this._module.addPresentationalDependency(new AppEntryDependency(resourcePath, appName)) + } else { + rulesRunnerOptions.mainKey = pagesMap[resourcePath] ? 'page' : 'component' } const moduleId = ctorType === 'app' ? MPX_APP_MODULE_ID : 'm' + mpx.pathHash(filePath) - - if (ctorType !== 'app') { - rulesRunnerOptions.mainKey = pagesMap[resourcePath] ? 'page' : 'component' - } const rulesRunner = getRulesRunner(rulesRunnerOptions) if (rulesRunner) rulesRunner(json) if (json.usingComponents) { @@ -241,6 +240,38 @@ module.exports = function (content) { } if (mode === 'web') { + if (ctorType === 'app' && !queryObj.isApp) { + return async.waterfall([ + (callback) => { + processJSON(parts.json, { loaderContext, pagesMap, componentsMap }, callback) + }, + (jsonRes, callback) => { + processMainScript(parts.script, { + loaderContext, + ctorType, + srcMode, + moduleId, + isProduction, + jsonConfig: jsonRes.jsonObj, + outputPath: queryObj.outputPath || '', + localComponentsMap: jsonRes.localComponentsMap, + tabBar: jsonRes.jsonObj.tabBar, + tabBarMap: jsonRes.tabBarMap, + tabBarStr: jsonRes.tabBarStr, + localPagesMap: jsonRes.localPagesMap, + resource: this.resource + }, callback) + } + ], (err, scriptRes) => { + if (err) return callback(err) + this.loaderIndex = -1 + return callback(null, scriptRes.output) + }) + } + // 通过RecordVueContentDependency和vueContentCache确保子request不再重复生成vueContent + const cacheContent = mpx.vueContentCache.get(filePath) + if (cacheContent) return callback(null, cacheContent) + return async.waterfall([ (callback) => { async.parallel([ From db83c366e5c79d1ca5f774c442cdf1a4837e0406 Mon Sep 17 00:00:00 2001 From: xuegan Date: Thu, 28 Mar 2024 11:31:13 +0800 Subject: [PATCH 18/21] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8feat-react=20pr?= =?UTF-8?q?ocessWeb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/webpack-plugin/lib/loader.js | 108 +++------------ packages/webpack-plugin/lib/native-loader.js | 126 ++++-------------- packages/webpack-plugin/lib/web/index.js | 108 +++++++++++++++ .../lib/web/processMainScript.js | 8 +- 4 files changed, 154 insertions(+), 196 deletions(-) create mode 100644 packages/webpack-plugin/lib/web/index.js diff --git a/packages/webpack-plugin/lib/loader.js b/packages/webpack-plugin/lib/loader.js index 360dd55117..30a8b1122b 100644 --- a/packages/webpack-plugin/lib/loader.js +++ b/packages/webpack-plugin/lib/loader.js @@ -19,6 +19,7 @@ const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDepen const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter') const { MPX_APP_MODULE_ID } = require('./utils/const') const path = require('path') +const processWeb = require('./web') const processMainScript = require('./web/processMainScript') const getRulesRunner = require('./platform') @@ -148,96 +149,23 @@ module.exports = function (content) { } // 处理mode为web时输出vue格式文件 if (mode === 'web') { - if (ctorType === 'app' && !queryObj.isApp) { - return async.waterfall([ - (callback) => { - processJSON(parts.json, { loaderContext, pagesMap, componentsMap }, callback) - }, - (jsonRes, callback) => { - processMainScript(parts.script, { - loaderContext, - ctorType, - srcMode, - moduleId, - isProduction, - jsonConfig: jsonRes.jsonObj, - outputPath: queryObj.outputPath || '', - localComponentsMap: jsonRes.localComponentsMap, - tabBar: jsonRes.jsonObj.tabBar, - tabBarMap: jsonRes.tabBarMap, - tabBarStr: jsonRes.tabBarStr, - localPagesMap: jsonRes.localPagesMap, - resource: this.resource - }, callback) - } - ], (err, scriptRes) => { - if (err) return callback(err) - this.loaderIndex = -1 - return callback(null, scriptRes.output) - }) - } - // 通过RecordVueContentDependency和vueContentCache确保子request不再重复生成vueContent - const cacheContent = mpx.vueContentCache.get(filePath) - if (cacheContent) return callback(null, cacheContent) - - return async.waterfall([ - (callback) => { - async.parallel([ - (callback) => { - processTemplate(parts.template, { - loaderContext, - hasScoped, - hasComment, - isNative, - srcMode, - moduleId, - ctorType, - usingComponents, - componentGenerics - }, callback) - }, - (callback) => { - processStyles(parts.styles, { - ctorType, - autoScope, - moduleId - }, callback) - }, - (callback) => { - processJSON(parts.json, { - loaderContext, - pagesMap, - componentsMap - }, callback) - } - ], (err, res) => { - callback(err, res) - }) - }, - ([templateRes, stylesRes, jsonRes], callback) => { - output += templateRes.output - output += stylesRes.output - output += jsonRes.output - processScript(parts.script, { - loaderContext, - ctorType, - srcMode, - moduleId, - isProduction, - componentGenerics, - jsonConfig: jsonRes.jsonObj, - outputPath: queryObj.outputPath || '', - builtInComponentsMap: templateRes.builtInComponentsMap, - genericsInfo: templateRes.genericsInfo, - wxsModuleMap: templateRes.wxsModuleMap, - localComponentsMap: jsonRes.localComponentsMap - }, callback) - } - ], (err, scriptRes) => { - if (err) return callback(err) - output += scriptRes.output - this._module.addPresentationalDependency(new RecordVueContentDependency(filePath, output)) - callback(null, output) + processWeb({ + parts, + loaderContext, + pagesMap, + componentsMap, + queryObj, + ctorType, + srcMode, + moduleId, + isProduction, + hasScoped, + hasComment, + isNative, + usingComponents, + componentGenerics, + autoScope, + callback }) } const moduleGraph = this._compilation.moduleGraph diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index 8866623eb8..c831e71bed 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -11,13 +11,9 @@ const { JSON_JS_EXT, MPX_APP_MODULE_ID } = require('./utils/const') const getRulesRunner = require('./platform') const getEntryName = require('./utils/get-entry-name') const AppEntryDependency = require('./dependencies/AppEntryDependency') +const processWeb = require('./web') const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency') -const processTemplate = require('./web/processTemplate') -const processStyles = require('./web/processStyles') -const processJSON = require('./web/processJSON') -const processScript = require('./web/processScript') const RecordVueContentDependency = require('./dependencies/RecordVueContentDependency') -const processMainScript = require("./web/processMainScript"); // todo native-loader考虑与mpx-loader或加强复用,原生组件约等于4个区块都为src的.mpx文件 module.exports = function (content) { @@ -185,7 +181,6 @@ module.exports = function (content) { useJSONJS }, null, this, callback) }, (content, callback) => { - let componentPlaceholder = [] let json try { json = JSON5.parse(content) @@ -193,6 +188,8 @@ module.exports = function (content) { return callback(e) } let usingComponents = Object.keys(mpx.usingComponents) + let componentPlaceholder = [] + let componentGenerics = {} const rulesRunnerOptions = { mode, srcMode, @@ -238,105 +235,28 @@ module.exports = function (content) { if (json.componentPlaceholder) { componentPlaceholder = componentPlaceholder.concat(Object.values(json.componentPlaceholder)) } + if (json.componentGenerics) { + componentGenerics = Object.assign({}, json.componentGenerics) + } if (mode === 'web') { - if (ctorType === 'app' && !queryObj.isApp) { - return async.waterfall([ - (callback) => { - processJSON(parts.json, { loaderContext, pagesMap, componentsMap }, callback) - }, - (jsonRes, callback) => { - processMainScript(parts.script, { - loaderContext, - ctorType, - srcMode, - moduleId, - isProduction, - jsonConfig: jsonRes.jsonObj, - outputPath: queryObj.outputPath || '', - localComponentsMap: jsonRes.localComponentsMap, - tabBar: jsonRes.jsonObj.tabBar, - tabBarMap: jsonRes.tabBarMap, - tabBarStr: jsonRes.tabBarStr, - localPagesMap: jsonRes.localPagesMap, - resource: this.resource - }, callback) - } - ], (err, scriptRes) => { - if (err) return callback(err) - this.loaderIndex = -1 - return callback(null, scriptRes.output) - }) - } - // 通过RecordVueContentDependency和vueContentCache确保子request不再重复生成vueContent - const cacheContent = mpx.vueContentCache.get(filePath) - if (cacheContent) return callback(null, cacheContent) - - return async.waterfall([ - (callback) => { - async.parallel([ - (callback) => { - processTemplate(parts.template, { - loaderContext, - hasScoped, - hasComment, - isNative, - srcMode, - moduleId, - ctorType, - usingComponents: [], - componentGenerics: {} - }, callback) - }, - (callback) => { - processStyles(parts.styles, { - ctorType, - autoScope, - moduleId - }, callback) - }, - (callback) => { - processJSON(parts.json, { - loaderContext, - pagesMap, - componentsMap - }, callback) - } - ], (err, res) => { - callback(err, res) - }) - }, - ([templateRes, stylesRes, jsonRes], callback) => { - output += templateRes.output - output += stylesRes.output - output += jsonRes.output - if (ctorType === 'app' && jsonRes.jsonObj.window && jsonRes.jsonObj.window.navigationBarTitleText) { - mpx.appTitle = jsonRes.jsonObj.window.navigationBarTitleText - } - - processScript(parts.script, { - loaderContext, - ctorType, - srcMode, - moduleId, - isProduction, - componentGenerics: {}, - jsonConfig: jsonRes.jsonObj, - outputPath: queryObj.outputPath || '', - tabBarMap: jsonRes.tabBarMap, - tabBarStr: jsonRes.tabBarStr, - builtInComponentsMap: templateRes.builtInComponentsMap, - genericsInfo: templateRes.genericsInfo, - wxsModuleMap: templateRes.wxsModuleMap, - localComponentsMap: jsonRes.localComponentsMap, - localPagesMap: jsonRes.localPagesMap - }, callback) - } - ], (err, scriptRes) => { - if (err) return callback(err) - output += scriptRes.output - this._module.addPresentationalDependency(new RecordVueContentDependency(filePath, output)) - callback(null, output) + processWeb({ + parts, + loaderContext, + pagesMap, + componentsMap, + queryObj, + ctorType, + srcMode, + moduleId, + isProduction, + hasScoped, + hasComment, + isNative, + usingComponents, + componentGenerics, + autoScope, + callback }) } diff --git a/packages/webpack-plugin/lib/web/index.js b/packages/webpack-plugin/lib/web/index.js new file mode 100644 index 0000000000..d16abc5a81 --- /dev/null +++ b/packages/webpack-plugin/lib/web/index.js @@ -0,0 +1,108 @@ +const async = require('async') +const processJSON = require('./processJSON') +const processMainScript = require('./processMainScript') +const processTemplate = require('./processTemplate') +const processStyles = require('./processStyles') +const processScript = require('./processScript') +const RecordVueContentDependency = require('../dependencies/RecordVueContentDependency') + +module.exports = function ({ + parts, + loaderContext, pagesMap, componentsMap, queryObj, ctorType, srcMode, moduleId, isProduction, hasScoped, hasComment, isNative, usingComponents, componentGenerics, autoScope, callback + }) { + const mpx = loaderContext.getMpx() + if (ctorType === 'app' && !queryObj.isApp) { + return async.waterfall([ + (callback) => { + processJSON(parts.json, { + loaderContext, + pagesMap, + componentsMap + }, callback) + }, + (jsonRes, callback) => { + processMainScript(parts.script, { + loaderContext, + ctorType, + srcMode, + moduleId, + isProduction, + jsonConfig: jsonRes.jsonObj, + outputPath: queryObj.outputPath || '', + localComponentsMap: jsonRes.localComponentsMap, + tabBar: jsonRes.jsonObj.tabBar, + tabBarMap: jsonRes.tabBarMap, + tabBarStr: jsonRes.tabBarStr, + localPagesMap: jsonRes.localPagesMap + }, callback) + } + ], (err, scriptRes) => { + if (err) return callback(err) + loaderContext.loaderIndex = -1 + return callback(null, scriptRes.output) + }) + } + // 通过RecordVueContentDependency和vueContentCache确保子request不再重复生成vueContent + const cacheContent = mpx.vueContentCache.get(loaderContext.resourcePath) + if (cacheContent) return callback(null, cacheContent) + let output = '' + return async.waterfall([ + (callback) => { + async.parallel([ + (callback) => { + processTemplate(parts.template, { + loaderContext, + hasScoped, + hasComment, + isNative, + srcMode, + moduleId, + ctorType, + usingComponents, + componentGenerics + }, callback) + }, + (callback) => { + processStyles(parts.styles, { + ctorType, + autoScope, + moduleId + }, callback) + }, + (callback) => { + processJSON(parts.json, { + loaderContext, + pagesMap, + componentsMap + }, callback) + } + ], (err, res) => { + callback(err, res) + }) + }, + ([templateRes, stylesRes, jsonRes], callback) => { + output += templateRes.output + output += stylesRes.output + output += jsonRes.output + processScript(parts.script, { + loaderContext, + ctorType, + srcMode, + moduleId, + isProduction, + componentGenerics, + jsonConfig: jsonRes.jsonObj, + outputPath: queryObj.outputPath || '', + builtInComponentsMap: templateRes.builtInComponentsMap, + genericsInfo: templateRes.genericsInfo, + wxsModuleMap: templateRes.wxsModuleMap, + localComponentsMap: jsonRes.localComponentsMap + }, callback) + } + ], (err, scriptRes) => { + if (err) return callback(err) + output += scriptRes.output + loaderContext._module.addPresentationalDependency(new RecordVueContentDependency(loaderContext.resourcePath, output)) + callback(null, output) + }) +} diff --git a/packages/webpack-plugin/lib/web/processMainScript.js b/packages/webpack-plugin/lib/web/processMainScript.js index 175361da8c..5125b20336 100644 --- a/packages/webpack-plugin/lib/web/processMainScript.js +++ b/packages/webpack-plugin/lib/web/processMainScript.js @@ -2,6 +2,7 @@ const addQuery = require('../utils/add-query') const normalize = require('../utils/normalize') const optionProcessorPath = normalize.lib('runtime/optionProcessor') +const eventPath = normalize.lib('runtime/components/web/event') const { buildComponentsMap, buildPagesMap, @@ -22,8 +23,7 @@ module.exports = function (script, { tabBar, tabBarMap, tabBarStr, - localPagesMap, - resource + localPagesMap }, callback) { const { i18n, webConfig, hasUnoCSS } = loaderContext.getMpx() const { pagesMap, firstPage, globalTabBar } = buildPagesMap({ @@ -65,7 +65,9 @@ module.exports = function (script, { globalTabBar }) - output += `\n var App = require(${stringifyRequest(loaderContext, addQuery(resource, { isApp: true }))}).default\n` + output += `\n require(${stringifyRequest(loaderContext, eventPath)})\n` + + output += `\n var App = require(${stringifyRequest(loaderContext, addQuery(loaderContext.resource, { isApp: true }))}).default\n` output += ` export default processAppOption({ From dd4d680b0c3c57632e48d2cc4e1ac5c4ddddf769 Mon Sep 17 00:00:00 2001 From: xuegan Date: Sat, 6 Apr 2024 12:47:58 +0800 Subject: [PATCH 19/21] fix: some bug --- packages/webpack-plugin/lib/index.js | 2 +- packages/webpack-plugin/lib/loader.js | 2 +- packages/webpack-plugin/lib/native-loader.js | 2 +- .../lib/runtime/components/web/event.js | 98 +++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 packages/webpack-plugin/lib/runtime/components/web/event.js diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index d24cd46b8a..6c40553575 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1623,7 +1623,7 @@ try { }) } } - if (queryObj.isNative && nativeLoaderIndex !== loaders.length - 1) { + if (queryObj.isNative && (!loaders.length || nativeLoaderIndex !== loaders.length - 1)) { loaders.push({ loader: require.resolve('@vue/vue-loader-v15/lib/index.js') }) diff --git a/packages/webpack-plugin/lib/loader.js b/packages/webpack-plugin/lib/loader.js index 30a8b1122b..667a032d70 100644 --- a/packages/webpack-plugin/lib/loader.js +++ b/packages/webpack-plugin/lib/loader.js @@ -149,7 +149,7 @@ module.exports = function (content) { } // 处理mode为web时输出vue格式文件 if (mode === 'web') { - processWeb({ + return processWeb({ parts, loaderContext, pagesMap, diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index c831e71bed..93fec8bf7e 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -240,7 +240,7 @@ module.exports = function (content) { } if (mode === 'web') { - processWeb({ + return processWeb({ parts, loaderContext, pagesMap, diff --git a/packages/webpack-plugin/lib/runtime/components/web/event.js b/packages/webpack-plugin/lib/runtime/components/web/event.js new file mode 100644 index 0000000000..3f705a47d4 --- /dev/null +++ b/packages/webpack-plugin/lib/runtime/components/web/event.js @@ -0,0 +1,98 @@ +import { extendEvent } from './getInnerListeners' +import { isBrowser } from '../../env' + +function MpxEvent (layer) { + this.targetElement = null + + this.touches = [] + + this.touchStartX = 0 + + this.touchStartY = 0 + + this.startTimer = null + + this.needTap = true + + this.isTouchDevice = document && ('ontouchstart' in document.documentElement) + + this.onTouchStart = (event) => { + if (event.targetTouches?.length > 1) { + return true + } + + this.touches = event.targetTouches + this.targetElement = event.target + this.needTap = true + this.startTimer = null + this.touchStartX = this.touches[0].pageX + this.touchStartY = this.touches[0].pageY + this.startTimer = setTimeout(() => { + this.needTap = false + this.sendEvent(this.targetElement, 'longpress', event) + this.sendEvent(this.targetElement, 'longtap', event) + }, 350) + } + + this.onTouchMove = (event) => { + const touch = event.changedTouches[0] + if (Math.abs(touch.pageX - this.touchStartX) > 1 || Math.abs(touch.pageY - this.touchStartY) > 1) { + this.needTap = false + this.startTimer && clearTimeout(this.startTimer) + this.startTimer = null + } + } + + this.onTouchEnd = (event) => { + if (event.targetTouches?.length > 1) { + return true + } + this.startTimer && clearTimeout(this.startTimer) + this.startTimer = null + if (this.needTap) { + this.sendEvent(this.targetElement, 'tap', event) + } + } + + this.onClick = (event) => { + this.targetElement = event.target + this.sendEvent(this.targetElement, 'tap', event) + } + this.sendEvent = (targetElement, type, event) => { + const touchEvent = new CustomEvent(type, { + bubbles: true, + cancelable: true + }) + const changedTouches = event.changedTouches || [] + extendEvent(touchEvent, { + timeStamp: event.timeStamp, + currentTarget: event.target, + changedTouches, + touches: changedTouches, + detail: { + // pc端点击事件可能没有changedTouches,所以直接从 event中取 + x: changedTouches[0]?.pageX || event.pageX || 0, + y: changedTouches[0]?.pageY || event.pageY || 0 + } + }) + targetElement && targetElement.dispatchEvent(touchEvent) + } + + this.addListener = () => { + if (this.isTouchDevice) { + layer.addEventListener('touchstart', this.onTouchStart, true) + layer.addEventListener('touchmove', this.onTouchMove, true) + layer.addEventListener('touchend', this.onTouchEnd, true) + } else { + layer.addEventListener('click', this.onClick, true) + } + } + this.addListener() +} + +if (isBrowser) { + document.addEventListener('DOMContentLoaded', function () { + // eslint-disable-next-line no-new + new MpxEvent(document.body) + }, false) +} From c224b8fe97b558980d6272ba0a52e0092a82beae Mon Sep 17 00:00:00 2001 From: xuegan Date: Sat, 6 Apr 2024 13:07:51 +0800 Subject: [PATCH 20/21] =?UTF-8?q?feat:=20=E8=BE=93=E5=87=BAali=20native-lo?= =?UTF-8?q?ader=E6=8B=BC=E6=8E=A5=E4=BF=AE=E6=94=B9=E4=B8=BAafterResolve?= =?UTF-8?q?=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/platform/builtInMixins/setDataMixin.js | 3 +++ packages/webpack-plugin/lib/index.js | 13 +++++++++++++ packages/webpack-plugin/lib/json-compiler/helper.js | 6 +----- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 packages/core/src/platform/builtInMixins/setDataMixin.js diff --git a/packages/core/src/platform/builtInMixins/setDataMixin.js b/packages/core/src/platform/builtInMixins/setDataMixin.js new file mode 100644 index 0000000000..8eac00d2de --- /dev/null +++ b/packages/core/src/platform/builtInMixins/setDataMixin.js @@ -0,0 +1,3 @@ +export default function setDataMixin () { + return {} +} diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 6c40553575..90bed5f97c 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -1631,6 +1631,19 @@ try { loader: require.resolve(nativeLoaderPath) }) } + } else { + let nativeLoaderIndex = -1 + loaders.forEach((loader, index) => { + const currentLoader = toPosix(loader.loader) + if (currentLoader.includes('native-loader')) { + nativeLoaderIndex = index + } + }) + if (queryObj.isNative && !queryObj.type && (!loaders.length || nativeLoaderIndex !== loaders.length - 1)) { + loaders.push({ + loader: require.resolve(nativeLoaderPath) + }) + } } createData.request = stringifyLoadersAndResource(loaders, createData.resource) diff --git a/packages/webpack-plugin/lib/json-compiler/helper.js b/packages/webpack-plugin/lib/json-compiler/helper.js index d696aadf7f..7059e18e49 100644 --- a/packages/webpack-plugin/lib/json-compiler/helper.js +++ b/packages/webpack-plugin/lib/json-compiler/helper.js @@ -93,11 +93,7 @@ module.exports = function createJSONHelper ({ loaderContext, emitWarning, custom } } if (ext === '.js') { - if (mode === 'web') { - resource = `${resource}?isNative=true` - } else { - resource = `!!${nativeLoaderPath}!${resource}?isNative=true` - } + resource = `${resource}?isNative=true` } const entry = getDynamicEntry(resource, 'component', outputPath, tarRoot, relativePath) From aa20bf347f673969e9a6bd859fc78bb631700ea3 Mon Sep 17 00:00:00 2001 From: xuegan Date: Sat, 6 Apr 2024 13:11:15 +0800 Subject: [PATCH 21/21] fix: lint error --- .../src/platform/builtInMixins/setDataMixin.web.js | 2 -- packages/core/src/vuePlugin.js | 14 +++++++------- packages/webpack-plugin/lib/loader.js | 6 ------ packages/webpack-plugin/lib/native-loader.js | 1 - 4 files changed, 7 insertions(+), 16 deletions(-) diff --git a/packages/core/src/platform/builtInMixins/setDataMixin.web.js b/packages/core/src/platform/builtInMixins/setDataMixin.web.js index c5b27d0f54..defe0165c0 100644 --- a/packages/core/src/platform/builtInMixins/setDataMixin.web.js +++ b/packages/core/src/platform/builtInMixins/setDataMixin.web.js @@ -1,5 +1,3 @@ -import { isObject, isFunction, error } from '@mpxjs/utils' - export default function setDataMixin () { return { beforeCreate () { diff --git a/packages/core/src/vuePlugin.js b/packages/core/src/vuePlugin.js index 3aa36cfe25..f81b1b494e 100644 --- a/packages/core/src/vuePlugin.js +++ b/packages/core/src/vuePlugin.js @@ -57,7 +57,7 @@ export default function install (Vue) { } Vue.prototype.setData = function (newData, callback) { if (!isObject(newData)) { - error(`The data entry type of the setData method must be object, The type of data ${data} is incorrect`) + error(`The data entry type of the setData method must be object, The type of data ${newData} is incorrect`) return } const rawData = this.$data @@ -65,12 +65,12 @@ export default function install (Vue) { if (key.includes('.') || key.includes('[')) { // key 为索引的路径式设置 (如 'a.b', 'a[0].b.c') const fullKeyItems = formatKey(key) - let target = this.$data; + let target = this.$data const lastItem = fullKeyItems.pop() if (fullKeyItems.length > 0) { fullKeyItems.forEach((item) => { - const nestedKey = item.name; + const nestedKey = item.name if (item.isArray) { if (!Array.isArray(target[nestedKey])) { this.$set(target, nestedKey, []) @@ -79,11 +79,11 @@ export default function install (Vue) { if (!hasOwn(target, nestedKey)) { this.$set(target, nestedKey, {}) } - target = item.isArray? target[nestedKey][item.index]: target[nestedKey] + target = item.isArray ? target[nestedKey][item.index] : target[nestedKey] }) } - lastItem.isArray? this.$set(target[lastItem.name], lastItem.index, value) : target[lastItem.name] = value + lastItem.isArray ? this.$set(target[lastItem.name], lastItem.index, value) : target[lastItem.name] = value } else { // key 为正常顶层属性 if (hasOwn(rawData, key)) { @@ -105,9 +105,9 @@ export default function install (Vue) { const property = { name: propertyName, isArray: !!index, - index: index ? parseInt(index, 10) : undefined, + index: index ? parseInt(index, 10) : undefined } - parsed.push(property); + parsed.push(property) } return parsed } diff --git a/packages/webpack-plugin/lib/loader.js b/packages/webpack-plugin/lib/loader.js index 667a032d70..762ec987ae 100644 --- a/packages/webpack-plugin/lib/loader.js +++ b/packages/webpack-plugin/lib/loader.js @@ -5,22 +5,16 @@ const parseRequest = require('./utils/parse-request') const { matchCondition } = require('./utils/match-condition') const addQuery = require('./utils/add-query') const async = require('async') -const processJSON = require('./web/processJSON') -const processScript = require('./web/processScript') -const processStyles = require('./web/processStyles') -const processTemplate = require('./web/processTemplate') const getJSONContent = require('./utils/get-json-content') const normalize = require('./utils/normalize') const getEntryName = require('./utils/get-entry-name') const AppEntryDependency = require('./dependencies/AppEntryDependency') const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency') -const RecordVueContentDependency = require('./dependencies/RecordVueContentDependency') const CommonJsVariableDependency = require('./dependencies/CommonJsVariableDependency') const tsWatchRunLoaderFilter = require('./utils/ts-loader-watch-run-loader-filter') const { MPX_APP_MODULE_ID } = require('./utils/const') const path = require('path') const processWeb = require('./web') -const processMainScript = require('./web/processMainScript') const getRulesRunner = require('./platform') module.exports = function (content) { diff --git a/packages/webpack-plugin/lib/native-loader.js b/packages/webpack-plugin/lib/native-loader.js index 93fec8bf7e..417ec589e3 100644 --- a/packages/webpack-plugin/lib/native-loader.js +++ b/packages/webpack-plugin/lib/native-loader.js @@ -13,7 +13,6 @@ const getEntryName = require('./utils/get-entry-name') const AppEntryDependency = require('./dependencies/AppEntryDependency') const processWeb = require('./web') const RecordResourceMapDependency = require('./dependencies/RecordResourceMapDependency') -const RecordVueContentDependency = require('./dependencies/RecordVueContentDependency') // todo native-loader考虑与mpx-loader或加强复用,原生组件约等于4个区块都为src的.mpx文件 module.exports = function (content) {