GitXplorerGitXplorer
x

vite-plugin-commonjs-externals

public
65 stars
8 forks
2 issues

Commits

List of commits on branch master.
Verified
0e0e337f1475c043289c7d6e43e591dece6813ec

default support vue sfc and readme added about optimizeDeps.exclude (#8)

ddusionlike committed 2 years ago
Unverified
6d895f4b91f1668b15df2ef4701a9bdd412c24b7

release: 0.1.1

xxiaoxiangmoe committed 4 years ago
Unverified
6d74ff128ff08ceb587f843c86a694b2527293cb

refactor: extract function transformEsm

xxiaoxiangmoe committed 4 years ago
Unverified
9c99a1e464e5687c368a3cd8daee766bc9a1f0bb

Optional chaining

committed 4 years ago
Unverified
33537ac3a317936f26a2b113f302469dc6d68008

Initial commit

xxiaoxiangmoe committed 4 years ago

README

The README file for this repository.

vite-plugin-commonjs-externals npm

Provides commonjs externals support for Vite.

Description

Prevent bundling of certain esm imported packages and instead retrieve these external dependencies at runtime by commonjs require.

For example:

import commonjsExternals from 'vite-plugin-commonjs-externals';

const externals = ['path', /^electron(\/.+)?$/];

export default {
  optimizeDeps: {
    exclude: externals,
  },
  plugins: commonjsExternals({
    externals,
  }),
};

This will convert it

import fs from 'fs';
import * as path from 'path';
import e1 from 'electron';
import e2, * as e3 from 'electron/main';

console.log({ fs, path, e1, e2, e3 });

to

import * as fs from 'fs';
const path = (() => {
  const mod = require('path');
  return mod?.__esModule
    ? mod
    : Object.assign(Object.create(null), mod, {
        default: mod,
        [Symbol.toStringTag]: 'Module',
      });
})();
const { default: e1 } = (() => {
  const mod = require('electron');
  return mod?.__esModule
    ? mod
    : Object.assign(Object.create(null), mod, {
        default: mod,
        [Symbol.toStringTag]: 'Module',
      });
})();
const e3 = (() => {
  const mod = require('electron/main');
  return mod?.__esModule
    ? mod
    : Object.assign(Object.create(null), mod, {
        default: mod,
        [Symbol.toStringTag]: 'Module',
      });
})();
const { default: e2 } = e3;
console.log({ fs, path, e1, e2, e3 });

React + Electron renderer Config Example

// vite.config.ts
import { defineConfig } from 'vite';
import { escapeRegExp } from 'lodash';
import reactRefresh from '@vitejs/plugin-react-refresh';
import builtinModules from 'builtin-modules';
// For two package.json structure
import pkg from '../the-path-to-main-process-dir/package.json';
// For single package.json structure
import pkg from './package.json';
import commonjsExternals from 'vite-plugin-commonjs-externals';

const commonjsPackages = [
  'electron',
  'electron/main',
  'electron/common',
  'electron/renderer',
  'original-fs',
  ...builtinModules,
  ...Object.keys(pkg.dependencies).map(
    name => new RegExp('^' + escapeRegExp(name) + '(\\/.+)?$')
  ),
] as const;

export default defineConfig({
  optimizeDeps: {
    exclude: commonjsPackages,
  },
  plugins: [reactRefresh(), commonjsExternals({ externals: commonjsPackages })],
});