build.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var webpack = require('webpack');
  2. var path = require('path');
  3. var fs = require('fs');
  4. var pkg = require('./package.json');
  5. const TerserPlugin = require('terser-webpack-plugin');
  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(/wx-125\d{7}/, 'test-1250000000');
  25. newContent = newContent.replace(/"appid": "wx\w+"/, '"appid": "wx0000000000000000"');
  26. if (newContent !== content) {
  27. console.log('replace ' + filePath);
  28. fs.writeFileSync(filePath, newContent);
  29. }
  30. });
  31. };
  32. replaceVersion();
  33. var config = {
  34. mode: process.env.NODE_ENV,
  35. devtool: "none",
  36. watch: true,
  37. entry: path.resolve(__dirname, './index.js'),
  38. output: {
  39. path: path.resolve(__dirname, './demo/lib/'),
  40. publicPath: path.resolve(__dirname, './demo/lib/'),
  41. filename: 'cos-wx-sdk-v5.js',
  42. libraryTarget: 'umd',
  43. library: 'COS'
  44. },
  45. module: {
  46. rules: [
  47. {
  48. test: /\.m?js$/,
  49. loader: 'babel-loader',
  50. exclude: /node_modules/,
  51. }
  52. ],
  53. },
  54. optimization: {
  55. minimize: false,
  56. minimizer: [
  57. new TerserPlugin({
  58. cache: true,
  59. parallel: true,
  60. sourceMap: true,
  61. extractComments: false,
  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/config.js',
  76. 'demo/project.config.json',
  77. 'demo-album/config.js',
  78. 'demo-album/project.config.json',
  79. ]);
  80. config.watch = false;
  81. config.output.filename = 'cos-wx-sdk-v5.min.js';
  82. config.optimization = {
  83. minimize: true,
  84. minimizer: [
  85. new TerserPlugin({
  86. cache: true,
  87. parallel: true,
  88. sourceMap: true,
  89. extractComments: false,
  90. terserOptions: {
  91. compress: {
  92. drop_debugger: true,
  93. drop_console: true,
  94. }
  95. },
  96. }),
  97. ],
  98. };
  99. config.plugins = (config.plugins || []).concat([
  100. new webpack.DefinePlugin({
  101. 'process.env': {
  102. NODE_ENV: '"production"'
  103. }
  104. }),
  105. ]);
  106. }
  107. webpack(config, function (err, stats) {
  108. // 每次运行 npm run build,将 sourcePath 代码复制一份放入 targetPath
  109. var sourcePath = path.resolve(__dirname, './demo/lib/cos-wx-sdk-v5.js');
  110. var targetPath = path.resolve(__dirname, './demo-album/lib/cos-wx-sdk-v5.js');
  111. fs.createReadStream(sourcePath).pipe(fs.createWriteStream(targetPath));
  112. if (process.env.NODE_ENV === 'production') {
  113. var minSourcePath = path.resolve(__dirname, './demo/lib/cos-wx-sdk-v5.min.js');
  114. var mintTargetPath = path.resolve(__dirname, './demo-album/lib/cos-wx-sdk-v5.min.js');
  115. fs.createReadStream(minSourcePath).pipe(fs.createWriteStream(mintTargetPath));
  116. }
  117. if (err) throw err
  118. process.stdout.write(stats.toString({
  119. colors: true,
  120. modules: false,
  121. children: false,
  122. chunks: false,
  123. chunkModules: false
  124. }) + '\n\n');
  125. console.log('Build complete.');
  126. });