Skip to content

Commit 27a58cd

Browse files
authored
Merge pull request #2 from mpvue/feature/ttaplipay
Feature/ttaplipay
2 parents d2d69f2 + 1b646d7 commit 27a58cd

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const mpvueVendorPlugin = require('webpack-mpvue-vendor-plugin')
1414
filename: 'foo.bundle.js'
1515
},
1616
plugins: [
17-
new mpvueVendorPlugin()
17+
new mpvueVendorPlugin({
18+
platform: process.env.PLATFORM
19+
})
1820
]
1921
};
2022
```

index.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,63 @@
77
// return typeof global !== 'undefined' ? global : this;
88
// })();
99

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+
`;
1219

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+
}
1823

19-
compilation.assets[fileName] = {
20-
source: () => {
21-
let from = /g\s=\s\(function\(\)\s\{\r?\n?\s+return\sthis;\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 = /\.js$/;
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\(function\(\)\s\{\r?\n?\s+return\sthis;\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+
};
2864
}
29-
};
30-
callback();
65+
});
3166
});
3267
};
3368

34-
module.exports = mpvueVendorPlugin;
69+
module.exports = mpvueVendorPlugin;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-mpvue-vendor-plugin",
3-
"version": "0.0.1",
3+
"version": "2.0.0",
44
"main": "index.js",
55
"directories": {
66
"lib": "lib"

0 commit comments

Comments
 (0)