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 🙏

© 2026 – Pkg Stats / Ryan Hefner

we-script

v1.0.1

Published

we-script 微信小程序加载远程JavaScript

Readme

we-script

支持加载远程 JavaScript 脚本并执行,支持 ES5 语法

示例:

we-script-demo

Usage

小程序如何使用 npm 包,官方文档:https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html

npm install --save we-script

step1 安装完成后,点击开发者工具中的菜单栏:工具 --> 构建 npm

step2 安装完成后,点击开发者工具中的菜单栏:工具 --> 项目详情 --> 本地设置 --> [勾选] 使用 npm 模块

在需要用的页面或组件的json文件添加声明,示例:

index.json

{
  "usingComponents": {
    "we-script": "we-script"
  }
}

index.wxml

<we-script src="url">
  <view>hello we-script<view>
</we-script>

:多个we-script会并行加载及无序执行,无法保证执行顺序。如:

// 并行加载及无序执行
<we-script src="url1" />
<we-script src="url2" />
<we-script src="url3" />

如需要确保执行顺序,应该使用数组,例如:

数组方式

<we-script src="{{[url1,url2,url3]}}">
  <view>hello we-script<view>
</we-script>

we-script也支持嵌套,如:

<we-script  src="url1">
  <we-script src="url2">
    <view>hello we-script<view>
  </we-script>
</we-script>

注意: 在嵌套的情况下we-script加载和执行也是并行且无序的,因为小程序生命周期触发机制(BUG?)导致,如果想在嵌套模式下保证顺序,需要自己手动控制,示例:

<we-script bind:onLoad="loadScript" src="url1">
  <we-script wx:if="url1_load_success" src="url2">
    <view>hello we-script<view>
  </we-script>
</we-script>

重要: 远程加载执行的代码所生成的函数,变量等数据存储在we-script默认的上下文中,可通过onLoad事件获取默认上下文,或通过onInit事件自定义上下文

示例:

we-script-demo

we-script 属性

  • src

    类型:string | string[]

    要加载的远程脚本

  • text

    类型:string | string[]

    需要执行的 JavaScript 脚本字符串,text 优先级高于 src (互斥)

  • timeout

    类型:number 默认值:60000 毫秒

    设置每个远程脚本加载超时时间

  • cache

    类型:boolean

    默认值:true

    是否启用加载缓存,缓存策略是以当前请求地址作为key,缓存周期为当前用户在使用期间的生命周期。

  • once

    类型:boolean

    默认值:true

    相同上下文及相同地址的脚本只执行一次。

we-script 事件

  • onInit

    类型:(e) => void

    interface OnInitDetail {
    	getContext: () => {};
    	setContext: (context: {}) => void;
    }

    初始事件,监听该事件可获取当前执行上下文或自定义执行上下文。

    示例:

    // index.js
    {
      onInit(e){
        // 自定义执行上下文
        e.detail.setContext({
          value: 100
        })
      }
    }
    // index.wxml
    <we-script src="url" bind:onInit="onInit" />
  • onLoad

    类型:(e) => void

    interface onLoadDetail {
    	context: {};
    }

    加载并执行成功后触发

  • onError

    类型:(e) => void

    interface onErrorDetail {
    	error: any;
    }

    加载失败或执行错误后触发

其他

  • 该组件使用eval5来解析JavaScript语法,支持 ES5

  • 上生产环境前别忘记给需要加载的地址配置合法域名

  • we-script 内置类型及方法:

NaN;
Infinity;
undefined;
null;
Object;
Array;
String;
Boolean;
Number;
Date;
RegExp;
Error;
URIError;
TypeError;
RangeError;
SyntaxError;
ReferenceError;
Math;
parseInt;
parseFloat;
isNaN;
isFinite;
decodeURI;
decodeURIComponent;
encodeURI;
encodeURIComponent;
escape;
unescape;
eval;
Function;
console;
setTimeout;
clearTimeout;
setInterval;
clearInterval;
wx;

内置类型和当前运行 JavaScript 环境相关,如环境本身不支持则不支持!