Skip to content

Commit

Permalink
Improve support for import identifiers, fixes FormidableLabs#69
Browse files Browse the repository at this point in the history
  • Loading branch information
jdlm-stripe committed Aug 30, 2021
1 parent 2d211bc commit eb631f0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
test/import-identifiers
28 changes: 28 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ const processNode = (replacements, nodePath, replaceFn, comparator) => {
}
};

/**
* Checks if the given identifier is a an ES module import
* @param {babelNode} identifierNodePath The node to check
* @return {boolean} Indicates if the provided node is an import specifier or references one
*/
const isImportIdentifier = (identifierNodePath) => {
const name = get(identifierNodePath, ["node", "name"]);
const containerType = get(identifierNodePath, ["container", "type"]);

if (containerType === "ImportDefaultSpecifier" || containerType === "ImportSpecifier") {
return true;
}

// Check if the identifier references a module import of the same name
const binding = identifierNodePath.scope.getBinding(name);
if (binding && binding.kind === "module") {
return true;
}

return false;
};

const memberExpressionComparator = (nodePath, value) => nodePath.matchesPattern(value);
const identifierComparator = (nodePath, value) => nodePath.node.name === value;
const unaryExpressionComparator = (nodePath, value) => nodePath.node.argument.name === value;
Expand All @@ -79,6 +101,12 @@ const plugin = function ({ types: t }) {

// const x = { version: VERSION };
Identifier(nodePath, state) {
// Don't transform import idenifiers. This is meant to mimic webpack's
// DefinePlugin behavior.
if (isImportIdentifier(nodePath)) {
return;
}

processNode(state.opts, nodePath, t.valueToNode, identifierComparator);
},

Expand Down
9 changes: 9 additions & 0 deletions test/import-identifiers/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import DONT_REPLACE_ME, { DONT_REPLACE_ME_EITHER } from 'foo';

DONT_REPLACE_ME;
DONT_REPLACE_ME_EITHER;

function childScope() {
DONT_REPLACE_ME;
DONT_REPLACE_ME_EITHER;
}
9 changes: 9 additions & 0 deletions test/import-identifiers/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import DONT_REPLACE_ME, { DONT_REPLACE_ME_EITHER } from 'foo';

DONT_REPLACE_ME;
DONT_REPLACE_ME_EITHER;

function childScope() {
DONT_REPLACE_ME;
DONT_REPLACE_ME_EITHER;
}
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ describe("babel-plugin-transform-define", () => {
path.join(__dirname, "./load-dynamic-babelrc/actual.js"),
path.join(__dirname, "./load-dynamic-babelrc/expected.js")
));

it("should not transform import identifiers jdlm", () => {
// Don't use `getBabelOpts` here cause we want to avoid preset-env which
// injects a bunch of extra code to handle es modules. It makes the test
// output harder to read.
const babelOpts = {
plugins: [
[path.resolve(__dirname, "../lib/index.js"), { DONT_REPLACE_ME: "injected" }]
]
};

return assertTransform(
path.join(__dirname, "./import-identifiers/actual.js"),
path.join(__dirname, "./import-identifiers/expected.js"), babelOpts);
});
});

describe("unit tests", () => {
Expand Down

0 comments on commit eb631f0

Please sign in to comment.