@planningcenter/vite-plugin-module-federation-rails
is a Vite Plugin meant
to work with the module_federation_rails
gem. It will build files for Module Federation, as
well as allowing module federation to function correctly with an active vite server.
yarn add -D @planningcenter/vite-plugin-module-federation-rails
In your rollup configuration, add the plugin:
import { moduleFederation } from "@planningcenter/vite-plugin-module-federation-rails"
export default {
plugins: [
moduleFederation({
// You can use this to expose files, but it is not necessary.
exposes: {},
// where the files will be built to
outputPath: "dist",
// dependencies shared with libraries loaded via module federation.
sharedDependencies: ["react", "react-dom"],
// exports from shared dependencies that are not needed (and not exposed)
skip: ["react-dom/server"],
}),
],
}
type ViteModuleFederationOptions = {
/**
The command that is run to fetch the importmap that is generated by the rails app.
The command needs to output a json object that would go in an importmap script.
defaults to `bundle exec rake module_federation_rails:build`
**/
buildCommand?: string
/**
Dev mode skips some of the minimization and optimization for
ease of understanding in development environments. It also adds some logging
of key processes for clarity.
defaults to `false`
**/
dev?: boolean
/**
`exposes` is the endpoints that are exposes for import. For instance,
a key of './' will be allow the end user to import like
`import foo from 'my-library`, where a key of './Box' will allow an import
like `import Box from 'my-library/Box`. Using more exposes allow consuming
apps to be more selective of what parts of a library they use so that they
can allow end users to only load what they are actually using.
The value associated is the relative path to the file (from the base in the
tsconfig file).
example:
exposes: {
'./': './src/index.ts',
'./Box': './src/Box/index.ts',
'./Button': './src/Button/index.ts',
'./ThemeProvider': './src/ThemeProvider/index.ts',
}
**/
exposes: { [key: string]: string }
/**
the output directory where files will be generated to.
Generally the same as the output directory in the config.
**/
outputPath: string
/**
a list of the peerDependencies that are required to be exposed and defined
by the consuming app. It is very possible that we could just infer this from
the package.json, but for now, I am manually putting this here so that we can
be a bit more explicit.
**/
peerDependencies?: string[]
/**
sharedDependencies are the names of imports that you identify as libraries
that you might want to share a version other than the version defined inside
the app. This is most often used by consuming apps for sharing peer
dependencies from a library (like react). It can also be used to share a
version of a shared library for optimization. For instance, you might have
a minimum version of `moment-js` that is required for your library, but
apps are most likely going to be using a version themselves.
Adding that library as a sharedDependency will share a
file of that library for a standard version, but it will default to using
the version set up in the app if it is shared. This is an optional
optimization and is often not needed. If issues arise from different versions
not working (a singleton is needed), then use peerDependencies.
**/
sharedDependencies?: string[]
/**
If you want to not build certain parts of a shared library, then you can add
part of the exported files to `skip` to exclude them from the build. This
is an optimization, but can often help to resolve problems when libraries
export parts of their libraries which are not ESM compatible (designed for
server side code).
**/
skip?: string[]
/**
The workspaceRoot is the root folder to where to load files from. defaults
to the current root folder of the vite config.
**/
workspaceRoot?: string
}
function ModuleFederation(options: ViteModuleFederationOptions): VitePlugin