Install
npm install -g unpkgr
or npm install -D unpkgr
Usage
In your package.json:
...
"unpkgr": {
"outputPath": "app/dependencies",
"prefix": "dependencies",
"before": [ "other/scripts.js" ],
"after": [ "yourApplication.js" ],
"forceNew": false,
"dependencyList": [
"react@15.6.1/dist/react.js",
"http://some.other/file.js"
]
}
...
You can also create a separate file containing this json object, unlabeled
{
"outputPath": "app/dependencies",
"etc": "etc"
}
Run unpkgr
D:\projects\directory > unpkgr
Add the script loader to the html
<script src="dependencies/index.js"></script>
Profit
All of the modules are available on the window
object (assuming they are umds). This is intended to make early experimentation faster.
You can now experiment with modules in line or add external references to your build process, keeping it separated from the dependencies for faster compilation times, and easier debugging of transpiled outputs.
Minimal external reference example for Rollup config file
export default {
dest: "yourApplication.js",
entry "yourSource.js",
format: "iife",
external: [
"whatever",
"whatever2"
],
globals: {
"whatever": "React",
"whatever2": "ReactDOM"
}
}
Full CLI/Config Doc
Commmand line option names and config file option names are all the same & command line options take priority over config file options. Config file may not use shortcuts.
unpkgr [--option||-o ...] outputPath
{
"outputPath": "",
"option": "" || Boolean || ["", ...]
}
Option | Shortcut | Input Type | Description |
---|---|---|---|
config | -c | string | Path to look for json object instead of ./package.json/unpkgr |
prefix | -p | string | Directory prefix for indexing of downloaded files eg [prefix]/file_0.js |
forceNew | -f | bool | Download files even if they exist already |
before | -b | comma,sep,strings | Files to load before these dependencies, these paths dont have prefix applied automatically |
after | -a | comma,sep,strings | Files to load after these dependencies, typically just your bundle.js, these paths dont have prefix applied automatically |
dependencyList | -d | comma,sep,strings | The dependencies you would like to load |
Example command with only options not using a config
unpkgr -d react@15.6.1/dist/react.js,react-dom@15.6.1/dist/react-dom.js -b beforeTestA.js,before/testB.js -a afterTest.js -p test/prefix -f true unpkTest
Node API Usage
const unpkg = require("unpkgr")([...dependencies], [...existingFiles])
unpkg
.then(({ files, index }) => {
// files == [ ... { name, content } ]
// index == { content, placeholder, list }
//you can pass off the file list
someConcatter(...files) //and probably throwaway the index
//or use the index and write the files yourself
files.forEach((file) =>
fs.writeFileSync(
path.join(myWriteDir, file.name),
file.content
)
)
fs.writeFileSync(
path.join(myWriteDir, "index.js"),
index.content.replace(index.placeholder, JSON.stringify(index.list))
)
//or just see what you got
files.forEach(file => console.log(`\n\n
//////Loaded File//////
name: ${ file.name }
content:\n\n${ file.content.slice(0, 128) }\n\n
...
`))
console.log(`\n\n
//////Loaded Index//////
list: [${ index.list.join(", ") }]
placeholder: ${ index.placeholder }
content:\n\n${ index.content }\n\n
`)
})
.catch(console.log)