Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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) }),
),
)
}
}
27 changes: 26 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,23 @@ class LoadablePlugin {
apply(compiler) {
this.compiler = compiler

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

if (this.opts.serverSideModuleFederation) {
Object.defineProperty(process, 'serverSideModuleFederation', {
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 that's a fancy way to "teleport" settings.
However, the particular way you did it will break some assumptions of node creating a hard to understand "gap" for a few (rare) users of thread-loader or similar stuff.

Ease to fix. Just store what is required in process.env - a special place designed for this.

value: true,
writable: false,
})
}

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

// Add a custom chunk loading callback
Expand Down