Skip to content

Commit

Permalink
feat: add new moduleFederation option
Browse files Browse the repository at this point in the history
  • Loading branch information
brunos3d committed Dec 15, 2022
1 parent ede5558 commit 6c24c14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
6 changes: 4 additions & 2 deletions packages/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const properties = [

const LOADABLE_COMMENT = '#__LOADABLE__'

const loadablePlugin = declare((api, { defaultImportSpecifier = 'loadable' }) => {
const loadablePlugin = declare((api, babelOptions) => {
const { defaultImportSpecifier = 'loadable' } = babelOptions

const { types: t } = api

function collectImportCallPaths(startPath) {
Expand All @@ -33,7 +35,7 @@ const loadablePlugin = declare((api, { defaultImportSpecifier = 'loadable' }) =>
return imports
}

const propertyFactories = properties.map(init => init(api))
const propertyFactories = properties.map(init => init(api, babelOptions))

function isValidIdentifier(path, loadableImportSpecifier, lazyImportSpecifier) {
// `loadable()`
Expand Down
41 changes: 30 additions & 11 deletions packages/babel-plugin/src/properties/resolve.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { getImportArg } from '../util'

export default function resolveProperty({ types: t, template }) {
const buildStatements = template`
if (require.resolveWeak) {
return require.resolveWeak(ID)
}
return eval('require.resolve')(ID)
`
export default function resolveProperty(
{ types: t, template },
{ moduleFederation },
) {
const templates = {
federated: template`
require(ID)
if (require.resolveWeak) {
return require.resolveWeak(ID)
}
return eval('require.resolve')(ID)
`,
standard: template`
if (require.resolveWeak) {
return require.resolveWeak(ID)
}
return eval('require.resolve')(ID)
`,
}

function getCallValue(callPath) {
const importArg = getImportArg(callPath)
Expand All @@ -27,11 +41,16 @@ export default function resolveProperty({ types: t, template }) {
return t.stringLiteral(importArg.node.value)
}

return ({ callPath, funcPath }) =>
t.objectMethod(
return ({ callPath, funcPath }) => {
const targetTemplate = moduleFederation ? 'federated' : 'standard'

return t.objectMethod(
'method',
t.identifier('resolve'),
funcPath.node.params,
t.blockStatement(buildStatements({ ID: getCallValue(callPath) })),
t.blockStatement(
templates[targetTemplate]({ ID: getCallValue(callPath) }),
),
)
}
}

0 comments on commit 6c24c14

Please sign in to comment.