JS library builder via webpack
Write your JS library and export it for Node.js, browser or kafe (ES5/ES6+) via webpack and Babel
$ npm install @absolunet/library-builder
Imposes some conventions to work well
index.js
webpack.config.js
dist/
↳ browser.js
↳ browser-es5.js
↳ kafe.js
↳ kafe-es5.js
↳ node.js
lib/
- Each build will be outputted in a
dist
folder with a predefined name. - Main script would be under
index.js
with its subfiles underlib
.
- Internal dependencies (which will be outputted in the final built file) are dealt with the
ES6 modules (import / export)
syntax. - External dependencies (which will be referenced in the final built file) are dealt with the
Node.js modules (require)
syntax. index.js
should end with a defaultexport
statement representing the whole library.
- External dependencies will be left untouched in the final built file thus making classic
require()
Node.js statements. (See webpack configuration) - Final usage will look like this
const mylibrary = require('@organisation/my-library'); // dist/node.js
- External dependencies will be transform to a variable assignation, assuming that the dependencies are already loaded elsewhere. (See webpack configuration)
const cl = require('cool-lib');
// Becomes
const cl = window.coolLib;
- Final usage will look like this
console.log(window.mylibrary); // dist/browser.js
- The
dist/browser.js
is a pure ES6+ built and thedist/browser-es5.js
is the same but with a Babel compilation to ES5 syntax.
- Is the same as the browser build but with the output scoped under
window.kafe
- Final usage will look like this
console.log(window.kafe.mylibrary); // dist/kafe.js
- The
dist/kafe.js
is a pure ES6+ built and thedist/kafe-es5.js
is the same but with a Babel compilation to ES5 syntax.
Your webpack.config.js
should look like this
const LibraryBuilder = require('@absolunet/library-builder');
const builder = new LibraryBuilder({
name: 'mylibrary',
root: __dirname
});
//-- Node.js
const nodeExternals = {
externals: [
'cool-lib', // Dependencies to reference and not include
'meaningful-helper'
]
};
//-- Browser
const browserExternals = {
externals: {
'cool-lib': 'window.coolLib', // Dependencies to reference and their variable counterpart
'meaningful-helper': 'window.mnfHelper'
}
};
module.exports = [
builder.config.mergeWithNode(nodeExternals), // If you want a Node.js build
builder.config.mergeWithBrowser(browserExternals), // If you want a browser ES6+ build
builder.config.mergeWithBrowserES5(browserExternals), // If you want a browser ES5 build
builder.config.mergeWithKafe(browserExternals), // If you want a kafe ES6+ build
builder.config.mergeWithKafeES5(browserExternals) // If you want a kafe ES5 build
];
Your package.json
should contain these entries
{
"main": "dist/node.js",
"browser": "dist/browser.js -OR- dist/kafe.js",
"scripts": {
"build": "node node_modules/@absolunet/library-builder/bin/build.js"
},
"devDependencies": {
"@absolunet/library-builder": "1.1.0",
"lodash.merge": "4.6.1"
},
"dependencies": {
"cool-lib": "^1.2.3",
"meaningful-helper": "^4.5.6"
}
}
This way to build you only need to run npm run build
Required
Type: String
Package name (for kafe build)
Required
Type: String
Path to root folder (typically __dirname
)
Base webpack configuration for Node.js export
Base webpack configuration for browser ES6+ export
Base webpack configuration for browser ES5 export (via Babel)
Base webpack configuration for kafe ES6+ export
Base webpack configuration for kafe ES5 export (via Babel)
Returns Object
webpack configuration
Add custom configuration to base configuration
Required
Type: Object
webpack configuration to merge with Node.js configuration
Returns Object
webpack configuration
Add custom configuration to base configuration
Required
Type: Object
webpack configuration to merge with browser ES6+ configuration
Returns Object
webpack configuration
Add custom configuration to base configuration
Required
Type: Object
webpack configuration to merge with browser ES5 configuration
Returns Object
webpack configuration
Add custom configuration to base configuration
Required
Type: Object
webpack configuration to merge with kafe ES6+ configuration
Returns Object
webpack configuration
Add custom configuration to base configuration
Required
Type: Object
webpack configuration to merge with kafe ES5 configuration
MIT © Absolunet