Skip to content

Commit

Permalink
feat(alita): add webpack mini-program target
Browse files Browse the repository at this point in the history
  • Loading branch information
ykforerlang committed Dec 31, 2019
1 parent dd30b9f commit 9c7ea8b
Show file tree
Hide file tree
Showing 5 changed files with 668 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ interface IConfigure {
allCompSet?: any

analyzer?: boolean

// 小程序平台全局对象
mpGlobalObject: string
}

const configure = {} as IConfigure
const configure = {
// TODO 可配置,适配其他小程序
mpGlobalObject: "wx"
} as IConfigure

export default configure
16 changes: 16 additions & 0 deletions src/packByWebpack/miniprogramTarget/MiniprogramJsonpPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import MpJsonpMainTemplatePlugin from './MpJsonpMainTemplatePlugin'
import MpJsonpChunkTemplatePlugin from './MpJsonpChunkTemplatePlugin'
import JsonpHotUpdateChunkTemplatePlugin from 'webpack/lib/web/JsonpHotUpdateChunkTemplatePlugin'

export default class MiniprogramJsonpPlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap("MiniprogramJsonpPlugin", compilation => {
new MpJsonpMainTemplatePlugin().apply(compilation.mainTemplate);
new MpJsonpChunkTemplatePlugin().apply(compilation.chunkTemplate);
new JsonpHotUpdateChunkTemplatePlugin().apply(
compilation.hotUpdateChunkTemplate
);
});
}
}

66 changes: 66 additions & 0 deletions src/packByWebpack/miniprogramTarget/MpJsonpChunkTemplatePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import configure from '../../configure'

const { ConcatSource } = require("webpack-sources");

const getEntryInfo = chunk => {
return [chunk.entryModule].filter(Boolean).map(m =>
[m.id].concat(
Array.from(chunk.groupsIterable)[0]
// @ts-ignore
.chunks.filter(c => c !== chunk)
.map(c => c.id)
)
);
};


export default class MpJsonpChunkTemplatePlugin {
/**
* @param {ChunkTemplate} chunkTemplate the chunk template
* @returns {void}
*/
apply(chunkTemplate) {
chunkTemplate.hooks.render.tap(
"JsonpChunkTemplatePlugin",
(modules, chunk) => {
const jsonpFunction = chunkTemplate.outputOptions.jsonpFunction;

const source = new ConcatSource();
const prefetchChunks = chunk.getChildIdsByOrders().prefetch;
source.add(
`(${configure.mpGlobalObject}[${JSON.stringify(
jsonpFunction
)}] = ${configure.mpGlobalObject}[${JSON.stringify(
jsonpFunction
)}] || []).push([${JSON.stringify(chunk.ids)},`
);
source.add(modules);
const entries = getEntryInfo(chunk);
if (entries.length > 0) {
source.add(`,${JSON.stringify(entries)}`);
} else if (prefetchChunks && prefetchChunks.length) {
source.add(`,0`);
}

if (prefetchChunks && prefetchChunks.length) {
source.add(`,${JSON.stringify(prefetchChunks)}`);
}
source.add("])");
return source;
}
);
chunkTemplate.hooks.hash.tap("JsonpChunkTemplatePlugin", hash => {
hash.update("JsonpChunkTemplatePlugin");
hash.update("4");
hash.update(`${chunkTemplate.outputOptions.jsonpFunction}`);
hash.update(`${configure.mpGlobalObject}`);
});
chunkTemplate.hooks.hashForChunk.tap(
"JsonpChunkTemplatePlugin",
(hash, chunk) => {
hash.update(JSON.stringify(getEntryInfo(chunk)));
hash.update(JSON.stringify(chunk.getChildIdsByOrders().prefetch) || "");
}
);
}
}
Loading

0 comments on commit 9c7ea8b

Please sign in to comment.