提交 75e6f5a3 编写于 作者: D DCloud_LXH

feat: 完善编译到微信小程序插件

上级 26dd6b3e
......@@ -27,7 +27,8 @@ module.exports = {
'theme.json',
'sitemap.json',
'ext.json',
'custom-tab-bar'
'custom-tab-bar',
'plugin.json'
]
const workers = platformOptions.workers
workers && copyOptions.push(workers)
......@@ -52,4 +53,4 @@ module.exports = {
})
return copyOptions
}
}
}
const path = require('path')
const fs = require('fs')
const {
runByHBuilderX,
......@@ -43,8 +44,14 @@ module.exports = (api, options) => {
process.env.UNI_SUBPACKGE = args.subpackage
}
if (args.plugin && platforms.includes(process.env.UNI_PLATFORM)) {
process.env.UNI_MP_PLUGIN = args.plugin
if (args.plugin) {
if (process.env.UNI_PLATFORM === 'mp-weixin') {
process.env.UNI_MP_PLUGIN = args.plugin
analysisPluginDir()
} else {
console.error('编译到小程序插件只支持微信小程序')
process.exit(0)
}
}
require('./util').initAutomator(args)
......@@ -196,3 +203,48 @@ async function build (args, api, options) {
module.exports.defaultModes = {
'uni-build': process.env.NODE_ENV
}
/**
* 编译到微信小程序插件 文件校验
*/
function analysisPluginDir () {
// plugin.json 是否存在
const pluginJsonName = 'plugin.json'
const pluginJsonPath = path.resolve(process.env.UNI_INPUT_DIR, pluginJsonName)
if (!fs.existsSync(pluginJsonPath)) {
console.error(`${pluginJsonName}文件不存在,请检查后重试`)
process.exit(0)
}
const pluginJson = require(pluginJsonPath)
// index.js 入口文件是否存在
process.env.UNI_MP_PLUGIN_MAIN = pluginJson.main
const UNI_MP_PLUGIN_MAIN = process.env.UNI_MP_PLUGIN_MAIN
const mainFilePath = path.resolve(process.env.UNI_INPUT_DIR, UNI_MP_PLUGIN_MAIN)
if (UNI_MP_PLUGIN_MAIN && !fs.existsSync(mainFilePath)) {
console.error(`${UNI_MP_PLUGIN_MAIN}入口文件不存在,请检查后重试`)
process.exit(0)
}
// 目前编译到小程序插件,需要在 pages.json 中配置页面,在main.js中引入使用一下组件,因此先不做一下校验
// 配置的路径是否存在
/* const pages = pluginJson.pages || {}
const publicComponents = pluginJson.publicComponents || {}
const allFilesPath = Object.values(pages).map(item => item + '.vue').concat(Object.values(publicComponents).map(item => item + '.vue'))
const inexistenceFiles = []
if (allFilesPath.length) {
allFilesPath.forEach(pagePath => {
const curentPageAbsolutePath = path.resolve(process.env.UNI_INPUT_DIR, pagePath)
if (!fs.existsSync(curentPageAbsolutePath)) {
inexistenceFiles.push(curentPageAbsolutePath)
}
})
}
if (inexistenceFiles.length) {
inexistenceFiles.forEach(path => {
console.error(`${path}文件不存在,请检查后重试`)
})
process.exit(0)
} */
}
......@@ -156,7 +156,7 @@ module.exports = {
const statCode = process.env.UNI_USING_STAT ? 'import \'@dcloudio/uni-stat\';' : ''
const beforeCode = 'import \'uni-pages\';'
let beforeCode = 'import \'uni-pages\';'
const plugins = [
new WebpackUniAppPlugin(),
......@@ -168,6 +168,17 @@ module.exports = {
plugins.push(new PreprocessAssetsPlugin())
}
if (process.env.UNI_MP_PLUGIN) {
// 小程序插件入口使用
// packages\webpack-uni-mp-loader\lib\plugin\index-new.js -> addMPPluginRequire
beforeCode += 'wx.__webpack_require__ = __webpack_require__;'
const UNI_MP_PLUGIN_MAIN = process.env.UNI_MP_PLUGIN_MAIN
if (UNI_MP_PLUGIN_MAIN) {
process.UNI_ENTRY[UNI_MP_PLUGIN_MAIN.split('.')[0]] = path.resolve(process.env.UNI_INPUT_DIR, UNI_MP_PLUGIN_MAIN)
}
}
return {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
entry () {
......
......@@ -65,6 +65,37 @@ function addSubPackagesRequire (compilation) {
})
}
function addMPPluginRequire (compilation) {
// 编译到小程序插件 特殊处理入口文件
if (process.env.UNI_MP_PLUGIN) {
const assetsKeys = Object.keys(compilation.assets)
assetsKeys.forEach(name => {
if (name === process.env.UNI_MP_PLUGIN_MAIN) {
const modules = compilation.modules
const mainFilePath = path.resolve(process.env.UNI_INPUT_DIR, process.env.UNI_MP_PLUGIN_MAIN).replace(/\\/g, '/')
const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === mainFilePath).id
const newlineIndex = compilation.assets[name].source().lastIndexOf('\n')
const source = compilation.assets[name].source().substring(0, newlineIndex) +
`\nmodule.exports = wx.__webpack_require__(${uniModuleId});\n` +
compilation.assets[name].source().substring(newlineIndex + 1)
compilation.assets[name] = {
size () {
return Buffer.byteLength(source, 'utf8')
},
source () {
return source
}
}
}
})
}
}
class WebpackUniMPPlugin {
apply (compiler) {
if (!process.env.UNI_USING_NATIVE && !process.env.UNI_USING_V3_NATIVE) {
......@@ -72,6 +103,8 @@ class WebpackUniMPPlugin {
return new Promise((resolve, reject) => {
addSubPackagesRequire(compilation)
addMPPluginRequire(compilation)
generateJson(compilation)
// app.js,app.wxss
......@@ -110,4 +143,4 @@ class WebpackUniMPPlugin {
}
}
module.exports = WebpackUniMPPlugin
module.exports = WebpackUniMPPlugin
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册