7
7
// return typeof global !== 'undefined' ? global : this;
8
8
// })();
9
9
10
- function mpvueVendorPlugin ( ) {
11
- }
10
+ // 支付宝环境中,每个模块打包后在模块内部global会被强制赋值undefined,可以挂载到$global中
11
+ // Component变量只有在项目中通过json配置声明组件后,打包的时候才会声明赋值,否则访问不到
12
+ const banner = `
13
+ if (!global) {
14
+ var globalModule = require('global');
15
+ var Component = Component ? Component : globalModule.AFAppX.WorkerComponent;
16
+ var global = globalModule.AFAppX.$global || {};
17
+ }
18
+ ` ;
12
19
13
- mpvueVendorPlugin . prototype . apply = function ( compiler ) {
14
- compiler . plugin ( 'emit' , ( compilation , callback ) => {
15
- const fileName = 'common/vendor.js' ;
16
- const asset = compilation . assets [ fileName ] ;
17
- let fileContent = asset . source ( ) ;
20
+ function mpvueVendorPlugin ( options ) {
21
+ this . options = options || { } ;
22
+ }
18
23
19
- compilation . assets [ fileName ] = {
20
- source : ( ) => {
21
- let from = / g \s = \s \( f u n c t i o n \( \) \s \{ \r ? \n ? \s + r e t u r n \s t h i s ; \r ? \n ? \s * \} \) \( \) \; / ;
22
- let to = `g = (function() { return typeof global !== 'undefined' ? global : this; })();`
23
- fileContent = fileContent . replace ( from , to )
24
- return fileContent ;
25
- } ,
26
- size : ( ) => {
27
- return Buffer . byteLength ( fileContent , 'utf8' ) ;
24
+ mpvueVendorPlugin . prototype . apply = function ( compiler ) {
25
+ const isAlipay = this . options . platform && this . options . platform === 'my' ;
26
+ if ( isAlipay ) {
27
+ compiler . plugin ( "emit" , ( compilation , callback ) => {
28
+ const regExp = / \. j s $ / ;
29
+ const filesName = Object . keys ( compilation . assets ) . filter ( name =>
30
+ name . match ( regExp )
31
+ ) ;
32
+ filesName . forEach ( name => {
33
+ let asset = compilation . assets [ name ] ;
34
+ let fileContent = asset . source ( ) ;
35
+ compilation . assets [ name ] = {
36
+ source : ( ) => {
37
+ return banner + "\n" + fileContent ;
38
+ } ,
39
+ size : ( ) => {
40
+ return Buffer . byteLength ( fileContent , "utf8" ) ;
41
+ }
42
+ } ;
43
+ } ) ;
44
+ callback ( ) ;
45
+ } ) ;
46
+ }
47
+ compiler . plugin ( 'compilation' , ( compilation ) => {
48
+ compilation . plugin ( 'additional-chunk-assets' , ( ) => {
49
+ const fileName = 'common/vendor.js' ;
50
+ const asset = compilation . assets [ fileName ] ;
51
+ if ( asset ) {
52
+ let fileContent = asset . source ( ) ;
53
+ compilation . assets [ fileName ] = {
54
+ source : ( ) => {
55
+ let from = / g \s = \s \( f u n c t i o n \( \) \s \{ \r ? \n ? \s + r e t u r n \s t h i s ; \r ? \n ? \s * \} \) \( \) \; / ;
56
+ let to = `g = (function() { return typeof global !== 'undefined' ? global : this; })();`
57
+ fileContent = fileContent . replace ( from , to )
58
+ return fileContent ;
59
+ } ,
60
+ size : ( ) => {
61
+ return Buffer . byteLength ( fileContent , 'utf8' ) ;
62
+ }
63
+ } ;
28
64
}
29
- } ;
30
- callback ( ) ;
65
+ } ) ;
31
66
} ) ;
32
67
} ;
33
68
34
- module . exports = mpvueVendorPlugin ;
69
+ module . exports = mpvueVendorPlugin ;
0 commit comments