webpack.config.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @fileoverview webpack.
  3. *
  4. * @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
  5. * @version 0.2.0.1, Jan 4, 2020
  6. */
  7. const path = require('path')
  8. const webpack = require('webpack')
  9. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  10. const {CleanWebpackPlugin} = require('clean-webpack-plugin')
  11. const CopyPlugin = require('copy-webpack-plugin')
  12. const TerserPlugin = require('terser-webpack-plugin')
  13. const BundleAnalyzerPlugin = require(
  14. 'webpack-bundle-analyzer').BundleAnalyzerPlugin
  15. const pkg = require('./package.json')
  16. const banner = new webpack.BannerPlugin({
  17. banner: `Vditor v${pkg.version} - A markdown editor written in TypeScript.
  18. MIT License
  19. Copyright (c) 2018-present B3log 开源, b3log.org
  20. Permission is hereby granted, free of charge, to any person obtaining a copy
  21. of this software and associated documentation files (the "Software"), to deal
  22. in the Software without restriction, including without limitation the rights
  23. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  24. copies of the Software, and to permit persons to whom the Software is
  25. furnished to do so, subject to the following conditions:
  26. The above copyright notice and this permission notice shall be included in all
  27. copies or substantial portions of the Software.
  28. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. SOFTWARE.
  35. `,
  36. entryOnly: true,
  37. })
  38. module.exports = [
  39. {
  40. mode: 'production',
  41. output: {
  42. filename: '[name].js',
  43. path: path.resolve(__dirname, 'dist'),
  44. // chunkFilename: '[name].bundle.js',
  45. // publicPath: `${pkg.cdn}/vditor@${pkg.version}/dist/`,
  46. libraryTarget: 'umd',
  47. library: 'Vditor',
  48. libraryExport: 'default',
  49. globalObject: 'this',
  50. },
  51. entry: {
  52. 'index.min': './src/index.ts',
  53. 'method.min': './src/method.ts',
  54. 'index': './src/index.ts',
  55. 'method': './src/method.ts',
  56. },
  57. optimization: {
  58. minimize: true,
  59. minimizer: [
  60. new TerserPlugin({
  61. include: ['index.min.js', 'method.min.js'],
  62. terserOptions: {
  63. format: {
  64. comments: false,
  65. },
  66. },
  67. extractComments: false,
  68. }),
  69. ],
  70. },
  71. resolve: {
  72. extensions: ['.ts', '.js', '.less', 'png'],
  73. },
  74. module: {
  75. rules: [
  76. {
  77. test: /\.png$/,
  78. include: [path.resolve(__dirname, './src/assets/images')],
  79. use: [
  80. 'file-loader',
  81. ],
  82. },
  83. {
  84. test: /\.js$/,
  85. exclude: '/node_modules/',
  86. use: {
  87. loader: 'babel-loader',
  88. options: {
  89. presets: [
  90. [
  91. '@babel/env',
  92. {
  93. targets: {
  94. browsers: [
  95. 'last 2 Chrome major versions',
  96. 'last 2 Firefox major versions',
  97. 'last 2 Safari major versions',
  98. 'last 2 Edge major versions',
  99. 'last 2 iOS major versions',
  100. 'last 2 ChromeAndroid major versions',
  101. ],
  102. },
  103. },
  104. ],
  105. ],
  106. },
  107. },
  108. },
  109. {
  110. test: /\.ts$/,
  111. use: 'ts-loader',
  112. },
  113. {
  114. test: /\.less$/,
  115. include: [path.resolve(__dirname, 'src/assets')],
  116. use: [
  117. MiniCssExtractPlugin.loader,
  118. {
  119. loader: 'css-loader', // translates CSS into CommonJS
  120. options: {
  121. url: false,
  122. },
  123. },
  124. {
  125. loader: 'postcss-loader',
  126. options: {
  127. postcssOptions: {
  128. plugins: [
  129. ['autoprefixer', {grid: true, remove: false}],
  130. ],
  131. },
  132. },
  133. },
  134. {
  135. loader: 'less-loader', // compiles Less to CSS
  136. },
  137. ],
  138. },
  139. ],
  140. },
  141. plugins: [
  142. // new BundleAnalyzerPlugin(),
  143. new CleanWebpackPlugin({
  144. cleanOnceBeforeBuildPatterns: [
  145. path.join(__dirname, 'dist')],
  146. }),
  147. new webpack.DefinePlugin({
  148. VDITOR_VERSION: JSON.stringify(pkg.version),
  149. }),
  150. new MiniCssExtractPlugin({
  151. filename: 'index.css',
  152. }),
  153. banner,
  154. new CopyPlugin({
  155. patterns: [
  156. {from: 'src/css', to: 'css'},
  157. {from: 'src/images', to: 'images'},
  158. {from: 'src/js', to: 'js'},
  159. {from: 'types', to: 'types'},
  160. ],
  161. }),
  162. ],
  163. }]