Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I import webpack.config.ts from an external reference? #2042

Open
gaoyan123 opened this issue Nov 3, 2022 · 1 comment
Open

Can I import webpack.config.ts from an external reference? #2042

gaoyan123 opened this issue Nov 3, 2022 · 1 comment

Comments

@gaoyan123
Copy link

gaoyan123 commented Nov 3, 2022

No description provided.

@gaoyan123
Copy link
Author

Can you import webpack.config.ts from external references in styleguide.config.js?

  webpackConfig: Object.assign(
    {
      module: {
        rules: [
          {
            test: /\.(js|jsx|ts|tsx)?$/,
            loader: require.resolve('babel-loader'),
            exclude: /node_modules/,
            query: {
              cacheDirectory: true,
              plugins: [['import', { libraryName: 'antd', style: true }]],
            },
          },
          {
            test: /\.(css|less)?$/,
            use: [
              { loader: require.resolve('style-loader') },
              { loader: require.resolve('css-loader') },
              {
                loader: require.resolve('less-loader'),
                options: {
                  javascriptEnabled: true,
                  modifyVars: true,
                },
              },
            ],
          },
          {
            test: /\.png?$/,
            use: 'file-loader',
          },
          {
            test: /\.svg$/,
            loader: 'url-loader',
          },
        ],
      },
    },
    require(path.resolve(__dirname, './build/webpack.config.base.ts'))
  ),

My webpack.config.ts is as follows

import path from 'path'
import webpack, { Configuration } from 'webpack'

import WebpackBar from 'webpackbar'
import TerserPlugin from 'terser-webpack-plugin'
import devConfig from './dev.config'

const { env } = devConfig
const { NODE_ENV, BUILD_ENV = 'dev' } = process.env
const ENV_CONFIG = env[BUILD_ENV]
const webpackConfig: Configuration = {
  mode: NODE_ENV as 'development' | 'production',

  node: {
    __dirname: true,
    __filename: false,
  },

  resolve: {
    alias: {
      '@': path.resolve(__dirname, '../app'),
      '@root': path.resolve(__dirname, '../'),
    },
    extensions: ['.ts', '.tsx', '.js'],
  },
  externals: {
    // nodegit: 'commonjs2 nodegit',
    'better-sqlite3': 'commonjs2 better-sqlite3',
    electronUpdater: 'commonjs2 electron-updater',
  },
  plugins: [
    new webpack.DefinePlugin(
      ((): { [key: string]: any } => {
        const defines = {}
        const variables = Object.assign({}, ENV_CONFIG.variables)
        Object.keys(variables).forEach(key => {
          const val = variables[key]
          defines[`process.env.${key}`] = typeof val === 'string' ? `"${val}"` : JSON.stringify(val)
        })
        defines['$mainApi'] = 'global.__$mainApi'
        defines['$api'] = 'global.__$api'
        defines['$netApi'] = 'global.__$netApi'
        defines['$tools'] = 'global.__$tools'
        defines['$store'] = 'global.__$store'
        // defines['$websocket'] = 'global.__$websocket'
        defines['$jsMind'] = 'global.__$jsMind'
        defines['$intl'] = 'global.__$intl'
        return defines
      })()
    ),
    new WebpackBar(),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),
  ] as webpack.Plugin[],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant