-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alita): add webpack mini-program target
- Loading branch information
1 parent
dd30b9f
commit 9c7ea8b
Showing
5 changed files
with
668 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/packByWebpack/miniprogramTarget/MiniprogramJsonpPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
src/packByWebpack/miniprogramTarget/MpJsonpChunkTemplatePlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) || ""); | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.