Coder Social home page Coder Social logo

es6-to-amd's Introduction

ES to AMD converter

npm

ES (EcmaScript) Module to AMD (Asynchronous Module Definition) converter

Table of Contents

Background

The import/export syntax is becoming more and more popular. Given a huge, legacy AMD application it's not trivial to migrate it all at once. The converter can help you transpile the modules into the AMD syntax temporarily for backwards compatibility.

Install

npm install @buxlabs/es6-to-amd

Usage

node

Convert a single file with:

const es6toamd = require('@buxlabs/es6-to-amd');
const source = 'export default { hello: 'world' }';
const result = es6toamd(source); // define({ hello: 'world' });

Examples

ES

import Backbone from 'backbone';

export default Backbone.Model.extend({});

AMD

define(['backbone'], function (Backbone) {
    'use strict';
    return Backbone.Model.extend({});
});

There are more examples in the test/fixture directory.

Maintainers

@emilos.

Contributing

All contributions are highly appreciated! Open an issue or submit a PR.

License

MIT © buxlabs

es6-to-amd's People

Contributors

bnf avatar dependabot[bot] avatar emilos avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bnf

es6-to-amd's Issues

wrong parsing of named function expression

var Foo = function (code, message) {
    this.code = code;
    this.name = message;
}

Foo.prototype = Error.prototype;

export default Foo;

results in

var Foo = function (code, message) {
    this.code = code;
    this.name = message || "";
}

Foo.prototype = Error.prototype;

define(Foo);

export default array bug

export default ["foo", "bar"];

results with

define(["foo", "bar"]);

and the expected result is:

define(function () { return ["foo", "bar"]; });

Bug in import / export conversion

import foo from "foo";
import "bar";
import plugin from "bar.plugin";

results in

define([
    "foo",
    "bar",
    "bar.plugin"
], function (foo, plugin) {
  
});

the expected result is:

define([
    "foo",
    "bar",
    "bar.plugin"
], function (foo, /* any free var name */, plugin) {
  
});

Generate free identifier for ImportSpecifier

in:

import { Model } from 'backbone';

export default Model.extend({});

out:

define(['backbone'], function (a) {
    'use strict';
    return a.Model.extend({});
});

The above works, however if we use multiple ImportSpecifiers then the new variable new will not be assigned properly https://github.com/buxlabs/es6-to-amd/blob/master/src/class/Module.js#L41.

e.g. this will break:

import { Model } from 'backbone';
import { each } from 'underscore';

export default Model.extend({
    hello() {
        _.each([1, 2, 3], () => {});
    }
});

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.