You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

webpack_pro.config.js 7.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. const cssnano = require('cssnano');
  2. const fastGlob = require('fast-glob');
  3. const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
  6. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  7. const PostCSSPresetEnv = require('postcss-preset-env');
  8. const PostCSSSafeParser = require('postcss-safe-parser');
  9. const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
  10. const TerserPlugin = require('terser-webpack-plugin');
  11. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  12. const {statSync} = require('fs');
  13. const {resolve, parse} = require('path');
  14. //const {SourceMapDevToolPlugin} = require('webpack');
  15. const glob = (pattern) => fastGlob.sync(pattern, {cwd: __dirname, absolute: true});
  16. const themes = {};
  17. for (const path of glob('web_src/less/themes/*.less')) {
  18. themes[parse(path).name] = [path];
  19. }
  20. const standalone = {};
  21. const stadalonePaths = [
  22. ...glob('web_src/js/standalone/**/*.js'),
  23. ...glob('web_src/less/standalone/**/*.less'),
  24. ];
  25. for (const path of stadalonePaths) {
  26. standalone[parse(path).name] = [path];
  27. }
  28. const vuePages = {};
  29. for (const path of glob('web_src/vuepages/**/vp-*.js')) {
  30. vuePages[parse(path).name] = [path];
  31. }
  32. const isProduction = process.env.NODE_ENV !== 'development';
  33. module.exports = {
  34. mode: isProduction ? 'production' : 'development',
  35. entry: {
  36. index: [
  37. resolve(__dirname, 'web_src/js/index.js'),
  38. resolve(__dirname, 'web_src/less/index.less'),
  39. ],
  40. jquery: [
  41. resolve(__dirname, 'web_src/js/jquery.js'),
  42. ],
  43. icons: glob('node_modules/@primer/octicons/build/svg/**/*.svg'),
  44. ...standalone,
  45. ...themes,
  46. ...vuePages
  47. },
  48. devtool: false,
  49. output: {
  50. path: resolve(__dirname, 'public'),
  51. filename: 'js/[name].js',
  52. chunkFilename: 'js/[name].js',
  53. },
  54. node:{
  55. fs: 'empty'
  56. },
  57. optimization: {
  58. minimize: isProduction,
  59. minimizer: [
  60. new TerserPlugin({
  61. sourceMap: true,
  62. extractComments: false,
  63. terserOptions: {
  64. keep_fnames: /^(HTML|SVG)/, // https://github.com/fgnass/domino/issues/144
  65. output: {
  66. comments: false,
  67. },
  68. },
  69. }),
  70. new OptimizeCSSAssetsPlugin({
  71. cssProcessor: cssnano,
  72. cssProcessorOptions: {
  73. parser: PostCSSSafeParser,
  74. },
  75. cssProcessorPluginOptions: {
  76. preset: [
  77. 'default',
  78. {
  79. discardComments: {
  80. removeAll: true,
  81. },
  82. },
  83. ],
  84. },
  85. }),
  86. ],
  87. splitChunks: {
  88. chunks: 'async',
  89. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  90. cacheGroups: {
  91. // this bundles all monaco's languages into one file instead of emitting 1-65.js files
  92. monaco: {
  93. test: /monaco-editor/,
  94. name: 'monaco',
  95. chunks: 'async'
  96. }
  97. }
  98. }
  99. },
  100. module: {
  101. rules: [
  102. {
  103. test: /\.vue$/,
  104. exclude: /node_modules/,
  105. loader: 'vue-loader',
  106. },
  107. {
  108. test: require.resolve('jquery-datetimepicker'),
  109. use: 'imports-loader?define=>false,exports=>false',
  110. },
  111. {
  112. test: /\.worker\.js$/,
  113. exclude: /monaco/,
  114. use: [
  115. {
  116. loader: 'worker-loader',
  117. options: {
  118. name: '[name].js',
  119. inline: true,
  120. fallback: false,
  121. },
  122. },
  123. ],
  124. },
  125. {
  126. test: /\.ts$/,
  127. use: [
  128. {
  129. loader: "ts-loader",
  130. }
  131. ],
  132. exclude: /node_modules/
  133. },
  134. {
  135. test: /\.js$/,
  136. exclude: /node_modules/,
  137. use: [
  138. {
  139. loader: 'babel-loader',
  140. options: {
  141. cacheDirectory: true,
  142. cacheCompression: false,
  143. cacheIdentifier: [
  144. resolve(__dirname, 'package.json'),
  145. resolve(__dirname, 'package-lock.json'),
  146. resolve(__dirname, 'webpack.config.js'),
  147. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  148. sourceMaps: true,
  149. presets: [
  150. [
  151. '@babel/preset-env',
  152. {
  153. useBuiltIns: 'usage',
  154. corejs: 3,
  155. },
  156. ],
  157. ],
  158. plugins: [
  159. [
  160. '@babel/plugin-transform-runtime',
  161. {
  162. regenerator: true,
  163. }
  164. ],
  165. '@babel/plugin-proposal-object-rest-spread',
  166. ],
  167. },
  168. },
  169. ],
  170. },
  171. {
  172. test: /\.(less|css)$/i,
  173. use: [
  174. {
  175. loader: MiniCssExtractPlugin.loader,
  176. },
  177. {
  178. loader: 'css-loader',
  179. options: {
  180. importLoaders: 2,
  181. url: (_url, resourcePath) => {
  182. // only resolve URLs for dependencies
  183. return resourcePath.includes('node_modules');
  184. },
  185. }
  186. },
  187. {
  188. loader: 'postcss-loader',
  189. options: {
  190. plugins: () => [
  191. PostCSSPresetEnv(),
  192. ],
  193. },
  194. },
  195. {
  196. loader: 'less-loader',
  197. },
  198. ],
  199. },
  200. {
  201. test: /\.svg$/,
  202. use: [
  203. {
  204. loader: 'svg-sprite-loader',
  205. options: {
  206. extract: true,
  207. spriteFilename: 'img/svg/icons.svg',
  208. symbolId: (path) => {
  209. const {name} = parse(path);
  210. if (/@primer[/\\]octicons/.test(path)) {
  211. return `octicon-${name}`;
  212. }
  213. return name;
  214. },
  215. },
  216. },
  217. {
  218. loader: 'svgo-loader',
  219. },
  220. ],
  221. },
  222. {
  223. test: /\.(ttf|woff2?)$/,
  224. use: [
  225. {
  226. loader: 'file-loader',
  227. options: {
  228. name: '[name].[ext]',
  229. outputPath: 'fonts/',
  230. publicPath: (url) => `../fonts/${url}`, // seems required for monaco's font
  231. },
  232. },
  233. ],
  234. },
  235. ],
  236. },
  237. plugins: [
  238. new VueLoaderPlugin(),
  239. // avoid generating useless js output files for css- and svg-only chunks
  240. new FixStyleOnlyEntriesPlugin({
  241. extensions: ['less', 'scss', 'css', 'svg'],
  242. silent: true,
  243. }),
  244. new MiniCssExtractPlugin({
  245. filename: 'css/[name].css',
  246. chunkFilename: 'css/[name].css',
  247. }),
  248. //new SourceMapDevToolPlugin({
  249. //filename: 'js/[name].js.map',
  250. //include: [
  251. //'js/index.js',
  252. //],
  253. //}),
  254. new SpriteLoaderPlugin({
  255. plainSprite: true,
  256. }),
  257. new MonacoWebpackPlugin({
  258. filename: 'js/monaco-[name].worker.js',
  259. })
  260. ],
  261. performance: {
  262. hints: false,
  263. maxEntrypointSize: Infinity,
  264. maxAssetSize: Infinity,
  265. },
  266. resolve: {
  267. symlinks: false,
  268. alias: {
  269. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  270. '~': resolve(__dirname, 'web_src/vuepages'),
  271. },
  272. extensions: ['.tsx', '.ts', '.js']
  273. },
  274. watchOptions: {
  275. ignored: [
  276. 'node_modules/**',
  277. ],
  278. },
  279. stats: {
  280. children: false,
  281. },
  282. };