Skip to content

Commit

Permalink
Add gist support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Sep 2, 2020
1 parent e4435f5 commit 08ae976
Show file tree
Hide file tree
Showing 9 changed files with 6,571 additions and 412 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ packages/babel-parser/test/expressions
eslint/*/lib
eslint/*/node_modules
eslint/*/test/fixtures
sandbox/src/
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: CI

# on: [push, pull_request]
on: [push, pull_request]

jobs:
prepare-yarn-cache:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"workspaces": [
"codemods/*",
"eslint/*",
"packages/*"
"packages/*",
"sandbox"
],
"resolutions": {
"@lerna/version": "patch:@lerna/version@npm:3.20.2#.yarn-patches/@lerna/version.patch",
Expand Down
10 changes: 5 additions & 5 deletions sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"private": true,
"description": "",
"dependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"@babel/core": "workspace:^7.0.0",
"@babel/plugin-transform-runtime": "workspace:^7.0.0",
"@babel/preset-env": "workspace:^7.0.0",
"@babel/preset-react": "workspace:^7.0.0",
"@babel/preset-typescript": "workspace:^7.0.0",
"codemirror": "^5.54.0",
"react": "^16.11.0",
"react-codemirror2": "^7.2.0",
Expand Down
3 changes: 2 additions & 1 deletion sandbox/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,10 @@ export default function App({
defaultSource,
defaultBabelConfig,
defCustomPlugin,
gist,
}) {
const [source, setSource] = React.useState(defaultSource);
const [enableCustomPlugin, toggleCustomPlugin] = React.useState(false);
const [enableCustomPlugin, toggleCustomPlugin] = React.useState(gist);
const [customPlugin, setCustomPlugin] = React.useState(defCustomPlugin);
const [babelConfig, setBabelConfig] = useState(
Array.isArray(defaultBabelConfig)
Expand Down
4 changes: 2 additions & 2 deletions sandbox/src/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// from developit
function memo(fn, c = {}) {
return (p) => c[p] || (c[p] = fn(p));
return p => c[p] || (c[p] = fn(p));
}

// npm.im/greenlet
Expand Down Expand Up @@ -53,7 +53,7 @@ function greenlet(e) {
}

export const gzipSize = memo(
greenlet((code) => {
greenlet(code => {
if (!self.pako)
importScripts("https://unpkg.com/pako@1.0.11/dist/pako_deflate.min.js");
const compressed = self.pako.gzip(code);
Expand Down
95 changes: 82 additions & 13 deletions sandbox/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App";

const SOURCE = `const foo = (...a) => \`\${a?.b}\`;
let GIST = false;
let SOURCE = `const foo = (...a) => \`\${a?.b}\`;
enum Direction {
Left,
Up,
Expand All @@ -24,7 +25,7 @@ class A {
return <a></a>;
}
}`;
const CONFIG = [
let CONFIG = [
{
presets: [
[
Expand Down Expand Up @@ -72,7 +73,7 @@ const CONFIG = [
// plugins: [["@babel/plugin-transform-runtime", { useESModules: true }]],
// },
];
const PLUGIN = `export default function customPlugin(babel) {
let PLUGIN = `export default function customPlugin(babel) {
return {
visitor: {
Identifier(path) {
Expand All @@ -84,13 +85,81 @@ const PLUGIN = `export default function customPlugin(babel) {
}
`;

ReactDOM.render(
<React.StrictMode>
<App
defaultBabelConfig={CONFIG}
defaultSource={SOURCE}
defCustomPlugin={PLUGIN}
/>
</React.StrictMode>,
document.getElementById("root")
);
// https://stackoverflow.com/questions/51546372/how-to-parse-the-content-from-response-using-gist-api
// ex: https://gist.github.com/astexplorer/02baa12f126af2f270d0177e245874cf/264d268511bd722e5d07db78813b485960413473
// GET /gists/:gist_id
// GET /gists/:gist_id/:sha
async function fetchData({ id, version }) {
version = version ? `/${version}` : "";
const response = await fetch(`https://api.github.com/gists/${id}${version}`);
const data = await response.json();
return {
source: data.files["source.js"].content,
plugin: data.files["transform.js"].content,
};
}

// ex: https://api.github.com/gists/02baa12f126af2f270d0177e245874cf
function getGistFromHash() {
// https://gist.github.com/astexplorer/02baa12f126af2f270d0177e245874cf
const gist = window.location.hash.match(
/#https:\/\/gist.github.com\/(\w+)\/(\w+)\/?(\w+)?/
);
if (gist) {
return {
owner: gist[1],
id: gist[2],
version: gist[3],
};
}

// https://astexplorer.net/#/gist/02baa12f126af2f270d0177e245874cf/264d268511bd722e5d07db78813b485960413473
const astExplorer = window.location.hash.match(
/#https:\/\/astexplorer.net\/#\/gist\/(\w+)\/?(\w+)?/
);
if (astExplorer) {
return {
id: astExplorer[1],
version: astExplorer[2],
};
}
}

async function initState() {
const data = getGistFromHash();
if (!data) return;

GIST = true;
const key = `babel_sandbox:${data.id}${
data.version ? `:${data.version}` : ""
}`;
const stored = window.sessionStorage.getItem(key);
if (stored) {
const storedObj = JSON.parse(stored);
if (storedObj.source) SOURCE = storedObj.source;
if (storedObj.plugin) PLUGIN = storedObj.plugin;
} else {
try {
const { source, plugin } = await fetchData(data);
SOURCE = source;
PLUGIN = plugin;
window.sessionStorage.setItem(key, JSON.stringify({ source, plugin }));
} catch (e) {
throw new Error(e);
}
}
}

initState().then(() => {
ReactDOM.render(
<React.StrictMode>
<App
defaultBabelConfig={CONFIG}
defaultSource={SOURCE}
defCustomPlugin={PLUGIN}
gist={GIST}
/>
</React.StrictMode>,
document.getElementById("root")
);
});
10 changes: 7 additions & 3 deletions sandbox/src/metadataPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function babelPlugin(babel) {
const { types: t, template } = babel;

return {
name: "babel-internal-modify-replacewith",
name: "transform-babel-metadata",
visitor: {
// + replaceWithMultiple
// path.replaceWith(a) -> path.replaceWith(a, "name")
Expand Down Expand Up @@ -37,7 +37,7 @@ module.exports = function babelPlugin(babel) {
*/
const hasComment = path.parentPath.node.leadingComments;
let comment;
if (hasComment) {
if (hasComment && hasComment.length) {
comment = hasComment[0].value.match(/node: (.*)/);
if (comment) {
comment = template.expression(comment[1])();
Expand Down Expand Up @@ -119,7 +119,11 @@ module.exports = function babelPlugin(babel) {
)
: null;
if (node) props.push(node);
path.node.arguments.push(t.objectExpression(props));
const metaNode = t.objectExpression(props);
if (path.addMetadata) {
path.addMetadata(metaNode, "transform-babel-metadata");
}
path.node.arguments.push(metaNode);
}
},
},
Expand Down
Loading

0 comments on commit 08ae976

Please sign in to comment.