codekingpro/portable-devtools
114k
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10/* eslint-env node */11// Import file, libraries and plugins12const path = require('path');13const webpack = require('webpack');14const sourceDir = __dirname + '/pgadmin/static';15// webpack.shim.js contains path references for resolve > alias configuration16// and other util function used in CommonsChunksPlugin.17const webpackShimConfig = require('./webpack.shim');18const PRODUCTION = process.env.NODE_ENV === 'production';19const TerserPlugin = require('terser-webpack-plugin');20const MiniCssExtractPlugin = require('mini-css-extract-plugin');21const extractStyle = new MiniCssExtractPlugin({22 filename: '[name].css',23 chunkFilename: '[name].css',24});25const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;26const CopyPlugin = require('copy-webpack-plugin');27const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');28 29const envType = PRODUCTION ? 'production': 'development';30const devToolVal = PRODUCTION ? false : 'eval';31const analyzerMode = process.env.ANALYZE=='true' ? 'static' : 'disabled';32 33const outputPath = __dirname + '/pgadmin/static/js/generated';34 35// Expose libraries in app context so they need not to36// require('libname') when used in a module37const providePlugin = new webpack.ProvidePlugin({38 _: 'lodash',39 pgAdmin: 'sources/pgadmin',40 'moment': 'moment',41 'window.moment':'moment',42 process: 'process/browser',43 Buffer: ['buffer', 'Buffer']44});45 46// Helps in debugging each single file, it extracts the module files47// from bundle so that they are accessible by search in Chrome's sources panel.48// Reference: https://webpack.js.org/plugins/source-map-dev-tool-plugin/#components/sidebar/sidebar.jsx49const sourceMapDevToolPlugin = new webpack.SourceMapDevToolPlugin({50 filename: '[name].js.map',51 exclude: /(vendor|codemirror|pgadmin\.js|pgadmin.theme|pgadmin.static|style\.js|popper)/,52 columns: false,53});54 55// can be enabled using bundle:analyze56const bundleAnalyzer = new BundleAnalyzerPlugin({57 analyzerMode: analyzerMode,58 reportFilename: 'analyze_report.html',59});60 61const copyFiles = new CopyPlugin({62 patterns: [63 {64 from: './pgadmin/static/img/*.png',65 to: 'img/[name][ext]',66 },67 ],68});69 70module.exports = [{71 mode: envType,72 devtool: devToolVal,73 stats: { children: false, builtAt: true, chunks: true, timings: true },74 // The base directory, an absolute path, for resolving entry points and loaders75 // from configuration.76 context: __dirname,77 // Specify entry points of application78 entry: {79 'app.bundle': sourceDir + '/bundle/app.js',80 'security.pages': 'security.pages',81 sqleditor: './pgadmin/tools/sqleditor/static/js/index.js',82 schema_diff: './pgadmin/tools/schema_diff/static/js/index.js',83 erd_tool: './pgadmin/tools/erd/static/js/index.js',84 psql_tool: './pgadmin/tools/psql/static/js/index.js',85 debugger: './pgadmin/tools/debugger/static/js/index.js',86 style: ['./pgadmin/static/css/style.css', './pgadmin/static/js/pgadmin.fonticon.js']87 },88 // path: The output directory for generated bundles(defined in entry)89 // Ref: https://webpack.js.org/configuration/output/#output-library90 output: {91 libraryTarget: 'amd',92 path: outputPath,93 filename: '[name].js',94 chunkFilename: '[name].chunk.js?id=[chunkhash]',95 libraryExport: 'default',96 publicPath: '',97 },98 // Templates files which contains python code needs to load dynamically99 // Such files specified in externals are loaded at first and defined in100 // the start of generated bundle within define(['libname'],fn) etc.101 externals: webpackShimConfig.externals,102 module: {103 // References:104 // Module and Rules: https://webpack.js.org/configuration/module/105 // Loaders: https://webpack.js.org/loaders/106 //107 // imports-loader: it adds dependent modules(use:imports-loader?module1)108 // at the beginning of module it is dependency.109 // It solves number of problems110 // Ref: http:/github.com/webpack-contrib/imports-loader/111 rules: [{112 test: /\.fonticon\.js/,113 use: [114 MiniCssExtractPlugin.loader,115 {116 loader: 'css-loader',117 options: {118 url: false,119 },120 },121 'webfonts-loader',122 ],123 },{124 test: /\.jsx?$/,125 exclude: [/node_modules/, /vendor/],126 use: {127 loader: 'babel-loader',128 },129 },{130 test: /\.m?js$/,131 resolve: {132 fullySpecified: false133 },134 },{135 test: /\.tsx?$|\.ts?$/,136 use: {137 loader: 'babel-loader',138 options: {139 presets: [['@babel/preset-env', {'modules': 'commonjs', 'useBuiltIns': 'usage', 'corejs': 3}], '@babel/preset-react', '@babel/preset-typescript'],140 plugins: ['@babel/plugin-proposal-class-properties', '@babel/proposal-object-rest-spread'],141 },142 },143 }, {144 test: /external_table.*\.js/,145 use: {146 loader: 'babel-loader',147 options: {148 presets: [['@babel/preset-env', {'modules': 'commonjs', 'useBuiltIns': 'usage', 'corejs': 3}]],149 },150 },151 }, {152 // Transforms the code in a way that it works in the webpack environment.153 // It uses imports-loader internally to load dependency. Its154 // configuration is specified in webpack.shim.js155 // Ref: https://www.npmjs.com/package/shim-loader156 test: /\.js/,157 exclude: [/external_table/],158 loader: 'shim-loader',159 options: webpackShimConfig,160 include: path.join(__dirname, '/pgadmin/browser'),161 }, {162 // imports-loader: it adds dependent modules(use:imports-loader?module1)163 // at the beginning of module it is dependency.164 // It solves number of problems165 // Ref: http:/github.com/webpack-contrib/imports-loader/166 test: require.resolve('./pgadmin/tools/sqleditor/static/js/index'),167 use: {168 loader: 'imports-loader',169 options: {170 type: 'commonjs',171 imports: [172 'pure|pgadmin.tools.user_management',173 'pure|pgadmin.browser.bgprocessmanager',174 'pure|pgadmin.node.server_group',175 'pure|pgadmin.node.server',176 'pure|pgadmin.node.database',177 'pure|pgadmin.node.role',178 'pure|pgadmin.node.cast',179 'pure|pgadmin.node.publication',180 'pure|pgadmin.node.subscription',181 'pure|pgadmin.node.tablespace',182 'pure|pgadmin.node.resource_group',183 'pure|pgadmin.node.event_trigger',184 'pure|pgadmin.node.extension',185 'pure|pgadmin.node.language',186 'pure|pgadmin.node.foreign_data_wrapper',187 'pure|pgadmin.node.foreign_server',188 'pure|pgadmin.node.user_mapping',189 'pure|pgadmin.node.schema',190 'pure|pgadmin.node.catalog',191 'pure|pgadmin.node.foreign_table_column',192 'pure|pgadmin.node.catalog_object',193 'pure|pgadmin.node.collation',194 'pure|pgadmin.node.domain',195 'pure|pgadmin.node.domain_constraints',196 'pure|pgadmin.node.foreign_table',197 'pure|pgadmin.node.fts_configuration',198 'pure|pgadmin.node.fts_dictionary',199 'pure|pgadmin.node.fts_parser',200 'pure|pgadmin.node.fts_template',201 'pure|pgadmin.node.function',202 'pure|pgadmin.node.procedure',203 'pure|pgadmin.node.edbfunc',204 'pure|pgadmin.node.edbproc',205 'pure|pgadmin.node.edbvar',206 'pure|pgadmin.node.trigger_function',207 'pure|pgadmin.node.package',208 'pure|pgadmin.node.sequence',209 'pure|pgadmin.node.synonym',210 'pure|pgadmin.node.type',211 'pure|pgadmin.node.rule',212 'pure|pgadmin.node.index',213 'pure|pgadmin.node.row_security_policy',214 'pure|pgadmin.node.trigger',215 'pure|pgadmin.node.catalog_object_column',216 'pure|pgadmin.node.view',217 'pure|pgadmin.node.mview',218 'pure|pgadmin.node.table',219 'pure|pgadmin.node.partition',220 'pure|pgadmin.node.compound_trigger',221 'pure|pgadmin.node.aggregate',222 'pure|pgadmin.node.operator',223 'pure|pgadmin.node.dbms_job_scheduler',224 'pure|pgadmin.node.replica_node'225 ],226 },227 },228 },{229 test: require.resolve('./pgadmin/static/bundle/browser'),230 use: {231 loader: 'imports-loader',232 options: {233 type: 'commonjs',234 imports: [235 'pure|pgadmin.about',236 'pure|pgadmin.preferences',237 'pure|pgadmin.settings',238 'pure|pgadmin.tools.backup',239 'pure|pgadmin.tools.restore',240 'pure|pgadmin.tools.grant_wizard',241 'pure|pgadmin.tools.maintenance',242 'pure|pgadmin.tools.import_export',243 'pure|pgadmin.tools.import_export_servers',244 'pure|pgadmin.tools.debugger',245 'pure|pgadmin.node.pga_job',246 'pure|pgadmin.tools.schema_diff',247 'pure|pgadmin.tools.file_manager',248 'pure|pgadmin.tools.search_objects',249 'pure|pgadmin.tools.erd',250 'pure|pgadmin.tools.psql',251 'pure|pgadmin.tools.sqleditor',252 'pure|pgadmin.misc.cloud',253 ],254 },255 },256 },257 {258 test: /\.svg$/,259 oneOf: [260 {261 issuer: /\.[jt]sx?$/,262 resourceQuery: /svgr/,263 use: ['@svgr/webpack'],264 },265 {266 type: 'asset',267 parser: {268 dataUrlCondition: {269 maxSize: 4 * 1024, // 4kb270 }271 }272 },273 ],274 },{275 test: /\.(jpe?g|png|gif)$/i,276 type: 'asset',277 parser: {278 dataUrlCondition: {279 maxSize: 4 * 1024, // 4kb280 },281 },282 generator: {283 filename: 'img/[name].[ext]',284 },285 exclude: /vendor/,286 },{287 test: /\.(eot|ttf|woff|woff2)$/,288 type: 'asset/resource',289 generator: {290 filename: 'fonts/[name].[ext]',291 },292 include: [293 /node_modules/,294 path.join(sourceDir, '/css/'),295 path.join(sourceDir, '/fonts/'),296 ],297 exclude: /vendor/,298 },299 {300 test: /\.css$/,301 use: [302 {303 loader: MiniCssExtractPlugin.loader,304 options: {305 publicPath: '',306 },307 },308 'css-loader',309 {310 loader: 'postcss-loader',311 options: {312 postcssOptions: () =>({313 plugins: [314 require('autoprefixer')(),315 ],316 }),317 },318 },319 ],320 }],321 // Prevent module from parsing through webpack, helps in reducing build time322 noParse: [/moment.js/],323 },324 resolve: {325 alias: webpackShimConfig.resolveAlias,326 modules: ['node_modules', '.'],327 extensions: ['.js', '.jsx', '.ts', '.tsx'],328 unsafeCache: true,329 fallback: {330 'fs': false331 },332 },333 // Watch mode Configuration: After initial build, webpack will watch for334 // changes in files and compiles only files which are changed,335 // if watch is set to True336 // Reference: https://webpack.js.org/configuration/watch/#components/sidebar/sidebar.jsx337 watchOptions: {338 aggregateTimeout: 300,339 poll: 1000,340 ignored: /node_modules/,341 },342 optimization: {343 minimizer: PRODUCTION ? [344 new TerserPlugin({345 parallel: false,346 extractComments: true,347 terserOptions: {348 compress: true,349 },350 }),351 new ImageMinimizerPlugin({352 test: /\.(jpe?g|png|gif)$/i,353 minimizer: {354 implementation: ImageMinimizerPlugin.imageminMinify,355 options: {356 plugins: [357 ['mozjpeg', { progressive: true }],358 ['optipng', { optimizationLevel: 7 }],359 ],360 },361 },362 }),363 ] : [],364 splitChunks: {365 cacheGroups: {366 vendor_sqleditor: {367 name: 'vendor_sqleditor',368 filename: 'vendor.sqleditor.js',369 chunks: 'all',370 reuseExistingChunk: true,371 priority: 9,372 minChunks: 2,373 enforce: true,374 test(module) {375 return webpackShimConfig.matchModules(module, ['jsoneditor', 'leaflet']);376 },377 },378 vendor_react: {379 name: 'vendor_react',380 filename: 'vendor.react.js',381 chunks: 'all',382 reuseExistingChunk: true,383 priority: 8,384 minChunks: 2,385 enforce: true,386 test(module) {387 return webpackShimConfig.matchModules(module, ['react', 'react-dom']);388 },389 },390 vendor_main: {391 name: 'vendor_main',392 filename: 'vendor.main.js',393 chunks: 'all',394 reuseExistingChunk: true,395 priority: 7,396 minChunks: 2,397 enforce: true,398 test(module) {399 return webpackShimConfig.matchModules(module, ['codemirror', 'rc-', '@material-ui']);400 },401 },402 vendor_others: {403 name: 'vendor_others',404 filename: 'vendor.others.js',405 chunks: 'all',406 reuseExistingChunk: true,407 priority: 6,408 minChunks: 2,409 enforce: true,410 test(module) {411 return webpackShimConfig.isExternal(module);412 },413 },414 secondary: {415 name: 'pgadmin_commons',416 filename: 'pgadmin_commons.js',417 chunks: 'all',418 priority: 5,419 minChunks: 2,420 enforce: true,421 test(module) {422 return webpackShimConfig.isPgAdminLib(module);423 },424 },425 browser_nodes: {426 name: 'browser_nodes',427 filename: 'browser_nodes.js',428 chunks: 'all',429 priority: 4,430 minChunks: 2,431 enforce: true,432 test(module) {433 return webpackShimConfig.isBrowserNode(module);434 },435 },436 },437 },438 },439 // Define list of Plugins used in Production or development mode440 // Ref:https://webpack.js.org/concepts/plugins/#components/sidebar/sidebar.jsx441 plugins: PRODUCTION ? [442 extractStyle,443 providePlugin,444 sourceMapDevToolPlugin,445 bundleAnalyzer,446 copyFiles,447 ]: [448 extractStyle,449 providePlugin,450 sourceMapDevToolPlugin,451 copyFiles,452 ],453}];454 