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

resource-load-webpack-plugin

v1.0.48

Published

webpack资源加载改成ajax请求

Downloads

6

Readme

resource-load-webpack-plugin

webpack4 资源加载改成ajax请求

webpack中的js加载默认用的是script标签, 本插件会把它替换成ajax拉取再eval。 支持失败重载和localStorage缓存js

因为是ajax请求js, 如果用跨域资源,需要配置跨域头: Access-Control-Allow-Origin。

安装

npm i resource-load-webpack-plugin --save-dev

使用

const jmResourceLoadPlugin = require('resource-load-webpack-plugin');

plugins: [
   new jmResourceLoadPlugin({
        // 在页面加入全局代码 , 比如要在index.html中加入一段全局处理逻辑或定义变量
        globalScript: `console.log('resource-load-webpack-plugin');`,
        // 加载完成回调,可以注入一段js,用户加载完成的一个自定义逻辑,比较上报日志等,非必须
        // type: 'success' | 'fail' | 'timeout',   url: 资源地址, xhr: 加载资源的ajax对象或script标签       
        // retryTime 如果指定了重试参数,这里表示当前是第几次重试,正常加载是0,后面累加
        loadCompleteTemplate: `console.log('load:', type, url, xhr, retryTime)`,
        // 加载前处理逻辑,可以针对加载url初始化
        // 这里的是当加载失败时,去除域名,改为从主站获取,如果不需要请删除这里
         // loadType: 'ajax' | 'tag' 支持ajax和script加载,可以根据条件修改这个变量 默认 ajax
         // url  当前加载的地址
        loadBeforeTemplate: `
                            if(retryTime > 0) url = url.replace(/^(http(s)?:)?\\/\\/[^\\/]+/i, '');
                            loadType = '${process.env.NODE_ENV === 'production'?'ajax':'tag'}';
                        `,
        // 失败重试次数,默认2, 最大只能5次,否则采用5
        retryTime: 2,
        // 是否用于加载CSS
        cssLoad: true,
        // 脚本解析方式,开发环境下用标签tag来解析比较好调试
        syncRunType: process.env.NODE_ENV === 'production'?'eval':'tag',
        // 缓存url的正则, 不配置就不进行local缓存, 建议只缓存核心变化少的资源
        // 请保证唯一性,key会当作缓存的key,比如下面示例的 chunk-common
        // 可以在正式环境才缓存,开发环境请不要配置,不然不好调试
        localCacheRegs: process.env.NODE_ENV === 'production'?
        {
            "chunk-common.js": /js\/chunk-common/i,
            "chunk-common.css": /css\/chunk-common/i,
        }: null
    }),
]

配置示例:example