Skip to content

Commit af14a21

Browse files
committed
Fallback when no addGlobalData
1 parent b87ff0a commit af14a21

File tree

8 files changed

+178
-176
lines changed

8 files changed

+178
-176
lines changed

Diff for: addCachedGlobalData.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
2-
// This utility ensures that a function ensures that data is only calculated once per build
3-
module.exports = async (eleventyConfig, cb, key) => {
4-
// Compile once to prime cache (data varaiable is our cache) and add watch targets
5-
let data = await cb();
6-
let buildCounter = 0;
7-
let lastCompile = 0;
8-
9-
// After each build increment the build counter
10-
eleventyConfig.on("afterBuild", () => {
11-
buildCounter++;
12-
});
13-
14-
// If this is a function 11ty will attempt to resolve the data
15-
// If it is async 11ty will add the key scss and resolve data when called
16-
eleventyConfig.addGlobalData(key, async () => {
17-
// if the lastCompile can buildCounter don't match the cached data
18-
if (lastCompile !== buildCounter) {
19-
lastCompile = buildCounter;
20-
data = await cb();
21-
}
22-
return data;
23-
});
24-
};
1+
2+
// This utility ensures that a function ensures that data is only calculated once per build
3+
module.exports = async (eleventyConfig, cb, key) => {
4+
// Compile once to prime cache (data varaiable is our cache) and add watch targets
5+
let data = await cb();
6+
let buildCounter = 0;
7+
let lastCompile = 0;
8+
9+
// After each build increment the build counter
10+
eleventyConfig.on("afterBuild", () => {
11+
buildCounter++;
12+
});
13+
14+
// If this is a function 11ty will attempt to resolve the data
15+
// If it is async 11ty will add the key scss and resolve data when called
16+
eleventyConfig.addGlobalData(key, async () => {
17+
// if the lastCompile can buildCounter don't match the cached data
18+
if (lastCompile !== buildCounter) {
19+
lastCompile = buildCounter;
20+
data = await cb();
21+
}
22+
return data;
23+
});
24+
};

Diff for: index.js

+67-65
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
1-
const { buildSync } = require("esbuild");
2-
const addCachedGlobalData = require("./addCachedGlobalData");
3-
const esBuildTransform = require("./transform");
4-
const esBuildShortcode = require("./shortcode");
5-
const path = require("path");
6-
7-
const compileEsBuildTargets = (targets, esBuildOptions) => {
8-
// targets:
9-
// [{ main: "/path/to.js"}]
10-
11-
const files = Object.entries(targets); // [["main","path/to.js"], ...]
12-
13-
// Note: ES build doesn't provide a way to get bundle information so
14-
// we can't add the bundled files as watch targets
15-
const data = {};
16-
for (let index = 0; index < files.length; index++) {
17-
const key = files[index][0];
18-
const file = files[index][1];
19-
const res = buildSync({
20-
entryPoints: [file],
21-
minify: process.NODE_ENV === "development" ? false : true,
22-
bundle: true,
23-
write: false,
24-
...esBuildOptions
25-
});
26-
data[key] = new TextDecoder("utf-8").decode(res.outputFiles[0].contents);
27-
}
28-
return data;
29-
};
30-
31-
const esBuildPlugin = async (eleventyConfig, { targets = {}, esBuildOptions = {} } = {}) => {
32-
if (Object.entries(targets).length === 0) {
33-
// Support older versions of 11ty
34-
if(eleventyConfig.addGlobalData) {
35-
// return an empty object if no enteries
36-
eleventyConfig.addGlobalData("esbuild", {});
37-
}
38-
console.log(`No js targets found.`);
39-
console.log(
40-
`Plugin expects data to be in the shape: { targets: name: "path/to/file.js"}`
41-
);
42-
return;
43-
}
44-
45-
46-
if(eleventyConfig.addGlobalData) {
47-
await addCachedGlobalData(
48-
eleventyConfig,
49-
() => compileEsBuildTargets(targets, esBuildOptions),
50-
"esbuild"
51-
);
52-
}
53-
54-
// Ideally we'd add a watch for each assets consumed by the entry but as yet I don't think that can be done with esbuild
55-
Object.entries(targets).forEach(([_, watchPath]) => {
56-
const relativePath = path.relative(process.cwd(), watchPath);
57-
eleventyConfig.addWatchTarget(relativePath);
58-
});
59-
60-
};
61-
62-
esBuildPlugin.esBuildTransform = esBuildTransform;
63-
esBuildPlugin.esBuildShortcode = esBuildShortcode;
64-
65-
module.exports = esBuildPlugin;
1+
const { buildSync } = require("esbuild");
2+
const addCachedGlobalData = require("./addCachedGlobalData");
3+
const esBuildTransform = require("./transform");
4+
const esBuildShortcode = require("./shortcode");
5+
const path = require("path");
6+
7+
const compileEsBuildTargets = (targets, esBuildOptions) => {
8+
// targets:
9+
// [{ main: "/path/to.js"}]
10+
11+
const files = Object.entries(targets); // [["main","path/to.js"], ...]
12+
13+
// Note: ES build doesn't provide a way to get bundle information so
14+
// we can't add the bundled files as watch targets
15+
const data = {};
16+
for (let index = 0; index < files.length; index++) {
17+
const key = files[index][0];
18+
const file = files[index][1];
19+
const res = buildSync({
20+
entryPoints: [file],
21+
minify: process.NODE_ENV === "development" ? false : true,
22+
bundle: true,
23+
write: false,
24+
...esBuildOptions
25+
});
26+
data[key] = new TextDecoder("utf-8").decode(res.outputFiles[0].contents);
27+
}
28+
return data;
29+
};
30+
31+
const esBuildPlugin = async (eleventyConfig, { targets = {}, esBuildOptions = {} } = {}) => {
32+
if (Object.entries(targets).length === 0) {
33+
// Support older versions of 11ty
34+
if(eleventyConfig.addGlobalData) {
35+
// return an empty object if no enteries
36+
eleventyConfig.addGlobalData("esbuild", {});
37+
}
38+
console.log(`No js targets found.`);
39+
console.log(
40+
`Plugin expects data to be in the shape: { targets: name: "path/to/file.js"}`
41+
);
42+
return;
43+
}
44+
45+
46+
if(eleventyConfig.addGlobalData) {
47+
await addCachedGlobalData(
48+
eleventyConfig,
49+
() => compileEsBuildTargets(targets, esBuildOptions),
50+
"esbuild"
51+
);
52+
} else {
53+
compileEsBuildTargets(targets, esBuildOptions);
54+
}
55+
56+
// Ideally we'd add a watch for each assets consumed by the entry but as yet I don't think that can be done with esbuild
57+
Object.entries(targets).forEach(([_, watchPath]) => {
58+
const relativePath = path.relative(process.cwd(), watchPath);
59+
eleventyConfig.addWatchTarget(relativePath);
60+
});
61+
62+
};
63+
64+
esBuildPlugin.esBuildTransform = esBuildTransform;
65+
esBuildPlugin.esBuildShortcode = esBuildShortcode;
66+
67+
module.exports = esBuildPlugin;

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jamshop/eleventy-plugin-esbuild",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"main": "index.js",
55
"license": "MIT",
66
"dependencies": {

Diff for: shortcode.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const { transformSync } = require("esbuild");
2-
3-
// Note: transform will not bundle!
4-
module.exports = (content) => {
5-
const result = transformSync(content, { loader: "js" });
6-
if (result.js) {
7-
return `<script>${result.js}</script>`;
8-
}
9-
return `<script>console.log(${JSON.stringify(result.errors)})</script>`;
1+
const { transformSync } = require("esbuild");
2+
3+
// Note: transform will not bundle!
4+
module.exports = (content) => {
5+
const result = transformSync(content, { loader: "js" });
6+
if (result.js) {
7+
return `<script>${result.js}</script>`;
8+
}
9+
return `<script>console.log(${JSON.stringify(result.errors)})</script>`;
1010
};

Diff for: tests/index.test.js

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
const test = require("ava");
2-
const pluginEsbuild = require("../index");
3-
const TemplateConfig = require("@11ty/eleventy/src/TemplateConfig");
4-
const eleventyConfig = require("@11ty/eleventy/src/EleventyConfig");
5-
const defaultConfig = require("@11ty/eleventy/src/defaultConfig.js");
6-
const TemplateRender = require("@11ty/eleventy/src/TemplateRender");
7-
const EleventyExtensionMap = require("@11ty/eleventy/src/EleventyExtensionMap");
8-
9-
function getNewTemplateRender(name, inputDir) {
10-
let tr = new TemplateRender(name, inputDir);
11-
tr.extensionMap = new EleventyExtensionMap();
12-
return tr;
13-
}
14-
15-
test("pluginEsbuild global data matches snapshot", (t) => {
16-
eleventyConfig.addPlugin(pluginEsbuild, {
17-
targets: { main: require.resolve("./stub.js") },
18-
});
19-
let cfg = new TemplateConfig(defaultConfig, "./config.js").getConfig();
20-
t.not(Object.keys(cfg.globalData).indexOf("js"), -1);
21-
const { js } = cfg.globalData;
22-
t.snapshot(js);
23-
});
24-
25-
test("Handlebars javascript Shortcode", async (t) => {
26-
let tr = getNewTemplateRender("hbs");
27-
tr.engine.addPairedShortcodes({
28-
javascript: pluginEsbuild.javascriptShortcode,
29-
});
30-
31-
let fn = await tr.getCompiledTemplate(
32-
`{{#javascript}}
33-
console.log("hello world!");
34-
{{/javascript}}`
35-
);
36-
37-
t.snapshot(await fn());
38-
});
1+
const test = require("ava");
2+
const pluginEsbuild = require("../index");
3+
const TemplateConfig = require("@11ty/eleventy/src/TemplateConfig");
4+
const eleventyConfig = require("@11ty/eleventy/src/EleventyConfig");
5+
const defaultConfig = require("@11ty/eleventy/src/defaultConfig.js");
6+
const TemplateRender = require("@11ty/eleventy/src/TemplateRender");
7+
const EleventyExtensionMap = require("@11ty/eleventy/src/EleventyExtensionMap");
8+
9+
function getNewTemplateRender(name, inputDir) {
10+
let tr = new TemplateRender(name, inputDir);
11+
tr.extensionMap = new EleventyExtensionMap();
12+
return tr;
13+
}
14+
15+
test("pluginEsbuild global data matches snapshot", (t) => {
16+
eleventyConfig.addPlugin(pluginEsbuild, {
17+
targets: { main: require.resolve("./stub.js") },
18+
});
19+
let cfg = new TemplateConfig(defaultConfig, "./config.js").getConfig();
20+
t.not(Object.keys(cfg.globalData).indexOf("js"), -1);
21+
const { js } = cfg.globalData;
22+
t.snapshot(js);
23+
});
24+
25+
test("Handlebars javascript Shortcode", async (t) => {
26+
let tr = getNewTemplateRender("hbs");
27+
tr.engine.addPairedShortcodes({
28+
javascript: pluginEsbuild.javascriptShortcode,
29+
});
30+
31+
let fn = await tr.getCompiledTemplate(
32+
`{{#javascript}}
33+
console.log("hello world!");
34+
{{/javascript}}`
35+
);
36+
37+
t.snapshot(await fn());
38+
});

Diff for: tests/snapshots/index.test.js.md

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# Snapshot report for `packages/eleventy-plugin-esbuild/tests/index.test.js`
2-
3-
The actual snapshot is saved in `index.test.js.snap`.
4-
5-
Generated by [AVA](https://avajs.dev).
6-
7-
## pluginEsbuild global data matches snapshot
8-
9-
> Snapshot 1
10-
11-
{
12-
main: `(()=>{console.log("hello world");})();␊
13-
`,
14-
}
15-
16-
## Handlebars javascript Shortcode
17-
18-
> Snapshot 1
19-
20-
`<script>console.log("hello world!");␊
21-
</script>`
1+
# Snapshot report for `packages/eleventy-plugin-esbuild/tests/index.test.js`
2+
3+
The actual snapshot is saved in `index.test.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## pluginEsbuild global data matches snapshot
8+
9+
> Snapshot 1
10+
11+
{
12+
main: `(()=>{console.log("hello world");})();␊
13+
`,
14+
}
15+
16+
## Handlebars javascript Shortcode
17+
18+
> Snapshot 1
19+
20+
`<script>console.log("hello world!");␊
21+
</script>`

Diff for: tests/stub.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log("hello world");
1+
console.log("hello world");

Diff for: transform.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
const { transformSync } = require("esbuild");
2-
3-
// Note: transform will not bundle!
4-
module.exports = (code, outputPath) => {
5-
if (outputPath.endsWith(".js")) {
6-
return transformSync(code, { loader: "js" });
7-
}
8-
if (outputPath.endsWith(".jsx")) {
9-
return transformSync(code, { loader: "jsx" });
10-
}
11-
if (outputPath.endsWith(".ts")) {
12-
return transformSync(code, { loader: "ts" });
13-
}
14-
if (outputPath.endsWith(".tsx")) {
15-
return transformSync(code, { loader: "tsx" });
16-
}
17-
};
1+
const { transformSync } = require("esbuild");
2+
3+
// Note: transform will not bundle!
4+
module.exports = (code, outputPath) => {
5+
if (outputPath.endsWith(".js")) {
6+
return transformSync(code, { loader: "js" });
7+
}
8+
if (outputPath.endsWith(".jsx")) {
9+
return transformSync(code, { loader: "jsx" });
10+
}
11+
if (outputPath.endsWith(".ts")) {
12+
return transformSync(code, { loader: "ts" });
13+
}
14+
if (outputPath.endsWith(".tsx")) {
15+
return transformSync(code, { loader: "tsx" });
16+
}
17+
};

0 commit comments

Comments
 (0)