Skip to content

Commit 9c7ea8b

Browse files
committedDec 31, 2019
feat(alita): add webpack mini-program target
1 parent dd30b9f commit 9c7ea8b

File tree

5 files changed

+668
-1
lines changed

5 files changed

+668
-1
lines changed
 

‎src/configure.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,14 @@ interface IConfigure {
3939
allCompSet?: any
4040

4141
analyzer?: boolean
42+
43+
// 小程序平台全局对象
44+
mpGlobalObject: string
4245
}
4346

44-
const configure = {} as IConfigure
47+
const configure = {
48+
// TODO 可配置,适配其他小程序
49+
mpGlobalObject: "wx"
50+
} as IConfigure
4551

4652
export default configure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import MpJsonpMainTemplatePlugin from './MpJsonpMainTemplatePlugin'
2+
import MpJsonpChunkTemplatePlugin from './MpJsonpChunkTemplatePlugin'
3+
import JsonpHotUpdateChunkTemplatePlugin from 'webpack/lib/web/JsonpHotUpdateChunkTemplatePlugin'
4+
5+
export default class MiniprogramJsonpPlugin {
6+
apply(compiler) {
7+
compiler.hooks.thisCompilation.tap("MiniprogramJsonpPlugin", compilation => {
8+
new MpJsonpMainTemplatePlugin().apply(compilation.mainTemplate);
9+
new MpJsonpChunkTemplatePlugin().apply(compilation.chunkTemplate);
10+
new JsonpHotUpdateChunkTemplatePlugin().apply(
11+
compilation.hotUpdateChunkTemplate
12+
);
13+
});
14+
}
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import configure from '../../configure'
2+
3+
const { ConcatSource } = require("webpack-sources");
4+
5+
const getEntryInfo = chunk => {
6+
return [chunk.entryModule].filter(Boolean).map(m =>
7+
[m.id].concat(
8+
Array.from(chunk.groupsIterable)[0]
9+
// @ts-ignore
10+
.chunks.filter(c => c !== chunk)
11+
.map(c => c.id)
12+
)
13+
);
14+
};
15+
16+
17+
export default class MpJsonpChunkTemplatePlugin {
18+
/**
19+
* @param {ChunkTemplate} chunkTemplate the chunk template
20+
* @returns {void}
21+
*/
22+
apply(chunkTemplate) {
23+
chunkTemplate.hooks.render.tap(
24+
"JsonpChunkTemplatePlugin",
25+
(modules, chunk) => {
26+
const jsonpFunction = chunkTemplate.outputOptions.jsonpFunction;
27+
28+
const source = new ConcatSource();
29+
const prefetchChunks = chunk.getChildIdsByOrders().prefetch;
30+
source.add(
31+
`(${configure.mpGlobalObject}[${JSON.stringify(
32+
jsonpFunction
33+
)}] = ${configure.mpGlobalObject}[${JSON.stringify(
34+
jsonpFunction
35+
)}] || []).push([${JSON.stringify(chunk.ids)},`
36+
);
37+
source.add(modules);
38+
const entries = getEntryInfo(chunk);
39+
if (entries.length > 0) {
40+
source.add(`,${JSON.stringify(entries)}`);
41+
} else if (prefetchChunks && prefetchChunks.length) {
42+
source.add(`,0`);
43+
}
44+
45+
if (prefetchChunks && prefetchChunks.length) {
46+
source.add(`,${JSON.stringify(prefetchChunks)}`);
47+
}
48+
source.add("])");
49+
return source;
50+
}
51+
);
52+
chunkTemplate.hooks.hash.tap("JsonpChunkTemplatePlugin", hash => {
53+
hash.update("JsonpChunkTemplatePlugin");
54+
hash.update("4");
55+
hash.update(`${chunkTemplate.outputOptions.jsonpFunction}`);
56+
hash.update(`${configure.mpGlobalObject}`);
57+
});
58+
chunkTemplate.hooks.hashForChunk.tap(
59+
"JsonpChunkTemplatePlugin",
60+
(hash, chunk) => {
61+
hash.update(JSON.stringify(getEntryInfo(chunk)));
62+
hash.update(JSON.stringify(chunk.getChildIdsByOrders().prefetch) || "");
63+
}
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)