0.0.2 • Published 7 years ago

unpkgr2 v0.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

Exactly the same as unpkgr, except you must manually invoke the loader, and can pass additional script paths at that time. See below for more info.

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>

Call the script loader

<script>
  __loadDependencyScripts__("anotherScript.js", "anotherScript2.js")
    .then(App.init) //or however you bootstrap your application
</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 || ["", ...]
}
OptionShortcutInput TypeDescription
config-cstringPath to look for json object instead of ./package.json/unpkgr
prefix-pstringDirectory prefix for indexing of downloaded files eg prefix/file_0.js
forceNew-fboolDownload files even if they exist already
before-bcomma,sep,stringsFiles to load before these dependencies, these paths dont have prefix applied automatically
after-acomma,sep,stringsFiles to load after these dependencies, typically just your bundle.js, these paths dont have prefix applied automatically
dependencyList-dcomma,sep,stringsThe 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)
0.0.2

7 years ago

0.0.1

7 years ago