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

webpack5-assets-load-error-demotion-webpack-plugin

v1.0.2

Published

前端资源加载解决方案

Downloads

7

Readme

webpack5-assets-reload-webpack-plugin

Front-end resource disaster recovery solutions

Development ideas can be found in this article;

Currently only webpack 5 is supported, if you need more versions please raise issues to me, I will deal with it as soon as possible.

Install

npm i webpack5-assets-reload-webpack-plugin -D

Usage

example

demo:

// webpack.config.js
const { AssetsReloadWebpackPlugin } = require('webpack5-assets-reload-webpack-plugin')

module.exports = {
  // ....
  plugins: [
    new AssetsReloadWebpackPlugin({
      // inlineAssets
      inlineAssets: [
        ['https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min1.css', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css'],
        ['https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue2.js', 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js'],
        ['https://cdnjs.cloudflare.com/ajax/libs/vuex/2.5.0/vuex.js', "https://cdnjs.cloudflare.com/ajax/libs/vuex/2.5.0/vuex.js"],
        ['https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.7/vue-router.js', "https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.7/vue-router.js"]
      ],

      // dynamicAssets
      dynamicAssets: {
        animate: ['https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min222.css', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css'],
        vue: ['https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue2.js', 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js'],
        vuex: ['https://cdnjs.cloudflare.com/ajax/libs/vuex/2.5.0/vuex.js', "https://cdnjs.cloudflare.com/ajax/libs/vuex/2.5.0/vuex.js"],
        vueRouter: ['https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.7/vue-router.js', "https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.0.7/vue-router.js"]
      },

      // chunkAssets
      chunkAssetsPublicpath: [
        'https://www.backupPublic01.com/', 
        'https://www.backupPublic02.com/', 
        'https://www.backupPublic03.com/'
      ],

      maxChunkAssetsRetries: 4,
      chunkAssetsRetryDelay: function(times) {
        return times * 1000
      }

    })
  ]
}

Distinguish between types of assets

inlineAssets

Synchronized resources inside HTML documents

such as:

<head>
  <!-- ... -->
  <link href="./test.css" rel="stylesheet" >
  <script type="text/javascript" src="./jquery.js" ></script>
<head>

chunkAssets

Asynchronously loaded js chunks are usually introduced in webpack using the following

import('lodash').then(_ => {
  // ...
})

chunk

dynamicAssets

External assets that need to be loaded manually for certain situations within the project

Because chunk assets have a problem, that is, there is no good way to unload the assets that have been loaded. When chunk a is loaded, and then loaded chunk b, the js and css of chunk a are still in effect in the dom and will not be deleted. What is the problem with this?

For example, in a project with two routes /a/b and /a/c, for some reason /a/b needs antd.css, but /a/c can't use antd.css for historical reasons, and introducing antd.css its global style will break the original /a/c and cause the layout to collapse. Even if you only introduce antd.css in /a/b, as long as the user's access path is /a/b -> /a/c, the layout will be broken.

Options

inlineAssets

options.inlineAssets

Type: two-dimensional array

Default: No default value

Description:

The addresses associated with the same assets are placed in the same array, which can be of any length, and the actual requests are tried in the order in which they are filled in, for example, asstes A, which will first try index 0, return successfully if 0 succeeds, try index 1 if 0 fails, and so on, until there are no more addresses to try.

The assets are inserted into the HTML document in the one-dimensional order of the array.

{
  inlineAssets: [
    [ 'assets A backup address 01', 'assets A backup address 02',  'assets A address 03'],
    [ 'assets B backup address 01', 'assets B backup address 02'],
    [ 'assets C backup address 01'],
  ]
}

Results Preview:

chunk

options.inlineAssetsGlobalVariable

Type: string

Default: __CDN_RELOAD__

Description:

The exception handling functions for script and link tags need to be attached to the window, and this parameter can modify the variable name used to attach to the window

chunkAssets

options.chunkAssetsPublicpath

Type: array

Default: []

Description:

The retry strategy for chunk assets is slightly different, we will first access your output.publicPath in webpack and return it if it succeeds, if it fails we will switch publicPath to the address configured by the plugin's chunkAssetsPublicpath parameter and try it one by one. If the maximum number of retries is greater than the combined length of output.publicPath + chunkAssetsPublicpath, all requests exceeding that length will use the address at the end of chunkAssetsPublicpath.

options.chunkAssetsReloadQueryValue

Type: function

Default:

function (url, times) {
  if( times === 0 ) {
    return url
  }
  if( url.indexOf('?') === -1 ) {
    return url + '?reload=' + times
  }else {
    return url + '&reload=' + times
  }
}

Description:

Control the query parameters for retry requests

reload query

options.maxChunkAssetsRetries

Type: number

Default: 3

Description: Maximum number of load retries

options.chunkAssetsRetryDelay

Type: function | number

Default: 3000

Description: The time interval between error retries. The function type accepts a parameter times, indicating the number of retries.

dynamicAssets

options.dynamicAssets

Type: object

Default: No default value

Description:

useage:

{
  dynamicAssets: {
    // [cdnName]: [backupAddress, backupAddress, ...]
    vue: [
      'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue2.js', 
      'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js'
    ]
  },
}

How to use the loader

window.$cdn.mount('vue', () => {
  console.log('success')
}, (error) => {
  console.log('error')
})

window.$cdn.mount(
  // cdnName
  'vue', 
  // tag custom Option
  {  crossorigin: 'anonymous' },
  //  callback
  () => {
    console.log('success')
  }, 
  (error) => {
    console.log('error')
  }
)

// destroy
window.$cdn.destroy('vue', () => {
  console.log('success')
}, (error) => {
  console.log('error')
})

options.dynamicAssetsGlobalVariable

Type: string

Default: $cdn

Description:

The plugin needs to hook the specific logic for loading assets to a variable in window, and this parameter controls the name of that variable.

options.inlineCore

Type: boolean

Default: true

Description Is it inline to the HTML document? False generates a separate JS file

options.inject

Type: head | body

Default: head

Description: At which tag is core code inserted when inlineCore is true