Skip to content

Commit

Permalink
fix: reload the list of imports after the first change
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Aug 9, 2020
1 parent 1631446 commit a586244
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,17 @@ const transform = (file, api, options) => {
const ignoreListForExt = cjs2esm.extension.ignore
.map((ignore) => new RegExp(ignore));

// Get the list of import statements on the file.
const imports = root.find(j.ImportDeclaration)
.filter((item) => {
const importPath = item.value.source.value;
return (
importPath.startsWith('.') ||
importPath.match(/^\w/)
);
});
// =================================================
// Parse the import statements to add missing extensions.
// =================================================
imports
root.find(j.ImportDeclaration)
// Filter out the ones that are on the ignore list.
.filter((item) => {
const importPath = item.value.source.value;
return !ignoreListForExt.some((exp) => importPath.match(exp));
return !ignoreListForExt.some((exp) => importPath.match(exp)) && (
importPath.startsWith('.') ||
importPath.match(/^\w/)
);
})
.replaceWith((item) => {
const importPath = item.value.source.value;
Expand All @@ -93,7 +87,8 @@ const transform = (file, api, options) => {
);
} else {
// If it's a directory, call the function that checks for a `package.json` or an `index`.
replacement = createReplacementForFolder(absPath, importPath);
const folderReplacement = createReplacementForFolder(absPath, importPath);
replacement = folderReplacement || importPath;
}

/**
Expand All @@ -112,7 +107,7 @@ const transform = (file, api, options) => {
// Parse the modules modifications.
// =================================================
if (cjs2esm.modules.length) {
imports
root.find(j.ImportDeclaration)
// Filter out the import statments that don't need to be modified.
.filter((item) => {
const importPath = item.value.source.value;
Expand Down

0 comments on commit a586244

Please sign in to comment.