'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const path = require('path');
const { generateAsset } = require('./utils');
 
// App() 必须在 app.js 中调用,必须调用且只能调用一次。不然会出现无法预期的后果。
class RunDefaultAppPlugin {
    apply (compiler) {
        compiler.hooks.emit.tapPromise('RunDefaultAppPlugin', compilation => {
            return new Promise((resolve, reject) => {
                try {
                    // debugger
                    const appJsInfo = compilation.assets["app.js"];
                    if (!appJsInfo) {
                        console.error('independent.error', 'invalid runDefaultApp');
                        resolve();
                        return;
                    }
                    const appSource = appJsInfo.source();
                    // App() 必须在 app.js 中调用,必须调用且只能调用一次。不然会出现无法预期的后果。
                    const runApp = 'if(getApp && !getApp()){ App({}) }';
                    compilation.assets["app.js"] = generateAsset(`${appSource};${runApp}`);
 
                    resolve();
                } catch (e) {
                    console.error('independent.error', 'RunDefaultAppPlugin', e);
                    reject(e);
                }
            });
        });
    }
}
 
module.exports = RunDefaultAppPlugin;