Add babel-plugin-unpkg-rewrite package
This commit is contained in:
		
							
								
								
									
										38
									
								
								packages/babel-plugin-unpkg-rewrite/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								packages/babel-plugin-unpkg-rewrite/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| A [Babel](http://babeljs.io/) plugin that rewrites bare ES module specifiers as | ||||
| [unpkg](https://unpkg.com) URLs. | ||||
|  | ||||
| ## input | ||||
|  | ||||
| ```jsx | ||||
| import React from "react" | ||||
| import router from "@angular/router" | ||||
| import map from "lodash.map" | ||||
| import fs from "pn/fs" | ||||
| import cupcakes from "./cupcakes" | ||||
| import shoelaces from "/shoelaces" | ||||
| import something from "//something.com/whatevs" | ||||
| import something from "http://something.com/whatevs" | ||||
| let ReactDOM = require("react-dom") | ||||
| export React from "react" | ||||
| export { Component } from "react" | ||||
| export * from "react" | ||||
| export var message = "hello" | ||||
| ``` | ||||
|  | ||||
| ## output | ||||
|  | ||||
| ```jsx | ||||
| import React from "https://unpkg.com/react@15.6.1?module" | ||||
| import router from "https://unpkg.com/@angular/router@4.3.5?module" | ||||
| import map from "https://unpkg.com/lodash.map@4.6.0?module" | ||||
| import fs from "https://unpkg.com/pn@1.0.0/fs?module" | ||||
| import cupcakes from "./cupcakes?module" | ||||
| import shoelaces from "/shoelaces?module" | ||||
| import something from "//something.com/whatevs" | ||||
| import something from "http://something.com/whatevs" | ||||
| let ReactDOM = require("react-dom") | ||||
| export React from "https://unpkg.com/react@15.6.1?module" | ||||
| export { Component } from "https://unpkg.com/react@15.6.1?module" | ||||
| export * from "https://unpkg.com/react@15.6.1?module" | ||||
| export var message = "hello" | ||||
| ``` | ||||
| @ -0,0 +1,76 @@ | ||||
| const babel = require("babel-core") | ||||
| const unpkgRewrite = require("../index") | ||||
|  | ||||
| const testCases = [ | ||||
|   { | ||||
|     before: "import React from 'react';", | ||||
|     after: "import React from 'https://unpkg.com/react@15.6.1?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import router from '@angular/router';", | ||||
|     after: "import router from 'https://unpkg.com/@angular/router@4.3.5?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import map from 'lodash.map';", | ||||
|     after: "import map from 'https://unpkg.com/lodash.map@4.6.0?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import fs from 'pn/fs';", | ||||
|     after: "import fs from 'https://unpkg.com/pn@1.0.0/fs?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import cupcakes from './cupcakes';", | ||||
|     after: "import cupcakes from './cupcakes?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import shoelaces from '/shoelaces';", | ||||
|     after: "import shoelaces from '/shoelaces?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import something from '//something.com/whatevs';", | ||||
|     after: "import something from '//something.com/whatevs';" | ||||
|   }, | ||||
|   { | ||||
|     before: "import something from 'http://something.com/whatevs';", | ||||
|     after: "import something from 'http://something.com/whatevs';" | ||||
|   }, | ||||
|   { | ||||
|     before: "let ReactDOM = require('react-dom');", | ||||
|     after: "let ReactDOM = require('react-dom');" | ||||
|   }, | ||||
|   { | ||||
|     before: "export React from 'react';", | ||||
|     after: "export React from 'https://unpkg.com/react@15.6.1?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "export { Component } from 'react';", | ||||
|     after: "export { Component } from 'https://unpkg.com/react@15.6.1?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "export * from 'react';", | ||||
|     after: "export * from 'https://unpkg.com/react@15.6.1?module';" | ||||
|   }, | ||||
|   { | ||||
|     before: "export var message = 'hello';", | ||||
|     after: "export var message = 'hello';" | ||||
|   } | ||||
| ] | ||||
|  | ||||
| const dependencies = { | ||||
|   react: "15.6.1", | ||||
|   "@angular/router": "4.3.5", | ||||
|   "lodash.map": "4.6.0", | ||||
|   pn: "1.0.0" | ||||
| } | ||||
|  | ||||
| testCases.forEach(testCase => { | ||||
|   describe(`rewriting "${testCase.before}"`, () => { | ||||
|     it(`becomes "${testCase.after}"`, () => { | ||||
|       const result = babel.transform(testCase.before, { | ||||
|         plugins: [unpkgRewrite(dependencies)] | ||||
|       }) | ||||
|  | ||||
|       expect(result.code).toEqual(testCase.after) | ||||
|     }) | ||||
|   }) | ||||
| }) | ||||
							
								
								
									
										36
									
								
								packages/babel-plugin-unpkg-rewrite/modules/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								packages/babel-plugin-unpkg-rewrite/modules/index.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | ||||
| const unpkg = require("unpkg") | ||||
| const warning = require("warning") | ||||
|  | ||||
| function unpkgRewrite(dependencies = {}) { | ||||
|   return { | ||||
|     inherits: require("babel-plugin-syntax-export-extensions"), | ||||
|  | ||||
|     visitor: { | ||||
|       "ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration"(path) { | ||||
|         if (!path.node.source) return // probably a variable declaration | ||||
|  | ||||
|         const id = path.node.source.value | ||||
|  | ||||
|         if (unpkg.isRemoteModuleIdentifier(id)) { | ||||
|           return // leave it alone | ||||
|         } else if (unpkg.isLocalModuleIdentifier(id)) { | ||||
|           path.node.source.value = `${id}?module` | ||||
|         } else if (unpkg.isBareModuleIdentifier(id)) { | ||||
|           const { packageName, file } = unpkg.parseBareModuleIdentifier(id) | ||||
|  | ||||
|           warning( | ||||
|             dependencies[packageName], | ||||
|             'Missing version info for package "%s" in dependencies; falling back to "latest"', | ||||
|             packageName | ||||
|           ) | ||||
|  | ||||
|           const version = dependencies[packageName] || "latest" | ||||
|  | ||||
|           path.node.source.value = `https://unpkg.com/${packageName}@${version}${file}?module` | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| module.exports = unpkgRewrite | ||||
							
								
								
									
										30
									
								
								packages/babel-plugin-unpkg-rewrite/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								packages/babel-plugin-unpkg-rewrite/package.json
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| { | ||||
|   "name": "babel-plugin-unpkg-rewrite", | ||||
|   "version": "3.3.0", | ||||
|   "description": "Rewrite bare ES module specifiers as unpkg.com URLs with Babel", | ||||
|   "repository": "unpkg/unpkg", | ||||
|   "license": "MIT", | ||||
|   "files": [ | ||||
|     "modules/*.js" | ||||
|   ], | ||||
|   "main": "modules/index.js", | ||||
|   "dependencies": { | ||||
|     "babel-plugin-syntax-export-extensions": "^6.13.0", | ||||
|     "unpkg": "^0.2.0", | ||||
|     "warning": "^3.0.0" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "babel": "^6.23.0", | ||||
|     "babel-cli": "^6.22.2", | ||||
|     "babel-core": "^6.23.1" | ||||
|   }, | ||||
|   "keywords": [ | ||||
|     "babel", | ||||
|     "babel-plugin", | ||||
|     "resolve", | ||||
|     "module", | ||||
|     "modules", | ||||
|     "esm", | ||||
|     "unpkg" | ||||
|   ] | ||||
| } | ||||
							
								
								
									
										1249
									
								
								packages/babel-plugin-unpkg-rewrite/yarn.lock
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1249
									
								
								packages/babel-plugin-unpkg-rewrite/yarn.lock
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user