Skip to content

Commit

Permalink
Also preprocess site examples with babel
Browse files Browse the repository at this point in the history
  • Loading branch information
stwa committed Jun 28, 2019
1 parent a71265a commit 5be02ae
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage/
site/public
site/resources
site/static/js/bundle.js
site/**/*.dist.js
3 changes: 3 additions & 0 deletions site/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
4 changes: 1 addition & 3 deletions site/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

all: js site

js: static/js/bundle.js

static/js/bundle.js: index.js node_modules
js: node_modules
npm run build

node_modules: package.json
Expand Down
8 changes: 7 additions & 1 deletion site/layouts/partials/example-single.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{{ $id := .File.ContentBaseName }}
{{ $dir := .File.Dir }}
<div id="{{ $id }}">
{{ .Content }}

{{ range .Resources.Match "*.js" }}
{{ $example := strings.TrimSuffix ".js" .Name }}
{{ $distFile := printf "%s/%s.dist.js" $dir $example }}
{{ if not (eq ".dist" (path.Ext $example)) }}

<div id="{{ $example }}" class="mount"></div>
{{ highlight .Content "javascript" "" }}
<script type="text/javascript">
{
let mountPoint = document.createElement("div");
document.querySelector("#{{ $id }} > #{{ $example }}.mount").appendChild(mountPoint);
{{ .Content | safeJS }}
{{ readFile $distFile | safeJS }}
}
</script>

{{ end }}
{{ end }}
</div>
57 changes: 41 additions & 16 deletions site/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,46 @@ import resolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import { terser } from "rollup-plugin-terser";

let commonPlugins = [
resolve({
dedupe: ["flyps"],
}),
commonjs(),
babel({ exclude: "node_modules/**" }),
terser(),
];
import path from "path";
import fs from "fs";

let listFiles = dir =>
fs.readdirSync(dir).reduce((acc, filename) => {
let filepath = path.join(dir, filename);
if (fs.lstatSync(filepath).isDirectory()) {
return [...acc, ...listFiles(filepath)];
}
return [...acc, filepath];
}, []);

export default [{
input: "index.js",
output: {
file: "static/js/bundle.js",
format: "umd",
name: "bundle",
let examples = listFiles("content/example")
.filter(file => file.endsWith(".js") && !file.endsWith(".dist.js"))
.map(js => ({
input: js,
output: {
file: path.join(path.dirname(js), path.basename(js, ".js") + ".dist.js"),
format: "umd",
name: path.basename(js, ".js"),
},
plugins: [babel({ exclude: "node_modules/**" })],
}));

export default [
{
input: "index.js",
output: {
file: "static/js/bundle.js",
format: "umd",
name: "bundle",
},
plugins: [
resolve({
dedupe: ["flyps"],
}),
commonjs(),
babel({ exclude: "node_modules/**" }),
terser(),
],
},
plugins: commonPlugins,
}];
...examples,
];

0 comments on commit 5be02ae

Please sign in to comment.