1.0.4 ā€¢ Published 5 years ago

as-umd-module v1.0.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 years ago

as-umd-module

NPM version License Build Status Twitter

A TypeScript library. It converts JavaScript values to a UMD formatted string.

Install

Node:

NPM repository

https://nodei.co/npm/as-umd-module.png?downloads=true&downloadRank=true&stars=true

npm install as-umd-module

GitHub repository

npm install @lopatnov/as-umd-module

Browser:

<script src="//lopatnov.github.io/as-umd-module/dist/as-umd-module.js"></script>

Import package to the project

import asUmdModule from "as-umd-module";

or

var asUmdModule = require("as-umd-module")

Convert JavaScript values into umd module string

asUmdModule(...values: IModuleValue[]) => string

where

interface IModuleValue {
  name: string;
  exports: any;
}

Example of using:

let umdModule = asUmdModule({
    name: "onmessage",
    exports: function(e) {
        console.log('Worker: Message received from main script');
        let result = multiply(e.data[0], e.data[1]);
        if (isNaN(result)) {
            postMessage('Please write two numbers');
        } else {
            let workerResult = 'Result: ' + result;
            console.log('Worker: Posting message back to main script');
            postMessage(workerResult);
        }
    }
}, {
    name: "multiply",
    exports: function(a, b) {
        return a * b;
    }
});

console.log(umdModule);

/***
Expected result of umdModule variable is string like
"(function (global, factory) {
        typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
        typeof define === 'function' && define.amd ? define(['exports'], factory) :
        (global = global || self, factory(global));
    }(this, function (exports) { 'use strict';
    exports.onmessage = function(e) {
        console.log('Worker: Message received from main script');
        let result = multiply(e.data[0], e.data[1]);
        if (isNaN(result)) {
            postMessage('Please write two numbers');
        } else {
            let workerResult = 'Result: ' + result;
            console.log('Worker: Posting message back to main script');
            postMessage(workerResult);
        }
    };
    exports.multiply = function(a, b) {
        return a * b;
    };

    Object.defineProperty(exports, '__esModule', { value: true });
}));"

***/

Demo

See, how it's working: https://runkit.com/lopatnov/as-umd-module-demo

Test it with a runkit: https://npm.runkit.com/as-umd-module

TBD

ā€” Resolve common references

Rights and Agreements

License Apache-2.0

Copyright 2019 Oleksandr Lopatnov

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.0

5 years ago