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.

212 rivejä
7.2 KiB

6 vuotta sitten
  1. 'use strict';
  2. const glob = require('glob');
  3. const nodeExternals = require('webpack-node-externals');
  4. const path = require('path');
  5. const CleanPlugin = require('clean-webpack-plugin');
  6. const HtmlInlineSourcePlugin = require('html-webpack-inline-source-plugin');
  7. const HtmlPlugin = require('html-webpack-plugin');
  8. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  9. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  10. module.exports = function(env, argv) {
  11. env = env || {};
  12. env.production = !!env.production;
  13. env.optimizeImages = env.production || !!env.optimizeImages;
  14. return [getExtensionConfig(env), getUIConfig(env)];
  15. };
  16. function getExtensionConfig(env) {
  17. const plugins = [new CleanPlugin(['out'])];
  18. return {
  19. name: 'extension',
  20. entry: './src/extension.ts',
  21. mode: env.production ? 'production' : 'development',
  22. target: 'node',
  23. devtool: !env.production ? 'eval-source-map' : undefined,
  24. output: {
  25. libraryTarget: 'commonjs2',
  26. filename: 'extension.js',
  27. path: path.resolve(__dirname, 'out'),
  28. devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]'
  29. },
  30. resolve: {
  31. extensions: ['.tsx', '.ts', '.js']
  32. },
  33. externals: [nodeExternals()],
  34. module: {
  35. rules: [
  36. {
  37. test: /\.ts$/,
  38. enforce: 'pre',
  39. use: 'tslint-loader'
  40. },
  41. {
  42. test: /\.tsx?$/,
  43. use: 'ts-loader',
  44. exclude: /node_modules/
  45. }
  46. ]
  47. },
  48. plugins: plugins,
  49. stats: { all: false, assets: true, builtAt: true, errors: true, timings: true, warnings: true }
  50. };
  51. }
  52. function getUIConfig(env) {
  53. const clean = ['settings.html', 'welcome.html'];
  54. if (env.optimizeImages) {
  55. console.log('Optimizing images (src/ui/images/settings/*.png)...');
  56. clean.push('images/settings');
  57. }
  58. const plugins = [
  59. new CleanPlugin(clean),
  60. new MiniCssExtractPlugin({
  61. filename: '[name].css'
  62. }),
  63. new HtmlPlugin({
  64. excludeAssets: [/.*\.main\.js/],
  65. excludeChunks: ['welcome'],
  66. template: 'settings/index.html',
  67. filename: path.resolve(__dirname, 'settings.html'),
  68. inject: true,
  69. inlineSource: env.production ? '.(js|css)$' : undefined,
  70. // inlineSource: '.(js|css)$',
  71. minify: env.production
  72. ? {
  73. removeComments: true,
  74. collapseWhitespace: true,
  75. removeRedundantAttributes: true,
  76. useShortDoctype: true,
  77. removeEmptyAttributes: true,
  78. removeStyleLinkTypeAttributes: true,
  79. keepClosingSlash: true
  80. }
  81. : false
  82. }),
  83. new HtmlPlugin({
  84. excludeAssets: [/.*\.main\.js/],
  85. excludeChunks: ['settings'],
  86. template: 'welcome/index.html',
  87. filename: path.resolve(__dirname, 'welcome.html'),
  88. inject: true,
  89. inlineSource: env.production ? '.(js|css)$' : undefined,
  90. // inlineSource: '.(js|css)$',
  91. minify: env.production
  92. ? {
  93. removeComments: true,
  94. collapseWhitespace: true,
  95. removeRedundantAttributes: true,
  96. useShortDoctype: true,
  97. removeEmptyAttributes: true,
  98. removeStyleLinkTypeAttributes: true,
  99. keepClosingSlash: true
  100. }
  101. : false
  102. }),
  103. new HtmlInlineSourcePlugin(),
  104. new ImageminPlugin({
  105. disable: !env.optimizeImages,
  106. externalImages: {
  107. context: path.resolve(__dirname, 'src/ui/images'),
  108. sources: glob.sync('src/ui/images/settings/*.png'),
  109. destination: path.resolve(__dirname, 'images')
  110. },
  111. gifsicle: null,
  112. jpegtran: null,
  113. optipng: null,
  114. pngquant: {
  115. quality: '85-100',
  116. speed: env.production ? 1 : 10
  117. },
  118. svgo: null
  119. })
  120. ];
  121. return {
  122. name: 'ui',
  123. context: path.resolve(__dirname, 'src/ui'),
  124. // This is ugly having main.scss on both bundles, but if it is added separately it will generate a js bundle :(
  125. entry: {
  126. settings: ['./settings/index.ts', './scss/main.scss'],
  127. welcome: ['./welcome/index.ts', './scss/main.scss']
  128. // main: ['./scss/main.scss']
  129. },
  130. mode: env.production ? 'production' : 'development',
  131. devtool: !env.production ? 'eval-source-map' : undefined,
  132. output: {
  133. filename: '[name].js',
  134. path: path.resolve(__dirname, 'out/ui'),
  135. publicPath: '{{root}}/out/ui/'
  136. },
  137. optimization: {
  138. splitChunks: {
  139. cacheGroups: {
  140. styles: {
  141. name: 'styles',
  142. test: /\.css$/,
  143. chunks: 'all',
  144. enforce: true
  145. }
  146. }
  147. }
  148. },
  149. resolve: {
  150. extensions: ['.tsx', '.ts', '.js'],
  151. modules: [path.resolve(__dirname, 'src/ui'), 'node_modules']
  152. },
  153. module: {
  154. rules: [
  155. {
  156. test: /\.ts$/,
  157. enforce: 'pre',
  158. use: [
  159. {
  160. loader: 'tslint-loader',
  161. options: {
  162. tsConfigFile: 'ui.tsconfig.json'
  163. }
  164. }
  165. ]
  166. },
  167. {
  168. test: /\.tsx?$/,
  169. use: {
  170. loader: 'ts-loader',
  171. options: {
  172. configFile: 'ui.tsconfig.json'
  173. }
  174. },
  175. exclude: /node_modules/
  176. },
  177. {
  178. test: /\.scss$/,
  179. use: [
  180. {
  181. loader: MiniCssExtractPlugin.loader
  182. },
  183. {
  184. loader: 'css-loader',
  185. options: {
  186. minimize: env.production,
  187. sourceMap: !env.production,
  188. url: false
  189. }
  190. },
  191. {
  192. loader: 'sass-loader',
  193. options: {
  194. sourceMap: !env.production
  195. }
  196. }
  197. ],
  198. exclude: /node_modules/
  199. }
  200. ]
  201. },
  202. plugins: plugins,
  203. stats: { all: false, assets: true, builtAt: true, errors: true, timings: true, warnings: true }
  204. };
  205. }