webpack.config.common.js 4.2 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 CircularDependencyPlugin = require('circular-dependency-plugin');
  7. const sharedConfig = {
  8. module: {
  9. rules: [
  10. {
  11. test: /\.(html|ico)$/,
  12. use: [{
  13. loader: 'file-loader',
  14. options: { name: '[name].[ext]' }
  15. }]
  16. },
  17. {
  18. test: /\.(s*)css$/,
  19. use: [
  20. MiniCssExtractPlugin.loader,
  21. { loader: 'css-loader', options: { sourceMap: false } },
  22. { loader: 'resolve-url-loader', options: { sourceMap: false } },
  23. { loader: 'sass-loader', options: { sourceMap: false } },
  24. ]
  25. }
  26. ]
  27. },
  28. plugins: [
  29. // new CircularDependencyPlugin({
  30. // include: [ path.resolve(__dirname, 'lib/') ],
  31. // failOnError: false,
  32. // cwd: process.cwd(),
  33. // }),
  34. new ExtraWatchWebpackPlugin({
  35. files: [
  36. './lib/**/*.scss',
  37. './lib/**/*.html'
  38. ],
  39. }),
  40. new webpack.DefinePlugin({
  41. // __VERSION__: webpack.DefinePlugin.runtimeValue(() => JSON.stringify(require('./package.json').version), true),
  42. // __VERSION_TIMESTAMP__: webpack.DefinePlugin.runtimeValue(() => `${new Date().valueOf()}`, true),
  43. 'process.env.DEBUG': JSON.stringify(process.env.DEBUG)
  44. }),
  45. new MiniCssExtractPlugin({ filename: 'molstar.css', }),
  46. new VersionFile({
  47. extras: { timestamp: `${new Date().valueOf()}` },
  48. packageFile: path.resolve(__dirname, 'package.json'),
  49. templateString: `export const PLUGIN_VERSION = '<%= package.version %>';\nexport const PLUGIN_VERSION_DATE = new Date(<%= extras.timestamp %>);`,
  50. outputFile: path.resolve(__dirname, 'lib/mol-plugin/version.js')
  51. })
  52. ],
  53. resolve: {
  54. modules: [
  55. 'node_modules',
  56. path.resolve(__dirname, 'lib/')
  57. ],
  58. },
  59. watchOptions: {
  60. aggregateTimeout: 750
  61. },
  62. devtool: ''
  63. }
  64. function createEntry(src, outFolder, outFilename, isNode) {
  65. return {
  66. node: isNode ? void 0 : { fs: 'empty' }, // TODO find better solution? Currently used in file-handle.ts
  67. target: isNode ? 'node' : void 0,
  68. entry: path.resolve(__dirname, `lib/${src}.js`),
  69. output: { filename: `${outFilename}.js`, path: path.resolve(__dirname, `build/${outFolder}`) },
  70. ...sharedConfig
  71. }
  72. }
  73. function createEntryPoint(name, dir, out, library) {
  74. return {
  75. node: { fs: 'empty' }, // TODO find better solution? Currently used in file-handle.ts
  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. }