const path = require('path');

const CopyPlugin = require('copy-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

const tsConfigPath = path.join(__dirname, '../tsconfig.json');
const distDir = path.join(__dirname, '../app/dist/webview');

module.exports = {
  entry: require.resolve('@opensumi/ide-webview/lib/electron-webview/host-preload.js'),
  target: 'node',
  output: {
    filename: 'host-preload.js',
    path: distDir,
    clean: true,
  },
  node: false,
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.json', '.less'],
    plugins: [
      new TsconfigPathsPlugin({
        configFile: tsConfigPath,
      }),
    ],
  },
  mode: 'development',
  devtool: 'eval',
  module: {
    // https://github.com/webpack/webpack/issues/196#issuecomment-397606728
    exprContextCritical: false,
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        options: {
          configFile: tsConfigPath,
        },
      },
    ],
  },
  externals: [
    function ({ request }, callback) {
      if (['node-pty', '@parcel/watcher', 'spdlog', 'nsfw', 'electron'].indexOf(request) !== -1) {
        return callback(null, 'commonjs ' + request);
      }
      callback();
    },
  ],
  resolveLoader: {
    modules: [path.join(__dirname, '../node_modules')],
    extensions: ['.ts', '.tsx', '.js', '.json', '.less'],
    mainFields: ['loader', 'main'],
  },
  plugins: [
    new CopyPlugin({
      patterns: [
        {
          from: require.resolve('@opensumi/ide-webview/lib/electron-webview/plain-preload.js'),
          to: path.join(distDir, 'plain-preload.js'),
        },
      ],
    }),
  ],
};
