eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Alias more extensions as 11ty.js

Open pspeter3 opened this issue 2 years ago • 12 comments

Allow more extensions to be treated at the 11ty.js template engine. This builds on the work of #2249 and solves #2248. A user could run Eleventy via ts-node or esbuild-register and have the appropriate behavior now.

pspeter3 avatar Mar 16 '22 22:03 pspeter3

In my example, I was able to run node --require esbuild-register node_modules/.bin/eleventy --config=.eleventy.ts to have a configuration file and layouts that were in TypeScript.

pspeter3 avatar Mar 16 '22 22:03 pspeter3

@zachleat is there a way to make this change to the extension map from the eleventyConfig object instead of baking it into the framework by default?

pspeter3 avatar Mar 21 '22 23:03 pspeter3

@pspeter3 You could create a typescript plugin using eleventy 1.x built-in methods. I use similar setup to add TS support in my project.

Sample config using esbuild:

config.addTemplateFormats("11ty.ts");
config.addExtension("11ty.ts", {
  outputFileExtension: "js",
  compile: (_fileContent: string, filePath: string) => {
    return () => {
      return buildSync({
        entryPoints: [filePath],
        write: false,
        bundle: true,
        treeShaking: true,
        minify: options.minify,
        sourcemap: options.sourcemaps,
      }).outputFiles[0].text;
    };
  },
});

The same can be done for data files, but support for it is pending: https://github.com/11ty/eleventy/pull/2188

DamianOsipiuk avatar Mar 30 '22 08:03 DamianOsipiuk

Thanks @DamianOsipiuk. Does this cause the TypeScript files to be treated as normal 11ty.js files after the transformation?

pspeter3 avatar Mar 31 '22 15:03 pspeter3

No, this example is only bundling and returning JavaScript, but you could extend it to add the same features.

DamianOsipiuk avatar Apr 07 '22 19:04 DamianOsipiuk

I don't want to have to extend it though. I would ideally like to have the features of the 11ty.js extension by default which I think this would give.

pspeter3 avatar Apr 07 '22 19:04 pspeter3

Can you attach a demo repo or test case showing how this would be used with sample input files? I don’t need a formal code test case written for this repo, just want an easy thing to see a bigger picture of how this would work

zachleat avatar Apr 09 '22 20:04 zachleat

Yup! I'll make test case next week.

pspeter3 avatar Apr 09 '22 20:04 pspeter3

@zachleat I made an example repo. I had to remove the patch-package patch since Github would not let me push it to a Gist. It should work otherwise. You can see index.11ty.tsx as the example.

pspeter3 avatar Apr 11 '22 17:04 pspeter3

Oops! Forgot the link to the Gist https://gist.github.com/pspeter3/56931a837cb854c55bb06024287ead95

pspeter3 avatar Apr 11 '22 17:04 pspeter3

Just following up, is there something else I can do to help here?

pspeter3 avatar Apr 18 '22 17:04 pspeter3

Hi! It's been a few months and I'm wondering what I can do to help get this merged?

pspeter3 avatar Aug 18 '22 15:08 pspeter3

Moved this comment: https://github.com/11ty/eleventy/issues/2248#issuecomment-1341732716

zachleat avatar Dec 07 '22 23:12 zachleat

I love the simplicity of this one (the gist was amazing too). I’m working through the aliasing feature to enable this without having to hardcode these extensions in core, I think that will be a better way forward (though more challenging to implement 😅)

zachleat avatar Dec 07 '22 23:12 zachleat

That sounds great, I'm looking forward to the aliasing solution!

pspeter3 avatar Dec 07 '22 23:12 pspeter3