当前位置:   article > 正文

Superset二次开发之webpack.config.js 功能模块解读

Superset二次开发之webpack.config.js 功能模块解读

 Webpack 构建工具的核心配置文件,它定义了如何处理项目中的源代码,包括编译、转换、合并、分包、压缩等多个环节。

 

  1. /* eslint-disable no-console */
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. const fs = require('fs');
  21. const path = require('path');
  22. const webpack = require('webpack');
  23. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  24. const CopyPlugin = require('copy-webpack-plugin');
  25. const HtmlWebpackPlugin = require('html-webpack-plugin');
  26. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  27. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
  28. const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
  29. const createMdxCompiler = require('@storybook/addon-docs/mdx-compiler-plugin');
  30. const {
  31. WebpackManifestPlugin,
  32. getCompilerHooks,
  33. } = require('webpack-manifest-plugin');
  34. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  35. const parsedArgs = require('yargs').argv;
  36. const getProxyConfig = require('./webpack.proxy-config');
  37. const packageConfig = require('./package');
  38. // input dir
  39. const APP_DIR = path.resolve(__dirname, './');
  40. // output dir
  41. const BUILD_DIR = path.resolve(__dirname, '../superset/static/assets');
  42. const ROOT_DIR = path.resolve(__dirname, '..');
  43. const {
  44. mode = 'development',
  45. devserverPort = 9000,
  46. measure = false,
  47. analyzeBundle = false,
  48. analyzerPort = 8888,
  49. nameChunks = false,
  50. } = parsedArgs;
  51. const isDevMode = mode !== 'production';
  52. const isDevServer = process.argv[1].includes('webpack-dev-server');
  53. const ASSET_BASE_URL = process.env.ASSET_BASE_URL || '';
  54. const output = {
  55. path: BUILD_DIR,
  56. publicPath: `${ASSET_BASE_URL}/static/assets/`,
  57. };
  58. if (isDevMode) {
  59. output.filename = '[name].[contenthash:8].entry.js';
  60. output.chunkFilename = '[name].[contenthash:8].chunk.js';
  61. } else if (nameChunks) {
  62. output.filename = '[name].[chunkhash].entry.js';
  63. output.chunkFilename = '[name].[chunkhash].chunk.js';
  64. } else {
  65. output.filename = '[name].[chunkhash].entry.js';
  66. output.chunkFilename = '[chunkhash].chunk.js';
  67. }
  68. if (!isDevMode) {
  69. output.clean = true;
  70. }
  71. const plugins = [
  72. new webpack.ProvidePlugin({
  73. process: 'process/browser.js',
  74. }),
  75. // creates a manifest.json mapping of name to hashed output used in template files
  76. new WebpackManifestPlugin({
  77. publicPath: output.publicPath,
  78. seed: { app: 'superset' },
  79. // This enables us to include all relevant files for an entry
  80. generate: (seed, files, entrypoints) => {
  81. // Each entrypoint's chunk files in the format of
  82. // {
  83. // entry: {
  84. // css: [],
  85. // js: []
  86. // }
  87. // }
  88. const entryFiles = {};
  89. Object.entries(entrypoints).forEach(([entry, chunks]) => {
  90. entryFiles[entry] = {
  91. css: chunks
  92. .filter(x => x.endsWith('.css'))
  93. .map(x => `${output.publicPath}${x}`),
  94. js: chunks
  95. .filter(x => x.endsWith('.js'))
  96. .map(x => `${output.publicPath}${x}`),
  97. };
  98. });
  99. return {
  100. ...seed,
  101. entrypoints: entryFiles,
  102. };
  103. },
  104. // Also write manifest.json to disk when running `npm run dev`.
  105. // This is required for Flask to work.
  106. writeToFileEmit: isDevMode && !isDevServer,
  107. }),
  108. // expose mode variable to other modules
  109. new webpack.DefinePlugin({
  110. 'process.env.WEBPACK_MODE': JSON.stringify(mode),
  111. 'process.env.REDUX_DEFAULT_MIDDLEWARE':
  112. process.env.REDUX_DEFAULT_MIDDLEWARE,
  113. }),
  114. new CopyPlugin({
  115. patterns: [
  116. 'package.json',
  117. { from: 'src/assets/images', to: 'i
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/338612
推荐阅读
相关标签
  

闽ICP备14008679号