Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
Use npm.umd.entry config to customise the UMD build entry point
Browse files Browse the repository at this point in the history
Fixes #411
  • Loading branch information
insin committed Mar 15, 2018
1 parent 7de9145 commit 69ca959
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased (in `master`)

## Added

- Added [`npm.umd.entry` config](https://github.com/insin/nwb/blob/master/docs/Configuration.md#entry-string) to specify a different entry point for the UMD build in npm module projects. Currently, the UMD build requires an entry point which *must* have a `default` export [[#411](https://github.com/insin/nwb/issues/411)]

## Fixed

- Always add `CopyPlugin` when [`webpack.copy` config](https://github.com/insin/nwb/blob/master/docs/Configuration.md#copy-array--object) is provided [[#431](https://github.com/insin/nwb/issues/431)]
Expand Down
28 changes: 28 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ The configuration object can include the following properties:
- [`npm.esModules`](#esmodules-boolean) - toggle creation of an ES modules build
- UMD build
- [`npm.umd`](#umd-string--object--false) - configure a UMD build which exports a global variable
- [`umd.entry`](#entry-string) - use a different entry point for the UMD build
- [`umd.global`](#global-string) - global variable name exported by UMD build
- [`umd.externals`](#externals-object) - dependencies to use via global variables in UMD build
- [`package.json` fields](#packagejson-umd-banner-configuration)
Expand Down Expand Up @@ -1127,6 +1128,33 @@ module.exports = {

If you also have some external dependencies to configure, use an object containing the following properties:

###### `entry`: `String`

Specifies a module which will be the entry point for the UMD build. Otherwise the default entry (`src/index.js`) is used.

Currently, the UMD entry point *must* have a `default` export, but you might want to have named exports from the default entry point for people using your module via npm, e.g. if you're publishing a component library.

```js
module.exports = {
npm: {
umd: {
global: 'MyComponentLibrary',
entry: './umd.js',
externals: {
'react': 'React'
}
}
}
}
```

In this scenario, `umd.js` could `import *` a module which uses named exports and re-export its contents as `default`:

```js
import * as components from './src/index.js`
export default components
```
###### `global`: `String`
The name of the global variable the UMD build will export.
Expand Down
9 changes: 9 additions & 0 deletions src/config/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function processNpmBuildConfig({report, userConfig}) {
}
else {
let {
entry,
global: umdGlobal,
externals,
...unexpectedConfig
Expand All @@ -76,6 +77,14 @@ export function processNpmBuildConfig({report, userConfig}) {
)
}

if ('entry' in umd && typeOf(entry) !== 'string') {
report.error(
'npm.umd.entry',
entry,
`Must be a ${chalk.cyan('String')}`
)
}

if ('global' in umd && typeOf(umdGlobal) !== 'string') {
report.error(
'npm.umd.global',
Expand Down
2 changes: 1 addition & 1 deletion src/moduleBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function buildUMD(args, buildConfig, userConfig, cb) {
let entry = path.resolve(args._[1] || 'src/index.js')
let webpackBuildConfig = {
babel: buildConfig.babel,
entry: [entry],
entry: [userConfig.npm.umd.entry || entry],
output: {
filename: `${pkg.name}.js`,
library: userConfig.npm.umd.global,
Expand Down
24 changes: 24 additions & 0 deletions tests/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ describe('processUserConfig()', () => {
it('babel.runtime is not valid', () => {
check({babel: {runtime: 'welp'}}, 'babel.runtime', /Must be/)
})
it('npm contains unexpected prop', () => {
check({npm: {invalid: true}}, 'npm', /Unexpected prop/)
})
it('npm.cjs is an invalid type', () => {
check({npm: {cjs: 'yes'}}, 'npm.cjs', /Must be/)
})
it('npm.esModules is an invalid type', () => {
check({npm: {esModules: 'no'}}, 'npm.esModules', /Must be/)
})
it('npm.umd is an invalid type', () => {
check({npm: {umd: /invalid/}}, 'npm.umd', /Must be/)
})
it('npm.umd contains unexpected prop', () => {
check({npm: {umd: {invalid: true}}}, 'npm.umd', /Unexpected prop/)
})
it('npm.umd.entry is an invalid type', () => {
check({npm: {umd: {entry: /invalid/}}}, 'npm.umd.entry', /Must be/)
})
it('npm.umd.global is an invalid type', () => {
check({npm: {umd: {global: /invalid/}}}, 'npm.umd.global', /Must be/)
})
it('npm.umd.externals is an invalid type', () => {
check({npm: {umd: {externals: /invalid/}}}, 'npm.umd.externals', /Must be/)
})
it('webpack contains unexpected prop', () => {
check({webpack: {invalid: true}}, 'webpack', /Unexpected prop/)
})
Expand Down

0 comments on commit 69ca959

Please sign in to comment.