Skip to content

Commit

Permalink
experimental nollup support
Browse files Browse the repository at this point in the history
  • Loading branch information
rixo committed May 17, 2020
1 parent 69cce85 commit faca5e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
37 changes: 21 additions & 16 deletions src/api/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ class Watcher extends EventEmitter {
return;
}

this.dev_server = new DevServer(this.dev_port);

this.filewatchers.push(
watch_dir(
routes,
Expand Down Expand Up @@ -210,7 +208,17 @@ class Watcher extends EventEmitter {
)
);

if (this.live) {
let deferred = new Deferred();

// TODO watch the configs themselves?
const compilers: Compilers = await create_compilers(this.bundler, cwd, src, dest, true);

// Nollup provides its own dev server & protocol
if (compilers.dev_server) {
this.dev_server = new DevServer(this.dev_port);
}

if (this.live && this.dev_server) {
this.filewatchers.push(
fs.watch(`${src}/template.html`, () => {
this.dev_server.send({
Expand All @@ -220,11 +228,6 @@ class Watcher extends EventEmitter {
);
}

let deferred = new Deferred();

// TODO watch the configs themselves?
const compilers: Compilers = await create_compilers(this.bundler, cwd, src, dest, true);

const emitFatal = () => {
this.emit('fatal', <FatalEvent>{
message: `Server crashed`
Expand Down Expand Up @@ -253,14 +256,16 @@ class Watcher extends EventEmitter {
process: this.proc
});

if (this.hot && this.bundler === 'webpack') {
this.dev_server.send({
status: 'completed'
});
} else if (this.live) {
this.dev_server.send({
action: 'reload'
});
if (this.dev_server) {
if (this.hot && this.bundler === 'webpack') {
this.dev_server.send({
status: 'completed'
});
} else if (this.live) {
this.dev_server.send({
action: 'reload'
});
}
}
}))
.catch(err => {
Expand Down
13 changes: 10 additions & 3 deletions src/core/create_compilers/RollupCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CompileResult } from './interfaces';
import RollupResult from './RollupResult';

let rollup: any;
let nollup: any;

export default class RollupCompiler {
_: Promise<any>;
Expand All @@ -14,14 +15,16 @@ export default class RollupCompiler {
errors: any[];
chunks: any[];
css_files: Array<{ id: string, code: string }>;
use_nollup: any;

constructor(config: any) {
constructor(config: any, use_nollup: boolean = false) {
this._ = this.get_config(config);
this.input = null;
this.warnings = [];
this.errors = [];
this.chunks = [];
this.css_files = [];
this.use_nollup = use_nollup;
}

async get_config(mod: any) {
Expand Down Expand Up @@ -88,7 +91,10 @@ export default class RollupCompiler {
const config = await this._;
const sourcemap = config.output.sourcemap;

const watcher = rollup.watch(config);
const watcher = this.use_nollup
// NOTE depends on experimental Nollup branch
? nollup.watch(config)
: rollup.watch(config);

watcher.on('change', (id: string) => {
this.chunks = [];
Expand Down Expand Up @@ -137,8 +143,9 @@ export default class RollupCompiler {
});
}

static async load_config(cwd: string) {
static async load_config(cwd: string, use_nollup: boolean = false) {
if (!rollup) rollup = relative('rollup', cwd);
if (use_nollup && !nollup) nollup = relative('nollup/lib/dev-server', cwd);

const input = path.resolve(cwd, 'rollup.config.js');

Expand Down
15 changes: 11 additions & 4 deletions src/core/create_compilers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import RollupCompiler from './RollupCompiler';
import { WebpackCompiler } from './WebpackCompiler';
import { set_dev, set_src, set_dest } from '../../config/env';

// TODO make that a regular option
const NOLLUP = !!process.env.NOLLUP

export type Compiler = RollupCompiler | WebpackCompiler;

export type Compilers = {
client: Compiler;
server: Compiler;
serviceworker?: Compiler;
dev_server: boolean;
}

export default async function create_compilers(
Expand All @@ -23,7 +27,7 @@ export default async function create_compilers(
set_dest(dest);

if (bundler === 'rollup') {
const config = await RollupCompiler.load_config(cwd);
const config = await RollupCompiler.load_config(cwd, NOLLUP);
validate_config(config, 'rollup');

normalize_rollup_config(config.client);
Expand All @@ -34,9 +38,11 @@ export default async function create_compilers(
}

return {
client: new RollupCompiler(config.client),
client: new RollupCompiler(config.client, NOLLUP && dev),
server: new RollupCompiler(config.server),
serviceworker: config.serviceworker && new RollupCompiler(config.serviceworker)
serviceworker: config.serviceworker && new RollupCompiler(config.serviceworker),
// Nollup provides its own dev server
dev_server: !NOLLUP,
};
}

Expand All @@ -47,7 +53,8 @@ export default async function create_compilers(
return {
client: new WebpackCompiler(config.client),
server: new WebpackCompiler(config.server),
serviceworker: config.serviceworker && new WebpackCompiler(config.serviceworker)
serviceworker: config.serviceworker && new WebpackCompiler(config.serviceworker),
dev_server: true,
};
}

Expand Down

0 comments on commit faca5e0

Please sign in to comment.