Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

426 rindas
11 KiB

pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 4 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
pirms 5 gadiem
  1. //@ts-check
  2. /** @typedef {import('webpack').Configuration} WebpackConfig **/
  3. /* eslint-disable @typescript-eslint/no-var-requires */
  4. /* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
  5. /* eslint-disable @typescript-eslint/strict-boolean-expressions */
  6. /* eslint-disable @typescript-eslint/prefer-optional-chain */
  7. 'use strict';
  8. const fs = require('fs');
  9. const path = require('path');
  10. const glob = require('glob');
  11. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  12. const { CleanWebpackPlugin: CleanPlugin } = require('clean-webpack-plugin');
  13. const CircularDependencyPlugin = require('circular-dependency-plugin');
  14. const CspHtmlPlugin = require('csp-html-webpack-plugin');
  15. const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
  16. const HtmlPlugin = require('html-webpack-plugin');
  17. const HtmlSkipAssetsPlugin = require('html-webpack-skip-assets-plugin').HtmlWebpackSkipAssetsPlugin;
  18. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  19. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  20. const TerserPlugin = require('terser-webpack-plugin');
  21. class InlineChunkHtmlPlugin {
  22. constructor(htmlPlugin, patterns) {
  23. this.htmlPlugin = htmlPlugin;
  24. this.patterns = patterns;
  25. }
  26. getInlinedTag(publicPath, assets, tag) {
  27. if (
  28. (tag.tagName !== 'script' || !(tag.attributes && tag.attributes.src)) &&
  29. (tag.tagName !== 'link' || !(tag.attributes && tag.attributes.href))
  30. ) {
  31. return tag;
  32. }
  33. let chunkName = tag.tagName === 'link' ? tag.attributes.href : tag.attributes.src;
  34. if (publicPath) {
  35. chunkName = chunkName.replace(publicPath, '');
  36. }
  37. if (!this.patterns.some(pattern => chunkName.match(pattern))) {
  38. return tag;
  39. }
  40. const asset = assets[chunkName];
  41. if (asset == null) {
  42. return tag;
  43. }
  44. return { tagName: tag.tagName === 'link' ? 'style' : tag.tagName, innerHTML: asset.source(), closeTag: true };
  45. }
  46. apply(compiler) {
  47. let publicPath = compiler.options.output.publicPath || '';
  48. if (publicPath && !publicPath.endsWith('/')) {
  49. publicPath += '/';
  50. }
  51. compiler.hooks.compilation.tap('InlineChunkHtmlPlugin', compilation => {
  52. const getInlinedTagFn = tag => this.getInlinedTag(publicPath, compilation.assets, tag);
  53. this.htmlPlugin.getHooks(compilation).alterAssetTagGroups.tap('InlineChunkHtmlPlugin', assets => {
  54. assets.headTags = assets.headTags.map(getInlinedTagFn);
  55. assets.bodyTags = assets.bodyTags.map(getInlinedTagFn);
  56. });
  57. });
  58. }
  59. }
  60. module.exports =
  61. /**
  62. * @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; optimizeImages?: boolean; } | undefined } env
  63. * @param {{ mode: 'production' | 'development' | 'none' | undefined; }} argv
  64. * @returns { WebpackConfig[] }
  65. */
  66. function (env, argv) {
  67. const mode = argv.mode || 'none';
  68. env = {
  69. analyzeBundle: false,
  70. analyzeDeps: false,
  71. optimizeImages: mode === 'production',
  72. ...env,
  73. };
  74. if (env.analyzeBundle || env.analyzeDeps) {
  75. env.optimizeImages = false;
  76. } else if (!env.optimizeImages && !fs.existsSync(path.join(__dirname, 'images/settings'))) {
  77. env.optimizeImages = true;
  78. }
  79. return [getExtensionConfig(mode, env), getWebviewsConfig(mode, env)];
  80. };
  81. /**
  82. * @param { 'production' | 'development' | 'none' } mode
  83. * @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; optimizeImages?: boolean; }} env
  84. * @returns { WebpackConfig }
  85. */
  86. function getExtensionConfig(mode, env) {
  87. /**
  88. * @type WebpackConfig['plugins']
  89. */
  90. const plugins = [
  91. new CleanPlugin({ cleanOnceBeforeBuildPatterns: ['**/*', '!**/webviews/**'] }),
  92. new ForkTsCheckerPlugin({
  93. async: false,
  94. eslint: { enabled: true, files: 'src/**/*.ts', options: { cache: true } },
  95. formatter: 'basic',
  96. }),
  97. ];
  98. if (env.analyzeDeps) {
  99. plugins.push(
  100. new CircularDependencyPlugin({
  101. cwd: __dirname,
  102. exclude: /node_modules/,
  103. failOnError: false,
  104. onDetected: function ({ module: _webpackModuleRecord, paths, compilation }) {
  105. if (paths.some(p => p.includes('container.ts'))) return;
  106. compilation.warnings.push(new Error(paths.join(' -> ')));
  107. },
  108. }),
  109. );
  110. }
  111. if (env.analyzeBundle) {
  112. plugins.push(new BundleAnalyzerPlugin());
  113. }
  114. return {
  115. name: 'extension',
  116. entry: './src/extension.ts',
  117. mode: mode,
  118. target: 'node',
  119. node: {
  120. __dirname: false,
  121. },
  122. devtool: 'source-map',
  123. output: {
  124. path: path.join(__dirname, 'dist'),
  125. libraryTarget: 'commonjs2',
  126. filename: 'gitlens.js',
  127. chunkFilename: 'feature-[name].js',
  128. },
  129. optimization: {
  130. minimizer: [
  131. new TerserPlugin({
  132. parallel: true,
  133. terserOptions: {
  134. ecma: 8,
  135. // Keep the class names otherwise @log won't provide a useful name
  136. keep_classnames: true,
  137. module: true,
  138. },
  139. }),
  140. ],
  141. splitChunks: {
  142. cacheGroups: {
  143. defaultVendors: false,
  144. },
  145. },
  146. },
  147. externals: {
  148. vscode: 'commonjs vscode',
  149. },
  150. module: {
  151. rules: [
  152. {
  153. exclude: /\.d\.ts$/,
  154. include: path.join(__dirname, 'src'),
  155. test: /\.tsx?$/,
  156. use: {
  157. loader: 'ts-loader',
  158. options: {
  159. experimentalWatchApi: true,
  160. transpileOnly: true,
  161. },
  162. },
  163. },
  164. ],
  165. },
  166. resolve: {
  167. alias: {
  168. 'universal-user-agent': path.join(__dirname, 'node_modules/universal-user-agent/dist-node/index.js'),
  169. },
  170. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  171. symlinks: false,
  172. },
  173. plugins: plugins,
  174. stats: {
  175. preset: 'errors-warnings',
  176. assets: true,
  177. colors: true,
  178. env: true,
  179. errorsCount: true,
  180. warningsCount: true,
  181. timings: true,
  182. },
  183. };
  184. }
  185. /**
  186. * @param { 'production' | 'development' | 'none' } mode
  187. * @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; optimizeImages?: boolean; }} env
  188. * @returns { WebpackConfig }
  189. */
  190. function getWebviewsConfig(mode, env) {
  191. const basePath = path.join(__dirname, 'src/webviews/apps');
  192. const clean = ['**/*'];
  193. if (env.optimizeImages) {
  194. console.log('Optimizing images (src/webviews/apps/images/settings/*.png)...');
  195. clean.push(path.join(__dirname, 'images/settings/*'));
  196. }
  197. const cspPolicy = {
  198. 'default-src': "'none'",
  199. 'img-src': ['#{cspSource}', 'https:', 'data:'],
  200. 'script-src': ['#{cspSource}', "'nonce-Z2l0bGVucy1ib290c3RyYXA='"],
  201. 'style-src': ['#{cspSource}'],
  202. };
  203. if (mode !== 'production') {
  204. cspPolicy['script-src'].push("'unsafe-eval'");
  205. }
  206. /**
  207. * @type WebpackConfig['plugins']
  208. */
  209. const plugins = [
  210. new CleanPlugin({ cleanOnceBeforeBuildPatterns: clean, cleanStaleWebpackAssets: false }),
  211. new ForkTsCheckerPlugin({
  212. async: false,
  213. eslint: {
  214. enabled: true,
  215. files: path.join(basePath, '**/*.ts'),
  216. options: { cache: true },
  217. },
  218. formatter: 'basic',
  219. typescript: {
  220. configFile: path.join(basePath, 'tsconfig.json'),
  221. },
  222. }),
  223. new MiniCssExtractPlugin({
  224. filename: '[name].css',
  225. }),
  226. new HtmlPlugin({
  227. template: 'rebase/rebase.html',
  228. chunks: ['rebase'],
  229. filename: path.join(__dirname, 'dist/webviews/rebase.html'),
  230. inject: true,
  231. inlineSource: mode === 'production' ? '.css$' : undefined,
  232. cspPlugin: {
  233. enabled: true,
  234. policy: cspPolicy,
  235. nonceEnabled: {
  236. 'script-src': true,
  237. 'style-src': true,
  238. },
  239. },
  240. minify:
  241. mode === 'production'
  242. ? {
  243. removeComments: true,
  244. collapseWhitespace: true,
  245. removeRedundantAttributes: false,
  246. useShortDoctype: true,
  247. removeEmptyAttributes: true,
  248. removeStyleLinkTypeAttributes: true,
  249. keepClosingSlash: true,
  250. minifyCSS: true,
  251. }
  252. : false,
  253. }),
  254. new HtmlPlugin({
  255. template: 'settings/settings.html',
  256. chunks: ['settings'],
  257. filename: path.join(__dirname, 'dist/webviews/settings.html'),
  258. inject: true,
  259. inlineSource: mode === 'production' ? '.css$' : undefined,
  260. cspPlugin: {
  261. enabled: true,
  262. policy: cspPolicy,
  263. nonceEnabled: {
  264. 'script-src': true,
  265. 'style-src': true,
  266. },
  267. },
  268. minify:
  269. mode === 'production'
  270. ? {
  271. removeComments: true,
  272. collapseWhitespace: true,
  273. removeRedundantAttributes: false,
  274. useShortDoctype: true,
  275. removeEmptyAttributes: true,
  276. removeStyleLinkTypeAttributes: true,
  277. keepClosingSlash: true,
  278. minifyCSS: true,
  279. }
  280. : false,
  281. }),
  282. new HtmlPlugin({
  283. template: 'welcome/welcome.html',
  284. chunks: ['welcome'],
  285. filename: path.join(__dirname, 'dist/webviews/welcome.html'),
  286. inject: true,
  287. inlineSource: mode === 'production' ? '.css$' : undefined,
  288. cspPlugin: {
  289. enabled: true,
  290. policy: cspPolicy,
  291. nonceEnabled: {
  292. 'script-src': true,
  293. 'style-src': true,
  294. },
  295. },
  296. minify:
  297. mode === 'production'
  298. ? {
  299. removeComments: true,
  300. collapseWhitespace: true,
  301. removeRedundantAttributes: false,
  302. useShortDoctype: true,
  303. removeEmptyAttributes: true,
  304. removeStyleLinkTypeAttributes: true,
  305. keepClosingSlash: true,
  306. minifyCSS: true,
  307. }
  308. : false,
  309. }),
  310. new HtmlSkipAssetsPlugin({}),
  311. new CspHtmlPlugin(),
  312. new ImageminPlugin({
  313. disable: !env.optimizeImages,
  314. externalImages: {
  315. context: path.join(basePath, 'images'),
  316. sources: glob.sync(path.join(basePath, 'images/settings/*.png')),
  317. destination: path.join(__dirname, 'images'),
  318. },
  319. cacheFolder: path.join(__dirname, 'node_modules', '.cache', 'imagemin-webpack-plugin'),
  320. gifsicle: null,
  321. jpegtran: null,
  322. optipng: null,
  323. pngquant: {
  324. quality: '85-100',
  325. speed: mode === 'production' ? 1 : 10,
  326. },
  327. svgo: null,
  328. }),
  329. new InlineChunkHtmlPlugin(HtmlPlugin, mode === 'production' ? ['\\.css$'] : []),
  330. ];
  331. return {
  332. name: 'webviews',
  333. context: basePath,
  334. entry: {
  335. rebase: './rebase/rebase.ts',
  336. settings: './settings/settings.ts',
  337. welcome: './welcome/welcome.ts',
  338. },
  339. mode: mode,
  340. target: 'web',
  341. devtool: mode === 'production' ? undefined : 'eval-source-map',
  342. output: {
  343. filename: '[name].js',
  344. path: path.join(__dirname, 'dist/webviews'),
  345. publicPath: '#{root}/dist/webviews/',
  346. },
  347. module: {
  348. rules: [
  349. {
  350. exclude: /\.d\.ts$/,
  351. include: path.join(__dirname, 'src'),
  352. test: /\.tsx?$/,
  353. use: {
  354. loader: 'ts-loader',
  355. options: {
  356. configFile: path.join(basePath, 'tsconfig.json'),
  357. experimentalWatchApi: true,
  358. transpileOnly: true,
  359. },
  360. },
  361. },
  362. {
  363. test: /\.scss$/,
  364. use: [
  365. {
  366. loader: MiniCssExtractPlugin.loader,
  367. },
  368. {
  369. loader: 'css-loader',
  370. options: {
  371. sourceMap: true,
  372. url: false,
  373. },
  374. },
  375. {
  376. loader: 'sass-loader',
  377. options: {
  378. sourceMap: true,
  379. },
  380. },
  381. ],
  382. exclude: /node_modules/,
  383. },
  384. ],
  385. },
  386. resolve: {
  387. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  388. modules: [basePath, 'node_modules'],
  389. symlinks: false,
  390. },
  391. plugins: plugins,
  392. stats: {
  393. preset: 'errors-warnings',
  394. assets: true,
  395. colors: true,
  396. env: true,
  397. errorsCount: true,
  398. warningsCount: true,
  399. timings: true,
  400. },
  401. };
  402. }