File size: 7,084 Bytes
93dd899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import path from 'path';
import webpack from 'webpack';
import type { Plugin } from '@docusaurus/types';

export default function webpackExtendPlugin(): Plugin<void> {
  return {
    name: 'custom-webpack-plugin',
    configureWebpack(config) {
      const isDev = process.env.NODE_ENV === 'development';

      // Use NormalModuleReplacementPlugin to forcefully replace react-table
      // This is necessary because regular aliases don't work for modules in nested node_modules
      const reactTableShim = path.resolve(__dirname, './shims/react-table.js');
      config.plugins?.push(
        new webpack.NormalModuleReplacementPlugin(
          /^react-table$/,
          reactTableShim,
        ),
      );

      // Stub out heavy third-party packages that are transitive dependencies of
      // superset-frontend components. The barrel file (components/index.ts)
      // re-exports all components, so webpack must resolve their imports even
      // though these components are never rendered on the docs site.
      const nullModuleShim = path.resolve(__dirname, './shims/null-module.js');
      const heavyDepsPatterns = [
        /^brace(\/|$)/, // ACE editor modes/themes
        /^react-ace(\/|$)/,
        /^ace-builds(\/|$)/,
        /^react-js-cron(\/|$)/, // Cron picker + CSS
        // react-resize-detector: NOT shimmed — DropdownContainer needs it at runtime
        // for overflow detection. Resolves from superset-frontend/node_modules.
        /^react-window(\/|$)/,
        /^re-resizable(\/|$)/,
        /^react-draggable(\/|$)/,
        /^ag-grid-react(\/|$)/,
        /^ag-grid-community(\/|$)/,
      ];
      heavyDepsPatterns.forEach(pattern => {
        config.plugins?.push(
          new webpack.NormalModuleReplacementPlugin(pattern, nullModuleShim),
        );
      });

      // Load YAML file as raw text string
      config.module?.rules?.push({
        test: /\.ya?ml$/,
        type: 'asset/source',
      });

      // Add swc-loader rule for superset-frontend files
      // SWC is a Rust-based transpiler that's significantly faster than babel
      const supersetFrontendPath = path.resolve(
        __dirname,
        '../../superset-frontend',
      );
      config.module?.rules?.push({
        test: /\.(tsx?|jsx?)$/,
        include: supersetFrontendPath,
        exclude: /node_modules/,
        use: {
          loader: 'swc-loader',
          options: {
            // Ignore superset-frontend/.swcrc which references plugins not
            // installed in the docs workspace (e.g. @swc/plugin-emotion)
            swcrc: false,
            jsc: {
              parser: {
                syntax: 'typescript',
                tsx: true,
              },
              transform: {
                react: {
                  runtime: 'automatic',
                  importSource: '@emotion/react',
                },
              },
            },
          },
        },
      });

      return {
        devtool: isDev ? false : config.devtool,
        cache: {
          type: 'filesystem' as const,
          buildDependencies: {
            config: [__filename],
          },
        },
        ...(isDev && {
          optimization: {
            ...config.optimization,
            minimize: false,
            removeAvailableModules: false,
            removeEmptyChunks: false,
            splitChunks: false,
          },
        }),
        resolve: {
          // Add superset-frontend node_modules to module resolution
          modules: [
            ...(config.resolve?.modules || []),
            path.resolve(__dirname, '../../superset-frontend/node_modules'),
          ],
          alias: {
            ...config.resolve.alias,
            // Ensure single React instance across all modules (critical for hooks to work)
            react: path.resolve(__dirname, '../node_modules/react'),
            'react-dom': path.resolve(__dirname, '../node_modules/react-dom'),
            // Allow importing from superset-frontend
            src: path.resolve(__dirname, '../../superset-frontend/src'),
            // Lightweight shim for @superset-ui/core that re-exports only the
            // utilities needed by components (ensureIsArray, usePrevious, etc.).
            // Avoids pulling in the full barrel which includes d3, color, query
            // modules and causes OOM. Required for Rspack which is stricter about
            // module resolution than webpack.
            '@superset-ui/core$': path.resolve(
              __dirname,
              './shims/superset-ui-core.ts',
            ),
            // Add aliases for our components to make imports easier
            '@docs/components': path.resolve(__dirname, '../src/components'),
            '@superset/components': path.resolve(
              __dirname,
              '../../superset-frontend/packages/superset-ui-core/src/components',
            ),
            // Also alias the full package path for internal imports within components
            '@superset-ui/core/components': path.resolve(
              __dirname,
              '../../superset-frontend/packages/superset-ui-core/src/components',
            ),
            // Use a shim for react-table to handle CommonJS to ES module interop
            // react-table v7 is CommonJS, but Superset components import it with ES module syntax
            'react-table': path.resolve(__dirname, './shims/react-table.js'),
            // Extension API package - resolve @apache-superset/core and its sub-paths
            // to source so the docs build doesn't depend on pre-built lib/ artifacts.
            // More specific sub-path aliases must come first; webpack matches the
            // longest prefix.
            '@apache-superset/core/components': path.resolve(
              __dirname,
              '../../superset-frontend/packages/superset-core/src/components',
            ),
            '@apache-superset/core/api/core': path.resolve(
              __dirname,
              '../../superset-frontend/packages/superset-core/src/api/core',
            ),
            '@apache-superset/core': path.resolve(
              __dirname,
              '../../superset-frontend/packages/superset-core/src',
            ),
          },
        },
      };
    },
  };
}