Giter Site home page Giter Site logo

Comments (6)

pauleveritt avatar pauleveritt commented on July 28, 2024

Maybe a better way to think about it: eliminate the TSX part. Just think of it as a JavaScript template calling a shortcode.

How would a universal shortcode call css and get something added to the bundle, when used in a JavaScript template?

from eleventy.

zachleat avatar zachleat commented on July 28, 2024

Some overlap with #3310

from eleventy.

monochromer avatar monochromer commented on July 28, 2024

In Eleventy itself, the context this is binded to filters (what makes it possible to work for filters from the bundle plugin) something like this:

// which data keys to bind to `this` in JavaScript template functions
static DATA_KEYS_TO_BIND = ["page", "eleventy"];

getJavaScriptFunctions(inst) {
let fns = {};
let configFns = this.config.javascriptFunctions;
for (let key in configFns) {
// prefer pre-existing `page` javascriptFunction, if one exists
if (key === "page") {
// do nothing
} else {
// note: wrapping creates a new function
fns[key] = JavaScript.wrapJavaScriptFunction(inst, configFns[key]);
}
}
return fns;
}
static wrapJavaScriptFunction(inst, fn) {
return function (...args) {
for (let key of JavaScript.DATA_KEYS_TO_BIND) {
if (inst && inst[key]) {
this[key] = inst[key];
}
}
return fn.call(this, ...args);
};
}

from eleventy.

pauleveritt avatar pauleveritt commented on July 28, 2024

@monochromer My plugin bundle concern...let's say I have a JavaScript template that inserts the CSS bundle in the head, then later, calls a shortcode. The shortcode adds something to the CSS bundle. But it is called after the bundle is put in the head.

I don't think the binding to this is enough for this. There's something deferred happening when the bundle gets put in the head, with some re-evaluation/resolution happening after the first pass through. (Confession: I should read the source rather than guess.)

from eleventy.

monochromer avatar monochromer commented on July 28, 2024

Eleventy bundle plugin functions inserts placeholders into html, then transforms replace them with content or links of bundles.

from eleventy.

pauleveritt avatar pauleveritt commented on July 28, 2024

Thanks a bunch for that tip @monochromer .... I now see the placeholder, its string scheme, and the event that processes after build.

Thanks to my buddy Khalid, we found where the problem lies. addContent needs a `page.url.

My subcomponent doesn't have this.page. I would need to pass in the URL each time, and find a way to get to page.url in each subcomponent. I made an arrow function in compile to automate that.

I made a video walking through the problem and hacky-y solution.

Not much 11ty can do to help. It doesn't control the creation of the "shortcode" (subcomponent.) I could imagine some relationship with the jsx-async-runtime project (nice small codebase) where it allowed a pluggable factory to control the this.

I'd be ok with closing this ticket.

from eleventy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.