Giter Club home page Giter Club logo

mod-1's Introduction

#modJS

简介

modJS是一套的前端模块加载解决方案。与传统的模块加载相比,modJS会根据产品实际使用场景,自动选择一种相应的方案,使最终的实现非常轻量简洁。

使用

模块的定义

  • modJS是一个精简版的AMD/CMD规范,并不完全遵守AMD/CMD规范,目的在于希望给使用者提供一个类似nodeJS一样的开发体验,同时具备很好的线上性能。

使用define来定义一个模块:

define (id, factory)

在平常开发中,我们只需写factory中的代码即可,无需手动定义模块。发布工具会自动将模块代码嵌入factory的闭包里。

factory提供了3个参数:require, exports, module,用于模块的引用和导出。

  • modJS的发布工具会保证你的程序在使用之前,所有依赖的模块都已加载。因此当我们需要一个模块时,只需提供一个模块名即可获取:

require (id)

和NodeJS里获取模块的方式一样,非常简单。

因为所需的模块都已预先加载,因此require可以立即返回该模块。

  • 考虑到有些模块无需在启动时载入,因此modJS提供了可以在运行时异步加载模块的接口:

require.async (names, onload, onerror)

names可以是一个模块名,或者是数组形式的模块名列表。

当所有都加载都完成时,onload被调用,names对应的所有模块实例将作为参数传入。

如果加载错误或者网络超时,onerror将被触发。

超时时间可以通过require.timeout设置,默认为5000(ms)。

使用require.async获取的模块不会被发布工具安排在预加载中,因此在完成回调之前require将会抛出模块未定义错误。

require.loadJs (url)

异步加载脚本文件,不做任何回调

require.loadCss ({url: cssfile})

异步加载CSS文件,并添加到页面

require.loadCss ({content: csstext})

创建一个样式列表并将css内容写入

##说明

modJS只实现了AMD的一个子集,如果需要使用完整兼容AMD规范的版本,请使用amd目录下的esl-mod.js,这个版本是基于esl版本基础之上实现了fis的需求。

相关项目

自动化框架:fis

自动添加define插件:fis-postprocessor-jswrapper

mod-1's People

Contributors

2betop avatar fouber avatar hefangshi avatar oxund avatar waltershen avatar wangcheng714 avatar zjcqoo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.