webpack.config.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const HtmlWebpackPlugin = require('html-webpack-plugin');
  2. const pkg = require('./package.json');
  3. const webpack = require('webpack');
  4. const path = require('path');
  5. const fs = require('fs');
  6. const rootDir = path.resolve(__dirname);
  7. let plugins = [];
  8. module.exports = env => {
  9. const name = pkg.name;
  10. const isProd = env === 'prod';
  11. const output = {
  12. path: path.join(__dirname),
  13. filename: 'dist/grapes.min.js',
  14. library: name,
  15. libraryExport: 'default',
  16. libraryTarget: 'umd',
  17. };
  18. if (isProd) {
  19. plugins = [
  20. new webpack.optimize.ModuleConcatenationPlugin(),
  21. new webpack.BannerPlugin(`${name} - ${pkg.version}`),
  22. ];
  23. } else if (env === 'dev') {
  24. output.filename = 'dist/grapes.js';
  25. } else {
  26. const index = 'index.html';
  27. const indexDev = `_${index}`;
  28. const template = fs.existsSync(indexDev) ? indexDev : index;
  29. plugins.push(new HtmlWebpackPlugin({ template, inject: false }));
  30. }
  31. return {
  32. entry: './src',
  33. output: output,
  34. plugins: plugins,
  35. mode: isProd ? 'production' : 'development',
  36. devtool: isProd ? 'source-map' : (!env ? 'cheap-module-eval-source-map' : false),
  37. devServer: {
  38. headers: { 'Access-Control-Allow-Origin': '*' },
  39. disableHostCheck: true,
  40. },
  41. module: {
  42. rules: [{
  43. test: /\/index\.js$/,
  44. loader: 'string-replace-loader',
  45. query: {
  46. search: '<# VERSION #>',
  47. replace: pkg.version
  48. }
  49. }, {
  50. test: /\.js$/,
  51. loader: 'babel-loader',
  52. include: /src/,
  53. options: { cacheDirectory: true },
  54. }],
  55. },
  56. resolve: {
  57. modules: ['src', 'node_modules'],
  58. alias: {
  59. jquery: 'cash-dom',
  60. backbone: `${rootDir}/node_modules/backbone`,
  61. underscore: `${rootDir}/node_modules/underscore`,
  62. }
  63. }
  64. };
  65. }