Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose compiler object responsible for compiling the preact application #88

Closed
wants to merge 3 commits into from
Closed

Expose compiler object responsible for compiling the preact application #88

wants to merge 3 commits into from

Conversation

robinvdvleuten
Copy link

@robinvdvleuten robinvdvleuten commented Jun 2, 2017

As mentioned by #70, I started creating a reusable compiler instance responsible for building or watching a preact application. The commands are now using this compiler to generate their output. The compiler is exposed as main entry so one can use it in their own project like this;

const createCompiler = require('preact-cli');

let compiler = createCompiler(/* any options can be passed here */);

// Compile the application and optionally serve it through the dev server by passing a bool.
await compiler.compile(true);

// Return a prerendered string for the given params
compiler.prerender(/* any params passed to prerender */)

I guess this can be seen as a first step in exposing an api to built upon; like exposing the prerender functionality to render compiled preact strings in a node.js app, replacing the dev server with dev middleware so you can mount the preact-cli on your own server, etc.

@robinvdvleuten
Copy link
Author

@developit did you manage to find some time to look at this PR? It would be great to have some sort of extendability built into this project

@developit
Copy link
Member

Not yet, no. Hopefully someone else can review before me!

src/compiler.js Outdated
import webpackConfig from './lib/webpack-config';
import runWebpack, { showStats } from './lib/run-webpack';

class Compiler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about everyone's opinions on this, but I wondered if this could have been done more directly via a closure + exported object (just a POJO factory). If everyone is good with classes I don't have a strong opinion here, just it's the first class into this codebase that isn't specifically for Webpack, so I thought I'd ask :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Can anyone make an opinion?)
The implementation looks awesome, but as a personal choice, I'd prefer non-class approaches 👍
I guess it depends on the project... are classes used in preact-cli?
If so, it could be good to keep consistency with pojo/factory approach 😃

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah maybe a pojo is a better idea, I'll convert it!


return {
async clean() {
let dest = resolve(env.cwd || process.cwd(), env.dest || 'build');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In retrospective this could have been config.output.path - if this lands before #56 I'll change it there

@developit developit mentioned this pull request Jun 28, 2017
@ForsakenHarmony
Copy link
Member

Is this still relevant with the ssr-bundle being produced now?

@ForsakenHarmony
Copy link
Member

given that there has been no activity I'm gonna close this

we're working towards modularising the cli which will also export the config

@Download
Copy link
Contributor

Why was this closed?
I still cannot find a good answer to the question:
How to take a preact-cli app and make it server-side rendering?
I was hoping this PR was the answer...

@lukeed
Copy link
Member

lukeed commented May 30, 2018

@Download You can require the build/ssr-build/ssr-bundle.js file directly and pass in whatever URLs you want to as props.url.

It's the same approach the CLI is using internally to produce the "prerendered" outputs -- except that you can do it at runtime.

I have a full example with this approach & am using it on a couple sites: https://github.com/lukeed/preact-cli-ssr

@Download
Copy link
Contributor

Thanks @lukeed an example is always best! I will check out your repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants