Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

244 lignes
8.2 KiB

  1. 'use strict';
  2. const fs = require('fs');
  3. const glob = require('glob');
  4. const path = require('path');
  5. const webpack = require('webpack');
  6. const CleanPlugin = require('clean-webpack-plugin');
  7. const HtmlInlineSourcePlugin = require('html-webpack-inline-source-plugin');
  8. const HtmlPlugin = require('html-webpack-plugin');
  9. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  10. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  11. const TerserPlugin = require('terser-webpack-plugin');
  12. module.exports = function(env, argv) {
  13. env = env || {};
  14. env.production = Boolean(env.production);
  15. env.optimizeImages = env.production || Boolean(env.optimizeImages);
  16. if (!env.optimizeImages && !fs.existsSync(path.resolve(__dirname, 'images/settings'))) {
  17. env.optimizeImages = true;
  18. }
  19. return [getExtensionConfig(env), getUIConfig(env)];
  20. };
  21. function getExtensionConfig(env) {
  22. const plugins = [new CleanPlugin(['dist'], { verbose: false }), new webpack.IgnorePlugin(/^spawn-sync$/)];
  23. return {
  24. name: 'extension',
  25. entry: './src/extension.ts',
  26. mode: env.production ? 'production' : 'development',
  27. target: 'node',
  28. node: {
  29. __dirname: false
  30. },
  31. devtool: 'source-map', //!env.production ? 'source-map' : undefined,
  32. output: {
  33. libraryTarget: 'commonjs2',
  34. filename: 'extension.js',
  35. devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]'
  36. },
  37. optimization: {
  38. minimizer: [
  39. new TerserPlugin({
  40. cache: true,
  41. parallel: true,
  42. sourceMap: true, // !env.production,
  43. terserOptions: {
  44. ecma: 8,
  45. // Keep the class names otherwise @log won't provide a useful name
  46. keep_classnames: true,
  47. module: true
  48. }
  49. })
  50. ]
  51. },
  52. externals: {
  53. vscode: 'commonjs vscode'
  54. },
  55. module: {
  56. rules: [
  57. {
  58. test: /\.ts$/,
  59. enforce: 'pre',
  60. use: 'tslint-loader',
  61. exclude: /node_modules/
  62. },
  63. {
  64. test: /\.tsx?$/,
  65. use: 'ts-loader',
  66. exclude: /node_modules|\.d\.ts$/
  67. }
  68. ],
  69. // Removes `Critical dependency: the request of a dependency is an expression` from `./node_modules/vsls/vscode.js`
  70. exprContextRegExp: /^$/,
  71. exprContextCritical: false
  72. },
  73. resolve: {
  74. extensions: ['.ts', '.tsx', '.js', '.jsx']
  75. },
  76. plugins: plugins,
  77. stats: {
  78. all: false,
  79. assets: true,
  80. builtAt: true,
  81. env: true,
  82. errors: true,
  83. timings: true,
  84. warnings: true
  85. }
  86. };
  87. }
  88. function getUIConfig(env) {
  89. const clean = ['settings.html', 'welcome.html'];
  90. if (env.optimizeImages) {
  91. console.log('Optimizing images (src/ui/images/settings/*.png)...');
  92. clean.push('images/settings');
  93. }
  94. const plugins = [
  95. new CleanPlugin(clean, { verbose: false }),
  96. new MiniCssExtractPlugin({
  97. filename: '[name].css'
  98. }),
  99. new HtmlPlugin({
  100. excludeChunks: ['welcome'],
  101. template: 'settings/index.html',
  102. filename: path.resolve(__dirname, 'settings.html'),
  103. inject: true,
  104. inlineSource: env.production ? '.(js|css)$' : undefined,
  105. minify: env.production
  106. ? {
  107. removeComments: true,
  108. collapseWhitespace: true,
  109. removeRedundantAttributes: true,
  110. useShortDoctype: true,
  111. removeEmptyAttributes: true,
  112. removeStyleLinkTypeAttributes: true,
  113. keepClosingSlash: true,
  114. minifyCSS: true
  115. }
  116. : false
  117. }),
  118. new HtmlPlugin({
  119. excludeChunks: ['settings'],
  120. template: 'welcome/index.html',
  121. filename: path.resolve(__dirname, 'welcome.html'),
  122. inject: true,
  123. inlineSource: env.production ? '.(js|css)$' : undefined,
  124. minify: env.production
  125. ? {
  126. removeComments: true,
  127. collapseWhitespace: true,
  128. removeRedundantAttributes: true,
  129. useShortDoctype: true,
  130. removeEmptyAttributes: true,
  131. removeStyleLinkTypeAttributes: true,
  132. keepClosingSlash: true,
  133. minifyCSS: true
  134. }
  135. : false
  136. }),
  137. new HtmlInlineSourcePlugin(),
  138. new ImageminPlugin({
  139. disable: !env.optimizeImages,
  140. externalImages: {
  141. context: path.resolve(__dirname, 'src/ui/images'),
  142. sources: glob.sync('src/ui/images/settings/*.png'),
  143. destination: path.resolve(__dirname, 'images')
  144. },
  145. cacheFolder: path.resolve(__dirname, '.cache-images'),
  146. gifsicle: null,
  147. jpegtran: null,
  148. optipng: null,
  149. pngquant: {
  150. quality: '85-100',
  151. speed: env.production ? 1 : 10
  152. },
  153. svgo: null
  154. })
  155. ];
  156. return {
  157. name: 'ui',
  158. context: path.resolve(__dirname, 'src/ui'),
  159. // This is ugly having main.scss on both bundles, but if it is added separately it will generate a js bundle :(
  160. entry: {
  161. settings: ['./settings/index.ts', './scss/main.scss'],
  162. welcome: ['./welcome/index.ts', './scss/main.scss']
  163. // main: ['./scss/main.scss']
  164. },
  165. mode: env.production ? 'production' : 'development',
  166. devtool: !env.production ? 'eval-source-map' : undefined,
  167. output: {
  168. filename: '[name].js',
  169. path: path.resolve(__dirname, 'dist/ui'),
  170. publicPath: '{{root}}/dist/ui/'
  171. },
  172. module: {
  173. rules: [
  174. {
  175. test: /\.ts$/,
  176. enforce: 'pre',
  177. use: [
  178. {
  179. loader: 'tslint-loader',
  180. options: {
  181. tsConfigFile: 'ui.tsconfig.json'
  182. }
  183. }
  184. ],
  185. exclude: /node_modules/
  186. },
  187. {
  188. test: /\.tsx?$/,
  189. use: {
  190. loader: 'ts-loader',
  191. options: {
  192. configFile: 'ui.tsconfig.json'
  193. }
  194. },
  195. exclude: /node_modules|\.d\.ts$/
  196. },
  197. {
  198. test: /\.scss$/,
  199. use: [
  200. {
  201. loader: MiniCssExtractPlugin.loader
  202. },
  203. {
  204. loader: 'css-loader',
  205. options: {
  206. sourceMap: !env.production,
  207. url: false
  208. }
  209. },
  210. {
  211. loader: 'sass-loader',
  212. options: {
  213. sourceMap: !env.production
  214. }
  215. }
  216. ],
  217. exclude: /node_modules/
  218. }
  219. ]
  220. },
  221. resolve: {
  222. extensions: ['.ts', '.tsx', '.js', '.jsx'],
  223. modules: [path.resolve(__dirname, 'src/ui'), 'node_modules']
  224. },
  225. plugins: plugins,
  226. stats: {
  227. all: false,
  228. assets: true,
  229. builtAt: true,
  230. env: true,
  231. errors: true,
  232. timings: true,
  233. warnings: true
  234. }
  235. };
  236. }