Deletes all files from the RequireJS bundle output directory, which do not start with the bundle name (<bundle>.js
and <bundle>.js.map
).
// Delete minified file copies that were concatenated to the bundle.
onModuleBundleComplete: function (data) {
const pruneBundleOutput = nodeRequire('requirejs-prune-bundle-output')
pruneBundleOutput('../output', data.name)
},
This module can be installed in your project using NPM or Yarn. Make sure, that you use Node.js version 6 or newer.
npm i -D requirejs-prune-bundle-output
yarn add requirejs-prune-bundle-output
RequireJS copies and minifies all source file to the output directory. However, when module bundles are used, only the bundle is needed to be deployed as the build output.
Let us have the following project structure, where the list of bundled modules is included in each component as index.js
:
sources
├── animals
│ ├── animal.js
│ ├── cat.js
│ ├── dog.js
│ ├── counter.js
│ └── index.js
├── loader
│ └── index.js
└── places
├── garden.js
└── index.js
With the following build configuration:
modules: [
{ name: 'loader/index' },
{ name: 'animals/index', exclude: [ 'loader/index' ] },
{ name: 'places/index', exclude: [ 'loader/index', 'animals/index' ] }
],
dir: '../output'
When built by r.js
, the following output will appear in the target directory:
output
├── loader
│ ├── index.js
│ └── index.js.map
├── animals
│ ├── animal.js
│ ├── cat.js
│ ├── dog.js
│ ├── counter.js
│ ├── index.js
│ └── index.js.map
└── places
├── garden.js
├── index.js
└── index.js.map
All files except for index.js*
should be removed before deploying the project output. When calling pruneBundleOutput
from the bundle completion callback:
modules: [
{ name: 'loader/index' },
{ name: 'animals/index', exclude: [ 'loader/index' ] },
{ name: 'places/index', exclude: [ 'loader/index', 'animals/index' ] }
],
dir: '../output',
onModuleBundleComplete: function (data) {
const pruneBundleOutput = nodeRequire('requirejs-prune-bundle-output')
pruneBundleOutput('../output', data.name)
},
The extra files will be removed from the directories with the output bundles, leaving only the files to deploy:
output
├── loader
│ ├── index.js
│ └── index.js.map
├── animals
│ ├── index.js
│ └── index.js.map
└── places
├── index.js
└── index.js.map
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
Copyright (c) 2019 Ferdinand Prantl
Licensed under the MIT license.