buxlabs / amd-to-es6 Goto Github PK
View Code? Open in Web Editor NEWconvert amd to es
License: MIT License
convert amd to es
License: MIT License
Target Code
define([],function(){
function a(){}
if ("function"===typeof Object.create)
return Object.create;
else
return function(b){a.prototype=b;var c=new a;a.prototype=null;return c};
});
Expected Result
function a() {}
export default ("function" === typeof Object.create)
? Object.create
: function(b){a.prototype=b;var c=new a;a.prototype=null;return c};
Actual Result
function a() {
}
if ('function' === typeof Object.create)
return Object.create;
else
return function (b) {
a.prototype = b;
var c = new a();
a.prototype = null;
return c;
};
For example, I want to transform MyLoader.define()
to ES module. Can I provide a parameter to set the definition?
It appears that there is no support for AMD blocks the utilize ES6 fat arrow implicit return. For example, the following block...
define(['marionette'], Marionette => Marionette.ItemView.extend({}))
Throws the following error...
/Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/src/lib/generateCode.js:99
var nodes = code.filter(function (node) {
^
TypeError: Cannot read property 'filter' of undefined
at module.exports (/Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/src/lib/generateCode.js:99:21)
at Module.convert (/Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/src/class/Module.js:24:20)
at module.exports (/Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/src/converter.js:14:12)
at /Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/bin/cli.js:53:24
at Array.forEach (native)
at convertFiles (/Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/bin/cli.js:50:11)
at Object.<anonymous> (/Users/username/Sites/web/gn-web/node_modules/@buxlabs/amd-to-es6/bin/cli.js:71:5)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
Is there a way to support converting this syntax? Thanks!
It would appear both inline and block comments are completely removed during the conversion. The following...
define([
'underscore',
// Move this
'marionette'
], (_, Marionette) =>
// This is a view
Marionette.ItemView.extend({
getTemplate () {
/**
* We are doing work here
*/
return ''
}
})
)
Results in...
import _ from 'underscore';
import Marionette from 'marionette';
export default Marionette.ItemView.extend({
getTemplate() {
return '';
}
});
Is there a way to preserve comments?
define(['exports', 'foo', 'bar'], function (exports, foo, bar) {
exports.foo = foo
exports.bar = bar
})
as:
import foo from 'foo'
import bar from 'bar'
export { foo, bar }
input:
define('ember-inflector/lib/helpers/pluralize', ['exports', 'ember-inflector', 'ember-inflector/lib/utils/make-helper'], function (exports, _emberInflector, _makeHelper) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = (0, _makeHelper.default)(function (params, hash) {
var count = void 0,
word = void 0,
withoutCount = false;
if (params.length === 1) {
word = params[0];
return (0, _emberInflector.pluralize)(word);
} else {
count = params[0];
word = params[1];
if (hash["without-count"]) {
withoutCount = hash["without-count"];
}
if (parseFloat(count) !== 1) {
word = (0, _emberInflector.pluralize)(word);
}
return withoutCount ? word : count + " " + word;
}
});
});
output:
import _emberInflector from 'ember-inflector';
import _makeHelper from 'ember-inflector/lib/utils/make-helper';
Object.defineProperty(exports, '__esModule', { value: true });
export var default = (0, _makeHelper.default)(function (params, hash) {
var count = void 0, word = void 0, withoutCount = false;
if (params.length === 1) {
word = params[0];
return (0, _emberInflector.pluralize)(word);
} else {
count = params[0];
word = params[1];
if (hash['without-count']) {
withoutCount = hash['without-count'];
}
if (parseFloat(count) !== 1) {
word = (0, _emberInflector.pluralize)(word);
}
return withoutCount ? word : count + ' ' + word;
}
});
Acorn and most other parsers fail on the export var default
code.
using 0.12.0
source:
define('ember-data/-debug/index', ['exports'], function (exports) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.instrument = instrument;
function instrument(method) {
return method();
}
/*
Assert that `addedRecord` has a valid type so it can be added to the
relationship of the `record`.
The assert basically checks if the `addedRecord` can be added to the
relationship (specified via `relationshipMeta`) of the `record`.
This utility should only be used internally, as both record parameters must
be an InternalModel and the `relationshipMeta` needs to be the meta
information about the relationship, retrieved via
`record.relationshipFor(key)`.
@method assertPolymorphicType
@param {InternalModel} internalModel
@param {RelationshipMeta} relationshipMeta retrieved via
`record.relationshipFor(key)`
@param {InternalModel} addedRecord record which
should be added/set for the relationship
*/
var assertPolymorphicType = void 0;
if (true) {
var checkPolymorphic = function checkPolymorphic(modelClass, addedModelClass) {
if (modelClass.__isMixin) {
//TODO Need to do this in order to support mixins, should convert to public api
//once it exists in Ember
return modelClass.__mixin.detect(addedModelClass.PrototypeMixin);
}
if (Ember.MODEL_FACTORY_INJECTIONS) {
modelClass = modelClass.superclass;
}
return modelClass.detect(addedModelClass);
};
exports.assertPolymorphicType = assertPolymorphicType = function assertPolymorphicType(parentInternalModel, relationshipMeta, addedInternalModel) {
var addedModelName = addedInternalModel.modelName;
var parentModelName = parentInternalModel.modelName;
var key = relationshipMeta.key;
var relationshipModelName = relationshipMeta.type;
var relationshipClass = parentInternalModel.store.modelFor(relationshipModelName);
var assertionMessage = 'You cannot add a record of modelClass \'' + addedModelName + '\' to the \'' + parentModelName + '.' + key + '\' relationship (only \'' + relationshipModelName + '\' allowed)';
(true && !(checkPolymorphic(relationshipClass, addedInternalModel.modelClass)) && Ember.assert(assertionMessage, checkPolymorphic(relationshipClass, addedInternalModel.modelClass)));
};
}
exports.assertPolymorphicType = assertPolymorphicType;
});
stack:
TypeError: Cannot read property 'name' of undefined
at nodes.forEach.node (C:\code\ember-cli\node_modules\@buxlabs\amd-to-es6\src\class\Exporter.js:24:45)
at Array.forEach (<anonymous>)
at Exporter.getExports (C:\code\ember-cli\node_modules\@buxlabs\amd-to-es6\src\class\Exporter.js:19:11)
at Exporter.harvest (C:\code\ember-cli\node_modules\@buxlabs\amd-to-es6\src\class\Exporter.js:13:17)
at Module.convert (C:\code\ember-cli\node_modules\@buxlabs\amd-to-es6\src\class\Module.js:31:37)
at module.exports (C:\code\ember-cli\node_modules\@buxlabs\amd-to-es6\src\converter.js:13:12)
at Object.transform (C:\code\ember-cli\node_modules\rollup-plugin-amd\dist\rollup-plugin-amd.cjs.js:23:31)
at C:\code\rollup\dist\rollup.js:18093:48
at <anonymous>
When I add -r it is erroring on creating directories since they don't exist...?
Hello,
While adding dynamic import support the comment retention support was removed. I presume this has to do with problematics to do with dynamic imports and comments, perhaps. In any case, it is a sad day to see the support go.
Are there any things that could be done to bring it back? In this code base or Acorn?
Line breaks are currently not being preserved. If the input is
define([], function() {
var a = {
a: 97
};
var b = {
b: 98
};
});
output
var a = { a: 97 };
var b = { b: 98 };
Would it be possible to add another option to preserve line breaks?
Hi there,
Context: I'm writing Kotlin JS, which generates a AMD-style JS module, which then gets plumbed through Rollup using rollup-plugin-amd
, which in turn depends on...this.
The JS Kotlin generates initially looks like this:
define(['exports', 'kotlin'], function (_, Kotlin) {
'use strict';
var defineInlineFunction = Kotlin.defineInlineFunction;
function main(args) {
}
_.main_kand9s$ = main;
main([]);
Kotlin.defineModule('my-module', _);
return _;
});
After trickling through the build process, the final output JS is this:
var app = (function (Kotlin) {
'use strict';
Kotlin = Kotlin && Kotlin.hasOwnProperty('default') ? Kotlin['default'] : Kotlin;
var defineInlineFunction = Kotlin.defineInlineFunction;
function main(args) {
}
_.main_kand9s$ = main;
return _;
}(kotlin));
Which looks fine-ish, except for one problem -- it's assuming there's a _
global when there is none. This causes JS errors when loading the page. As an immediate workaround, I'll create a "_" object on the page to absorb exports, but that seems odd to me. Thoughts?
Thanks!
Example:
const amd_to_es6 = require('@buxlabs/amd-to-es6');
const input = `
define(function (require) {
return require('./foo');
});
`
output = amd_to_es6(input);
console.log(output)
Actual:
export default require("./foo");
Expected:
import a from "./foo";
export default a;
Coffeescript outputs code like this.
const amd_to_es6 = require('@buxlabs/amd-to-es6');
const input = `
define(function (require) {
var x, y, Foo;
x = 1;
y = 'bar';
Foo = require('foo');
exports.Foo = Foo;
});
`
output = amd_to_es6(input);
console.log(output)
var x, y, Foo;
x = 1;
y = 'bar';
Foo = require("foo");
exports.Foo = Foo;
import Foo from "foo";
var x, y;
x = 1;
y = 'bar';
exports.Foo = Foo;
I'm getting this false error message in an unexpected place of the correct working code. I suppose it is directly related to this issue. You use an outdated affected version of the cherow library in your abstract-syntax-tree library, on which this project depends.
Hi.
I've used your code in this module, https://github.com/cgkineo/babel-plugin-transform-amd-to-es6 and converted it from using https://github.com/buxlabs/abstract-syntax-tree to babel ast.
I just wanted to say thank you. You're a life-saver.
Ollie.
The transpilation seems to be fails for files with code like:
MyObj?.methodCall()
for example, I'd like it to handle code like:
define([
'react',
'enzyme',
'../path/to/Component',
], (React, {mount}, Component) => {
const wrapper = mount(
<Component
someProp={true}
otherProp={someVar}
/>
);
});
is that something you plan to support?
Hi buxlabs,
First I wanna thanks for developing this great tool. This is what exactly I'm looking for.
I see in the cli call, it supports quotes as options. Just wondering if there is such a plan to support the non-cli calls? I am using it to build a webpack loader.
Thanks again,
shaosh
Despite the help saying the default is single quotes, all of my single quotes are converted to double quotes. I've tried
amdtoes6 --src=./app/ --replace -r
amdtoes6 --src=./app/ --replace -r --quotes=single
amdtoes6 --src=./app/ --replace -r --quotes "single"
amdtoes6 --src=./app/ --replace -r --quotes="single"
Thanks
define(['react'], function ({ Component }) {})
import { Component } from 'react'
define(['exports'], function (exports) {
exports.translate = exports.t = function () {}
})
probably in the first go as:
var a = function () {}
export { a as translate, a as t }
but there might be a nicer syntax for it
can it also transform cmd to es6?
in piuccio/rollup-plugin-amd#17 I discovered that, when es6 modules are run through this plugin, dynamic import()
calls cause parsing to fail. Likely this is because we need to pass a newer ecmaVersion
to acorn, so it'll parse according to the new standard instead of the old one. This might also require upgrading acorn. Potentially it would just make sense to pass a higher ecmaVersion to acorn in this project, but if not exposing the acorn options to the consumer would also work.
โฏ amdtoes6 --src=./src --replace
C:\a\node_modules\meriyah\dist\meriyah.umd.js:188
throw new ParseError(parser.index, parser.line, parser.column, type, ...params);
^
Falls on files with async/await.
error:
SyntaxError: Duplicate export 'defaultRules'
input:
define('ember-inflector/index', ['exports', 'ember-inflector/lib/system', 'ember-inflector/lib/ext/string'], function (exports, _system) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaultRules = exports.singularize = exports.pluralize = undefined;
_system.Inflector.defaultRules = _system.defaultRules;
Object.defineProperty(Ember, 'Inflector', {
get: function get() {
Ember.deprecate('Ember.Inflector is deprecated. Please explicitly: import Inflector from \'ember-inflector\';', false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.Inflector;
}
});
Object.defineProperty(Ember.String, 'singularize', {
get: function get() {
Ember.deprecate('Ember.String.singularize() is deprecated. Please explicitly: import { singularize } from \'ember-inflector\';', false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.singularize;
}
});
Object.defineProperty(Ember.String, 'pluralize', {
get: function get() {
Ember.deprecate('Ember.String.pluralize() is deprecated. Please explicitly: import { pluralize } from \'ember-inflector\';', false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.pluralize;
}
});
exports.default = _system.Inflector;
exports.pluralize = _system.pluralize;
exports.singularize = _system.singularize;
exports.defaultRules = _system.defaultRules;
});
output:
import 'ember-inflector/lib/ext/string';
export var defaultRules = exports.singularize = exports.pluralize = undefined;
_system.Inflector.defaultRules = _system.defaultRules;
Object.defineProperty(Ember, 'Inflector', {
get: function get() {
Ember.deprecate("Ember.Inflector is deprecated. Please explicitly: import Inflector from 'ember-inflector';", false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.Inflector;
}
});
Object.defineProperty(Ember.String, 'singularize', {
get: function get() {
Ember.deprecate("Ember.String.singularize() is deprecated. Please explicitly: import { singularize } from 'ember-inflector';", false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.singularize;
}
});
Object.defineProperty(Ember.String, 'pluralize', {
get: function get() {
Ember.deprecate("Ember.String.pluralize() is deprecated. Please explicitly: import { pluralize } from 'ember-inflector';", false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.pluralize;
}
});
export default _system.Inflector;
export var pluralize = _system.pluralize;
export var singularize = _system.singularize;
export var defaultRules = _system.defaultRules;
The AMD code is generated from babel, so it is ugly to begin with.
Hi, the transformer fails when processing an AMD module which have nested ES classes defining constructors.
define(['exports', 'require'], function(exports, require) {
class MyClass {
constructor() {
this.foo = 'bar';
this._helper = new Helper();
}
}
class Helper {
constructor() {
this.name = 'Helper';
}
init() {
// THIS NESTED CLASS WILL FAIL
this._private = class Helper {
constructor() {
this.value = 123;
}
}
this._util = new Util();
}
}
function Util() {
this.name = 'Util';
this._private = class Helper {
constructor() {
this.value = 123;
}
}
}
exports.MyClass = MyClass;
});
This results in the error
SyntaxError: Line 16, column 19: Duplicate constructor method in class
at constructError (/home/user/node_modules/cherow/dist/umd/cherow.js:5625:21)
at report (/home/user/node_modules/cherow/dist/umd/cherow.js:5664:7)
at parseClassElement (/home/user/node_modules/cherow/dist/umd/cherow.js:4398:19)
This works fine if the "constructor" method isn't defined.
Hello!
For background detail see: rollup/rollup#2523
Given an AMD module with a top level this
for example: var self = this
when this is converted to an ES6 module, it should be transpiled var self = window
because as per: http://exploringjs.com/es6/ch_modules.html#_browsers-scripts-versus-modules a top level this
equates to window
but in the ES6 world a top level this = undefined
. At the moment, after the conversion to ES6 the top level this
is currently having its value changed from window to undefined
breaking scripts..
A module that executes an if
statement and also sets exports.default
is converted to an invalid ES6 module with two default exports.
Run amdtoes6
on the following AMD module:
define(["require", "exports"], function (require, exports) {
"use strict";
if (false) {}
exports.default = function foo() {};
});
(function () {
if (false) {}
})();
var a = function foo() {};
export {a as default};
(or something equivalent)
export default (function () {
if (false) {}
})();
var a = function foo() {};
export {a as default};
Error: 'default' is not exported by ...
original: https://github.com/emberjs/ember-inflector/blob/master/addon/index.js
babel to AMD:
define('ember-inflector/index', ['exports', 'ember-inflector/lib/system', 'ember-inflector/lib/ext/string'], function (exports, _system) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaultRules = exports.singularize = exports.pluralize = undefined;
_system.Inflector.defaultRules = _system.defaultRules;
Object.defineProperty(Ember, 'Inflector', {
get: function get() {
Ember.deprecate('Ember.Inflector is deprecated. Please explicitly: import Inflector from \'ember-inflector\';', false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.Inflector;
}
});
Object.defineProperty(Ember.String, 'singularize', {
get: function get() {
Ember.deprecate('Ember.String.singularize() is deprecated. Please explicitly: import { singularize } from \'ember-inflector\';', false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.singularize;
}
});
Object.defineProperty(Ember.String, 'pluralize', {
get: function get() {
Ember.deprecate('Ember.String.pluralize() is deprecated. Please explicitly: import { pluralize } from \'ember-inflector\';', false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.pluralize;
}
});
exports.default = _system.Inflector;
exports.pluralize = _system.pluralize;
exports.singularize = _system.singularize;
exports.defaultRules = _system.defaultRules;
});
AMD to ES6:
import _system from 'ember-inflector/lib/system';
import 'ember-inflector/lib/ext/string';
_system.Inflector.defaultRules = _system.defaultRules;
Object.defineProperty(Ember, 'Inflector', {
get: function get() {
Ember.deprecate("Ember.Inflector is deprecated. Please explicitly: import Inflector from 'ember-inflector';", false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.Inflector;
}
});
Object.defineProperty(Ember.String, 'singularize', {
get: function get() {
Ember.deprecate("Ember.String.singularize() is deprecated. Please explicitly: import { singularize } from 'ember-inflector';", false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.singularize;
}
});
Object.defineProperty(Ember.String, 'pluralize', {
get: function get() {
Ember.deprecate("Ember.String.pluralize() is deprecated. Please explicitly: import { pluralize } from 'ember-inflector';", false, {
id: 'ember-inflector.globals',
until: '3.0.0'
});
return _system.pluralize;
}
});
export default _system.Inflector;
export var pluralize = _system.pluralize;
export var singularize = _system.singularize;
export var defaultRules = _system.defaultRules;
original: https://github.com/emberjs/ember-inflector/blob/master/addon/lib/system.js
babel to AMD:
define("ember-inflector/lib/system", ["exports", "ember-inflector/lib/system/inflector", "ember-inflector/lib/system/string", "ember-inflector/lib/system/inflections"], function (exports, _inflector, _string, _inflections) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaultRules = exports.pluralize = exports.singularize = exports.Inflector = undefined;
_inflector.default.inflector = new _inflector.default(_inflections.default);
exports.Inflector = _inflector.default;
exports.singularize = _string.singularize;
exports.pluralize = _string.pluralize;
exports.defaultRules = _inflections.default;
});
AMD to ES6:
import _inflector from 'ember-inflector/lib/system/inflector';
import _string from 'ember-inflector/lib/system/string';
import _inflections from 'ember-inflector/lib/system/inflections';
_inflector.default.inflector = new _inflector.default(_inflections.default);
export var Inflector = _inflector.default;
export var singularize = _string.singularize;
export var pluralize = _string.pluralize;
export var defaultRules = _inflections.default;
It appears that the babel to AMD step is creating code that assumes a default export from the exporter, when there is none. Then when the AMD to ES6 comes along, it throws on no default export. This seems like either a babel incorrect transpile, or a AMD code flaw, not necessarily something wrong with this library. What do you think?
Hello!
You missed the test like this
define("hello", () => { return function () { return "hello"; }; });
And it doesn't work actually. ( Can you fix it, please?
It would appear that an AMD block that returns a function
breaks the conversion. The following...
define(['marionette'], (Marionette) =>
() => {}
)
Throws...
/Users/user/Sites/other/es6-migrate/node_modules/@buxlabs/amd-to-es6/src/lib/generateCode.js:98
var nodes = code.map(function (node) {
^
TypeError: code.map is not a function
at module.exports (/Users/user/Sites/other/es6-migrate/node_modules/@buxlabs/amd-to-es6/src/lib/generateCode.js:98:22)
at Module.convert (/Users/user/Sites/other/es6-migrate/node_modules/@buxlabs/amd-to-es6/src/class/Module.js:19:20)
at module.exports (/Users/user/Sites/other/es6-migrate/node_modules/@buxlabs/amd-to-es6/src/converter.js:14:12)
at convertFile (/Users/user/Sites/other/es6-migrate/node_modules/@buxlabs/amd-to-es6/bin/cli.js:37:20)
at Object.<anonymous> (/Users/user/Sites/other/es6-migrate/node_modules/@buxlabs/amd-to-es6/bin/cli.js:65:5)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
Example:
const amd_to_es6 = require('@buxlabs/amd-to-es6');
const input = `
define(function (require) {
var a = require('./foo').bar;
return a;
});
`
output = amd_to_es6(input);
console.log(output)
Actual:
var a = require("./foo").bar;
export default a;
Expected:
import {bar} from './foo'
export default bar;
Or Expected:
import a from './foo'
var b = a.bar;
export default b;
Hi, I was wondering if you were planning about adding source map support for this conversion utility
Not sure if that was intentional or not. piuccio/rollup-plugin-amd#14 (comment)
Related to: 63139b7#r34489951
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.