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.

284 lines
9.3 KiB

  1. 'use strict';
  2. const fs = require('fs');
  3. const glob = require('glob');
  4. const path = require('path');
  5. const webpack = require('webpack');
  6. const CleanPlugin = require('clean-webpack-plugin');
  7. const FileManagerPlugin = require('filemanager-webpack-plugin');
  8. const HtmlInlineSourcePlugin = require('html-webpack-inline-source-plugin');
  9. const HtmlPlugin = require('html-webpack-plugin');
  10. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  11. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  12. const TerserPlugin = require('terser-webpack-plugin');
  13. module.exports = function(env, argv) {
  14. env = env || {};
  15. env.production = Boolean(env.production);
  16. env.optimizeImages = env.production || Boolean(env.optimizeImages);
  17. env.copyClipboardyFallbacks = env.production || Boolean(env.copyClipboardyFallbacks);
  18. if (!env.optimizeImages && !fs.existsSync(path.resolve(__dirname, 'images/settings'))) {
  19. env.optimizeImages = true;
  20. }
  21. if (!env.copyClipboardyFallbacks && !fs.existsSync(path.resolve(__dirname, 'fallbacks'))) {
  22. env.copyClipboardyFallbacks = true;
  23. }
  24. return [getExtensionConfig(env), getUIConfig(env)];
  25. };
  26. function getExtensionConfig(env) {
  27. const clean = ['dist'];
  28. if (env.copyClipboardyFallbacks) {
  29. clean.push('fallbacks');
  30. }
  31. const plugins = [new CleanPlugin(clean, { verbose: false }), new webpack.IgnorePlugin(/^spawn-sync$/)];
  32. if (env.copyClipboardyFallbacks) {
  33. plugins.push(
  34. // @ts-ignore
  35. new FileManagerPlugin({
  36. onEnd: [
  37. {
  38. copy: [
  39. {
  40. source: path.resolve(__dirname, 'node_modules/clipboardy/fallbacks'),
  41. destination: 'fallbacks/'
  42. }
  43. ]
  44. }
  45. ]
  46. })
  47. );
  48. }
  49. return {
  50. name: 'extension',
  51. entry: './src/extension.ts',
  52. mode: env.production ? 'production' : 'development',
  53. target: 'node',
  54. node: {
  55. __dirname: false
  56. },
  57. devtool: 'source-map', //!env.production ? 'source-map' : undefined,
  58. output: {
  59. libraryTarget: 'commonjs2',
  60. filename: 'extension.js',
  61. devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]'
  62. },
  63. optimization: {
  64. minimizer: [
  65. new TerserPlugin({
  66. cache: true,
  67. parallel: true,
  68. sourceMap: true, // !env.production,
  69. terserOptions: {
  70. ecma: 8,
  71. // Keep the class names otherwise @log won't provide a useful name
  72. keep_classnames: true,
  73. module: true
  74. }
  75. })
  76. ]
  77. },
  78. externals: {
  79. vscode: 'commonjs vscode'
  80. },
  81. module: {
  82. rules: [
  83. {
  84. test: /\.ts$/,
  85. enforce: 'pre',
  86. use: 'tslint-loader',
  87. exclude: /node_modules/
  88. },
  89. {
  90. test: /\.tsx?$/,
  91. use: 'ts-loader',
  92. exclude: /node_modules|\.d\.ts$/
  93. }
  94. ]
  95. },
  96. resolve: {
  97. extensions: ['.ts', '.tsx', '.js', '.jsx']
  98. },
  99. plugins: plugins,
  100. stats: {
  101. all: false,
  102. assets: true,
  103. builtAt: true,
  104. env: true,
  105. errors: true,
  106. timings: true,
  107. warnings: true
  108. }
  109. };
  110. }
  111. function getUIConfig(env) {
  112. const clean = ['settings.html', 'welcome.html'];
  113. if (env.optimizeImages) {
  114. console.log('Optimizing images (src/ui/images/settings/*.png)...');
  115. clean.push('images/settings');
  116. }
  117. const plugins = [
  118. new CleanPlugin(clean, { verbose: false }),
  119. new MiniCssExtractPlugin({
  120. filename: '[name].css'
  121. }),
  122. new HtmlPlugin({
  123. excludeAssets: [/.*\.main\.js/],
  124. excludeChunks: ['welcome'],
  125. template: 'settings/index.html',
  126. filename: path.resolve(__dirname, 'settings.html'),
  127. inject: true,
  128. inlineSource: env.production ? '.(js|css)$' : undefined,
  129. minify: env.production
  130. ? {
  131. removeComments: true,
  132. collapseWhitespace: true,
  133. removeRedundantAttributes: true,
  134. useShortDoctype: true,
  135. removeEmptyAttributes: true,
  136. removeStyleLinkTypeAttributes: true,
  137. keepClosingSlash: true,
  138. minifyCSS: true
  139. }
  140. : false
  141. }),
  142. new HtmlPlugin({
  143. excludeAssets: [/.*\.main\.js/],
  144. excludeChunks: ['settings'],
  145. template: 'welcome/index.html',
  146. filename: path.resolve(__dirname, 'welcome.html'),
  147. inject: true,
  148. inlineSource: env.production ? '.(js|css)$' : undefined,
  149. minify: env.production
  150. ? {
  151. removeComments: true,
  152. collapseWhitespace: true,
  153. removeRedundantAttributes: true,
  154. useShortDoctype: true,
  155. removeEmptyAttributes: true,
  156. removeStyleLinkTypeAttributes: true,
  157. keepClosingSlash: true,
  158. minifyCSS: true
  159. }
  160. : false
  161. }),
  162. new HtmlInlineSourcePlugin(),
  163. new ImageminPlugin({
  164. disable: !env.optimizeImages,
  165. externalImages: {
  166. context: path.resolve(__dirname, 'src/ui/images'),
  167. sources: glob.sync('src/ui/images/settings/*.png'),
  168. destination: path.resolve(__dirname, 'images')
  169. },
  170. cacheFolder: path.resolve(__dirname, '.cache-images'),
  171. gifsicle: null,
  172. jpegtran: null,
  173. optipng: null,
  174. pngquant: {
  175. quality: '85-100',
  176. speed: env.production ? 1 : 10
  177. },
  178. svgo: null
  179. })
  180. ];
  181. return {
  182. name: 'ui',
  183. context: path.resolve(__dirname, 'src/ui'),
  184. // This is ugly having main.scss on both bundles, but if it is added separately it will generate a js bundle :(
  185. entry: {
  186. settings: ['./settings/index.ts', './scss/main.scss'],
  187. welcome: ['./welcome/index.ts', './scss/main.scss']
  188. // main: ['./scss/main.scss']
  189. },
  190. mode: env.production ? 'production' : 'development',
  191. devtool: !env.production ? 'source-map' : undefined,
  192. output: {
  193. filename: '[name].js',
  194. path: path.resolve(__dirname, 'dist/ui'),
  195. publicPath: '{{root}}/dist/ui/'
  196. },
  197. optimization: {
  198. splitChunks: {
  199. cacheGroups: {
  200. styles: {
  201. name: 'styles',
  202. test: /\.css$/,
  203. chunks: 'all',
  204. enforce: true
  205. }
  206. }
  207. }
  208. },
  209. module: {
  210. rules: [
  211. {
  212. test: /\.ts$/,
  213. enforce: 'pre',
  214. use: [
  215. {
  216. loader: 'tslint-loader',
  217. options: {
  218. tsConfigFile: 'ui.tsconfig.json'
  219. }
  220. }
  221. ],
  222. exclude: /node_modules/
  223. },
  224. {
  225. test: /\.tsx?$/,
  226. use: {
  227. loader: 'ts-loader',
  228. options: {
  229. configFile: 'ui.tsconfig.json'
  230. }
  231. },
  232. exclude: /node_modules|\.d\.ts$/
  233. },
  234. {
  235. test: /\.scss$/,
  236. use: [
  237. {
  238. loader: MiniCssExtractPlugin.loader
  239. },
  240. {
  241. loader: 'css-loader',
  242. options: {
  243. sourceMap: !env.production,
  244. url: false
  245. }
  246. },
  247. {
  248. loader: 'sass-loader',
  249. options: {
  250. sourceMap: !env.production
  251. }
  252. }
  253. ],
  254. exclude: /node_modules/
  255. }
  256. ]
  257. },
  258. resolve: {
  259. extensions: ['.ts', '.tsx', '.js', '.jsx'],
  260. modules: [path.resolve(__dirname, 'src/ui'), 'node_modules']
  261. },
  262. plugins: plugins,
  263. stats: {
  264. all: false,
  265. assets: true,
  266. builtAt: true,
  267. env: true,
  268. errors: true,
  269. timings: true,
  270. warnings: true
  271. }
  272. };
  273. }