npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-optimizer

v1.4.3

Published

Manually Pre-Bundling of Vite

Downloads

19,050

Readme

vite-plugin-optimizer

手动版的 Vite 预构建

NPM version NPM Downloads awesome-vite

English | 简体中文

  • 兼容 Browser, Node.js and Electron
  • 自定义 Vite 预构建 Pre-Bundling 内容

安装

npm i vite-plugin-optimizer -D

使用

import optimizer from 'vite-plugin-optimizer'

export default {
  plugins: [
    optimizer({
      vue: `const vue = window.Vue; export { vue as default }`,
    }),
  ]
}

读取本地文件

optimizer({
  // 支持嵌套模块命名
  // 支持返回 Promise
  '@scope/name': () => require('fs/promises').readFile('path', 'utf-8'),
})

Node.js 与 Electron

optimizer({
  // 预构建 ipcRenderer 在 Electron 渲染进程中使用
  electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,

  // 这表示 'fs' 与 'node:fs' 同时支持
  // e.g.
  //   `import fs from 'fs'`
  //   or
  //   `import fs from 'node:fs'`
  fs: () => ({
    // 这与 `alias` 行为一致
    find: /^(node:)?fs$/,
    code: `const fs = require('fs'); export { fs as default }`;
  }),
})

高级

将 Node.js ESM 包转换成 CommonJs 模块供 Node.js/Electron 使用
e.g. execa, node-fetch

看看这 👉 vite-plugin-esmodule

API (Define)

optimizer(entries[, options])

function optimizer(entries: Entries, options?: OptimizerOptions): import('vite').Plugin;
export interface OptimizerArgs {
  /** 生成缓存文件路径 */
  dir: string;
}

export interface ResultDescription {
  /**
   * 这与 `alias` 行为一致。
   * 
   * e.g.  
   *   `import fs from 'fs'`  
   *   or  
   *   `import fs from 'node:fs'`  
   * 
   * @example
   * {
   *   // 这种写法表示同时支持 'fs' 和 'node:fs'。  
   *   find: /^(node:)?fs$/,
   *   replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js',
   * }
   */
  alias?: {
    find: string | RegExp;
    /**
     * 如果没有显式的指定,将会使用生成文件的路径作为默认值。
     */
    replacement?: string;
  };
  code?: string;
}

export interface Entries {
  [moduleId: string]:
  | string
  | ResultDescription
  | ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void);
}

export interface OptimizerOptions {
  /**
   * @default ".vite-plugin-optimizer"
   */
  dir?: string;
  resolveId?: ((id: string) => string | Promise<string | void> | void);
}

工作原理

用 Vue 来举个 🌰

optimizer({
  vue: `const vue = window.Vue; export { vue as default }`,
})
  1. 创建 node_modules/.vite-plugin-optimizer/vue.js 文件并包含下面的代码
const vue = window.Vue; export { vue as default }
  1. 创建一个 vue 的别名项,并且添加到 resolve.alias
{
  resolve: {
    alias: [
      {
        find: 'vue',
        replacement: '/User/work-directory/node_modules/.vite-plugin-optimizer/vue',
      },
    ],
  },
}

/**
 * 🚧
 * 如果你是用的是 function 并且没有返回值, 那么就不会注册 alias
 * 这种情况下, 你必须显式的返回 alias
 * 
 * e.g.
 * 
 * optimizer({
 *   async vue(args) {
 * 
 *     // ① 你可能会自己构建 `vue` 并且输出到指定的文件夹
 *     await require('vite').build({
 *       entry: require.resolve('vue'),
 *       outputDir: args.dir + '/vue',
 *     })
 * 
 *     return {
 *       alias: {
 *         find: 'vue',
 *         // ② 确保 replacement 指向 Vue 构建后的路径
 *         replacement: args.dir + '/vue',
 *       }
 *     }
 *   },
 * })
 */
  1. 默认会将 vue 添加到 optimizeDeps.exclude
export default {
  optimizeDeps: {
    // 你可以通过 `optimizeDeps.include` 避开这种行为
    exclude: ['vue'],
  },
}