webpack.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. var fs = require('fs');
  2. var path = require('path');
  3. var webpack = require('webpack');
  4. var pkg = require('./package');
  5. // replaceVersion
  6. var replaceVersion = function () {
  7. var filePath = path.resolve(__dirname, 'src/cos.js');
  8. var content = fs.readFileSync(filePath).toString();
  9. if (content) {
  10. var newContent = content.replace(/(COS\.version) *= *['"]\d+\.\d+\.\d+['"];/, "$1 = '" + pkg.version + "';");
  11. if (newContent !== content) {
  12. fs.writeFileSync(filePath, newContent);
  13. console.log('cos.js version updated.');
  14. }
  15. }
  16. };
  17. var replaceDevCode = function (list) {
  18. list.forEach(function (fileName) {
  19. var filePath = path.resolve(__dirname, fileName);
  20. var content = fs.readFileSync(filePath).toString();
  21. var newContent = content;
  22. newContent = newContent.replace(/https:\/\/\w+\.com\/[\w\-]+\/server\//, 'https://example.com/');
  23. newContent = newContent.replace(/test-125\d{7}/, 'test-1250000000');
  24. newContent = newContent.replace(/'proxy' => 'http:\/\/[^']+',/, "'proxy' => '',");
  25. newContent = newContent.replace(/proxy: 'http:\/\/[^']+',/, "proxy: '',");
  26. newContent = newContent.replace(/AKID\w+/, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  27. newContent = newContent.replace(/'secretKey' => '[^']+',/, "'secretKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',");
  28. newContent = newContent.replace(/secretKey: '[^']+',/, "secretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',");
  29. newContent = newContent.replace(/'allowActions' *=> *array\([^)]+\)/, `'allowActions' => array(
  30. // 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
  31. // 简单上传
  32. 'name/cos:PutObject',
  33. // 分片上传
  34. 'name/cos:InitiateMultipartUpload',
  35. 'name/cos:ListMultipartUploads',
  36. 'name/cos:ListParts',
  37. 'name/cos:UploadPart',
  38. 'name/cos:CompleteMultipartUpload'
  39. )`);
  40. if (newContent !== content) {
  41. console.log('replace ' + filePath);
  42. fs.writeFileSync(filePath, newContent);
  43. }
  44. });
  45. };
  46. replaceVersion();
  47. module.exports = {
  48. entry: path.resolve(__dirname, './index.js'),
  49. output: {
  50. path: path.resolve(__dirname, './dist'),
  51. publicPath: '/dist/',
  52. filename: 'cos-js-sdk-v5.js',
  53. libraryTarget: 'umd',
  54. library: 'COS',
  55. },
  56. module: {
  57. rules: [
  58. {
  59. test: /\.js$/,
  60. loader: 'babel-loader',
  61. exclude: /node_modules/
  62. }
  63. ]
  64. },
  65. devServer: {
  66. historyApiFallback: true,
  67. noInfo: true
  68. },
  69. performance: {
  70. hints: false
  71. },
  72. };
  73. if (process.env.NODE_ENV === 'production') {
  74. replaceDevCode([
  75. 'demo/demo.js',
  76. 'demo/queue/index.js',
  77. 'test/test.js',
  78. 'server/sts.js',
  79. 'server/sts.php',
  80. ]);
  81. module.exports.output.filename = 'cos-js-sdk-v5.min.js';
  82. module.exports.plugins = (module.exports.plugins || []).concat([
  83. new webpack.DefinePlugin({
  84. 'process.env': {
  85. NODE_ENV: '"production"'
  86. }
  87. }),
  88. new webpack.optimize.UglifyJsPlugin({
  89. sourceMap: false,
  90. output: {
  91. ascii_only: true,
  92. },
  93. compress: {
  94. warnings: false,
  95. },
  96. }),
  97. new webpack.LoaderOptionsPlugin({
  98. minimize: true
  99. }),
  100. ]);
  101. }