Skip to content

Commit 58df21e

Browse files
shellscapeNotWoods
authored andcommitted
chore(repo): new plugin data-uri (rollup#150)
* chore(repo): add data-uri plugin * chore: change test script * chore(repo): new plugin data-uri * fix: URL is not global in node 8 * chore: improve `resolved` typing Co-Authored-By: Tiger Oakes <toakes@mozilla.com> * chore: add function description for those who forgot why they installed the plugin Co-Authored-By: Tiger Oakes <toakes@mozilla.com> * chore: re-improve resolved typing * chore: rollup generated sourcemaps * docs: add supported mime types, base64 * feat: base64 support * docs: use es6 import Co-authored-by: Tiger Oakes <contact@tigeroakes.com>
1 parent 26f984b commit 58df21e

File tree

18 files changed

+513
-1
lines changed

18 files changed

+513
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# @rollup/plugin-data-uri ChangeLog
2+
3+
## 0.1.0
4+
5+
- First Release

β€Žpackages/data-uri/README.mdβ€Ž

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[npm]: https://img.shields.io/npm/v/@rollup/plugin-data-uri
2+
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-data-uri
3+
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-data-uri
4+
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-data-uri
5+
6+
[![npm][npm]][npm-url]
7+
[![size][size]][size-url]
8+
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
9+
10+
# @rollup/plugin-data-uri
11+
12+
🍣 A Rollup plugin which imports modules from Data URIs.
13+
14+
## Requirements
15+
16+
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
17+
18+
## Install
19+
20+
Using npm:
21+
22+
```console
23+
npm install @rollup/plugin-data-uri --save-dev
24+
```
25+
26+
## Usage
27+
28+
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
29+
30+
```js
31+
import dataUri from '@rollup/plugin-data-uri';
32+
33+
module.exports = {
34+
input: 'src/index.js',
35+
output: {
36+
dir: 'output',
37+
format: 'cjs'
38+
},
39+
plugins: [dataUri()]
40+
};
41+
```
42+
43+
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). If the build produces any errors, the plugin will write a "data-uri" character to stderr, which should be audible on most systems.
44+
45+
## Options
46+
47+
This plugin currently has no available options.
48+
49+
## Supported MIME Types
50+
51+
The following MIME types are supported by this plugin:
52+
53+
- `text/javascript`
54+
- `application/json`
55+
56+
This mirrors support in the [latest version of Node.js](https://nodejs.org/api/esm.html#esm_data_imports), with the exception of WebAssembly support.
57+
58+
## Base64 Encoding
59+
60+
Base64 encoding is supported for well-formed `data:` URIs. For example:
61+
62+
```js
63+
import batman from 'data:application/json;base64, eyAiYmF0bWFuIjogInRydWUiIH0=';
64+
```
65+
66+
## Dynamic Imports
67+
68+
Dynamic imports, such as `import('data:application/json, { "batman": "true" }')`, aren't supported by this plugin. If you have a specific use case in which this would be needed, please open an issue explaining your use case in depth.
69+
70+
## Meta
71+
72+
[CONTRIBUTING](/.github/CONTRIBUTING.md)
73+
74+
[LICENSE (MIT)](/LICENSE)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "@rollup/plugin-data-uri",
3+
"version": "0.1.0",
4+
"publishConfig": {
5+
"access": "public"
6+
},
7+
"description": "Import modules from Data URIs",
8+
"license": "MIT",
9+
"repository": "rollup/plugins",
10+
"author": "shellscape",
11+
"homepage": "https://github.com/rollup/plugins/tree/master/packages/data-uri",
12+
"bugs": "https://github.com/rollup/plugins/issues",
13+
"main": "dist/index.js",
14+
"engines": {
15+
"node": ">= 8.0.0"
16+
},
17+
"scripts": {
18+
"build": "rollup -c",
19+
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
20+
"ci:lint": "pnpm run build && pnpm run lint",
21+
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
22+
"ci:test": "pnpm run test -- --verbose",
23+
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
24+
"lint:docs": "prettier --single-quote --write README.md",
25+
"lint:js": "eslint --fix --cache src test --ext .js,.ts",
26+
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
27+
"prebuild": "del-cli dist",
28+
"prepublishOnly": "pnpm run lint && pnpm run build",
29+
"pretest": "pnpm run build -- --sourcemap",
30+
"test": "ava"
31+
},
32+
"files": [
33+
"dist",
34+
"types",
35+
"README.md",
36+
"LICENSE"
37+
],
38+
"keywords": [
39+
"data",
40+
"data-uri",
41+
"data-url",
42+
"plugin",
43+
"rollup",
44+
"uri",
45+
"url"
46+
],
47+
"peerDependencies": {
48+
"rollup": "^1.20.0"
49+
},
50+
"devDependencies": {
51+
"@rollup/plugin-typescript": "^2.1.0",
52+
"@rollup/pluginutils": "^3.0.1",
53+
"rollup": "^1.27.14",
54+
"typescript": "^3.7.4"
55+
},
56+
"ava": {
57+
"compileEnhancements": false,
58+
"extensions": [
59+
"ts"
60+
],
61+
"require": [
62+
"ts-node/register"
63+
],
64+
"files": [
65+
"!**/fixtures/**",
66+
"!**/output/**",
67+
"!**/helpers/**",
68+
"!**/recipes/**",
69+
"!**/types.ts"
70+
]
71+
},
72+
"module": "dist/index.es.js",
73+
"types": "types/index.d.ts"
74+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
3+
import pkg from './package.json';
4+
5+
export default {
6+
input: 'src/index.ts',
7+
plugins: [typescript()],
8+
external: [...Object.keys(pkg.devDependencies), 'url'],
9+
output: [
10+
{ format: 'cjs', file: pkg.main, sourcemap: true },
11+
{ format: 'esm', file: pkg.module, sourcemap: true }
12+
]
13+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { URL } from 'url';
2+
3+
import { Plugin, RollupError } from 'rollup';
4+
5+
import { dataToEsm } from '@rollup/pluginutils';
6+
7+
const reDataUri = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/;
8+
const mimeTypes = {
9+
js: 'text/javascript',
10+
json: 'application/json'
11+
};
12+
13+
export default function dataUri(): Plugin {
14+
const resolved: { [key: string]: { mime: string | null; content: string | null } } = {};
15+
16+
return {
17+
name: 'dataUri',
18+
19+
resolveId(id) {
20+
if (resolved[id]) {
21+
return id;
22+
}
23+
24+
if (!reDataUri.test(id)) {
25+
return null;
26+
}
27+
28+
const uri = new URL(id);
29+
30+
if (uri.protocol !== 'data:') {
31+
return null;
32+
}
33+
34+
const empty = [null, null, null, null, null];
35+
const [, mime, format, data] = reDataUri.exec(uri.pathname) || empty;
36+
37+
if (Object.values(mimeTypes).includes(mime as string) && data) {
38+
const base64 = format && /base64/i.test(format.substring(1));
39+
const content = base64 ? Buffer.from(data, 'base64').toString('utf-8') : data;
40+
41+
resolved[id] = { mime, content };
42+
43+
return id;
44+
}
45+
46+
return null;
47+
},
48+
49+
load(id) {
50+
if (!resolved[id]) {
51+
return null;
52+
}
53+
54+
const { mime, content } = resolved[id];
55+
56+
if (!content) {
57+
return null;
58+
}
59+
60+
if (mime === 'text/javascript') {
61+
return content;
62+
} else if (mime === 'application/json') {
63+
let json = '';
64+
try {
65+
json = JSON.parse(content);
66+
} catch (e) {
67+
const error: RollupError = {
68+
message: e.toString(),
69+
parserError: e,
70+
plugin: '@rollup/plugin-data-uri',
71+
pluginCode: 'DU$JSON'
72+
};
73+
this.error(error);
74+
}
75+
76+
return dataToEsm(json, {
77+
preferConst: true,
78+
compact: false,
79+
namedExports: true,
80+
indent: ' '
81+
});
82+
}
83+
return null;
84+
}
85+
};
86+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"globals": {
3+
"t": true
4+
},
5+
"rules": {
6+
"import/extensions": "off",
7+
"import/no-unresolved": "off"
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'data:application/json, { "batman": }';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import batman from 'data:application/json;base64, eyAiYmF0bWFuIjogInRydWUiIH0=';
2+
3+
t.truthy(batman.batman);
4+
t.snapshot(batman);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import 'data:text/javascript, t.truthy(true);';
2+
import { batman } from 'data:text/javascript, export const batman = true;\nconst joker = false;\nexport default joker;';
3+
4+
t.snapshot(batman);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import batman from 'data:application/json, { "batman": "true" }';
2+
3+
t.truthy(batman.batman);
4+
t.snapshot(batman);

0 commit comments

Comments
Β (0)