Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add moduleFederation option #938

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
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
60 changes: 49 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,35 @@ export default function resolveProperty({ types: t, template }) {
return t.stringLiteral(importArg.node.value)
}

return ({ callPath, funcPath }) =>
t.objectMethod(
return ({ callPath, funcPath }) => {
const { isBrowser } = process

if (moduleFederation) {
if ('isBrowser' in process) {
if (isBrowser === true) {
// eslint-disable-next-line no-console
console.warn(
'You are using Module Federation with target browser in webpack config. This is not recommended, cause it will disable code-splitting on client-side. Please use target "node" or false.',
)
}
} else {
// eslint-disable-next-line no-console
console.warn(
'process.isBrowser not found. Please use LoadablePlugin in webpack config.',
)
}
}

const targetTemplate =
moduleFederation && isBrowser === false ? '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) }),
),
)
}
}
13 changes: 13 additions & 0 deletions packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ class LoadablePlugin {
apply(compiler) {
this.compiler = compiler

const { webpack } = compiler

const defs = {
'process.isBrowser':
compiler.options.target === 'web' ||
compiler.options.target === undefined,
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To go with my suggestion of adding a serverSideModuleFederation argument to the webpack plugin, if we do then this logic could throw an error for misconfiguration:

Suggested change
const defs = {
'process.isBrowser':
compiler.options.target === 'web' ||
compiler.options.target === undefined,
}
if (this.opts.serverSideModuleFederation === true &&
compiler.options.target === 'web' ||
compiler.options.target === undefined
) {
throw new Error(`Enabling server-side module federation support for a client-side Webpack target (${compiler.options.target}) is unnecessary and will effectively disable all code-splitting.`
}


// eslint-disable-next-line global-require
new ((webpack && webpack.DefinePlugin) || require('webpack').DefinePlugin)(
defs,
).apply(compiler)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with my suggestion this would become

Suggested change
new ((webpack && webpack.DefinePlugin) || require('webpack').DefinePlugin)(
defs,
).apply(compiler)
if (this.opts.serverSideModuleFederation) {
// eslint-disable-next-line global-require
new ((webpack && webpack.DefinePlugin) || require('webpack').DefinePlugin)({
serverSideModuleFederation: true
}).apply(compiler)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job @bwhitty, I will test it here!! 🚀

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theKashey I am not being able to get the value of serverSideModuleFederation passed to DefinePlugin during the execution of the resolveProperty function, do you have some idea on how can I access this value?
I already tried with:

  • process.serverSideModuleFederation
  • global.serverSideModuleFederation
  • only serverSideModuleFederation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ScriptedAlchemy I got these snippets from NodeFederationPlugin do you have some idea why I can't get the serverSideModuleFederation during babel compilation execution?


const version = 'jsonpFunction' in compiler.options.output ? 4 : 5

// Add a custom chunk loading callback
Expand Down