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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "preact-cli",
"version": "1.1.0",
"description": "Start building a Preact Progressive Web App in seconds.",
"main": "lib/index.js",
"main": "lib/compiler.js",
"bin": {
"preact": "./lib/index.js"
},
Expand Down
16 changes: 5 additions & 11 deletions src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { resolve } from 'path';
import promisify from 'es6-promisify';
import rimraf from 'rimraf';
import asyncCommand from '../lib/async-command';
import webpackConfig from '../lib/webpack-config';
import runWebpack, { showStats, writeJsonStats } from '../lib/run-webpack';
import createCompiler from '../compiler';

export default asyncCommand({
command: 'build [src] [dest]',
Expand Down Expand Up @@ -39,18 +35,16 @@ export default asyncCommand({
},

async handler(argv) {
let config = webpackConfig(argv);
let compiler = createCompiler(argv);

if (argv.clean) {
let dest = resolve(argv.cwd || process.cwd(), argv.dest || 'build');
await promisify(rimraf)(dest);
await compiler.clean();
}

let stats = await runWebpack(false, config);
showStats(stats);
let stats = await compiler.compile();

if (argv.json) {
await writeJsonStats(stats);
await compiler.writeJsonStats(stats);
}
}
});
7 changes: 2 additions & 5 deletions src/commands/watch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncCommand from '../lib/async-command';
import webpackConfig from '../lib/webpack-config';
import runWebpack, { showStats } from '../lib/run-webpack';
import createCompiler from '../compile';

export default asyncCommand({
command: 'watch [src]',
Expand Down Expand Up @@ -35,9 +34,7 @@ export default asyncCommand({

async handler(argv) {
argv.production = false;
let config = webpackConfig(argv);

let stats = await runWebpack(true, config, showStats);
showStats(stats);
await createCompiler(argv).compile(true);
}
});
32 changes: 32 additions & 0 deletions src/compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { resolve } from 'path';
import promisify from 'es6-promisify';
import rimraf from 'rimraf';
import prerender from './lib/prerender';
import webpackConfig from './lib/webpack-config';
import runWebpack, { showStats, writeJsonStats } from './lib/run-webpack';

export default env => {
let config = webpackConfig(env)

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

await promisify(rimraf)(dest);
}

async compile(watch = false) {
let stats = await runWebpack(watch, config, showStats);
showStats(stats);

return stats;
}

prerender(params) {
return prerender(env, params);
}

writeJsonStats(stats) {
return writeJsonStats(stats);
}
}
}