You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webpack.config.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-18 18:22:23
  4. */
  5. const path = require('path')
  6. const webpack = require('webpack')
  7. const packageInfo = require('./package.json')
  8. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  9. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  10. const CopyWebpackPlugin = require('copy-webpack-plugin')
  11. const JavaScriptObfuscator = require('webpack-obfuscator')
  12. const cesiumBuild = 'node_modules/cesium/Build/Cesium'
  13. let cesiumCopyPlugin = [
  14. new CopyWebpackPlugin([
  15. { from: path.join(cesiumBuild, 'Assets'), to: 'resources/Assets' }
  16. ]),
  17. new CopyWebpackPlugin([
  18. { from: path.join(cesiumBuild, 'Workers'), to: 'resources/Workers' }
  19. ]),
  20. new CopyWebpackPlugin([
  21. { from: path.join(cesiumBuild, 'ThirdParty'), to: 'resources/ThirdParty' }
  22. ])
  23. ]
  24. function resolve(dir) {
  25. return path.join(__dirname, '.', dir)
  26. }
  27. function getTime() {
  28. let now = new Date()
  29. return `${now.getFullYear()}-${now.getMonth() +
  30. 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}`
  31. }
  32. module.exports = env => {
  33. const IS_PROD = (env && env.production) || false
  34. const publicPath = IS_PROD ? '/' : '/'
  35. let plugins = [
  36. ...cesiumCopyPlugin,
  37. new MiniCssExtractPlugin({
  38. filename: IS_PROD ? '[name].min.css' : '[name].css',
  39. allChunks: true
  40. }),
  41. new webpack.DefinePlugin({
  42. CESIUM_BASE_URL: JSON.stringify('./libs/dc-sdk/resources/'),
  43. 'build.version': JSON.stringify(packageInfo.version),
  44. 'build.time': JSON.stringify(getTime())
  45. })
  46. ]
  47. if (IS_PROD) {
  48. plugins.push(new OptimizeCssAssetsPlugin())
  49. plugins.push(new webpack.NoEmitOnErrorsPlugin())
  50. plugins.push(
  51. new JavaScriptObfuscator(
  52. {
  53. rotateStringArray: true
  54. },
  55. ['dc.base.min.js']
  56. )
  57. )
  58. }
  59. return {
  60. entry: {
  61. 'dc.base': ['base'],
  62. 'dc.core': ['entry', 'theme']
  63. },
  64. devtool: IS_PROD ? false : 'cheap-module-eval-source-map',
  65. output: {
  66. filename: IS_PROD ? '[name].min.js' : '[name].js',
  67. path: path.resolve(__dirname, 'dist'),
  68. publicPath: publicPath,
  69. sourcePrefix: ''
  70. },
  71. amd: {
  72. toUrlUndefined: true
  73. },
  74. node: {
  75. fs: 'empty'
  76. },
  77. module: {
  78. unknownContextCritical: false,
  79. rules: [
  80. {
  81. test: /\.js$/,
  82. exclude: /node_modules/,
  83. loader: 'babel-loader',
  84. options: {
  85. presets: ['@babel/preset-env'],
  86. plugins: ['@babel/transform-runtime'],
  87. compact: false,
  88. ignore: ['checkTree']
  89. }
  90. },
  91. {
  92. test: /\.css$/,
  93. use: [
  94. MiniCssExtractPlugin.loader,
  95. {
  96. loader: 'css-loader'
  97. },
  98. {
  99. loader: 'sass-loader'
  100. }
  101. ]
  102. },
  103. {
  104. test: /\.scss$/,
  105. use: [
  106. MiniCssExtractPlugin.loader,
  107. {
  108. loader: 'css-loader'
  109. },
  110. {
  111. loader: 'sass-loader'
  112. }
  113. ]
  114. },
  115. {
  116. test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
  117. loader: 'url-loader',
  118. options: {
  119. limit: 20000
  120. }
  121. },
  122. {
  123. test: /\.glsl$/,
  124. loader: 'webpack-glsl-loader'
  125. }
  126. ]
  127. },
  128. resolve: {
  129. extensions: ['.js', '.json', '.css'],
  130. alias: {
  131. '@': resolve('src'),
  132. base: './src/base/index.js',
  133. entry: './src/core/index.js',
  134. theme: './src/themes/index.js',
  135. cesium: path.resolve(__dirname, cesiumBuild)
  136. }
  137. },
  138. plugins
  139. }
  140. }