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

Remove memory watch and implement reload with normal watch #224

Merged
merged 2 commits into from
Dec 17, 2018
Merged
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
35 changes: 20 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"cli-columns": "3.1.2",
"compression-webpack-plugin": "2.0.0",
"connect-history-api-fallback": "1.5.0",
"connect-inject": "0.4.0",
"css-loader": "1.0.1",
"eventsource-polyfill": "0.9.6",
"express": "4.16.2",
Expand Down Expand Up @@ -144,7 +145,6 @@
"umd-compat-loader": "2.1.1",
"webpack": "4.25.1",
"webpack-chunk-hash": "0.6.0",
"webpack-dev-middleware": "3.4.0",
"webpack-hot-middleware": "2.24.3",
"webpack-manifest-plugin": "2.0.4",
"webpack-mild-compile": "3.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/dev.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function webpackConfig(args: any): webpack.Configuration {
}
}

if (args.watch !== 'memory' && args['build-time-render']) {
if (args['build-time-render']) {
config.plugins.push(
new BuildTimeRender({
...args['build-time-render'],
Expand Down
2 changes: 1 addition & 1 deletion src/dist.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function webpackConfig(args: any): webpack.Configuration {
}
}

if (args.watch !== 'memory' && args['build-time-render']) {
if (args['build-time-render']) {
config.plugins.push(
new BuildTimeRender({
...args['build-time-render'],
Expand Down
104 changes: 43 additions & 61 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { moveBuildOptions } from './util/eject';

const fixMultipleWatchTrigger = require('webpack-mild-compile');
const hotMiddleware = require('webpack-hot-middleware');
const webpackMiddleware = require('webpack-dev-middleware');
const connectInject = require('connect-inject');

const testModes = ['test', 'unit', 'functional'];

Expand Down Expand Up @@ -87,8 +87,21 @@ function buildNpmDependencies(): any {
}
}

function fileWatch(config: webpack.Configuration, args: any): Promise<void> {
const compiler = createWatchCompiler(config);
function fileWatch(config: webpack.Configuration, args: any, app?: express.Application): Promise<void> {
let compiler: webpack.Compiler;
if (args.serve && app) {
const entry = config.entry as any;
const plugins = config.plugins as webpack.Plugin[];
const timeout = 20 * 1000;
plugins.push(new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin());
Object.keys(entry).forEach((name) => {
entry[name].unshift('eventsource-polyfill');
});
compiler = createWatchCompiler(config);
app.use(hotMiddleware(compiler, { heartbeat: timeout / 2 }));
} else {
compiler = createWatchCompiler(config);
}

return new Promise<void>((resolve, reject) => {
const watchOptions = config.watchOptions as webpack.Compiler.WatchOptions;
Expand All @@ -105,43 +118,24 @@ function fileWatch(config: webpack.Configuration, args: any): Promise<void> {
});
}

function memoryWatch(config: webpack.Configuration, args: any, app: express.Application): Promise<void> {
const entry = config.entry as any;
const plugins = config.plugins as webpack.Plugin[];
const timeout = 20 * 1000;

plugins.push(new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin());
Object.keys(entry).forEach((name) => {
entry[name].unshift(`webpack-hot-middleware/client?timeout=${timeout}&reload=true`);
entry[name].unshift('eventsource-polyfill');
});

const watchOptions = config.watchOptions as webpack.Compiler.WatchOptions;
const compiler = createWatchCompiler(config);

compiler.hooks.done.tap('@dojo/cli-build-app', (stats) => {
logger(stats.toJson({ warningsFilter }), config, `Listening on port ${args.port}...`);
});

app.use(
webpackMiddleware(compiler, {
logLevel: 'silent',
noInfo: true,
publicPath: '/',
watchOptions
}),
hotMiddleware(compiler, {
heartbeat: timeout / 2
})
);

return Promise.resolve();
}

function serve(config: webpack.Configuration, args: any): Promise<void> {
let isHttps = false;

const app = express();
app.use(
connectInject({
rules: [
{
match: /<body>/,
snippet:
"<script>window.DojoHasEnvironment = { staticFeatures: { 'build-serve': true } };</script>",
fn: (match: string, snippet: string) => {
return snippet + match;
}
}
]
})
);

app.use(
history({
Expand Down Expand Up @@ -169,19 +163,17 @@ function serve(config: webpack.Configuration, args: any): Promise<void> {
})
);

if (args.watch !== 'memory') {
const outputDir = (config.output && config.output.path) || process.cwd();
if (args.mode === 'dist' && Array.isArray(args.compression)) {
const useBrotli = args.compression.includes('brotli');
app.use(
expressStaticGzip(outputDir, {
enableBrotli: useBrotli,
orderPreference: useBrotli ? ['br'] : undefined
})
);
} else {
app.use(express.static(outputDir));
}
const outputDir = (config.output && config.output.path) || process.cwd();
if (args.mode === 'dist' && Array.isArray(args.compression)) {
const useBrotli = args.compression.includes('brotli');
app.use(
expressStaticGzip(outputDir, {
enableBrotli: useBrotli,
orderPreference: useBrotli ? ['br'] : undefined
})
);
} else {
app.use(express.static(outputDir));
}

if (args.proxy) {
Expand All @@ -205,15 +197,8 @@ function serve(config: webpack.Configuration, args: any): Promise<void> {

return Promise.resolve()
.then(() => {
if (args.watch === 'memory' && args.mode === 'dev') {
return memoryWatch(config, args, app);
}

if (args.watch) {
if (args.watch === 'memory') {
console.warn('Memory watch requires `--mode=dev`. Using file watch instead...');
}
return fileWatch(config, args);
return fileWatch(config, args, app);
}

return build(config, args);
Expand Down Expand Up @@ -266,7 +251,7 @@ const command: Command = {
});

options('watch', {
describe: 'watch for file changes: "memory" (dev mode only) or "file" (all modes; default)',
describe: 'watch for file changes',
alias: 'w'
});

Expand Down Expand Up @@ -339,9 +324,6 @@ const command: Command = {
}

if (args.watch) {
if (args.watch === 'memory') {
console.warn('Memory watch requires the dev server. Using file watch instead...');
}
return fileWatch(config, args);
}

Expand Down
Loading