Skip to content

Commit db17361

Browse files
author
DCloud_LXH
committed
fix(mp-plugin): 小程序插件 main.js 文件中导出的函数位置调整
1 parent 5fa1c60 commit db17361

File tree

1 file changed

+145
-145
lines changed

1 file changed

+145
-145
lines changed
Lines changed: 145 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,145 @@
1-
const path = require('path')
2-
3-
const {
4-
md5,
5-
parseEntry,
6-
normalizePath
7-
} = require('@dcloudio/uni-cli-shared')
8-
9-
const {
10-
pagesJsonJsFileName
11-
} = require('@dcloudio/uni-cli-shared/lib/pages')
12-
13-
const generateApp = require('./generate-app')
14-
const generateJson = require('./generate-json')
15-
const generateComponent = require('./generate-component')
16-
17-
const emitFileCaches = {}
18-
19-
function emitFile (filePath, source, compilation) {
20-
const emitFileMD5 = md5(filePath + source)
21-
if (emitFileCaches[filePath] !== emitFileMD5) {
22-
emitFileCaches[filePath] = emitFileMD5
23-
compilation.assets[filePath] = {
24-
size () {
25-
return Buffer.byteLength(source, 'utf8')
26-
},
27-
source () {
28-
return source
29-
}
30-
}
31-
}
32-
}
33-
34-
function addSubPackagesRequire (compilation) {
35-
if (!process.env.UNI_OPT_SUBPACKAGES) {
36-
return
37-
}
38-
const assetsKeys = Object.keys(compilation.assets)
39-
Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
40-
const subPackageVendorPath = normalizePath(path.join(root, 'common/vendor.js'))
41-
if (assetsKeys.indexOf(subPackageVendorPath) !== -1) {
42-
// TODO 理论上仅需在分包第一个 js 中添加 require common vendor,但目前不同平台可能顺序不一致,
43-
// 故 每个分包里的 js 里均添加一次 require
44-
assetsKeys.forEach(name => {
45-
if (
46-
path.extname(name) === '.js' &&
47-
name.indexOf(root + '/') === 0 &&
48-
name !== subPackageVendorPath
49-
) {
50-
let relativePath = normalizePath(path.relative(path.dirname(name), subPackageVendorPath))
51-
if (!relativePath.startsWith('.')) {
52-
relativePath = './' + relativePath
53-
}
54-
const source = `require('${relativePath}');` + compilation.assets[name].source()
55-
56-
compilation.assets[name] = {
57-
size () {
58-
return Buffer.byteLength(source, 'utf8')
59-
},
60-
source () {
61-
return source
62-
}
63-
}
64-
}
65-
})
66-
}
67-
})
68-
}
69-
70-
function addMPPluginRequire (compilation) {
71-
// 编译到小程序插件 特殊处理入口文件
72-
const assetsKeys = Object.keys(compilation.assets)
73-
const UNI_MP_PLUGIN_MAIN = process.env.UNI_MP_PLUGIN_MAIN
74-
const UNI_MP_PLUGIN_EXPORT = JSON.parse(process.env.UNI_MP_PLUGIN_EXPORT)
75-
assetsKeys.forEach(name => {
76-
const needProcess = process.env.UNI_MP_PLUGIN ? name === UNI_MP_PLUGIN_MAIN : UNI_MP_PLUGIN_EXPORT.includes(name)
77-
if (needProcess) {
78-
const modules = compilation.modules
79-
const orignalSource = compilation.assets[name].source()
80-
const globalEnv = process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'wx'
81-
const filePath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, name))
82-
const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === filePath).id
83-
84-
const source = orignalSource + `\nmodule.exports = ${globalEnv}.__webpack_require_UNI_MP_PLUGIN__('${uniModuleId}');\n`
85-
86-
compilation.assets[name] = {
87-
size () {
88-
return Buffer.byteLength(source, 'utf8')
89-
},
90-
source () {
91-
return source
92-
}
93-
}
94-
}
95-
})
96-
}
97-
98-
class WebpackUniMPPlugin {
99-
apply (compiler) {
100-
if (!process.env.UNI_USING_NATIVE && !process.env.UNI_USING_V3_NATIVE) {
101-
compiler.hooks.emit.tapPromise('webpack-uni-mp-emit', compilation => {
102-
return new Promise((resolve, reject) => {
103-
addSubPackagesRequire(compilation)
104-
105-
addMPPluginRequire(compilation)
106-
107-
generateJson(compilation)
108-
109-
// app.js,app.wxss
110-
generateApp(compilation)
111-
.forEach(({
112-
file,
113-
source
114-
}) => emitFile(file, source, compilation))
115-
116-
generateComponent(compilation, compiler.options.output.jsonpFunction)
117-
118-
resolve()
119-
})
120-
})
121-
}
122-
compiler.hooks.invalid.tap('webpack-uni-mp-invalid', (fileName, changeTime) => {
123-
if (
124-
fileName &&
125-
typeof fileName === 'string'
126-
) { // 重新解析 entry
127-
const basename = path.basename(fileName)
128-
const deps = process.UNI_PAGES_DEPS || new Set()
129-
if (
130-
basename === 'pages.json' ||
131-
basename === pagesJsonJsFileName ||
132-
deps.has(normalizePath(fileName))
133-
) {
134-
try {
135-
parseEntry()
136-
} catch (e) {
137-
console.error(e)
138-
}
139-
}
140-
}
141-
})
142-
}
143-
}
144-
145-
module.exports = WebpackUniMPPlugin
1+
const path = require('path')
2+
3+
const {
4+
md5,
5+
parseEntry,
6+
normalizePath
7+
} = require('@dcloudio/uni-cli-shared')
8+
9+
const {
10+
pagesJsonJsFileName
11+
} = require('@dcloudio/uni-cli-shared/lib/pages')
12+
13+
const generateApp = require('./generate-app')
14+
const generateJson = require('./generate-json')
15+
const generateComponent = require('./generate-component')
16+
17+
const emitFileCaches = {}
18+
19+
function emitFile (filePath, source, compilation) {
20+
const emitFileMD5 = md5(filePath + source)
21+
if (emitFileCaches[filePath] !== emitFileMD5) {
22+
emitFileCaches[filePath] = emitFileMD5
23+
compilation.assets[filePath] = {
24+
size () {
25+
return Buffer.byteLength(source, 'utf8')
26+
},
27+
source () {
28+
return source
29+
}
30+
}
31+
}
32+
}
33+
34+
function addSubPackagesRequire (compilation) {
35+
if (!process.env.UNI_OPT_SUBPACKAGES) {
36+
return
37+
}
38+
const assetsKeys = Object.keys(compilation.assets)
39+
Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
40+
const subPackageVendorPath = normalizePath(path.join(root, 'common/vendor.js'))
41+
if (assetsKeys.indexOf(subPackageVendorPath) !== -1) {
42+
// TODO 理论上仅需在分包第一个 js 中添加 require common vendor,但目前不同平台可能顺序不一致,
43+
// 故 每个分包里的 js 里均添加一次 require
44+
assetsKeys.forEach(name => {
45+
if (
46+
path.extname(name) === '.js' &&
47+
name.indexOf(root + '/') === 0 &&
48+
name !== subPackageVendorPath
49+
) {
50+
let relativePath = normalizePath(path.relative(path.dirname(name), subPackageVendorPath))
51+
if (!relativePath.startsWith('.')) {
52+
relativePath = './' + relativePath
53+
}
54+
const source = `require('${relativePath}');` + compilation.assets[name].source()
55+
56+
compilation.assets[name] = {
57+
size () {
58+
return Buffer.byteLength(source, 'utf8')
59+
},
60+
source () {
61+
return source
62+
}
63+
}
64+
}
65+
})
66+
}
67+
})
68+
}
69+
70+
function addMPPluginRequire (compilation) {
71+
// 编译到小程序插件 特殊处理入口文件
72+
const assetsKeys = Object.keys(compilation.assets)
73+
const UNI_MP_PLUGIN_MAIN = process.env.UNI_MP_PLUGIN_MAIN
74+
const UNI_MP_PLUGIN_EXPORT = JSON.parse(process.env.UNI_MP_PLUGIN_EXPORT)
75+
assetsKeys.forEach(name => {
76+
const needProcess = process.env.UNI_MP_PLUGIN ? name === UNI_MP_PLUGIN_MAIN : UNI_MP_PLUGIN_EXPORT.includes(name)
77+
if (needProcess) {
78+
const modules = compilation.modules
79+
const orignalSource = compilation.assets[name].source()
80+
const globalEnv = process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'wx'
81+
const filePath = normalizePath(path.resolve(process.env.UNI_INPUT_DIR, name))
82+
const uniModuleId = modules.find(module => module.resource && normalizePath(module.resource) === filePath).id
83+
84+
const source = orignalSource + `\nmodule.exports = ${globalEnv}.__webpack_require_UNI_MP_PLUGIN__('${uniModuleId}');\n`
85+
86+
compilation.assets[name] = {
87+
size () {
88+
return Buffer.byteLength(source, 'utf8')
89+
},
90+
source () {
91+
return source
92+
}
93+
}
94+
}
95+
})
96+
}
97+
98+
class WebpackUniMPPlugin {
99+
apply (compiler) {
100+
if (!process.env.UNI_USING_NATIVE && !process.env.UNI_USING_V3_NATIVE) {
101+
compiler.hooks.emit.tapPromise('webpack-uni-mp-emit', compilation => {
102+
return new Promise((resolve, reject) => {
103+
addSubPackagesRequire(compilation)
104+
105+
addMPPluginRequire(compilation)
106+
107+
generateJson(compilation)
108+
109+
// app.js,app.wxss
110+
generateApp(compilation)
111+
.forEach(({
112+
file,
113+
source
114+
}) => emitFile(file, source, compilation))
115+
116+
generateComponent(compilation, compiler.options.output.jsonpFunction)
117+
118+
resolve()
119+
})
120+
})
121+
}
122+
compiler.hooks.invalid.tap('webpack-uni-mp-invalid', (fileName, changeTime) => {
123+
if (
124+
fileName &&
125+
typeof fileName === 'string'
126+
) { // 重新解析 entry
127+
const basename = path.basename(fileName)
128+
const deps = process.UNI_PAGES_DEPS || new Set()
129+
if (
130+
basename === 'pages.json' ||
131+
basename === pagesJsonJsFileName ||
132+
deps.has(normalizePath(fileName))
133+
) {
134+
try {
135+
parseEntry()
136+
} catch (e) {
137+
console.error(e)
138+
}
139+
}
140+
}
141+
})
142+
}
143+
}
144+
145+
module.exports = WebpackUniMPPlugin

0 commit comments

Comments
 (0)