webpack.config.common.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. '__MOLSTAR_DEBUG_TIMESTAMP__': webpack.DefinePlugin.runtimeValue(() => `${new Date().valueOf()}`, true)
  36. }),
  37. new MiniCssExtractPlugin({ filename: 'molstar.css', }),
  38. new VersionFile({
  39. extras: { timestamp: `${new Date().valueOf()}` },
  40. packageFile: path.resolve(__dirname, 'package.json'),
  41. templateString: `export var PLUGIN_VERSION = '<%= package.version %>';\nexport var PLUGIN_VERSION_DATE = new Date(typeof __MOLSTAR_DEBUG_TIMESTAMP__ !== 'undefined' ? __MOLSTAR_DEBUG_TIMESTAMP__ : <%= extras.timestamp %>);`,
  42. outputFile: path.resolve(__dirname, 'lib/mol-plugin/version.js')
  43. })
  44. ],
  45. resolve: {
  46. modules: [
  47. 'node_modules',
  48. path.resolve(__dirname, 'lib/')
  49. ],
  50. fallback: {
  51. fs: false,
  52. crypto: require.resolve('crypto-browserify'),
  53. path: require.resolve('path-browserify'),
  54. stream: require.resolve('stream-browserify'),
  55. }
  56. },
  57. watchOptions: {
  58. aggregateTimeout: 750
  59. }
  60. };
  61. function createEntry(src, outFolder, outFilename, isNode) {
  62. return {
  63. target: isNode ? 'node' : void 0,
  64. entry: path.resolve(__dirname, `lib/${src}.js`),
  65. output: { filename: `${outFilename}.js`, path: path.resolve(__dirname, `build/${outFolder}`) },
  66. ...sharedConfig
  67. };
  68. }
  69. function createEntryPoint(name, dir, out, library) {
  70. return {
  71. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  72. output: { filename: `${library || name}.js`, path: path.resolve(__dirname, `build/${out}`), library: library || out, libraryTarget: 'umd' },
  73. ...sharedConfig
  74. };
  75. }
  76. function createNodeEntryPoint(name, dir, out) {
  77. return {
  78. target: 'node',
  79. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  80. output: { filename: `${name}.js`, path: path.resolve(__dirname, `build/${out}`) },
  81. externals: {
  82. argparse: 'require("argparse")',
  83. 'node-fetch': 'require("node-fetch")',
  84. 'util.promisify': 'require("util.promisify")',
  85. xhr2: 'require("xhr2")',
  86. },
  87. ...sharedConfig
  88. };
  89. }
  90. function createApp(name, library) { return createEntryPoint('index', `apps/${name}`, name, library); }
  91. function createExample(name) { return createEntry(`examples/${name}/index`, `examples/${name}`, 'index'); }
  92. function createBrowserTest(name) { return createEntryPoint(name, 'tests/browser', 'tests'); }
  93. function createNodeApp(name) { return createNodeEntryPoint('index', `apps/${name}`, name); }
  94. module.exports = {
  95. createApp,
  96. createEntry,
  97. createExample,
  98. createBrowserTest,
  99. createNodeEntryPoint,
  100. createNodeApp
  101. };