7
7
// return typeof global !== 'undefined' ? global : this;
8
8
// })();
9
9
10
+ // 支付宝环境中,每个模块打包后在模块内部global会被强制赋值undefined,可以挂载到$global中
11
+ // Component变量只有在项目中通过json配置声明组件后,打包的时候才会声明赋值,否则访问不到
10
12
const banner = `
11
- if (!global && (typeof my !== 'undefined') ) {
13
+ if (!global) {
12
14
var globalModule = require('global');
13
- var Component = globalModule.AFAppX.WorkerComponent;
14
- global = globalModule.AFAppX.$global || {};
15
+ var Component = Component ? Component : globalModule.AFAppX.WorkerComponent;
16
+ var global = globalModule.AFAppX.$global || {};
15
17
}
16
18
` ;
17
19
18
- function mpvueVendorPlugin ( ) {
20
+ function mpvueVendorPlugin ( options ) {
21
+ this . options = options || { } ;
19
22
}
20
23
21
24
mpvueVendorPlugin . prototype . apply = function ( compiler ) {
22
- compiler . plugin ( "emit" , ( compilation , callback ) => {
23
- let regExp = / \. j s $ / ;
24
- let filesName = Object . keys ( compilation . assets ) . filter ( name =>
25
- name . match ( regExp )
26
- ) ;
27
- filesName . forEach ( name => {
28
- let asset = compilation . assets [ name ] ;
29
- let fileContent = asset . source ( ) ;
30
- compilation . assets [ name ] = {
31
- source : ( ) => {
32
- return banner + "\n" + fileContent ;
33
- } ,
34
- size : ( ) => {
35
- return Buffer . byteLength ( fileContent , "utf8" ) ;
36
- }
37
- } ;
25
+ const isAlipay = this . options . platform && this . options . platform . includes ( '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
+ } ;
64
+ }
38
65
} ) ;
39
- callback ( ) ;
40
66
} ) ;
41
67
} ;
42
68
43
- module . exports = mpvueVendorPlugin ;
69
+ module . exports = mpvueVendorPlugin ;
0 commit comments