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

Watch mode with no dev server #1264

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ $ preact watch

--src Specify source directory (default src)
--cwd A directory to use instead of $PWD (default .)
--esm Builds ES-2015 bundles for your code. (default true)
--devServer Determine if dev server should be enabled (default true)
--dest Specify output directory if dev server is disabled (default build)
--ignore Path relative to src to be ignored during watch if dev server is disabled
--esm Builds ES-2015 bundles for your code. (default true)
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: The slightly different indentation here is quite noticeable on the rendered file

--sw Generate and attach a Service Worker (default false)
--json Generate build stats for bundle analysis
--https Run server with HTTPS protocol
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/lib/commands/watch.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const runWebpack = require('../lib/webpack/run-webpack');
const { warn } = require('../util');

const toBool = (val) => val === void 0 || (val === 'false' ? false : val);

module.exports = async function (src, argv) {
if (argv.rhl) {
delete argv.rhl;
argv.refresh = argv.rhl;
}

argv.prerender = toBool(argv.prerender);
argv.src = src || argv.src;
argv.production = false;

Expand Down
11 changes: 11 additions & 0 deletions packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ prog
.describe('Start a live-reload server for development')
.option('--src', 'Specify source directory', 'src')
.option('--cwd', 'A directory to use instead of $PWD', '.')
.option('--devServer', 'Determine if dev server should be enabled', true)
.option(
'--dest',
'Specify output directory if dev server is disabled',
'build'
)
.option(
'--ignore',
'Path relative to src to be ignored during watch if dev server is disabled',
''
Copy link
Member

Choose a reason for hiding this comment

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

This default value means that src/ is ignored by default. I don't think this is intended?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
''
null

)
.option('--esm', 'Builds ES-2015 bundles for your code.', false)
.option('--clear', 'Clear the console', true)
.option('--sw', 'Generate and attach a Service Worker', undefined)
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/lib/lib/webpack/run-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ async function devBuild(env) {

await transformConfig(env, config);

const shouldRunDevServer = env.devServer;

if (!shouldRunDevServer && env.prerender) {
let ssrConfig = serverConfig(env);
await transformConfig(env, ssrConfig, true);
let serverCompiler = webpack(ssrConfig);
await runWatch(serverCompiler, {
ignored: env.source(env.ignore),
});
}

let userPort =
parseInt(process.env.PORT || config.devServer.port, 10) || 8080;
let port = await getPort({ port: userPort });
Expand All @@ -39,6 +50,8 @@ async function devBuild(env) {
});

compiler.hooks.done.tap('CliDevPlugin', (stats) => {
if (!shouldRunDevServer) return;

let devServer = config.devServer;
let protocol = process.env.HTTPS || devServer.https ? 'https' : 'http';
let host = process.env.HOST || devServer.host || 'localhost';
Expand Down Expand Up @@ -71,6 +84,13 @@ async function devBuild(env) {

compiler.hooks.failed.tap('CliDevPlugin', rej);

if (!shouldRunDevServer)
return res(
runWatch(compiler, {
ignored: env.source(env.ignore),
})
);

let c = Object.assign({}, config.devServer, {
stats: { colors: true },
hot: !env.refresh,
Expand Down Expand Up @@ -122,6 +142,20 @@ function runCompiler(compiler) {
});
}

function runWatch(compiler, options = {}) {
return new Promise((res, rej) => {
compiler.watch(options, (err, stats) => {
showStats(stats, true);

if (err || (stats && stats.hasErrors())) {
rej(`${red('\n\nBuild failed! \n\n')} ${err || ''}`);
}

res(stats);
});
});
}

function showStats(stats, isProd) {
if (stats) {
if (stats.hasErrors()) {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/lib/webpack/webpack-client-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const cleanFilename = (name) =>
);

async function clientConfig(env) {
const { isProd, source, src, refresh, cwd /*, port? */ } = env;
const { isProd, source, src, refresh, cwd, devServer /*, port? */ } = env;
const IS_SOURCE_PREACT_X_OR_ABOVE = isInstalledVersionPreactXOrAbove(cwd);
const asyncLoader = IS_SOURCE_PREACT_X_OR_ABOVE
? require.resolve('@preact/async-loader')
Expand All @@ -36,7 +36,7 @@ async function clientConfig(env) {
polyfills: resolve(__dirname, './polyfills'),
};

if (!isProd) {
if (!isProd && devServer) {
entry.bundle = [
entry.bundle,
'webpack-dev-server/client',
Expand Down