webpack.config.common.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 VersionFile = require('webpack-version-file-plugin');
  6. const sharedConfig = {
  7. module: {
  8. rules: [
  9. {
  10. test: /\.(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. { loader: 'css-loader', options: { sourceMap: false } },
  21. { loader: 'sass-loader', options: { sourceMap: false } },
  22. ]
  23. }
  24. ]
  25. },
  26. plugins: [
  27. new ExtraWatchWebpackPlugin({
  28. files: [
  29. './lib/**/*.scss',
  30. './lib/**/*.html'
  31. ],
  32. }),
  33. new webpack.DefinePlugin({
  34. 'process.env.DEBUG': JSON.stringify(process.env.DEBUG)
  35. }),
  36. new MiniCssExtractPlugin({ filename: 'molstar.css', }),
  37. new VersionFile({
  38. extras: { timestamp: `${new Date().valueOf()}` },
  39. packageFile: path.resolve(__dirname, 'package.json'),
  40. templateString: `export const PLUGIN_VERSION = '<%= package.version %>';\nexport const PLUGIN_VERSION_DATE = new Date(<%= extras.timestamp %>);`,
  41. outputFile: path.resolve(__dirname, 'lib/mol-plugin/version.js')
  42. })
  43. ],
  44. resolve: {
  45. modules: [
  46. 'node_modules',
  47. path.resolve(__dirname, 'lib/')
  48. ],
  49. },
  50. watchOptions: {
  51. aggregateTimeout: 750
  52. },
  53. devtool: ''
  54. }
  55. function createEntry(src, outFolder, outFilename, isNode) {
  56. return {
  57. node: isNode ? void 0 : { fs: 'empty' }, // TODO find better solution? Currently used in file-handle.ts
  58. target: isNode ? 'node' : void 0,
  59. entry: path.resolve(__dirname, `lib/${src}.js`),
  60. output: { filename: `${outFilename}.js`, path: path.resolve(__dirname, `build/${outFolder}`) },
  61. ...sharedConfig
  62. }
  63. }
  64. function createEntryPoint(name, dir, out, library) {
  65. return {
  66. node: { fs: 'empty' }, // TODO find better solution? Currently used in file-handle.ts
  67. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  68. output: { filename: `${library || name}.js`, path: path.resolve(__dirname, `build/${out}`), library: library || out, libraryTarget: 'umd' },
  69. ...sharedConfig
  70. }
  71. }
  72. function createNodeEntryPoint(name, dir, out) {
  73. return {
  74. target: 'node',
  75. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  76. output: { filename: `${name}.js`, path: path.resolve(__dirname, `build/${out}`) },
  77. externals: {
  78. argparse: 'require("argparse")',
  79. 'node-fetch': 'require("node-fetch")',
  80. 'util.promisify': 'require("util.promisify")',
  81. xhr2: 'require("xhr2")',
  82. },
  83. ...sharedConfig
  84. }
  85. }
  86. function createApp(name, library) { return createEntryPoint('index', `apps/${name}`, name, library) }
  87. function createExample(name) { return createEntry(`examples/${name}/index`, `examples/${name}`, 'index') }
  88. function createBrowserTest(name) { return createEntryPoint(name, 'tests/browser', 'tests') }
  89. function createNodeApp(name) { return createNodeEntryPoint('index', `apps/${name}`, name) }
  90. module.exports = {
  91. createApp,
  92. createEntry,
  93. createExample,
  94. createBrowserTest,
  95. createNodeEntryPoint,
  96. createNodeApp
  97. }