Giter Club home page Giter Club logo

namespace-wrapper's Introduction

Namespace Wrapper

This simple node application will wrap a JavaScript library written using AMD and compressed using r.js into a configurable namespace

The Problem

Imagine you have a compressed JavaScript library, named library1.js, defining the Foo and Bar modules; you would use it like this:

<script src="require.js"></script>
<script src="library1.js"></script>
<script>
  require(["Foo","Bar"],
  function(Foo,Bar) {
    //do stuff
  });
</script>

Now you want introduce a new functionality from library2.js. Unfortunately this library also defines two modules named Foo and Bar. The following code will obviously not work:

<script src="require.js"></script>
<script src="library1.js"></script>
<script src="library2.js"></script>
<script>
  require(["Foo","Bar","Foo","Bar"],
  function(Foo,Bar,Foo,Bar) {
    //do stuff
  });
</script>

The Solution

The solution would be to be able to refer to the modules of library1.js as library1/Foo and library1/Bar while referring to the modules of library2 using library2/Foo and library2/Bar

<script src="require.js"></script>
<script src="library1.js"></script>
<script src="library2.js"></script>
<script>
  require(["library1/Foo","library1/Bar","library2/Foo","library2/Bar"],
  function(Foo1,Bar1,Foo2,Bar2) {
    //do stuff
  });
</script>

Usage

The process requires node.js to run, be sure to have it installed on your machine.

Install the requirejs module using npm:

npm install requirejs

run the wrapper

node namespace-wrapper.js NAMESPACE ORIGINAL_LIBRARY_FILE TARGET_WRAPPED_LIBRARY_FILE

TARGET_WRAPPED_LIBRARY_FILE will contain the namespaced library.

Missing Features

I wrote the wrapper with a specific need in mind so that some feautures that were not needed were not taken into account. Here are its current (main) shortcomings:

  • The library to be wrapped MUST never use the require method inside it but only the define one
  • The library to be wrapped CAN only refer to other modules defined inside itself. If reference to a second library are contained the second library must be wrapped with the same namespace.

namespace-wrapper's People

Contributors

mone 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.