Typescript support for External Source Plugins #1422
-
Type of ChangeNew Feature SummaryPlease support using TypeScript to write an External Source Plugin DetailsI attempted to follow https://www.greenwoodjs.io/plugins/source/ changing the .js extension out for .ts and get an ERR_UNKNOWN_FILE_EXTENSION that persists even after I was assisted in solving these errors for other sections of my configuration here. I suspect that its actually possible to do this, but that it would be (currently) a multi-step process, where I'd need to transpile the plugin to javascript to then be able to run the greenwood develop or build commands. This request is to support, and document, a configuration that will consolidate that into a single command line that could be included as the develop script in a package.json file. I further suspect that this is related to #1250 where in the comments I requested support for the main configuration file to be supported as a typescript file. My intuition is that if you solved that, this would be mostly solved as well. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Yeah, pretty much. Right now the TypeScript plugin is only geared towards transforming "web" code, e.g. anything in your src/ directory, but not really anything else. Let me align this with #1250 though since I think this would be something we would apply to all plugins at large. My guess is that this would likely try and tackle this in two phases:
I've also been experimenting with Bun and Deno and they are working pretty well out of the box with some Greenwood demo apps as part of initial testing, and so now with NodeJS also supporting TS, I may even considering moving TS support right into Greenwood itself, probably with a flag though. We shall see! |
Beta Was this translation helpful? Give feedback.
-
First Party TypeScript SupportSo I had some time to play around with what first party TypeScript support might look like in Greenwood, and I think there might be a good path forward here, in particular now that NodeJS provides built-in support for type stripping (and some transformations). With this, a lot of the transformation work can be handled for us for free from the runtime itself. (and would also apply to Deno / Bun if and when we get to them!) $ NODE_OPTIONS='--experimental-strip-types' greenwood <command>
NodeJS SupportAs far as NodeJS support goes, it breaks downs like so:
The recommended compiler config is: {
"compilerOptions": {
"target": "esnext",
"module": "nodenext",
"allowImportingTsExtensions": true,
"rewriteRelativeImportExtensions": true,
"verbatimModuleSyntax": true,
"checkJs": true,
"allowJs": true,
"noEmit": true,
"skipLibCheck": true,
}
}
One important point the docs allude to is that it might be worth supporting a contingency wherein if a user wants full TypeScript support, then Greenwood might want to enable falling back to Example: // greenwood.config.ts
export default {
// would process using tsc instead of amaro
// and kick in for SSR pages and API routes
useTsc: true|false
}Configuration / PluginsThis one is pretty easy, we would just have to also check for a greenwood.config.ts file and with that, we should also get TS based plugins for free. // greenwood.config.ts
import { customExternalSourcePlugin } from "./external-content-source-plugin.ts";
const port: number = 8181;
export default {
devServer: {
port
},
port,
plugins: [
customExternalSourcePlugin()
]
}// external-content-source-plugin.ts
export const customExternalSourcePlugin = () => {
return {
type: "source",
name: "source-plugin-external-page",
provider: () => {
return async function () {
return [{
title: 'My custom external page',
body: '<h1>My custom external page</h1>',
route: '/external/',
label: 'External',
data: []
}];
}
},
};
};SSR Pages / API RoutesEnabling SSR pages (e.g. src/pages/index.ts) and API routes (e.g. src/pages/api/greeting.ts) would be pretty similar to enabling TS based configuration / plugisn; basically just allowing .ts as an extension in a couple places in Greenwood's graph lifecycle and during Rollup bundling:
Browser ScriptsHowever, this still leaves client / browser side code (e.g. This means Greenwood would have to at least pull in a dependency for this use case to do the transformations, and thus would have an internal "TypeScript" plugin. Luckily, we would at least have access to the same logic Node itself uses (amaro), which can be overridden through the resolutions / overrides feature of any modern package manager. TL;DRSo in summary, the status of things seems pretty good and straight-forward, all things considered:
The scope required of this feature would breakdown as follows:
In general, since the work on the Greenwood is pretty minimal, and we are seeing validation even from the runtime itself, and given how foundational and useful TypeScript is to web dev now, and that we're basically able to externalize all the configuration, dependency management, and most of the transformations, it seems pretty low risk / effort to bring it into core. Additionally, I think the chance to consolidate all usage documentation into one place and not have to juggle between too many choices for users (if you you're using it like this do that, but if in this case, do this, otherwise if you're using it like this, do this...) Please let me know if I have overlooked any particular surface areas of Greenwood in this analysis. I've moved this to a discussion for now and then assuming all these pieces pan out from folks following along and the rest of the Greenwood team are on-board with the plan, I'll create a dedicated issue for introducing "first party" TypeScript support into Greenwood, which I think would then probably just invalidate #1327 and #658. |
Beta Was this translation helpful? Give feedback.
-
|
OK, had a chance to discuss with the team, and everyone is supportive here, so created a tracking issue for built-in TypeScript support for Greenwood - #1424. Should be able to get to this in the next week and have this queued up for v0.32.0-alpha.1 which I hope to be able to cut next weekend. Please follow along with that issue and I will post in the Discord when the alpha release are available for testing / feedback. 👍 |
Beta Was this translation helpful? Give feedback.
OK, had a chance to discuss with the team, and everyone is supportive here, so created a tracking issue for built-in TypeScript support for Greenwood - #1424.
Should be able to get to this in the next week and have this queued up for v0.32.0-alpha.1 which I hope to be able to cut next weekend. Please follow along with that issue and I will post in the Discord when the alpha release are available for testing / feedback. 👍