├── package.json ├── .gitignore ├── bin └── umd-name-transform ├── LICENSE └── README.md /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "umd-name-transform", 3 | "version": "1.0.1", 4 | "description": "Transform stream for UMD bundle names", 5 | "bin": "./bin/umd-name-transform", 6 | "scripts": { 7 | "test": "tape" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/davidchase/umd-name-transform.git" 12 | }, 13 | "keywords": [ 14 | "UMD", 15 | "rollup", 16 | "stream", 17 | "transform" 18 | ], 19 | "author": "David Chase ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/davidchase/umd-name-transform/issues" 23 | }, 24 | "homepage": "https://github.com/davidchase/umd-name-transform#readme" 25 | } 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /bin/umd-name-transform: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const Transform = require('stream').Transform 4 | const fs = require('fs') 5 | const path = require('path') 6 | 7 | const head = xs => xs[0] 8 | const last = xs => xs[xs.length - 1] 9 | const dashesToCamel = str => str.replace(/-(.)/g, match => last(match).toUpperCase()) 10 | 11 | const umdTransform = new Transform({ 12 | transform(chunk, encoding, callback) { 13 | const str = String(chunk) 14 | const match = head(str.match(/global[a-zA-Z-.@/]+/g)) 15 | const substr = last(match.split('.')) 16 | const newStr = str.replace(`.${substr}`, `['${dashesToCamel(substr)}']`) 17 | 18 | callback(null, newStr) 19 | } 20 | }) 21 | 22 | process.stdin.pipe(umdTransform).pipe(fs.createWriteStream(path.resolve(process.cwd(), last(process.argv)))) 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 David Chase 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # umd-name-transform 2 | > Transform stream for UMD bundle names 3 | 4 | ![no-sudden-unpublish](https://img.shields.io/badge/no%20sudden-unpublish%20%E2%9A%93-ff69b4.svg?style=flat) 5 | 6 | ## Why 7 | 8 | Lets say you have a scoped npm package with some esnext code and you want to say use [rollup](rollupjs.org) to bundle it for the browser with UMD. 9 | 10 | ```sh 11 | rollup -c --name '@somescope/somepackagename' -o dist/somepackagename.js 12 | ``` 13 | 14 | the result will be 15 | 16 | ```js 17 | factory((global.@somescope/somepackagename = {}) 18 | ``` 19 | 20 | 21 | This module attempts to fix that so that results looks like 22 | 23 | ```js 24 | factory((global['@somescope/somepackagename']= {}) 25 | ``` 26 | 27 | Also will convert dashes to camelcase `my-module-with-dashes` to `myModuleWithDashes` 28 | 29 | ## Install 30 | 31 | ```sh 32 | npm install umd-name-transform --save-dev 33 | ``` 34 | 35 | ## Usage 36 | 37 | ```sh 38 | rollup -c --name '@somecope/somepackagename' | umd-name-transform -o dist/somepackagename.js 39 | ``` 40 | 41 | This will transform the output from rollup and write to `dist/somepackagename.js` 42 | 43 | **Note** this module assumes that you have created the given directories in the above case its `dist` if your are using cli: 44 | 45 | ```sh 46 | mkdir -p nameOfDir && rollup -c --name '@somecope/somepackagename' | umd-name-transform -o nameOfDir/somepackagename.js 47 | ``` 48 | 49 | ## TODO 50 | - [ ] Programmatic API support 51 | - [ ] Add tests to avoid typos 52 | --------------------------------------------------------------------------------