build.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var webpack = require('webpack');
  2. var path = require('path');
  3. var fs = require('fs');
  4. var pkg = require('./package.json');
  5. var replaceVersion = function () {
  6. var filePath = path.resolve(__dirname, 'src/cos.js');
  7. var content = fs.readFileSync(filePath).toString();
  8. if (content) {
  9. var newContent = content.replace(/(COS\.version) *= *['"]\d+\.\d+\.\d+['"];/, "$1 = '" + pkg.version + "';");
  10. if (newContent !== content) {
  11. fs.writeFileSync(filePath, newContent);
  12. console.log('cos.js version updated.');
  13. }
  14. }
  15. };
  16. var replaceDevCode = function (list) {
  17. list.forEach(function (fileName) {
  18. var filePath = path.resolve(__dirname, fileName);
  19. var content = fs.readFileSync(filePath).toString();
  20. var newContent = content;
  21. newContent = newContent.replace(/https:\/\/\w+\.com\/[\w\-]+\/server\//, 'https://example.com/');
  22. newContent = newContent.replace(/test-125\d{7}/, 'test-1250000000');
  23. newContent = newContent.replace(/wx-125\d{7}/, 'test-1250000000');
  24. newContent = newContent.replace(/"appid": "wx\w+"/, '"appid": "wx0000000000000000"');
  25. if (newContent !== content) {
  26. console.log('replace ' + filePath);
  27. fs.writeFileSync(filePath, newContent);
  28. }
  29. });
  30. };
  31. replaceVersion();
  32. var config = {
  33. watch: true,
  34. entry: path.resolve(__dirname, './index.js'),
  35. output: {
  36. path: path.resolve(__dirname, './demo/lib/'),
  37. publicPath: path.resolve(__dirname, './demo/lib/'),
  38. filename: 'cos-wx-sdk-v5.js',
  39. libraryTarget: 'umd',
  40. library: 'COS'
  41. },
  42. module: {
  43. rules: [
  44. {
  45. test: /\.m?js$/,
  46. loader: 'babel-loader',
  47. options: {
  48. presets: ['es2015']
  49. }
  50. }
  51. ]
  52. },
  53. devServer: {
  54. historyApiFallback: true,
  55. noInfo: true
  56. },
  57. performance: {
  58. hints: false
  59. },
  60. };
  61. if (process.env.NODE_ENV === 'production') {
  62. replaceDevCode([
  63. 'demo/config.js',
  64. 'demo/project.config.json',
  65. 'demo-album/config.js',
  66. 'demo-album/project.config.json',
  67. ]);
  68. config.watch = false;
  69. config.output.filename = 'cos-wx-sdk-v5.min.js';
  70. config.plugins = (config.plugins || []).concat([
  71. new webpack.DefinePlugin({
  72. 'process.env': {
  73. NODE_ENV: '"production"'
  74. }
  75. }),
  76. new webpack.optimize.UglifyJsPlugin({
  77. sourceMap: true,
  78. output: {
  79. ascii_only: true,
  80. },
  81. compress: {
  82. warnings: false,
  83. },
  84. }),
  85. new webpack.LoaderOptionsPlugin({
  86. minimize: true
  87. }),
  88. ]);
  89. }
  90. webpack(config, function (err, stats) {
  91. // 每次运行 npm run build,将 sourcePath 代码复制一份放入 targetPath
  92. var sourcePath = path.resolve(__dirname, './demo/lib/cos-wx-sdk-v5.js');
  93. var targetPath = path.resolve(__dirname, './demo-album/lib/cos-wx-sdk-v5.js');
  94. var minSourcePath = path.resolve(__dirname, './demo/lib/cos-wx-sdk-v5.min.js');
  95. var mintTargetPath = path.resolve(__dirname, './demo-album/lib/cos-wx-sdk-v5.min.js');
  96. fs.createReadStream(sourcePath).pipe(fs.createWriteStream(targetPath));
  97. fs.createReadStream(minSourcePath).pipe(fs.createWriteStream(mintTargetPath));
  98. if (err) throw err
  99. process.stdout.write(stats.toString({
  100. colors: true,
  101. modules: false,
  102. children: false,
  103. chunks: false,
  104. chunkModules: false
  105. }) + '\n\n');
  106. console.log('Build complete.');
  107. });