Skip to content

Commit

Permalink
feat: cjs named exports
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 4, 2020
1 parent 55bdb30 commit 1007306
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 5 deletions.
22 changes: 20 additions & 2 deletions core/instrument/src/babel/extract-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const traverseExports = (results: ExportTypes) => {
const { specifiers, source } = path.node;
if (Array.isArray(specifiers)) {
specifiers.forEach(specifier => {
globals[specifier.local.name] = {
localExports[specifier.local.name] = {
name: specifier.local.name,
internalName: specifier.imported
? specifier.imported.name
Expand Down Expand Up @@ -99,7 +99,25 @@ export const traverseExports = (results: ExportTypes) => {
};
}
},

AssignmentExpression: (path: any) => {
//exports.Button = Buttton;
const { node } = path;
if (
node.left &&
node.right &&
node.left.object &&
node.left.object.name === 'exports'
) {
const exportedName = node.left.property.name;
const localName = node.right.name;
const namedExport = localExports[localName];
if (namedExport) {
namedExport.internalName = namedExport.name;
namedExport.name = exportedName;
results.named[exportedName] = namedExport;
}
}
},
ExportSpecifier: (path: any) => {
const { node } = path;
const localName = node.local.name;
Expand Down
32 changes: 31 additions & 1 deletion core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import * as path from 'path';
import traverse from '@babel/traverse';
import { CodeLocation } from '@component-controls/specification';
import { ImportTypes, traverseImports } from './extract-imports';
import { traverseExports, ExportTypes } from './extract-exports';
import {
traverseExports,
ExportTypes,
ExportType,
EXPORT_ALL,
} from './extract-exports';

export interface FollowImportType {
exportedAs: string;
Expand Down Expand Up @@ -55,6 +60,31 @@ export const followImports = (
);
}
}
const allExports: ExportType[] = Object.keys(exports.named).reduce(
(acc: ExportType[], key: string) => [...acc, exports.named[key]],
[],
);
let foundInExportAll: FollowImportType | undefined;
allExports
.filter(e => e.internalName === EXPORT_ALL && e.from)
.some(e => {
if (e.from) {
const resolvedFilePath = resolve.sync(e.from, {
...resolveOptions,
basedir: folderName,
});
foundInExportAll = followImports(
baseImportedName,
resolvedFilePath,
parserOptions,
resolveOptions,
);
}
return foundInExportAll;
});
if (foundInExportAll) {
return foundInExportAll;
}
const imports: ImportTypes = {};
traverse(ast, traverseImports(imports));
const findImport = imports[baseImportedName];
Expand Down
25 changes: 23 additions & 2 deletions core/instrument/test/__snapshots__/extract-exports.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`extract-exports commonjs named export 1`] = `
Object {
"named": Object {
"Button": Object {
"internalName": "Button",
"loc": Object {
"end": Object {
"column": 23,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
"name": "Button",
},
},
}
`;

exports[`extract-exports export * from import 1`] = `
Object {
"named": Object {
Expand Down Expand Up @@ -234,11 +255,11 @@ Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 3,
"line": 1,
},
"start": Object {
"column": 9,
"line": 3,
"line": 1,
},
},
"name": "myStory",
Expand Down
51 changes: 51 additions & 0 deletions core/instrument/test/__snapshots__/follow-imports.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`follow-imports cjs named export 1`] = `
Object {
"exportedAs": "Button",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/exports/cjs-named-export.js",
"loc": Object {
"end": Object {
"column": 23,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
}
`;

exports[`follow-imports export all 1`] = `
Object {
"exportedAs": "Button",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/follow-imports/button-named-export.js",
"loc": Object {
"end": Object {
"column": 30,
"line": 1,
},
"start": Object {
"column": 28,
"line": 1,
},
},
}
`;

exports[`follow-imports export node modules 1`] = `
Object {
"exportedAs": "ControlsTable",
"filePath": "/Users/atanasster/component-controls/node_modules/@component-controls/storybook/dist/index.js",
"loc": Object {
"end": Object {
"column": 1,
"line": 66,
},
"start": Object {
"column": 28,
"line": 18,
},
},
}
`;

exports[`follow-imports one level default import 1`] = `
Object {
"exportedAs": "default",
Expand Down
2 changes: 2 additions & 0 deletions core/instrument/test/examples/exports/cjs-named-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const Button = () => {};
exports.Button = Button;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ControlsTable } from '@component-controls/storybook';

export { ControlsTable as Button };
1 change: 1 addition & 0 deletions core/instrument/test/examples/follow-imports/cjs-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Button } from '../exports/cjs-named-export';
1 change: 1 addition & 0 deletions core/instrument/test/examples/follow-imports/export-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './button-named-export';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Button } from './button-from-node-nodules';
6 changes: 6 additions & 0 deletions core/instrument/test/extract-exports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ describe('extract-exports', () => {
),
).toMatchSnapshot();
});

it('commonjs named export', () => {
expect(
extractExportsForFile('./examples/exports/cjs-named-export.js'),
).toMatchSnapshot();
});
});
24 changes: 24 additions & 0 deletions core/instrument/test/follow-imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,28 @@ describe('follow-imports', () => {
),
).toMatchSnapshot();
});
it('export all', () => {
expect(
extractFollowImportsForFile(
'Button',
'./examples/follow-imports/export-all.js',
),
).toMatchSnapshot();
});
it('cjs named export', () => {
expect(
extractFollowImportsForFile(
'Button',
'./examples/follow-imports/cjs-import.js',
),
).toMatchSnapshot();
});
it('export node modules', () => {
expect(
extractFollowImportsForFile(
'Button',
'./examples/follow-imports/button-from-node-nodules.js',
),
).toMatchSnapshot();
});
});

0 comments on commit 1007306

Please sign in to comment.