webpack.config.common.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const path = require('path');
  2. const fs = require('fs');
  3. const webpack = require('webpack');
  4. const ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const VERSION = require('./package.json').version;
  7. class VersionFilePlugin {
  8. apply() {
  9. fs.writeFileSync(
  10. path.resolve(__dirname, 'lib/mol-plugin/version.js'),
  11. `export var PLUGIN_VERSION = '${VERSION}';\nexport var PLUGIN_VERSION_DATE = new Date(typeof __MOLSTAR_DEBUG_TIMESTAMP__ !== 'undefined' ? __MOLSTAR_DEBUG_TIMESTAMP__ : ${new Date().valueOf()});`);
  12. }
  13. }
  14. const sharedConfig = {
  15. module: {
  16. rules: [
  17. {
  18. test: /\.(html|ico)$/,
  19. use: [{
  20. loader: 'file-loader',
  21. options: { name: '[name].[ext]' }
  22. }]
  23. },
  24. {
  25. test: /\.(s*)css$/,
  26. use: [
  27. MiniCssExtractPlugin.loader,
  28. { loader: 'css-loader', options: { sourceMap: false } },
  29. { loader: 'sass-loader', options: { sourceMap: false } },
  30. ]
  31. },
  32. {
  33. test: /\.(jpg)$/i,
  34. type: 'asset/resource',
  35. },
  36. ]
  37. },
  38. plugins: [
  39. new ExtraWatchWebpackPlugin({
  40. files: [
  41. './lib/**/*.scss',
  42. './lib/**/*.html'
  43. ],
  44. }),
  45. new webpack.DefinePlugin({
  46. 'process.env.DEBUG': JSON.stringify(process.env.DEBUG),
  47. '__MOLSTAR_DEBUG_TIMESTAMP__': webpack.DefinePlugin.runtimeValue(() => `${new Date().valueOf()}`, true)
  48. }),
  49. new MiniCssExtractPlugin({ filename: 'molstar.css' }),
  50. new VersionFilePlugin(),
  51. ],
  52. resolve: {
  53. modules: [
  54. 'node_modules',
  55. path.resolve(__dirname, 'lib/')
  56. ],
  57. fallback: {
  58. fs: false,
  59. crypto: require.resolve('crypto-browserify'),
  60. path: require.resolve('path-browserify'),
  61. stream: require.resolve('stream-browserify'),
  62. }
  63. },
  64. watchOptions: {
  65. aggregateTimeout: 750
  66. }
  67. };
  68. function createEntry(src, outFolder, outFilename, isNode) {
  69. return {
  70. target: isNode ? 'node' : void 0,
  71. entry: path.resolve(__dirname, `lib/${src}.js`),
  72. output: { filename: `${outFilename}.js`, path: path.resolve(__dirname, `build/${outFolder}`) },
  73. ...sharedConfig
  74. };
  75. }
  76. function createEntryPoint(name, dir, out, library) {
  77. return {
  78. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  79. output: { filename: `${library || name}.js`, path: path.resolve(__dirname, `build/${out}`), library: library || out, libraryTarget: 'umd', assetModuleFilename: 'images/[hash][ext][query]', 'publicPath': '' },
  80. ...sharedConfig
  81. };
  82. }
  83. function createNodeEntryPoint(name, dir, out) {
  84. return {
  85. target: 'node',
  86. entry: path.resolve(__dirname, `lib/${dir}/${name}.js`),
  87. output: { filename: `${name}.js`, path: path.resolve(__dirname, `build/${out}`) },
  88. externals: {
  89. argparse: 'require("argparse")',
  90. 'node-fetch': 'require("node-fetch")',
  91. 'util.promisify': 'require("util.promisify")',
  92. xhr2: 'require("xhr2")',
  93. },
  94. ...sharedConfig
  95. };
  96. }
  97. function createApp(name, library) { return createEntryPoint('index', `apps/${name}`, name, library); }
  98. function createExample(name) { return createEntry(`examples/${name}/index`, `examples/${name}`, 'index'); }
  99. function createBrowserTest(name) { return createEntryPoint(name, 'tests/browser', 'tests'); }
  100. function createNodeApp(name) { return createNodeEntryPoint('index', `apps/${name}`, name); }
  101. module.exports = {
  102. createApp,
  103. createEntry,
  104. createExample,
  105. createBrowserTest,
  106. createNodeEntryPoint,
  107. createNodeApp
  108. };