Version: 5.x

create-single-spa

Single-spa offers a CLI for those who prefer autogenerated and managed configurations for webpack, babel, jest, etc. You do not have to use the CLI in order to use single-spa.

The CLI is called create-single-spa (Github link). It is primarily intended for the creation of new projects, but may also be useful for migrating existing projects (especially migrating CRA or frameworkless projects).

Installation and Usage#

If you wish to have create-single-spa globally available, run the following in a terminal

npm install --global create-single-spa
# or
yarn global add create-single-spa

Then run the following:

create-single-spa

Alternatively, you may use create-single-spa without global installation:

npm init single-spa
# or
npx create-single-spa
# or
yarn create single-spa

This will open up a CLI prompt asking you what kind of project you want to create or update.

CLI arguments#

You may pass arguments to create-single-spa like so:

# Different ways of doing the same thing
create-single-spa --framework react
npm init single-spa --framework react
npx create-single-spa --framework react
yarn create single-spa --framework react

Here are the available CLI options:

--dir#

You may specify which directory create-single-spa runs in through either of the following ways:

# Two ways of doing the same thing
create-single-spa my-dir
create-single-spa --dir my-dir

--moduleType#

You can specify which kind of microfrontend you are creating with the --moduleType CLI argument:

create-single-spa --moduleType root-config
create-single-spa --moduleType app-parcel
create-single-spa --moduleType util-module

--framework#

You can specify which framework you're using with the --framework CLI argument. Note that if you specify a framework that you may omit the --moduleType, as it is inferred to be app-parcel.

create-single-spa --framework react
create-single-spa --framework vue
create-single-spa --framework angular

--layout#

When generating a root config, the --layout CLI argument indicates that you want to use single-spa-layout in your root config.

Project types#

create-single-spa asks you if you'd like to create a single-spa application, a utility module, or a root-config. All three module types assume that you are using the recommended setup.

If you select that you'd like to create a single-spa application, you will be prompted for which framework you'd like to choose. React is implemented with premade configurations for babel + webpack + jest. Angular is implemented with Angular CLI and single-spa-angular. Vue is implemented with Vue CLI and vue-cli-plugin-single-spa.

NPM packages#

Within the create-single-spa repo, there are several NPM packages. The following sections document each package:

create-single-spa#

Github project

The core CLI, which invokes generator-single-spa.

generator-single-spa#

Github project

A Yeoman generator that prompts the user and then creates files. This is primarily invoked via the create-single-spa CLI, but can also be composed if you'd like to customize it.

webpack-config-single-spa#

Github project

A shareable, customizable webpack config that is used for utility modules and single-spa applications.

Installation#

npm install --save-dev webpack-config-single-spa webpack-merge
# or
yarn add --dev webpack-config-single-spa webpack-merge

Usage#

const webpackMerge = require('webpack-merge');
const singleSpaDefaults = require('webpack-config-single-spa');
module.exports = (webpackConfigEnv, argv) => {
const defaultConfig = singleSpaDefaults({
// The name of the organization this application is written for
orgName: 'name-of-company',
// The name of the current project. This usually matches the git repo's name
projectName: 'name-of-project',
// See https://webpack.js.org/guides/environment-variables/#root for explanation of webpackConfigEnv
webpackConfigEnv,
// The CLI commands in the package.json script that triggered this build
argv,
// optional
// This changes whether package names that start with @your-org-name are
// treated as webpack externals or not. Defaults to true
orgPackagesAsExternal: true,
// optional, defaults to 1
// This is the rootDirectoryLevel that is passed to https://github.com/joeldenning/systemjs-webpack-interop
rootDirectoryLevel: 1,
// optional, defaults to false
// Disable html-webpack-plugin (and standalone-single-spa-webpack-plugin) entirely
// This is intended for root configs, but can be used in other cases, too
disableHtmlGeneration: false
});
return webpackMerge.smart(defaultConfig, {
// modify the webpack config however you'd like to by adding to this object
});
};

webpack-config-single-spa-react#

Github project

A shareable, customizable webpack config that adds react-specific configuration to webpack-config-single-spa.

Installation#

npm install --save-dev webpack-config-single-spa-react webpack-merge
# or
yarn add --dev webpack-config-single-spa-react webpack-merge

Usage#

const webpackMerge = require('webpack-merge');
const singleSpaDefaults = require('webpack-config-single-spa-react');
module.exports = (webpackConfigEnv, argv) => {
const defaultConfig = singleSpaDefaults({
// The name of the organization this application is written for
orgName: 'name-of-company',
// The name of the current project. This usually matches the git repo's name
projectName: 'name-of-project',
// See https://webpack.js.org/guides/environment-variables/#root for explanation of webpackConfigEnv
webpackConfigEnv,
// The CLI commands in the package.json script that triggered this build
argv,
// optional
// This changes whether package names that start with @your-org-name are
// treated as webpack externals or not. Defaults to true
orgPackagesAsExternal: true,
// optional, defaults to 1
// This is the rootDirectoryLevel that is passed to https://github.com/joeldenning/systemjs-webpack-interop
rootDirectoryLevel: 1,
// optional, defaults to {}
// This controls the options given to standalone-single-spa-webpack-plugin
// See https://github.com/single-spa/standalone-single-spa-webpack-plugin#usage
standaloneOptions: {
}
});
return webpackMerge.smart(defaultConfig, {
// modify the webpack config however you'd like to by adding to this object
});
};

webpack-config-single-spa-ts#

Github project

A shareable, customizable webpack config that adds typescript-specific configuration to webpack-config-single-spa. Note that webpack-config-single-spa-ts has a peerDependency on typescript.

Installation#

npm install --save-dev webpack-config-single-spa-ts webpack-merge
# or
yarn add --dev webpack-config-single-spa-ts webpack-merge

Usage#

const webpackMerge = require('webpack-merge');
const singleSpaDefaults = require('webpack-config-single-spa-ts');
module.exports = (webpackConfigEnv, argv) => {
const defaultConfig = singleSpaDefaults({
// The name of the organization this application is written for
orgName: 'name-of-company',
// The name of the current project. This usually matches the git repo's name
projectName: 'name-of-project',
// See https://webpack.js.org/guides/environment-variables/#root for explanation of webpackConfigEnv
webpackConfigEnv,
// The CLI commands in the package.json script that triggered this build
argv,
});
return webpackMerge.smart(defaultConfig, {
// modify the webpack config however you'd like to by adding to this object
});
};
const singleSpaTs = require('webpack-config-single-spa-ts');
// Alternatively, you may modify a webpack config directly
const myOtherWebpackConfig = {
/* ... */
};
const finalConfig = singleSpaDefaults.modifyConfig(myOtherWebpackConfig);

webpack-config-single-spa-react-ts#

Github project

A shareable, customizable webpack config that creates a webpack config that works with both react and typescript. Note that webpack-config-single-spa-react-ts simply merges the config from webpack-config-single-spa-react with that of webpack-config-single-spa-ts.

Installation#

npm install --save-dev webpack-config-single-spa-react-ts webpack-merge
# or
yarn add --dev webpack-config-single-spa-react-ts webpack-merge

Usage#

const webpackMerge = require('webpack-merge');
const singleSpaDefaults = require('webpack-config-single-spa-react-ts');
module.exports = (webpackConfigEnv, argv) => {
const defaultConfig = singleSpaDefaults({
// The name of the organization this application is written for
orgName: 'name-of-company',
// The name of the current project. This usually matches the git repo's name
projectName: 'name-of-project',
// optional
// This changes whether package names that start with @your-org-name are
// treated as webpack externals or not. Defaults to true
orgPackagesAsExternal: true,
// See https://webpack.js.org/guides/environment-variables/#root for explanation of webpackConfigEnv
webpackConfigEnv,
// optional
// This changes whether package names that start with @your-org-name are
// treated as webpack externals or not. Defaults to true
orgPackagesAsExternal: true,
// The CLI commands in the package.json script that triggered this build
argv,
// optional, defaults to 1
// This is the rootDirectoryLevel that is passed to https://github.com/joeldenning/systemjs-webpack-interop
rootDirectoryLevel: 1
// optional, defaults to false.
// When true, this removes html-webpack-plugin and standalone-single-spa-webpack-plugin
disableHtmlGeneration: false
})
return webpackMerge.smart(defaultConfig, {
// modify the webpack config however you'd like to by adding to this object
})
}

single-spa-web-server-utils#

The single-spa-web-server-utils package is a collection of functions that help when implementing a web server for an index.html file. This package can be used to inline import maps into an HTML, which helps with the performance of your application. Additionally, it can be used to modify a browser import map so that it's suitable for usage in NodeJS for dynamic module loading and server rendering (Dynamic Module Loading and Server Rendering)).

The web server utils poll the import map from a URL and generate a browserImportMap and nodeImportMap from the response.

Installation#

npm install --save single-spa-web-server-utils
# alternatively
yarn add single-spa-web-server-utils

getImportMaps#

The getImportMaps function accepts an object parameter and returns a promise that resolves with an object with two import maps: browserImportMap and nodeImportMap.

const { getImportMaps } = require('single-spa-web-server-utils');
const http = require('http');
const ejs = require('ejs');
const fs = require('fs');
const path = require('path');
const htmlTemplate = ejs.compile(
fs.readFileSync(path.resolve(process.cwd(), 'views/index.html'), 'utf-8'),
);
http.createServer((req, res) => {
getImportMaps({
// required
// The URL at which the server
url: 'https://my-cdn.com/live.importmap',
// optional - defaults to 30000
// The ms to wait when polling the import map
pollInterval: 30000,
// optional - defaults to false
// Whether to allow for import-map-overrides via cookies sent in the request.
// More details about overrides via cookies at
// https://github.com/joeldenning/import-map-overrides/blob/master/docs/api.md#node
allowOverrides: true,
// optional - only needed when allowOverrides is true
// The IncomingMessage from an http server. This is used to gather
// cookies for import-map-overrides
req,
// optional
// This allows you to remove entries from the downloaded import map
// from the returned `nodeImportMap`. This is useful for customizing
// an import map that is used in the browser so that it can be used
// for dynamic NodeJS module loading. Each key is a string import specifier.
// Keys that you return `true` for are preserved in the nodeImportMap.
nodeKeyFilter(key) {
return true;
},
}).then(({ browserImportMap, nodeImportMap }) => {
console.log(browserImportMap, nodeImportMap);
// Example of how to inline a browser import map
const htmlWithInlinedImportMap = htmlTemplate({
importMap: browserImportMap,
});
res.setResponseHeader('Content-Type', 'text/html');
res.status(200).send(htmlWithInlinedImportMap);
// Example of how to apply a NodeJS import map
// More info at https://github.com/node-loader/node-loader-import-maps
global.nodeLoader.setImportMapPromise(Promise.resolve(nodeImportMap));
import('module-in-import-map');
});
});

Customizing Webpack#

The create-single-spa CLI internally uses webpack-merge to merge together webpack configs. Additionally, the CLI generates a webpack.config.js file in each project where you can customize the webpack config further via webpack-merge.

Merging rules#

When merging webpack rules, use webpack-merge's mergeWithRules function to avoid duplicate rules.

Example

Loaders#

webpack-config-single-spa and its variants often depend on webpack loaders. Because webpack loaders are loaded via file path, it's possible to accidentally have duplicate copies of the same loader, if the same loader is also installed in both webpack-config-single-spa and in your project. This can result in errors.

To avoid duplicate copies of loaders, first check whether it is already installed by wepback-config-single-spa before adding it to your own project (see package.json). If the loader is listed there, then do not install it into your project, too. If you already have the loader installed in your project, uninstall it.

When referencing a loader that is installed as a dependency of webpack-config-single-spa, use require.resolve to ensure the loader is imported from the correct path:

const { mergeWithRules } = require('webpack-merge');
const singleSpaDefaults = require('webpack-config-single-spa');
module.exports = (webpackConfigEnv) => {
const defaultConfig = singleSpaDefaults({
orgName: "react-mf",
projectName: "styleguide",
webpackConfigEnv,
});
return mergeWithRules({
module: {
rules: {
test: "match",
use: "replace",
},
},
})(defaultConfig, {
module: {
rules: [
{
test: /\.css$/i,
use: [
// Use require.resolve to ensure the correct loader is used
require.resolve("style-loader", {
paths: [require.resolve("webpack-config-single-spa")],
}),
require.resolve("css-loader", {
paths: [require.resolve("webpack-config-single-spa")],
}),
"postcss-loader",
],
},
],
},
})
}