• Stars
    star
    332
  • Rank 126,550 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

One Click, Offline, CommonJS Modules in the browser

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

More Repositories

1

VimBox

Simple, Modern MacVim Configuration
Vim Script
915
star
2

flex

Reason CSS Flexbox implementation
Reason
388
star
3

FaxJs

Fax Javascript Ui Framework
JavaScript
375
star
4

rehp

ReHp
OCaml
222
star
5

paradoc

One Click Docs
JavaScript
178
star
6

pesy

Builds simple esy native packages with minimal configuration.
Shell
114
star
7

flatlandia

Vim colorscheme based on flatland with Airline integration.
Vim Script
113
star
8

vim-reasonml

esy + vim + reason +the latest Merlin
Vim Script
108
star
9

CommonML

Simple OCaml Development Workflow on CommonJS
CSS
96
star
10

reason-project-ideas

ReasonML Projects That People Have Expressed Demand For.
86
star
11

navigating-reason

Overview of Reason tooling options for the curious.
70
star
12

reactapp

Starter App For React and CommonJS
JavaScript
51
star
13

esy-issues

Easy ISSUES
JavaScript
49
star
14

vim-taste

Colors based on elementaryOS
Vim Script
34
star
15

reasonml-manual

ReasonML Manual
HTML
33
star
16

native-reason-react-roadmap

Public design discussion about various approaches to building Native Reason React.
24
star
17

PackageJsonForCompilers

Proposal for allowing compiled languages to use the package.json sandbox model
24
star
18

VimCompleteLikeAModernEditor

Make Vim complete like a modern editor - does the right thing. Works with Ultisnips and neocomplete.
Vim Script
23
star
19

reason-wall-demo

Wall (NanoVG port) demo converted to Reason.
OCaml
15
star
20

VimSplitBalancer

Vim Script
14
star
21

common-native-reason-issues

Common Issues When Developing Native Reason Projects
13
star
22

effective-esy-packaging

Effective Esy Packaging Guide
12
star
23

VimCleanColors

Collection of Clean Vim ColorSchemes With Airline Themes
Vim Script
11
star
24

moodules

OO style Libraries for Dune
Reason
8
star
25

file-driven-development

Utilities for a stateless, serverless development environment.
8
star
26

VimJSXHint

Inline JSHint error highlighting with support for JSX
JavaScript
7
star
27

react

React is a JavaScript library for building user interfaces. It's declarative, efficient, and extremely flexible. What's more, it works with the libraries and frameworks that you already know.
JavaScript
7
star
28

moodules-demo

Demo of virtual libraries implemented in user space.
JavaScript
5
star
29

esy-pesy-starter

Starter example using pesy
C++
3
star
30

refmt-as-ocp-indent

A quick workaround for Nuclide LSP bridge
Shell
3
star
31

VimAutoMakeDirectory

Asks to create directories in Vim when needed
Vim Script
3
star
32

VimJSDocSnippets

Automatically create JSDoc comments for functions using UltiSnips for Vim
Vim Script
3
star
33

create-reason

Esy template for Reason Projects.
3
star
34

VimCloser

Make Vim close tabs the way every other editor does - by switching to the next tab to the left.
Vim Script
2
star
35

atom-reason

CoffeeScript
2
star
36

npm-react-core

NPM build of Facebook's React JavaScript framework.
JavaScript
2
star
37

crayon

Vim Colorscheme Generator
1
star
38

CommonMLAnotherExampleDependency

Another example dependency for https://github.com/jordwalke/CommonML
OCaml
1
star
39

ocamlbuild-js_of_ocaml

An ocamlbuild plugin for js_of_ocaml
OCaml
1
star
40

node-haste

Haste implementation for node
JavaScript
1
star
41

MacVimSmartGUITabs

Shows GUI tabs only when not in fullscreen mode.
Vim Script
1
star
42

npm-check

Check for outdated, incorrect, and unused dependencies.
JavaScript
1
star
43

VimLastTab

Switch to the most recently used Tab
Vim Script
1
star
44

EditorsWithVimMode

List and summaries of editors that support vim mode. Brief evaluations of completeness.
1
star
45

CommonMLExampleDependency

Example dependency for https://github.com/jordwalke/CommonML
OCaml
1
star