Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

170 рядки
4.5 KiB

  1. 'use strict';
  2. //@ts-check
  3. /** @typedef {import('webpack').Configuration} WebpackConfig **/
  4. const { spawnSync } = require('child_process');
  5. var fs = require('fs');
  6. var glob = require('glob');
  7. const path = require('path');
  8. const { CleanWebpackPlugin: CleanPlugin } = require('clean-webpack-plugin');
  9. const esbuild = require('esbuild');
  10. const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
  11. const JSON5 = require('json5');
  12. const nodeExternals = require('webpack-node-externals');
  13. module.exports =
  14. /**
  15. * @param {{ esbuild?: boolean } | undefined } env
  16. * @param {{ mode: 'production' | 'development' | 'none' | undefined }} argv
  17. * @returns { WebpackConfig[] }
  18. */
  19. function (env, argv) {
  20. const mode = argv.mode || 'development';
  21. env = {
  22. esbuild: true,
  23. ...env,
  24. };
  25. return [getExtensionConfig('node', mode, env) /*, getExtensionConfig('webworker', mode, env)*/];
  26. };
  27. /**
  28. * @param { 'node' | 'webworker' } target
  29. * @param { 'production' | 'development' | 'none' } mode
  30. * @param {{ analyzeBundle?: boolean; analyzeDeps?: boolean; esbuild?: boolean; squoosh?: boolean } | undefined } env
  31. * @returns { WebpackConfig }
  32. */
  33. function getExtensionConfig(target, mode, env) {
  34. /**
  35. * @type WebpackConfig['plugins'] | any
  36. */
  37. const plugins = [
  38. new CleanPlugin({ cleanOnceBeforeBuildPatterns: ['out/**'] }),
  39. new ForkTsCheckerPlugin({
  40. async: false,
  41. // eslint: {
  42. // enabled: true,
  43. // files: 'src/**/*.ts',
  44. // options: {
  45. // // cache: true,
  46. // cacheLocation: path.join(
  47. // __dirname,
  48. // target === 'webworker' ? '.eslintcache.browser' : '.eslintcache',
  49. // ),
  50. // overrideConfigFile: path.join(
  51. // __dirname,
  52. // target === 'webworker' ? '.eslintrc.browser.json' : '.eslintrc.json',
  53. // ),
  54. // },
  55. // },
  56. formatter: 'basic',
  57. typescript: {
  58. configFile: path.join(
  59. __dirname,
  60. target === 'webworker' ? 'tsconfig.test.browser.json' : 'tsconfig.test.json',
  61. ),
  62. },
  63. }),
  64. ];
  65. return {
  66. name: `tests:${target}`,
  67. entry: {
  68. runTest: './src/test/runTest.ts',
  69. 'suite/index': './src/test/suite/index.ts',
  70. ...glob.sync('./src/test/suite/**/*.test.ts').reduce(function (obj, e) {
  71. obj['suite/' + path.parse(e).name] = e;
  72. return obj;
  73. }, {}),
  74. },
  75. mode: mode,
  76. target: target,
  77. devtool: 'source-map',
  78. output: {
  79. path:
  80. target === 'webworker'
  81. ? path.join(__dirname, 'out', 'test', 'browser')
  82. : path.join(__dirname, 'out', 'test'),
  83. filename: '[name].js',
  84. sourceMapFilename: '[name].js.map',
  85. libraryTarget: 'commonjs2',
  86. },
  87. externals: [{ vscode: 'commonjs vscode' }, nodeExternals()],
  88. module: {
  89. rules: [
  90. {
  91. exclude: /\.d\.ts$/,
  92. include: path.join(__dirname, 'src'),
  93. test: /\.tsx?$/,
  94. use: env.esbuild
  95. ? {
  96. loader: 'esbuild-loader',
  97. options: {
  98. implementation: esbuild,
  99. loader: 'ts',
  100. target: ['es2020', 'chrome91', 'node14.16'],
  101. tsconfigRaw: resolveTSConfig(
  102. path.join(
  103. __dirname,
  104. target === 'webworker'
  105. ? 'tsconfig.test.browser.json'
  106. : 'tsconfig.test.json',
  107. ),
  108. ),
  109. },
  110. }
  111. : {
  112. loader: 'ts-loader',
  113. options: {
  114. configFile: path.join(
  115. __dirname,
  116. target === 'webworker' ? 'tsconfig.test.browser.json' : 'tsconfig.test.json',
  117. ),
  118. experimentalWatchApi: true,
  119. transpileOnly: true,
  120. },
  121. },
  122. },
  123. ],
  124. },
  125. resolve: {
  126. alias: { '@env': path.resolve(__dirname, 'src', 'env', target === 'webworker' ? 'browser' : target) },
  127. fallback: target === 'webworker' ? { path: require.resolve('path-browserify') } : undefined,
  128. mainFields: target === 'webworker' ? ['browser', 'module', 'main'] : ['module', 'main'],
  129. extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
  130. },
  131. plugins: plugins,
  132. infrastructureLogging: {
  133. level: 'log', // enables logging required for problem matchers
  134. },
  135. stats: {
  136. preset: 'errors-warnings',
  137. assets: true,
  138. colors: true,
  139. env: true,
  140. errorsCount: true,
  141. warningsCount: true,
  142. timings: true,
  143. },
  144. };
  145. }
  146. /**
  147. * @param { string } configFile
  148. * @returns { string }
  149. */
  150. function resolveTSConfig(configFile) {
  151. const result = spawnSync('yarn', ['tsc', `-p ${configFile}`, '--showConfig'], {
  152. cwd: __dirname,
  153. encoding: 'utf8',
  154. shell: true,
  155. });
  156. const data = result.stdout;
  157. const start = data.indexOf('{');
  158. const end = data.lastIndexOf('}') + 1;
  159. const json = JSON5.parse(data.substring(start, end));
  160. return json;
  161. }