webpack.config.common.js 3.9 KB

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