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

On-demand build time render for when using watch with the development server #368

Closed
wants to merge 2 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 @@ -104,7 +104,7 @@
"sinon": "~4.5.0"
},
"dependencies": {
"@dojo/webpack-contrib": "7.0.0-alpha.6",
"@dojo/webpack-contrib": "7.0.0-alpha.8",
"brotli-webpack-plugin": "1.0.0",
"caniuse-lite": "1.0.30000973",
"chalk": "2.4.1",
Expand Down
20 changes: 17 additions & 3 deletions src/dev.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ServiceWorkerPlugin, {
import * as fs from 'fs';
import * as path from 'path';
import webpack = require('webpack');
import BuildTimeRender from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';
import BuildTimeRender, { BuildTimeRenderArguments } from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';
import ExternalLoaderPlugin from '@dojo/webpack-contrib/external-loader-plugin/ExternalLoaderPlugin';
import * as CleanWebpackPlugin from 'clean-webpack-plugin';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
Expand Down Expand Up @@ -119,15 +119,29 @@ window['${libraryName}'].base = '${base}'</script>`,
});
}

const btrOptions: BuildTimeRenderArguments = args['build-time-render'];
if (args['build-time-render']) {
const paths = btrOptions.paths || [];
let isStatic = btrOptions.static;
if (isStatic === true) {
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (typeof path === 'object' && path.static === false) {
isStatic = false;
break;
}
}
}

config.plugins.push(
new BuildTimeRender({
...args['build-time-render'],
...btrOptions,
sync: singleBundle,
entries: Object.keys(config.entry!),
basePath,
baseUrl: base,
scope: libraryName
scope: libraryName,
watch: Boolean(args.watch && args.serve && isStatic)
})
);
}
Expand Down
19 changes: 16 additions & 3 deletions src/dist.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BuildTimeRender from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';
import BuildTimeRender, { BuildTimeRenderArguments } from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';
import ExternalLoaderPlugin from '@dojo/webpack-contrib/external-loader-plugin/ExternalLoaderPlugin';
import BundleAnalyzerPlugin from '@dojo/webpack-contrib/webpack-bundle-analyzer/BundleAnalyzerPlugin';
import ServiceWorkerPlugin, {
Expand Down Expand Up @@ -122,15 +122,28 @@ function webpackConfig(args: any): webpack.Configuration {
new CleanWebpackPlugin(['dist', 'info'], { root: output!.path, verbose: false })
].filter((item) => item);

const btrOptions: BuildTimeRenderArguments = args['build-time-render'];
if (args['build-time-render']) {
const paths = btrOptions.paths || [];
let isStatic = btrOptions.static;
if (isStatic === true) {
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (typeof path === 'object' && path.static === false) {
isStatic = false;
break;
}
}
}
config.plugins.push(
new BuildTimeRender({
...args['build-time-render'],
...btrOptions,
entries: Object.keys(config.entry!),
sync: args.singleBundle,
basePath,
baseUrl: base,
scope: libraryName
scope: libraryName,
watch: Boolean(args.watch && args.serve && btrOptions.static)
})
);
}
Expand Down
43 changes: 43 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import * as https from 'https';
import * as expressCompression from 'compression';
import * as proxy from 'http-proxy-middleware';
import * as history from 'connect-history-api-fallback';
import BuildTimeRender, { BuildTimeRenderArguments } from '@dojo/webpack-contrib/build-time-render/BuildTimeRender';

const pkgDir = require('pkg-dir');
const expressStaticGzip = require('express-static-gzip');
import { libraryName } from './base.config';
import devConfigFactory from './dev.config';
import unitConfigFactory from './unit.config';
import functionalConfigFactory from './functional.config';
Expand All @@ -28,6 +30,8 @@ const connectInject = require('connect-inject');

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

const btrPages = new Set<string>();

function createCompiler(config: webpack.Configuration) {
const compiler = webpack(config);
fixMultipleWatchTrigger(compiler);
Expand Down Expand Up @@ -150,6 +154,45 @@ function serve(config: webpack.Configuration, args: any): Promise<void> {
});

const outputDir = (config.output && config.output.path) || process.cwd();
const jsonpName = (config.output && config.output.jsonpFunction) || 'unknown';
const btrOptions: BuildTimeRenderArguments = args['build-time-render'];

let isStaticBtr = btrOptions.static;
if (btrOptions) {
const paths = btrOptions.paths || [];
isStaticBtr = btrOptions.static;
if (isStaticBtr === true) {
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (typeof path === 'object' && path.static === false) {
isStaticBtr = false;
break;
}
}
}

if (args.watch && isStaticBtr) {
app.use(base, (req, _, next) => {
const { pathname: originalPath } = url.parse(req.url);
if (req.accepts('html') && originalPath && !originalPath.match(/\..*$/) && !btrPages.has(originalPath)) {
const path = originalPath.replace(/^\//, '').replace(/\/$/, '');
const btr = new BuildTimeRender({
...args['build-time-render'],
scope: libraryName,
baseUrl: args.base || '/',
discoverPaths: false,
basePath: process.cwd(),
entries: Object.keys(config.entry!)
});

btrPages.add(originalPath);
btr.runSinglePath(next, path, outputDir, jsonpName);
} else {
next();
}
});
}

if (args.mode !== 'dist' || !Array.isArray(args.compression)) {
app.use(base, expressCompression());
}
Expand Down