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

@xytoki/asset-loader

v0.2.2

Published

A webpack loader that catches all incoming assets, preloads it when needed, and replace the url to blob: url using @vue/reactive.

Downloads

6

Readme

asset-loader

一个(基本)无感预加载资源文件的Loader,支持webpack和vite。

原理

将被导入的资源文件的url替换为实现了toString函数、继承String的特殊对象,因此预加载执行之前对资源文件URL的调用不受影响,执行之后资源文件url全部变为blob URL,保证加载完全。

此时,只需要使资源文件地址的使用(如DOM挂载)晚于预加载,即可实现资源文件使用时地址全部被替换的效果。

案例

vue-router配合使用

vue-router的beforeResolve是较为接近刚完成模块的动态导入的钩子。因此,在此处完成动态加载,可确保组件创建时URL被替换完毕。

import { ensure } from '@xytoki/asset-loader'
router.beforeResolve(async () => {
    await ensure((loaded, total) => {
        // const progress = loaded / total
    })
})

usage in vite

import viteAssetLoader from '@xytoki/asset-loader/dist/vite';
export default defineConfig({
    plugins: [
        viteAssetLoader(
            /**
              如果需要指定被加载的文件,这里唯一的参数是一个正则。
              否则,将会使用vite的assetsInclude匹配被加载的文件。
             */
        )
    ],
});

usage in vue-cli for webpack4.x

config.module.rule('svg').use('asset-loader').loader('@xytoki/asset-loader/dist/webpack').before('file-loader')
for (let m of ['images', 'media', 'fonts']) {
    config.module.rule(m).use('asset-loader').loader('@xytoki/asset-loader/dist/webpack').before('url-loader')
}

usage in vue-cli for webpack5.x

config.module.rule('asset-loader').type('asset').set('resourceQuery', /asset-loader-raw/)
for (let m of ['images', 'media', 'fonts', 'svg']) {
    config.module.rule(m).type('javascript/auto').use('asset-loader').loader('@xytoki/asset-loader/dist/webpack')
}

其他API

为方便使用,除了替换URL之外,asset-loader还对图片进行了特殊处理,创建了对应的Image对象。 您可以方便地将此对象渲染至Canvas或传递给需要的库。

import { asset } from '@xytoki/asset-loader'
import image from './image.png'
const imageAsset = asset(image);
// image实际上也是asset对象,但为了减少错误和类型安全,建议使用asset函数。
const imageObj = imageAsset.imageElement
const blob = imageAsset.blob

更多细节可以参照src/runtime/index.ts