Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

427 wiersze
11 KiB

5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
5 lat temu
  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. libraryTarget: 'commonjs2',
  125. filename: 'gitlens.js',
  126. chunkFilename: 'feature-[name].js',
  127. },
  128. optimization: {
  129. minimizer: [
  130. new TerserPlugin({
  131. cache: true,
  132. parallel: true,
  133. sourceMap: true,
  134. terserOptions: {
  135. ecma: 8,
  136. // Keep the class names otherwise @log won't provide a useful name
  137. keep_classnames: true,
  138. module: true,
  139. },
  140. }),
  141. ],
  142. splitChunks: {
  143. cacheGroups: {
  144. defaultVendors: false,
  145. },
  146. },
  147. },
  148. externals: {
  149. vscode: 'commonjs vscode',
  150. },
  151. module: {
  152. rules: [
  153. {
  154. exclude: /\.d\.ts$/,
  155. include: path.join(__dirname, 'src'),
  156. test: /\.tsx?$/,
  157. use: {
  158. loader: 'ts-loader',
  159. options: {
  160. experimentalWatchApi: true,
  161. transpileOnly: true,
  162. },
  163. },
  164. },
  165. ],
  166. },
  167. resolve: {
  168. alias: {
  169. 'universal-user-agent': path.join(__dirname, 'node_modules/universal-user-agent/dist-node/index.js'),
  170. },
  171. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  172. symlinks: false,
  173. },
  174. plugins: plugins,
  175. // stats: {
  176. // all: false,
  177. // assets: true,
  178. // builtAt: true,
  179. // env: true,
  180. // errors: true,
  181. // timings: true,
  182. // warnings: true,
  183. // },
  184. };
  185. }
  186. /**
  187. * @param { 'production' | 'development' | 'none' } mode
  188. * @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; optimizeImages?: boolean; }} env
  189. * @returns { WebpackConfig }
  190. */
  191. function getWebviewsConfig(mode, env) {
  192. const basePath = path.join(__dirname, 'src/webviews/apps');
  193. const clean = ['**/*'];
  194. if (env.optimizeImages) {
  195. console.log('Optimizing images (src/webviews/apps/images/settings/*.png)...');
  196. clean.push(path.join(__dirname, 'images/settings/*'));
  197. }
  198. const cspPolicy = {
  199. 'default-src': "'none'",
  200. 'img-src': ['#{cspSource}', 'https:', 'data:'],
  201. 'script-src': ['#{cspSource}', "'nonce-Z2l0bGVucy1ib290c3RyYXA='"],
  202. 'style-src': ['#{cspSource}'],
  203. };
  204. if (mode !== 'production') {
  205. cspPolicy['script-src'].push("'unsafe-eval'");
  206. }
  207. /**
  208. * @type WebpackConfig['plugins']
  209. */
  210. const plugins = [
  211. new CleanPlugin({ cleanOnceBeforeBuildPatterns: clean, cleanStaleWebpackAssets: false }),
  212. new ForkTsCheckerPlugin({
  213. async: false,
  214. eslint: {
  215. enabled: true,
  216. files: path.join(basePath, '**/*.ts'),
  217. options: { cache: true },
  218. },
  219. formatter: 'basic',
  220. typescript: {
  221. configFile: path.join(basePath, 'tsconfig.json'),
  222. },
  223. }),
  224. new MiniCssExtractPlugin({
  225. filename: '[name].css',
  226. }),
  227. new HtmlPlugin({
  228. template: 'rebase/rebase.html',
  229. chunks: ['rebase'],
  230. filename: path.join(__dirname, 'dist/webviews/rebase.html'),
  231. inject: true,
  232. inlineSource: mode === 'production' ? '.css$' : undefined,
  233. cspPlugin: {
  234. enabled: true,
  235. policy: cspPolicy,
  236. nonceEnabled: {
  237. 'script-src': true,
  238. 'style-src': true,
  239. },
  240. },
  241. minify:
  242. mode === 'production'
  243. ? {
  244. removeComments: true,
  245. collapseWhitespace: true,
  246. removeRedundantAttributes: false,
  247. useShortDoctype: true,
  248. removeEmptyAttributes: true,
  249. removeStyleLinkTypeAttributes: true,
  250. keepClosingSlash: true,
  251. minifyCSS: true,
  252. }
  253. : false,
  254. }),
  255. new HtmlPlugin({
  256. template: 'settings/settings.html',
  257. chunks: ['settings'],
  258. filename: path.join(__dirname, 'dist/webviews/settings.html'),
  259. inject: true,
  260. inlineSource: mode === 'production' ? '.css$' : undefined,
  261. cspPlugin: {
  262. enabled: true,
  263. policy: cspPolicy,
  264. nonceEnabled: {
  265. 'script-src': true,
  266. 'style-src': true,
  267. },
  268. },
  269. minify:
  270. mode === 'production'
  271. ? {
  272. removeComments: true,
  273. collapseWhitespace: true,
  274. removeRedundantAttributes: false,
  275. useShortDoctype: true,
  276. removeEmptyAttributes: true,
  277. removeStyleLinkTypeAttributes: true,
  278. keepClosingSlash: true,
  279. minifyCSS: true,
  280. }
  281. : false,
  282. }),
  283. new HtmlPlugin({
  284. template: 'welcome/welcome.html',
  285. chunks: ['welcome'],
  286. filename: path.join(__dirname, 'dist/webviews/welcome.html'),
  287. inject: true,
  288. inlineSource: mode === 'production' ? '.css$' : undefined,
  289. cspPlugin: {
  290. enabled: true,
  291. policy: cspPolicy,
  292. nonceEnabled: {
  293. 'script-src': true,
  294. 'style-src': true,
  295. },
  296. },
  297. minify:
  298. mode === 'production'
  299. ? {
  300. removeComments: true,
  301. collapseWhitespace: true,
  302. removeRedundantAttributes: false,
  303. useShortDoctype: true,
  304. removeEmptyAttributes: true,
  305. removeStyleLinkTypeAttributes: true,
  306. keepClosingSlash: true,
  307. minifyCSS: true,
  308. }
  309. : false,
  310. }),
  311. new HtmlSkipAssetsPlugin({}),
  312. new CspHtmlPlugin(),
  313. new ImageminPlugin({
  314. disable: !env.optimizeImages,
  315. externalImages: {
  316. context: path.join(basePath, 'images'),
  317. sources: glob.sync(path.join(basePath, 'images/settings/*.png')),
  318. destination: path.join(__dirname, 'images'),
  319. },
  320. cacheFolder: path.join(__dirname, 'node_modules', '.cache', 'imagemin-webpack-plugin'),
  321. gifsicle: null,
  322. jpegtran: null,
  323. optipng: null,
  324. pngquant: {
  325. quality: '85-100',
  326. speed: mode === 'production' ? 1 : 10,
  327. },
  328. svgo: null,
  329. }),
  330. new InlineChunkHtmlPlugin(HtmlPlugin, mode === 'production' ? ['\\.css$'] : []),
  331. ];
  332. return {
  333. name: 'webviews',
  334. context: basePath,
  335. entry: {
  336. rebase: './rebase/rebase.ts',
  337. settings: './settings/settings.ts',
  338. welcome: './welcome/welcome.ts',
  339. },
  340. mode: mode,
  341. target: 'web',
  342. devtool: mode === 'production' ? undefined : 'eval-source-map',
  343. output: {
  344. filename: '[name].js',
  345. path: path.join(__dirname, 'dist/webviews'),
  346. publicPath: '#{root}/dist/webviews/',
  347. },
  348. module: {
  349. rules: [
  350. {
  351. exclude: /\.d\.ts$/,
  352. include: path.join(__dirname, 'src'),
  353. test: /\.tsx?$/,
  354. use: {
  355. loader: 'ts-loader',
  356. options: {
  357. configFile: path.join(basePath, 'tsconfig.json'),
  358. experimentalWatchApi: true,
  359. transpileOnly: true,
  360. },
  361. },
  362. },
  363. {
  364. test: /\.scss$/,
  365. use: [
  366. {
  367. loader: MiniCssExtractPlugin.loader,
  368. },
  369. {
  370. loader: 'css-loader',
  371. options: {
  372. sourceMap: true,
  373. url: false,
  374. },
  375. },
  376. {
  377. loader: 'sass-loader',
  378. options: {
  379. sourceMap: true,
  380. },
  381. },
  382. ],
  383. exclude: /node_modules/,
  384. },
  385. ],
  386. },
  387. resolve: {
  388. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  389. modules: [basePath, 'node_modules'],
  390. symlinks: false,
  391. },
  392. plugins: plugins,
  393. // stats: {
  394. // all: false,
  395. // assets: true,
  396. // builtAt: true,
  397. // env: true,
  398. // errors: true,
  399. // timings: true,
  400. // warnings: true,
  401. // },
  402. };
  403. }