项目作者: bjarneo

项目描述 :
UMD Bundler
高级语言: JavaScript
项目地址: git://github.com/bjarneo/nanon.git
创建时间: 2017-01-25T16:17:33Z
项目社区:https://github.com/bjarneo/nanon

开源协议:MIT License

下载


THIS PACKAGE IS NOT MAINTAINED

NOTE: THIS PACKAGE IS NOT MAINTAINED


nanon


nanon

This bundler aims to solve my personal UMD bundling. It makes it possible to transpile and bundle ES6/ES2017, React, and Preact with ease just by running one command.

Behind the scenes this wrapper uses Webpack and Google Closure Compiler.

What is UMD?

Usage

  1. $ npm i --save-dev nanon
  1. Usage
  2. $ nanon entrypoint.js output.bundle.js --name MyLibrary
  3. $ # Or
  4. $ nanon --input entrypoint.js --output output.bundle.js --name MyLibrary
  5. Options
  6. --input, -i Input ES6/ES2017 entrypoint
  7. --output, -o Output bundle name
  8. --name, -n Library name
  9. --polyfill, -p Should polyfill ES2017 features
  10. --watch, -w Turn on watch mode (webpack --watch)

Instead of using arguments within the CLI, nanon fetches config set in package.json

  1. "nanon": {
  2. "input": "index.js",
  3. "output": "dist/library-name.min.js",
  4. "name": "MyLibrary"
  5. },

Example

Projects using nanon

  1. $ nanon index.js output.bundle.js --name MyLibrary
  2. $ # Or if you've defined config in package.json
  3. $ nanon

Now you can import your code:

CommonJS

  1. const MyLibrary = require('./output.bundle');

RequireJS

  1. require(['MyLibrary'], function(MyLibrary) {
  2. MyLibrary.doSomething();
  3. });

Browser

  1. // Available on the window object
  2. // window['MyLibrary']
  3. // window.MyLibrary
  4. MyLibrary.doSomething();

Preact

Example creating UMD widgets with Preact (same applies for React):

  1. // index.js
  2. import { h, render, Component } from 'preact';
  3. class Clock extends Component {
  4. constructor() {
  5. super();
  6. this.state.time = Date.now();
  7. }
  8. componentDidMount() {
  9. this.timer = setInterval(() => {
  10. this.setState({ time: Date.now() });
  11. }, 1000);
  12. }
  13. componentWillUnmount() {
  14. clearInterval(this.timer);
  15. }
  16. render(props, state) {
  17. let time = new Date(state.time).toLocaleTimeString();
  18. return <span>{ time }</span>;
  19. }
  20. }
  21. module.exports = function MyClock() {
  22. render(<Clock ></Clock>, document.getElementById('clock'));
  23. }
  1. $ nanon index.js index.min.js --name=MyClock

Now you can include the bundle and use the clock:

  1. <script src="index.min.js"></script>
  2. <div id="clock"></div>
  3. <script>
  4. MyClock();
  5. </script>

Missing

  • Currently it bundles the react/preact framework. Will make this optional.
  • Watch mode is buggy
  • Development mode

Inspired by

Contribution

Contributions are appreciated.

License

MIT-licensed. See LICENSE.