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 BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  7. const { CleanWebpackPlugin: CleanPlugin } = require('clean-webpack-plugin');
  8. const CircularDependencyPlugin = require('circular-dependency-plugin');
  9. const CspHtmlPlugin = require('csp-html-webpack-plugin');
  10. const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
  11. const HtmlExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin');
  12. const HtmlPlugin = require('html-webpack-plugin');
  13. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  14. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  15. const TerserPlugin = require('terser-webpack-plugin');
  16. module.exports = function(env, argv) {
  17. env = env || {};
  18. env.analyzeBundle = Boolean(env.analyzeBundle);
  19. env.analyzeDeps = Boolean(env.analyzeDeps);
  20. env.production = env.analyzeBundle || Boolean(env.production);
  21. env.optimizeImages = Boolean(env.optimizeImages) || (env.production && !env.analyzeBundle);
  22. if (!env.optimizeImages && !fs.existsSync(path.resolve(__dirname, 'images/settings'))) {
  23. env.optimizeImages = true;
  24. }
  25. return [getExtensionConfig(env), getWebviewsConfig(env)];
  26. };
  27. function getExtensionConfig(env) {
  28. /**
  29. * @type any[]
  30. */
  31. const plugins = [
  32. new CleanPlugin({ cleanOnceBeforeBuildPatterns: ['**/*', '!**/webviews/**'] }),
  33. new ForkTsCheckerPlugin({
  34. async: false,
  35. eslint: true
  36. })
  37. ];
  38. if (env.analyzeDeps) {
  39. plugins.push(
  40. new CircularDependencyPlugin({
  41. cwd: __dirname,
  42. exclude: /node_modules/,
  43. failOnError: false,
  44. onDetected: function({ module: webpackModuleRecord, paths, compilation }) {
  45. if (paths.some(p => p.includes('container.ts'))) return;
  46. compilation.warnings.push(new Error(paths.join(' -> ')));
  47. }
  48. })
  49. );
  50. }
  51. if (env.analyzeBundle) {
  52. plugins.push(new BundleAnalyzerPlugin());
  53. }
  54. return {
  55. name: 'extension',
  56. entry: './src/extension.ts',
  57. mode: env.production ? 'production' : 'development',
  58. target: 'node',
  59. node: {
  60. __dirname: false
  61. },
  62. devtool: 'source-map',
  63. output: {
  64. libraryTarget: 'commonjs2',
  65. filename: 'extension.js'
  66. },
  67. optimization: {
  68. minimizer: [
  69. new TerserPlugin({
  70. cache: true,
  71. parallel: true,
  72. sourceMap: true,
  73. terserOptions: {
  74. ecma: 8,
  75. // Keep the class names otherwise @log won't provide a useful name
  76. // eslint-disable-next-line @typescript-eslint/camelcase
  77. keep_classnames: true,
  78. module: true
  79. }
  80. })
  81. ]
  82. },
  83. externals: {
  84. vscode: 'commonjs vscode'
  85. },
  86. module: {
  87. rules: [
  88. {
  89. exclude: /node_modules|\.d\.ts$/,
  90. test: /\.tsx?$/,
  91. use: {
  92. loader: 'ts-loader',
  93. options: {
  94. experimentalWatchApi: true,
  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. }