|
1 | 1 | const path = require('path'); |
| 2 | +const upath = require('upath'); |
2 | 3 | const relative = require('relative'); |
3 | 4 |
|
4 | 5 | function MpvuePlugin() {} |
5 | 6 |
|
6 | 7 | MpvuePlugin.prototype.apply = function(compiler) { |
7 | 8 | compiler.plugin('emit', function(compilation, callback) { |
8 | | - let commonsChunkNames = []; |
9 | | - // 获取所有的 chunk name |
10 | | - plugins.forEach(item => { |
11 | | - let { chunkNames } = item; |
12 | | - if (item.constructor.name === 'CommonsChunkPlugin' && chunkNames) { |
13 | | - commonsChunkNames = commonsChunkNames.concat(chunkNames); |
14 | | - } |
15 | | - }) |
16 | | - let pages = Object.keys(entry); |
17 | | - compilation.chunks.forEach(commonChunk => { |
18 | | - const { files, chunks: childChunks, name } = commonChunk; |
19 | | - let commonWxssFile = files.find(item => item.endsWith('.wxss')); |
20 | | - |
21 | | - if (commonsChunkNames.indexOf(name) > -1 && commonWxssFile) { |
22 | | - childChunks.forEach(item => { |
23 | | - let wxssFile = item.files.find(item => item.endsWith('.wxss')); |
24 | | - if (item.name === 'app' && wxssFile) { // 过滤 app |
25 | | - return; |
26 | | - } |
27 | | - try { |
28 | | - let wxss = compilation.assets[wxssFile].source(); |
29 | | - wxss = `@import "/${commonWxssFile}";\n${wxss}`; |
30 | | - compilation.assets[wxssFile].source = () => wxss; |
31 | | - } catch (error) { |
32 | | - console.error(error, wxssFile) |
33 | | - } |
| 9 | + Object.keys(compilation.entrypoints).forEach(key => { |
| 10 | + const entry = compilation.entrypoints[key]; |
| 11 | + const { chunks } = entry; |
| 12 | + const entryChunk = chunks.pop(); |
| 13 | + entryChunk.files.forEach(filePath => { |
| 14 | + const extname = path.extname(filePath); |
| 15 | + let content = compilation.assets[filePath].source(); |
| 16 | + chunks.reverse().forEach(chunk => { |
| 17 | + chunk.files.forEach(childFile => { |
| 18 | + if (path.extname(childFile) === extname && compilation.assets[filePath]) { |
| 19 | + const relativePath = upath.normalize(relative(filePath, childFile)) |
| 20 | + content = extname === '.wxss' ? |
| 21 | + `@import "${relativePath}";\n${content}` |
| 22 | + : `require("${relativePath}");\n${content}`; |
| 23 | + } |
| 24 | + }) |
| 25 | + compilation.assets[filePath].source = () => content; |
34 | 26 | }) |
35 | 27 | }) |
36 | 28 | }) |
|
0 commit comments