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

277 lines
9.3 KiB

преди 5 години
преди 6 години
преди 5 години
преди 5 години
преди 5 години
преди 5 години
преди 5 години
преди 6 години
преди 5 години
преди 5 години
преди 5 години
преди 5 години
  1. 'use strict';
  2. /* eslint-disable @typescript-eslint/no-var-requires */
  3. const fs = require('fs');
  4. const path = require('path');
  5. const glob = require('glob');
  6. const webpack = require('webpack');
  7. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  8. const { CleanWebpackPlugin: CleanPlugin } = require('clean-webpack-plugin');
  9. const CircularDependencyPlugin = require('circular-dependency-plugin');
  10. const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
  11. const HtmlExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin');
  12. const HtmlInlineSourcePlugin = require('html-webpack-inline-source-plugin');
  13. const HtmlPlugin = require('html-webpack-plugin');
  14. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  15. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  16. const TerserPlugin = require('terser-webpack-plugin');
  17. module.exports = function(env, argv) {
  18. env = env || {};
  19. env.analyzeBundle = Boolean(env.analyzeBundle);
  20. env.analyzeDeps = Boolean(env.analyzeDeps);
  21. env.production = env.analyzeBundle || Boolean(env.production);
  22. env.optimizeImages = Boolean(env.optimizeImages) || (env.production && !env.analyzeBundle);
  23. if (!env.optimizeImages && !fs.existsSync(path.resolve(__dirname, 'images/settings'))) {
  24. env.optimizeImages = true;
  25. }
  26. return [getExtensionConfig(env), getWebviewsConfig(env)];
  27. };
  28. function getExtensionConfig(env) {
  29. /**
  30. * @type any[]
  31. */
  32. const plugins = [
  33. new CleanPlugin({ cleanOnceBeforeBuildPatterns: ['**/*', '!**/webviews/**'] }),
  34. new ForkTsCheckerPlugin({
  35. async: false,
  36. eslint: true,
  37. useTypescriptIncrementalApi: true
  38. })
  39. ];
  40. if (env.analyzeDeps) {
  41. plugins.push(
  42. new CircularDependencyPlugin({
  43. cwd: __dirname,
  44. exclude: /node_modules/,
  45. failOnError: false,
  46. onDetected: function({ module: webpackModuleRecord, paths, compilation }) {
  47. if (paths.some(p => /container\.ts/.test(p))) return;
  48. compilation.warnings.push(new Error(paths.join(' -> ')));
  49. }
  50. })
  51. );
  52. }
  53. if (env.analyzeBundle) {
  54. plugins.push(new BundleAnalyzerPlugin());
  55. }
  56. return {
  57. name: 'extension',
  58. entry: './src/extension.ts',
  59. mode: env.production ? 'production' : 'development',
  60. target: 'node',
  61. node: {
  62. __dirname: false
  63. },
  64. devtool: 'source-map',
  65. output: {
  66. libraryTarget: 'commonjs2',
  67. filename: 'extension.js'
  68. },
  69. optimization: {
  70. minimizer: [
  71. new TerserPlugin({
  72. cache: true,
  73. parallel: true,
  74. sourceMap: true,
  75. terserOptions: {
  76. ecma: 8,
  77. // Keep the class names otherwise @log won't provide a useful name
  78. // eslint-disable-next-line @typescript-eslint/camelcase
  79. keep_classnames: true,
  80. module: true
  81. }
  82. })
  83. ]
  84. },
  85. externals: {
  86. vscode: 'commonjs vscode'
  87. },
  88. module: {
  89. rules: [
  90. {
  91. exclude: /node_modules|\.d\.ts$/,
  92. test: /\.tsx?$/,
  93. use: {
  94. loader: 'ts-loader',
  95. options: {
  96. transpileOnly: true,
  97. experimentalWatchApi: true
  98. }
  99. }
  100. }
  101. ]
  102. },
  103. resolve: {
  104. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json']
  105. },
  106. plugins: plugins,
  107. stats: {
  108. all: false,
  109. assets: true,
  110. builtAt: true,
  111. env: true,
  112. errors: true,
  113. timings: true,
  114. warnings: true
  115. }
  116. };
  117. }
  118. function getWebviewsConfig(env) {
  119. const clean = ['**/*'];
  120. if (env.optimizeImages) {
  121. console.log('Optimizing images (src/webviews/apps/images/settings/*.png)...');
  122. clean.push(path.resolve(__dirname, 'images/settings/*'));
  123. }
  124. /**
  125. * @type any[]
  126. */
  127. const plugins = [
  128. new CleanPlugin({ cleanOnceBeforeBuildPatterns: clean }),
  129. new ForkTsCheckerPlugin({
  130. tsconfig: path.resolve(__dirname, 'webviews.tsconfig.json'),
  131. async: false,
  132. eslint: true,
  133. useTypescriptIncrementalApi: true
  134. }),
  135. new MiniCssExtractPlugin({
  136. filename: '[name].css'
  137. }),
  138. new HtmlPlugin({
  139. excludeAssets: [/.+-styles\.js/],
  140. excludeChunks: ['welcome'],
  141. template: 'settings/index.html',
  142. filename: path.resolve(__dirname, 'dist/webviews/settings.html'),
  143. inject: true,
  144. // inlineSource: env.production ? '.(js|css)$' : undefined,
  145. minify: env.production
  146. ? {
  147. removeComments: true,
  148. collapseWhitespace: true,
  149. removeRedundantAttributes: true,
  150. useShortDoctype: true,
  151. removeEmptyAttributes: true,
  152. removeStyleLinkTypeAttributes: true,
  153. keepClosingSlash: true,
  154. minifyCSS: true
  155. }
  156. : false
  157. }),
  158. new HtmlPlugin({
  159. excludeAssets: [/.+-styles\.js/],
  160. excludeChunks: ['settings'],
  161. template: 'welcome/index.html',
  162. filename: path.resolve(__dirname, 'dist/webviews/welcome.html'),
  163. inject: true,
  164. // inlineSource: env.production ? '.(js|css)$' : undefined,
  165. minify: env.production
  166. ? {
  167. removeComments: true,
  168. collapseWhitespace: true,
  169. removeRedundantAttributes: true,
  170. useShortDoctype: true,
  171. removeEmptyAttributes: true,
  172. removeStyleLinkTypeAttributes: true,
  173. keepClosingSlash: true,
  174. minifyCSS: true
  175. }
  176. : false
  177. }),
  178. new HtmlExcludeAssetsPlugin(),
  179. new HtmlInlineSourcePlugin(),
  180. new ImageminPlugin({
  181. disable: !env.optimizeImages,
  182. externalImages: {
  183. context: path.resolve(__dirname, 'src/webviews/apps/images'),
  184. sources: glob.sync('src/webviews/apps/images/settings/*.png'),
  185. destination: path.resolve(__dirname, 'images')
  186. },
  187. cacheFolder: path.resolve(__dirname, 'node_modules', '.cache', 'imagemin-webpack-plugin'),
  188. gifsicle: null,
  189. jpegtran: null,
  190. optipng: null,
  191. pngquant: {
  192. quality: '85-100',
  193. speed: env.production ? 1 : 10
  194. },
  195. svgo: null
  196. })
  197. ];
  198. return {
  199. name: 'webviews',
  200. context: path.resolve(__dirname, 'src/webviews/apps'),
  201. entry: {
  202. 'main-styles': ['./scss/main.scss'],
  203. settings: ['./settings/index.ts'],
  204. welcome: ['./welcome/index.ts']
  205. },
  206. mode: env.production ? 'production' : 'development',
  207. devtool: env.production ? undefined : 'eval-source-map',
  208. output: {
  209. filename: '[name].js',
  210. path: path.resolve(__dirname, 'dist/webviews'),
  211. publicPath: '{{root}}/dist/webviews/'
  212. },
  213. module: {
  214. rules: [
  215. {
  216. exclude: /node_modules|\.d\.ts$/,
  217. test: /\.tsx?$/,
  218. use: {
  219. loader: 'ts-loader',
  220. options: {
  221. configFile: 'webviews.tsconfig.json',
  222. transpileOnly: true,
  223. experimentalWatchApi: true
  224. }
  225. }
  226. },
  227. {
  228. test: /\.scss$/,
  229. use: [
  230. {
  231. loader: MiniCssExtractPlugin.loader
  232. },
  233. {
  234. loader: 'css-loader',
  235. options: {
  236. sourceMap: true,
  237. url: false
  238. }
  239. },
  240. {
  241. loader: 'sass-loader',
  242. options: {
  243. sourceMap: true
  244. }
  245. }
  246. ],
  247. exclude: /node_modules/
  248. }
  249. ]
  250. },
  251. resolve: {
  252. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  253. modules: [path.resolve(__dirname, 'src/webviews/apps'), 'node_modules']
  254. },
  255. plugins: plugins,
  256. stats: {
  257. all: false,
  258. assets: true,
  259. builtAt: true,
  260. env: true,
  261. errors: true,
  262. timings: true,
  263. warnings: true
  264. }
  265. };
  266. }