babel-webpack-package-boilerplate v2.0.0
babel-webpack-package-boilerplate
Now updated for webpack 2!
This project is meant to show how easy and useful it can be to use webpack as a tool-chain for npm packages.
Fun things in this project:
- Source code and tests for the package are written with es2015+ and
stage-0 features (specifically the features supported by the env and
stage-0 presets for babel).
See any
*.jsfiles insrcfor examples. See.babelrcfor the babel configuration, andconfig/rules.jsto see how babel was integrated into webpack. - Platform-specific polyfills and transpilation. Use of the env preset for
babel allows webpack to avoid unused polyfills and source code transforms
if the target platform supports them. See
.babelrcto see how the preset was configured to support this. - Tree-shaking transpilation through webpack 2 and es2015+ modules. Due to the statically-analyzable module system provided by next-generation JavaScript, webpack can remove unused code and dependencies at transpiletime, resulting in smaller built artifacts.
- Modules in
libdirectories can be loaded universally, like modules found innode_modules. Seesrc/main.jsandsrc/test/index.jsfor examples. Seemodulesinconfig/resolve.jsto see how this was configured. - Modules in
srcthat end with*test.jsand modules that are direct children oftestdirectories can be run as mocha test modules, which will automatically be processed using webpack.
Getting Started
Clone the repository and install dependencies with npm.
$ git clone https://github.com/resisttheurge/babel-webpack-package-boilerplate.git
$ cd babel-webpack-package-boilerplate
$ npm installBuilding
The build script defined in the package.json file uses webpack to transpile
sources in the src directory. The successfully transpiled sources
are placed in the dist folder. This folder is preserved by npm, but ignored
by git.
$ npm run buildWatch-style building is supported by the build:watch script.
$ npm run build:watchThe prestart, and prepublish scripts defined in the
package.json file reference the build script, so there's no need to run
the build script manually in those situations.
Running
Run the project with npm start.
$ npm start
hello, world!As said before, this will automatically run the build script first.
Testing
Test the project with npm test.
$ npm testWatch-style testing is supported by the test:watch script.
$ npm run test:watchPublishing
Publish the project on the local machine (for testing) with npm install.
$ npm install . -g
$ babel-webpack-package-boilerplate
hello, world!Publish the package globally with npm publish.
$ npm publish
$ npm install -g babel-webpack-package-boilerplate
$ babel-webpack-package-boilerplate
hello, world!In both cases, again, this will automatically run the build script after installation.