|
1 | | -const path = require('path'); |
2 | | -const upath = require('upath'); |
3 | | -const relative = require('relative'); |
| 1 | +const path = require('path') |
| 2 | +const upath = require('upath') |
| 3 | +const relative = require('relative') |
4 | 4 |
|
5 | | -function MpvuePlugin() {} |
| 5 | +const emitHandle = (compilation, callback) => { |
| 6 | + Object.keys(compilation.entrypoints).forEach(key => { |
| 7 | + const { chunks } = compilation.entrypoints[key] |
| 8 | + const entryChunk = chunks.pop() |
6 | 9 |
|
7 | | -MpvuePlugin.prototype.apply = function(compiler) { |
8 | | - compiler.plugin('emit', function(compilation, callback) { |
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 | | - let relativePath = upath.normalize(relative(filePath, childFile)) |
| 10 | + entryChunk.files.forEach(filePath => { |
| 11 | + const assetFile = compilation.assets[filePath] |
| 12 | + const extname = path.extname(filePath) |
| 13 | + let content = assetFile.source() |
20 | 14 |
|
21 | | - // 百度小程序js引用不支持绝对路径,改为相对路径 |
22 | | - if (extname === '.js' && !/^\.(\.)?\//.test(relativePath)) { |
23 | | - relativePath = `./${relativePath}`; |
24 | | - } |
| 15 | + chunks.reverse().forEach(chunk => { |
| 16 | + chunk.files.forEach(subFile => { |
| 17 | + if (path.extname(subFile) === extname && assetFile) { |
| 18 | + let relativePath = upath.normalize(relative(filePath, subFile)) |
| 19 | + // 百度小程序 js 引用不支持绝对路径,改为相对路径 |
| 20 | + if (extname === '.js' && !/^\.(\.)?\//.test(relativePath)) { |
| 21 | + relativePath = `./${relativePath}` |
| 22 | + } |
25 | 23 |
|
26 | | - if (/^(\.wxss)|(\.css)$/.test(extname)) { |
27 | | - content = `@import "${relativePath}";\n${content}`; |
28 | | - } else { |
29 | | - content = `require("${relativePath}");\n${content}`; |
30 | | - } |
| 24 | + if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) { |
| 25 | + content = `@import "${relativePath}"\n${content}` |
| 26 | + } else { |
| 27 | + content = `require("${relativePath}")\n${content}` |
31 | 28 | } |
32 | | - }) |
33 | | - compilation.assets[filePath].source = () => content; |
| 29 | + } |
34 | 30 | }) |
| 31 | + assetFile.source = () => content |
35 | 32 | }) |
36 | 33 | }) |
37 | | - callback(); |
38 | | - }); |
39 | | -}; |
| 34 | + }) |
| 35 | + callback() |
| 36 | +} |
| 37 | + |
| 38 | +function MpvuePlugin() {} |
| 39 | +MpvuePlugin.prototype.apply = compiler => compiler.plugin('emit', emitHandle) |
40 | 40 |
|
41 | | -module.exports = MpvuePlugin; |
| 41 | +module.exports = MpvuePlugin |
0 commit comments