UNPKG

6.87 kBMarkdownView Raw
1# Polypacker
2### Universal javascript build and distribution tool built on top of webpack
3You might consider using polypacker over vanilla webpack if:
4* You dislike the fork and forget boilerplate pattern
5* You don't want to manage your own webpack configuration
6* You're building a universal javascript application or component
7
8Polypacker is inspired by [meteor isobuild](https://www.meteor.com/isobuild) and initially started as a fork of [jlongster/backend-with-webpack](https://github.com/jlongster/backend-with-webpack).
9
10### Disclaimer
11Polypacker is still experimental and incomplete. Some familiar webpack features are currently lacking (HMR for one) but the goal for the project is ultimately to have parity.
12
13## Quickstart
14`npm install --save-dev polypacker`
15and add something like the following to your `package.json`:
16```json
17{
18 "scripts": {
19 "start": "polypacker --run NODE",
20 "watch": "polypacker --watch",
21 "dist": "polypacker --environments [ PRODUCTION ]"
22 },
23 "polypacker": {
24 "arguments": {
25 "preset": "FULLSTACK_APPLICATION",
26 "babelPresets": [ "react" ]
27 }
28 }
29}
30```
31`npm run watch` will build development versions for both `NODE` and the `BROWSER`, and automatically run the `NODE` context by default:
32![example terminal output](https://cloud.githubusercontent.com/assets/8343799/20774555/793d7108-b71c-11e6-9fbd-c7295f459b99.png)
33It does this based on splitpoints in your code:
34```javascript
35 let server = ($ES.CONTEXT == 'NODE') ?
36 require('./server') :
37 require('./representation')
38```
39The global variable `$ES` (for ecmascript) that is injected with `DefinePlugin` currently has the structure:
40```javascript
41 $ES = {
42 ENV: ('PRODUCTION' || 'DEVELOPMENT'),
43 CONTEXT: ('NODE' || 'BROWSER')
44 }
45```
46
47## Using polypacks
48Polypacks are fullstack components distributed with `polypacker --preset FULLSTACK_COMPONENT`. Loading polypacks from an application that is itself polypacked is quite straightforward:
49```javascript
50 import DDPouchDb from 'polypack!domain-driven-pouchdb-persistence-plugin'
51```
52From a normal `node.js` specific app that isn't polypacked, just reference the distribution directly (the above gets rewritten to this anyways):
53```javascript
54 import DDPouchDb from 'domain-driven-pouchdb-persistence-plugin/dist/for/node_production'
55 // or for non es6, this time with dynamic import based on environment
56 var DDPouchDb = require(
57 'domain-driven-pouchdb-persistence-plugin/dist/for/node_' + \
58 process.NODE_ENV.toLowerCase() || 'development' \
59 ).default
60```
61## Examples
62* [fullstack react application with code splitting](https://github.com/polypacker/react-splitting-polypacker-example)
63* [self-rendering react router component](https://github.com/polypacker/example-react-router-polypack)
64
65## Plugins
66Polypacker aims to be extensible at every step of execution via plugin, allowing plugins to add CLI arguments, webpack configuration builders, and task runners. While this feature is still nasceant, you can take a look at the [simple test plugin](https://github.com/polypacker/simple-test-polypacker-plugin/blob/master/src/index.js) for a survey of the current extension points, and the [typescript plugin](https://github.com/polypacker/typescript-polypacker-plugin/blob/master/src/index.js) to see a realworld example.
67Plugins currently have to be referenced at each specific extension point in the `package.json` configuration, like so:
68```json
69 "polypacker": {
70 "arguments": {
71 "preset": "TYPESCRIPT"
72 },
73 "parser": {
74 "argumentSchema": "typescript-polypacker-plugin",
75 "presets": "typescript-polypacker-plugin"
76 },
77 "webpackConfiguration": {
78 "builders": "typescript-polypacker-plugin",
79 "moduleLoaders": "typescript-polypacker-plugin"
80 }
81 }
82```
83
84## Loaders
85Depended-upon webpack loaders will automatically be loaded, as long as there is a matching configuration in the [preconfigured webpack loaders](https://github.com/michaeljosephrosenthal/polypacker/blob/master/src/webpacker/autoLoader.js#L26-L54) or [plugins](https://github.com/polypacker/simple-test-polypacker-plugin/blob/master/src/index.js#L32).
86
87## CLI Usage / Help
88The CLI is built with [jargon-parser](https://github.com/polypacker/jargon-parser), which is still nasceant. It is slightly inaccurate in that it thinks all array options are required, and doesn't have knowledge of the `preset` and `arguments` mechanisms.
89```
90./node_modules/.bin/polypacker --help
91
92Usage: polypacker
93
94context-driven js distribution tool for multiple environments
95
96Arguments:
97
98 --entry <string> [optional] # main entry point for your program, across all contexts
99 --out <string> [optional] # destination for compiled bundle. If there are multiple, the destination of specific bundles will be decided by
100 the --combinator
101 --watch <boolean> [optional] # monitor source files for changes and recompile.
102 --chunkFilename <string> [optional] # If provided, enables code splitting with webpack.require. Examples patterns include '[id].chunk.js',
103 '[name].chunk.js'
104 --babelPresets [ <string>, ...babelPresets ] [required] # add a preset to the babel loader, between es2015 and stage-0
105 --outCombinator <string> [default: "_"] # string to combine arguments that "define" a compiler (environment, environment)
106 --outPrefix <string> [optional] # prefix for generated contextual modules. Appended to `out` directory
107 --modules <string> [optional] # where to look for modules
108 --environments [ <string>, ...environments ] [required] # an application lifecycle environment {DEVELOPMENT, PRODUCTION, etc} this distribution will run in
109 --contexts [ <string>, ...contexts ] [required] # a context {NODE, BROWSER, etc} this distribution will run in.
110 --task <string> [optional] # the task to run. If non is specified, it will be inferred from other arguments.
111 --run <string> [optional] # Which context to run on compilation, if any
112 --runner <string> [optional] # Which runner to run the selected compiler with, if any
113 --logLevel <any> [default: ERROR] # VERBOSE will output webpack stats and warnings
114 --preset <string> [optional] # module from which to import ALL of the potential presets (any missing presets will be identities)
115```
116