Giter Site home page Giter Site logo

one-click.js's Introduction

One Click, Offline, CommonJS Modules

one click

Use CommonJS modules directly in the browser with no build step and no web server.

  • No need to start up a web server just to write and run CommonJS modules in the browser.
  • Send static HTML/JS bundles to people that they can double click to open.
  • Hit reload in the browser to refresh. Works offline.

Try:

Download this repo and double click the ./example/index.html (that's sort of the whole point).

Using:

Either start with the example, or include a script tag like this in your project, specifying a data-main entrypoint module to load:

<script type="text/javascript" src="./one-click.js" data-main="./my-module.js">

It follows node resolution so:

require('something/foo.js')

Will be searched for in node_modules/foo.js.

And

require('./rel/foo.js')

Will be searched for relative to the file that is requiring it.

Not Using:

Do not use this in production. The only purpose of this utility is to make local development simpler.

Tradeoffs:

In order to make this work offline, One Click needs to initialize your modules twice, once in the background upon page load, in order to map out the dependency graph, and then another time to actually perform the module loading. You would only notice that this is happening if your modules have debugger statements in the top level module scope, or if they have some other side effects.

Details:

One of the reasons why this was challenging is because var declarations are global variables in JS, and since one-click.js doesn't do any transforms to your source files, it can't wrap them in functions to scope the variables. So instead, we embed each file in a separate iframe in order to load. This might cause some problems if you are counting on the window object being exactly equal to the window object in other modules. There may be a solution to that problem. Most of the globals on window are synchronized with the main window (except the window and document variables which cannot be synchronized).

Since each module is isolated in their own iframes, array literals [1, 2, 3] will technically be considered as originating from different Array classes depending on which module they were created in. This is already something that the web has to deal with because objects could be created in any iframe - so JS frameworks already guard against this and should work fine. The main thing they need to do is use Array.isArray instead of instanceof Array, and you should do so in your own code as well (regardless of if you use one-click.js).

If You Need To Access window or document:

Most commonJS modules and framework code will never assume there is exactly one window or document, so they tend to accept an argument for root node to their rendering functions (like ReactJS does). But if for some reason one of your modules does want to access the main window or document, it can do so with parent.window.document, or parent.window. When written that way, your code will work whether or not it in an isolated iframe module, or running on the root page (because on the root most page, parent is window).

See Also: require1k (Great reference, requires running a web server). require-polyfill

one-click.js's People

Contributors

jordwalke avatar

Stargazers

 avatar Rajendra Agrawal avatar  avatar Greg Slepak avatar Adam Wolff avatar Cyrus Goh avatar Wildan Zulfikar avatar elephantsaunter avatar Beluga avatar  avatar  avatar ptruser avatar  avatar Arman Taherian avatar Chuck Houpt avatar Avichay Eyal avatar  avatar Nadeera Sampath avatar Yasin ATEŞ avatar Dara Hak avatar  avatar hoangitk avatar savi2w avatar Diwaker Singh avatar Future Infinity avatar Constantin Câmpean avatar Alain Armand avatar 伏毅 avatar  avatar LaughingJacky avatar shuwan9 avatar MondyLab avatar  avatar Aaron Adler avatar  avatar Selim Rana avatar Titan avatar  avatar Jose Ramirez avatar Nor avatar Abhishek avatar Jun Liu avatar Seder(方进) avatar Ted Ly avatar Anatoly Belobrovik avatar 余腾靖 avatar peter lee avatar  avatar 蓝色的秋风 avatar ²¹带你去旅行♬ ♫♬ avatar  avatar Nikolas Evers avatar Wiihame Chen avatar Eleanor Carl avatar frank_wang avatar xushankun avatar xream avatar Johan Grahn avatar enoyao avatar  avatar Nguyễn Việt Hùng avatar  avatar Evgeny Bykovskikh avatar Jerel Miller avatar lin onetwo avatar PENG Rui avatar Fahad Hossain avatar MonkeyFigaro avatar Thomas Gratier avatar Martin Seeler avatar Dexin avatar Tai An Su avatar Leo avatar Lance avatar JLu avatar Phil Stubbs avatar Silent Lee avatar Sebastian Vilhelm Juhl avatar AXeL avatar John Wright avatar Seyram Alvin Ofori avatar Ralph Eclipse avatar  avatar Surya Prakash avatar layne.zhuang avatar paco avatar emmm avatar Arthur Park avatar Darryl D. avatar Alex Plex avatar Monir Abu Hilal avatar Enzo avatar Yousuf Jawwad avatar Carlos Villuendas Zambrana avatar Daniel Petelin avatar Manan Rana avatar  avatar Pierre Nel avatar Greg Santos avatar Huang Shuo-Han avatar

Watchers

Jihchi Lee avatar Assym avatar  avatar James Cloos avatar  avatar Richard avatar  avatar  avatar

one-click.js's Issues

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.