0.0.1-beta.2 • Published 10 months ago

@planningcenter/module-federation-builder v0.0.1-beta.2

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

Module Federation Builder

@planningcenter/module-federation-builder is a builder helper function that will use esbuild and rollup to generate files for your library for use with Module Federation.

Usage

yarn add -D @planningcenter/module-federation-builder

To use:

import build from "@planningcenter/module-federation-builder"

build({
  // these are the imports that will be exposed and what files are their source.
  exposes: {
    "./": "./src/index.ts",
    "./Box": "./src/Box/index.ts",
  },
  // where the files will be built to
  outputPath: "dist",
  // peer dependencies that will be externalized. These need to be provided via the importmap.
  peerDependencies: ["react", "react-dom"],
  // dependencies that can use a shared dependency with the consuming apps.
  sharedDependencies: ["zustand"],
  // exports from shared dependencies that are not needed (and not exposed)
  skip: ["zustand/context"],
})

API

type RollupModuleFederationOptions = {
  /**
	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 path to your tsconfig file.
	defaults to  "./tsconfig.json"
	**/
  tsConfigPath?: string
  /**
	The workspaceRoot is the root folder to where to load files from. defaults
	to the current root folder of the rollup config.
	**/
  workspaceRoot?: string
}

function build(options: RollupModuleFederationOptions): Promise<void>