Skip to content

Commit

Permalink
fix: Support AMD bundles with multiple define statements
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Jan 6, 2022
1 parent acbc7b8 commit d905023
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ yarn add babel-plugin-amd-checker

## Babel Configuration Examples

Prepend path to utility modules to be able to import them from `utils/...` without always providing the actual full path:
Prevent the transpiler to wrap source files that are already wrapped by `define` or `require` as AMD modules:

```js
{
Expand Down Expand Up @@ -57,6 +57,23 @@ A typical configuration combined with [babel-plugin-module-resolver-standalone]
}
```

Error handling during the transpilation in a RequireJS plugin:

```js
var amdChecker = require('babel-plugin-amd-checker')
babel.registerPlugin('amd-checker', amdChecker);

var code;
try {
code = babel.transform(text, options).code;
} catch (error) {
if (!(error instanceof amdChecker.AmdDetected)) {
return onload.error(error);
}
code = text;
}
```

## Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
Expand Down
32 changes: 23 additions & 9 deletions index.js
Expand Up @@ -9,19 +9,32 @@
})(this, function () {
// Detects a call to define, require or require.config functions.
function isDefineOrRequireOrRequireConfig(path) {
var expr, callee, args, arg;
var expr, callee, args, arg, func, obj;

if (!path.isExpressionStatement()) return false;

expr = path.get("expression");
expr = path.get('expression');
if (expr.isSequenceExpression()) {
expr = expr.get('expressions')[0];
}
if (!expr.isCallExpression()) return false;

args = expr.get("arguments");
args = expr.get('arguments');
if (args.length === 0) return false;

callee = expr.get("callee");
callee = expr.get('callee');
// namespace.define(...)
if (callee.isMemberExpression()) {
obj = callee.get('object');
if (!obj.isIdentifier()) return false;
func = callee.get('property');
} else {
func = callee;
}
if (!func.isIdentifier()) return false;

// define('name', [deps], factory)
if (callee.isIdentifier({ name: "define" })) {
if (func.node.name === 'define') {
arg = args.shift();
if (arg.isStringLiteral()) {
if (args.length === 0) return false;
Expand All @@ -33,17 +46,18 @@
}
return arg.isFunctionExpression() || arg.isObjectExpression();
}

// require([deps], success, error)
if (callee.isIdentifier({ name: "require" })) {
if (func.node.name === 'require') {
arg = args.shift();
if (!arg.isArrayExpression() || args.length === 0) return false;
arg = args.shift();
return arg.isFunctionExpression();
}

// require.config(object)
return callee.isMemberExpression() &&
callee.get('object').isIdentifier({ name: "require" }) &&
callee.get('property').isIdentifier({ name: "config" });
return obj && obj.isIdentifier({ name: 'require' }) &&
func.isIdentifier({ name: 'config' });
}

// Thrown to abort the transpilation of an already AMD module.
Expand Down

0 comments on commit d905023

Please sign in to comment.