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.

300 lines
7.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 CspHtmlPlugin = require('csp-html-webpack-plugin');
  11. const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
  12. const HtmlExcludeAssetsPlugin = require('html-webpack-exclude-assets-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. })
  38. ];
  39. if (env.analyzeDeps) {
  40. plugins.push(
  41. new CircularDependencyPlugin({
  42. cwd: __dirname,
  43. exclude: /node_modules/,
  44. failOnError: false,
  45. onDetected: function({ module: webpackModuleRecord, paths, compilation }) {
  46. if (paths.some(p => p.includes('container.ts'))) return;
  47. compilation.warnings.push(new Error(paths.join(' -> ')));
  48. }
  49. })
  50. );
  51. }
  52. if (env.analyzeBundle) {
  53. plugins.push(new BundleAnalyzerPlugin());
  54. }
  55. return {
  56. name: 'extension',
  57. entry: './src/extension.ts',
  58. mode: env.production ? 'production' : 'development',
  59. target: 'node',
  60. node: {
  61. __dirname: false
  62. },
  63. devtool: 'source-map',
  64. output: {
  65. libraryTarget: 'commonjs2',
  66. filename: 'extension.js'
  67. },
  68. optimization: {
  69. minimizer: [
  70. new TerserPlugin({
  71. cache: true,
  72. parallel: true,
  73. sourceMap: true,
  74. terserOptions: {
  75. ecma: 8,
  76. // Keep the class names otherwise @log won't provide a useful name
  77. // eslint-disable-next-line @typescript-eslint/camelcase
  78. keep_classnames: true,
  79. module: true
  80. }
  81. })
  82. ]
  83. },
  84. externals: {
  85. vscode: 'commonjs vscode'
  86. },
  87. module: {
  88. rules: [
  89. {
  90. exclude: /node_modules|\.d\.ts$/,
  91. test: /\.tsx?$/,
  92. use: {
  93. loader: 'ts-loader',
  94. options: {
  95. transpileOnly: true
  96. }
  97. }
  98. }
  99. ]
  100. },
  101. resolve: {
  102. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json']
  103. },
  104. plugins: plugins,
  105. stats: {
  106. all: false,
  107. assets: true,
  108. builtAt: true,
  109. env: true,
  110. errors: true,
  111. timings: true,
  112. warnings: true
  113. }
  114. };
  115. }
  116. function getWebviewsConfig(env) {
  117. const clean = ['**/*'];
  118. if (env.optimizeImages) {
  119. console.log('Optimizing images (src/webviews/apps/images/settings/*.png)...');
  120. clean.push(path.resolve(__dirname, 'images/settings/*'));
  121. }
  122. const cspPolicy = {
  123. 'default-src': "'none'",
  124. 'img-src': ['vscode-resource:', 'https:', 'data:'],
  125. 'script-src': ['vscode-resource:', "'nonce-Z2l0bGVucy1ib290c3RyYXA='"],
  126. 'style-src': ['vscode-resource:']
  127. };
  128. if (!env.production) {
  129. cspPolicy['script-src'].push("'unsafe-eval'");
  130. }
  131. /**
  132. * @type any[]
  133. */
  134. const plugins = [
  135. new CleanPlugin({ cleanOnceBeforeBuildPatterns: clean }),
  136. new ForkTsCheckerPlugin({
  137. tsconfig: path.resolve(__dirname, 'tsconfig.webviews.json'),
  138. async: false,
  139. eslint: true
  140. }),
  141. new MiniCssExtractPlugin({
  142. filename: '[name].css'
  143. }),
  144. new HtmlPlugin({
  145. excludeAssets: [/.+-styles\.js/],
  146. excludeChunks: ['welcome'],
  147. template: 'settings/index.html',
  148. filename: path.resolve(__dirname, 'dist/webviews/settings.html'),
  149. inject: true,
  150. // inlineSource: env.production ? '.(js|css)$' : undefined,
  151. cspPlugin: {
  152. enabled: true,
  153. policy: cspPolicy,
  154. nonceEnabled: {
  155. 'script-src': true,
  156. 'style-src': true
  157. }
  158. },
  159. minify: env.production
  160. ? {
  161. removeComments: true,
  162. collapseWhitespace: true,
  163. removeRedundantAttributes: false,
  164. useShortDoctype: true,
  165. removeEmptyAttributes: true,
  166. removeStyleLinkTypeAttributes: true,
  167. keepClosingSlash: true,
  168. minifyCSS: true
  169. }
  170. : false
  171. }),
  172. new HtmlPlugin({
  173. excludeAssets: [/.+-styles\.js/],
  174. excludeChunks: ['settings'],
  175. template: 'welcome/index.html',
  176. filename: path.resolve(__dirname, 'dist/webviews/welcome.html'),
  177. inject: true,
  178. // inlineSource: env.production ? '.(js|css)$' : undefined,
  179. cspPlugin: {
  180. enabled: true,
  181. policy: cspPolicy,
  182. nonceEnabled: {
  183. 'script-src': true,
  184. 'style-src': true
  185. }
  186. },
  187. minify: env.production
  188. ? {
  189. removeComments: true,
  190. collapseWhitespace: true,
  191. removeRedundantAttributes: false,
  192. useShortDoctype: true,
  193. removeEmptyAttributes: true,
  194. removeStyleLinkTypeAttributes: true,
  195. keepClosingSlash: true,
  196. minifyCSS: true
  197. }
  198. : false
  199. }),
  200. new HtmlExcludeAssetsPlugin(),
  201. new CspHtmlPlugin(),
  202. new ImageminPlugin({
  203. disable: !env.optimizeImages,
  204. externalImages: {
  205. context: path.resolve(__dirname, 'src/webviews/apps/images'),
  206. sources: glob.sync('src/webviews/apps/images/settings/*.png'),
  207. destination: path.resolve(__dirname, 'images')
  208. },
  209. cacheFolder: path.resolve(__dirname, 'node_modules', '.cache', 'imagemin-webpack-plugin'),
  210. gifsicle: null,
  211. jpegtran: null,
  212. optipng: null,
  213. pngquant: {
  214. quality: '85-100',
  215. speed: env.production ? 1 : 10
  216. },
  217. svgo: null
  218. })
  219. ];
  220. return {
  221. name: 'webviews',
  222. context: path.resolve(__dirname, 'src/webviews/apps'),
  223. entry: {
  224. 'main-styles': ['./scss/main.scss'],
  225. settings: ['./settings/index.ts'],
  226. welcome: ['./welcome/index.ts']
  227. },
  228. mode: env.production ? 'production' : 'development',
  229. devtool: env.production ? undefined : 'eval-source-map',
  230. output: {
  231. filename: '[name].js',
  232. path: path.resolve(__dirname, 'dist/webviews'),
  233. publicPath: '#{root}/dist/webviews/'
  234. },
  235. module: {
  236. rules: [
  237. {
  238. exclude: /node_modules|\.d\.ts$/,
  239. test: /\.tsx?$/,
  240. use: {
  241. loader: 'ts-loader',
  242. options: {
  243. configFile: 'tsconfig.webviews.json',
  244. transpileOnly: true
  245. }
  246. }
  247. },
  248. {
  249. test: /\.scss$/,
  250. use: [
  251. {
  252. loader: MiniCssExtractPlugin.loader
  253. },
  254. {
  255. loader: 'css-loader',
  256. options: {
  257. sourceMap: true,
  258. url: false
  259. }
  260. },
  261. {
  262. loader: 'sass-loader',
  263. options: {
  264. sourceMap: true
  265. }
  266. }
  267. ],
  268. exclude: /node_modules/
  269. }
  270. ]
  271. },
  272. resolve: {
  273. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  274. modules: [path.resolve(__dirname, 'src/webviews/apps'), 'node_modules']
  275. },
  276. plugins: plugins,
  277. stats: {
  278. all: false,
  279. assets: true,
  280. builtAt: true,
  281. env: true,
  282. errors: true,
  283. timings: true,
  284. warnings: true
  285. }
  286. };
  287. }