Skip to content

Commit

Permalink
feat: supporting exports syntax #9 (#10)
Browse files Browse the repository at this point in the history
* feat: supporting exports syntax

* fix: pr build sript

* fix: trying out some things

* fix: adding some more examples

* fix: finally fixing the problem

* fix: adding comments
  • Loading branch information
MadaraUchiha-314 authored Oct 21, 2023
1 parent d551397 commit 96a8e31
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
node-version: 18.x
registry-url: 'https://registry.npmjs.org'
- run: npm install --frozen-lockfile
- run: npm build --workspaces --if-present
- run: npm run build --workspaces --if-present
27 changes: 26 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/examples/project-a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0",
"redux": "^4.2.1",
"uuid": "9.0.0"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/examples/project-a/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default {
react: {},
'react-dom': {},
uuid: {},
redux: {},
},
}),
nodeResolve(),
Expand Down
6 changes: 5 additions & 1 deletion packages/examples/project-a/src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { Component } from 'react';
import React, { Component, useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid';
import { ABC } from './abc';

export { createStore, compose } from 'redux';
export { applyMiddleware as kindlyApplyMiddleware } from 'redux';

/* eslint-disable no-console */

export async function doSomething() {
console.log('Inside doSomething()');
console.log('React version is: ', React.version);
console.log('typeof React.Component is: ', typeof Component);
console.log(useMemo);
const ReactDOM = await import('react-dom');

console.log('ReactDOM version is: ', ReactDOM.version);
Expand Down
32 changes: 31 additions & 1 deletion packages/rollup-plugin-module-federation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { PACKAGE_JSON } from './constants.js';
const IMPORTS_TO_FEDERATED_IMPORTS_NODES = {
ImportDeclaration: 'ImportDeclaration',
ImportExpression: 'ImportExpression',
ExportNamedDeclaration: 'ExportNamedDeclaration',
// ExportAllDeclaration: 'ExportAllDeclaration',
};

const REMOTE_ENTRY_MODULE_ID = '__remoteEntry__';
Expand Down Expand Up @@ -101,6 +103,34 @@ export function getFederatedImportStatementForNode(node, moduleSpecifier) {
federatedImportStms.push(moduleSpecifier);
break;
}
case IMPORTS_TO_FEDERATED_IMPORTS_NODES.ExportNamedDeclaration: {
node.specifiers.forEach((specifier) => {
switch(specifier.type) {
case 'ExportSpecifier': {
if (specifier.exported.name !== specifier.local.name) {
/**
* export { ABC as XYZ } from 'pqr';
*/
federatedImportStms.push(
`const { ${specifier.local.name} } = await ${moduleSpecifier}; export { ${specifier.local.name} as ${specifier.exported.name} }`,
);
} else {
/**
* export { ABC } from 'pqr';
*/
federatedImportStms.push(
`const { ${specifier.local.name} } = await ${moduleSpecifier}; export { ${specifier.local.name} }`,
);
}
break;
}
default: {
throw Error(`Unsupported specifier.type: ${specifier.type}`);
}
}
});
break;
}
default: {
break;
}
Expand Down Expand Up @@ -415,7 +445,7 @@ export default function federation(federationConfig) {
if (
Object.keys(IMPORTS_TO_FEDERATED_IMPORTS_NODES).includes(
node.type,
)
) && node?.source?.value
) {
/**
* At this point rollup hasn't completed resolution of the import statements in this file.
Expand Down

0 comments on commit 96a8e31

Please sign in to comment.