Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Add babel-plugin-namespace-amd-define #5

Merged
merged 3 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/babel-plugin-namespace-amd-define/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
14 changes: 14 additions & 0 deletions packages/babel-plugin-namespace-amd-define/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "babel-plugin-namespace-amd-define",
"version": "0.0.0",
"description": "A Babel plugin to namespace (prefix) AMD define() calls.",
"main": "lib/index.js",
"scripts": {
"build": "babel --source-maps -D -d lib src",
"prepublish": "npm run build"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-es2015": "^6.24.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`correctly namespaces unqualified define calls 1`] = `
"
Liferay.Loader.define([], function () {});"
`;

exports[`does not namespace already qualified define calls 1`] = `
"
Other.Namespace.define([], function () {});"
`;

exports[`only namespaces the first appearance of define() in the source 1`] = `
"
if (window.define) {
Liferay.Loader.define([], function () {
console.log(define('this should not be namespaced'));
});
}
Liferay.Loader.define('this should not be namespaced');"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as babel from 'babel-core';
import plugin from '../index';

it('correctly namespaces unqualified define calls', () => {
const source = `
define([], function(){})
`;

const { code } = babel.transform(source, {
plugins: [plugin],
});

expect(code).toMatchSnapshot();
});

it('does not namespace already qualified define calls', () => {
const source = `
Other.Namespace.define([], function(){})
`;

const { code } = babel.transform(source, {
plugins: [plugin],
});

expect(code).toMatchSnapshot();
});

// It is not clear what would be the best way to handle multiple define calls in
// the code.
// On the one hand, it is faster to only change the first appearance as 99% of
// the time we will be processing AMD modules with define calls issued by us.
// On the other hand, some third party modules (like jQuery, for instance) issue
// define() calls inside their code. We expect only one such call, but if
// someone does anything more exotic than that, this plugin could "fail".
it('only namespaces the first appearance of define() in the source', () => {
const source = `
if(window.define) {
define([], function() {
console.log(define('this should not be namespaced'));
});
}
define('this should not be namespaced')
`;

const { code } = babel.transform(source, {
plugins: [plugin],
});

expect(code).toMatchSnapshot();
});
43 changes: 43 additions & 0 deletions packages/babel-plugin-namespace-amd-define/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Valid babel plugin options are:
* namespace: 'Liferay.Loader'
* @return {object} a babel visitor
*/
export default function({ types: t }) {
const namespaceVisitor = {
ExpressionStatement(path, state) {
if (state.namespaced) {
path.stop();
}

const node = path.node;
const expression = node.expression;

if (t.isCallExpression(expression)) {
const callee = expression.callee;

if (t.isIdentifier(callee, { name: 'define' })) {
const namespace = this.opts.namespace || 'Liferay.Loader';

callee.name = `${namespace}.define`;

state.namespaced = true;
path.stop();
}
}
},
};

return {
visitor: {
Program: {
exit(path, { opts }) {
// We must traverse the AST again because the third party
// transform-es2015-modules-amd emits its define() call after
// Program exit :-(
path.traverse(namespaceVisitor, { opts });
},
},
},
};
}
2 changes: 0 additions & 2 deletions packages/liferay-npm-build-tools-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-preset-es2015": "^6.24.1"
},
"dependencies": {
}
}