Skip to content

Commit

Permalink
Example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 7, 2019
1 parent 6c9fc60 commit 0e58e25
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
20 changes: 10 additions & 10 deletions packages/babel-plugin-transform-modules-amd/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {
import { template, types as t } from "@babel/core";

const buildWrapper = template(`
define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {
})
define(
%%MODULE_NAME%%,
%%AMD_ARGUMENTS%%,
function(%%IMPORT_NAMES%%) %%BODY%%
)
`);

export default declare((api, options) => {
Expand Down Expand Up @@ -84,20 +87,17 @@ export default declare((api, options) => {
const { body, directives } = path.node;
path.node.directives = [];
path.node.body = [];
const amdWrapper = path.pushContainer("body", [

path.pushContainer("body", [
buildWrapper({
MODULE_NAME: moduleName,

AMD_ARGUMENTS: t.arrayExpression(amdArgs),
IMPORT_NAMES: importNames,

BODY: t.blockStatement(body, directives),
}),
])[0];
const amdFactory = amdWrapper
.get("expression.arguments")
.filter(arg => arg.isFunctionExpression())[0]
.get("body");
amdFactory.pushContainer("directives", directives);
amdFactory.pushContainer("body", body);
]);
},
},
},
Expand Down
23 changes: 10 additions & 13 deletions packages/babel-plugin-transform-modules-umd/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ const buildPrerequisiteAssignment = template(`
const buildWrapper = template(`
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(MODULE_NAME, AMD_ARGUMENTS, factory);
define(%%MODULE_NAME%%, %%AMD_ARGUMENTS%%, factory);
} else if (typeof exports !== "undefined") {
factory(COMMONJS_ARGUMENTS);
factory(%%COMMONJS_ARGUMENTS%%);
} else {
var mod = { exports: {} };
factory(BROWSER_ARGUMENTS);
factory(%%BROWSER_ARGUMENTS%%);
GLOBAL_TO_ASSIGN;
%%GLOBAL_TO_ASSIGN%%;
}
})(this, function(IMPORT_NAMES) {
})
})(this, function(%%IMPORT_NAMES%%) %%BODY%%)
`);

export default declare((api, options) => {
Expand Down Expand Up @@ -204,7 +203,8 @@ export default declare((api, options) => {
const { body, directives } = path.node;
path.node.directives = [];
path.node.body = [];
const umdWrapper = path.pushContainer("body", [

path.pushContainer("body", [
buildWrapper({
MODULE_NAME: moduleName,

Expand All @@ -219,13 +219,10 @@ export default declare((api, options) => {
this.filename || "unknown",
moduleName,
),

BODY: t.blockStatement(body, directives),
}),
])[0];
const umdFactory = umdWrapper
.get("expression.arguments")[1]
.get("body");
umdFactory.pushContainer("directives", directives);
umdFactory.pushContainer("body", body);
]);
},
},
},
Expand Down

0 comments on commit 0e58e25

Please sign in to comment.