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
38 changes: 28 additions & 10 deletions packages/babel-plugin/src/properties/resolve.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
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)
`
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 +38,18 @@ export default function resolveProperty({ types: t, template }) {
return t.stringLiteral(importArg.node.value)
}

return ({ callPath, funcPath }) =>
t.objectMethod(
return ({ callPath, funcPath }) => {
const targetTemplate = process.env.serverSideModuleFederation
? '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) }),
),
)
}
}
30 changes: 29 additions & 1 deletion packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ class LoadablePlugin {
writeToDisk,
outputAsset = true,
chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__',
serverSideModuleFederation = false,
} = {}) {
this.opts = { filename, writeToDisk, outputAsset, path, chunkLoadingGlobal }
this.opts = {
filename,
writeToDisk,
outputAsset,
path,
chunkLoadingGlobal,
serverSideModuleFederation,
}

// The Webpack compiler instance
this.compiler = null
Expand Down Expand Up @@ -101,6 +109,26 @@ class LoadablePlugin {
apply(compiler) {
this.compiler = compiler

const { webpack } = compiler

if (
(this.opts.serverSideModuleFederation === true &&
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.

these parens make this logic wrong I think? should be

Suggested change
(this.opts.serverSideModuleFederation === true &&
compiler.options.target === 'web') ||
compiler.options.target === undefined
this.opts.serverSideModuleFederation === true &&
(compiler.options.target === 'web' || compiler.options.target === undefined)

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, it was not correct, just fixed, but it was not the problem

) {
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.`,
)
}

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

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

// Add a custom chunk loading callback
Expand Down