webpack.config.common.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const path = require('path');
  2. const webpack = require('webpack');
  3. const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. // const CircularDependencyPlugin = require('circular-dependency-plugin');
  6. const sharedConfig = {
  7. module: {
  8. rules: [
  9. {
  10. test: /\.(woff2?|ttf|otf|eot|svg|html|ico)$/,
  11. use: [{
  12. loader: 'file-loader',
  13. options: { name: '[name].[ext]' }
  14. }]
  15. },
  16. {
  17. test: /\.(s*)css$/,
  18. use: [
  19. MiniCssExtractPlugin.loader,
  20. 'css-loader', 'resolve-url-loader', 'sass-loader'
  21. ]
  22. }
  23. ]
  24. },
  25. plugins: [
  26. // new CircularDependencyPlugin({
  27. // include: [ path.resolve(__dirname, 'lib/') ],
  28. // failOnError: false,
  29. // cwd: process.cwd(),
  30. // }),
  31. new ExtraWatchWebpackPlugin({
  32. files: [
  33. './lib/**/*.scss',
  34. './lib/**/*.html'
  35. ],
  36. }),
  37. new webpack.DefinePlugin({
  38. __PLUGIN_VERSION_TIMESTAMP__: webpack.DefinePlugin.runtimeValue(() => `${new Date().valueOf()}`, true),
  39. 'process.env.DEBUG': JSON.stringify(process.env.DEBUG)
  40. }),
  41. new MiniCssExtractPlugin({ filename: 'app.css' })
  42. ],
  43. resolve: {
  44. modules: [
  45. 'node_modules',
  46. path.resolve(__dirname, 'lib/')
  47. ],
  48. },
  49. watchOptions: {
  50. aggregateTimeout: 750
  51. },
  52. devtool: ''
  53. }
  54. function createEntry(src, outFolder, outFilename, isNode) {
  55. return {
  56. node: isNode ? void 0 : { fs: 'empty' }, // TODO find better solution? Currently used in file-handle.ts
  57. target: isNode ? 'node' : void 0,
  58. entry: path.resolve(__dirname, `lib/${src}.js`),
  59. output: { filename: `${outFilename}.js`, path: path.resolve(__dirname, `build/${outFolder}`) },
  60. ...sharedConfig
  61. }
  62. }
  63. function createEntryPoint(name, dir, out) {
  64. return {
  65. node: { fs: 'empty' }, // TODO find better solution? Currently used in file-handle.ts
  66. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  67. output: { filename: `${name}.js`, path: path.resolve(__dirname, `build/${out}`) },
  68. ...sharedConfig
  69. }
  70. }
  71. function createNodeEntryPoint(name, dir, out) {
  72. return {
  73. target: 'node',
  74. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  75. output: { filename: `${name}.js`, path: path.resolve(__dirname, `build/${out}`) },
  76. externals: {
  77. argparse: 'require("argparse")',
  78. 'node-fetch': 'require("node-fetch")',
  79. 'util.promisify': 'require("util.promisify")',
  80. xhr2: 'require("xhr2")',
  81. },
  82. ...sharedConfig
  83. }
  84. }
  85. function createApp(name) { return createEntryPoint('index', `apps/${name}`, name) }
  86. function createBrowserTest(name) { return createEntryPoint(name, 'tests/browser', 'tests') }
  87. function createNodeApp(name) { return createNodeEntryPoint('index', `apps/${name}`, name) }
  88. module.exports = {
  89. createApp,
  90. createEntry,
  91. createBrowserTest,
  92. createNodeEntryPoint,
  93. createNodeApp
  94. }