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

webpack-dev-server-dynamic-entry

v1.0.3

Published

use dynamic entry to speed ​​up compilation for multiple entry in webpack

Downloads

14

Readme

webpack-dev-server-dynamic-entry

在webpack多入口编译中,如果入口很多,在开发时第一次启动编译会用很久的时间。

但是实际上有些入口是用不到的,编译它完全是浪费时间

在开发环境时可以使用动态入口,在入口被访问的时候才去编译它,以此来减少编译时间

原理

使用函数作为webpack配置的entry,用中间件拦截devServer的收到的每一个请求,当请求命中入口文件,把入口添加到entry里,然后让webpack重新编译,等编译成功再返回请求的数据

entry一定要在调webpack(config)之前去拦截,改成函数,看了webpack暴露的接口,可以动态添加入口,没有删除入口

最后的问题关键是维护一个请求路径到entry的映射, 这里的解决方案有:

  • 提供pathMaps配置,正则和entry的对应的集合
  • ensuePage函数默认行为,路径为'/',加载index配置的entry,否则去遍历所有的entryName,看是否在路径中,如果在就编译该entry
  • 重写ensuePage函数,此操作不覆盖pathMaps配置

使用

// webpack.config.js

const dynamicEntryPlugin = require('webpack-dev-server-dynamic-entry')
const dynamicEntry = dynamicEntryPlugin(entry, {index, ensuePage, pathMaps})

module.exports = {
  entry: isDev ? dynamicEntry.entry : entry,
  devServer: {
    before: dynamicEntry.before
  }
}

例子

cd ./example
npm install
npm start

只在开发模式的devServer的情况下使用

options配置

pathMaps

数组:默认无,路径到entry的映射,如下

{
  pathMaps: [
    {
      from: '/', // string, regex,
      to: '' // string (entry name), function(addEntry, match): Promise<any>
    }
  ]
}

ensuePage

函数或者非真值:devServer每次收到请求ensuePage都会被调用

async function ensuePage(req, res, addEntry, remainEntry): Promise

如果提供了pathMaps,不需要再使用ensuePage,可以设置为false,避免多余的去匹配路由操作

index

字符串:配置请求路径为'/'时,编译的入口,在ensuePage为默认值时生效,默认值为'main'。


# addEntry,在ensuePage中使用addEntry
返回promise,动态添加入口,并等待编译完成

## 参数
- name, string 必需 entry name
- path, string 非必需

## 使用场景
- 入口已在allEntry中,不传path
- 新创建的文件夹,根据请求路径,对应到入口路径动态添加入口