packages/cli: add flag for toggling type checking as a part of plugin and app serve

This commit is contained in:
Patrik Oldsberg
2020-05-16 18:04:26 +02:00
parent 5cb37b504a
commit 0fb2440d97
8 changed files with 54 additions and 28 deletions
+6 -2
View File
@@ -15,9 +15,13 @@
*/
import { startDevServer } from '../../lib/bundle';
import { Command } from 'commander';
export default async () => {
await startDevServer({ entry: 'src/index' });
export default async (cmd: Command) => {
await startDevServer({
entry: 'src/index',
checksEnabled: cmd.check,
});
// Wait for interrupt signal
await new Promise(() => {});
+6 -2
View File
@@ -15,9 +15,13 @@
*/
import { startDevServer } from '../../lib/bundle';
import { Command } from 'commander';
export default async () => {
await startDevServer({ entry: 'dev/index' });
export default async (cmd: Command) => {
await startDevServer({
entry: 'dev/index',
checksEnabled: cmd.check,
});
// Wait for interrupt signal
await new Promise(() => {});
+3 -1
View File
@@ -35,6 +35,7 @@ const main = (argv: string[]) => {
program
.command('app:serve')
.description('Serve an app for local development')
.option('--check', 'Enable type checking and linting')
.action(actionHandler(() => require('./commands/app/serve')));
program
@@ -60,6 +61,7 @@ const main = (argv: string[]) => {
program
.command('plugin:serve')
.description('Serves the dev/ folder of a plugin')
.option('--check', 'Enable type checking and linting')
.action(actionHandler(() => require('./commands/plugin/serve')));
program
@@ -155,7 +157,7 @@ function actionHandler<T extends readonly any[]>(
};
}
process.on('unhandledRejection', (rejection) => {
process.on('unhandledRejection', rejection => {
if (rejection instanceof Error) {
exitWithError(rejection);
} else {
+30 -20
View File
@@ -18,7 +18,7 @@ import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import ModuleScopePlugin from 'react-dev-utils/ModuleScopePlugin';
import { resolveBundlingPaths } from './paths';
import { BundlingPaths } from './paths';
import { loaders } from './loaders';
import { optimization } from './optimization';
import { BundlingOptions } from './types';
@@ -28,8 +28,34 @@ import { BundlingOptions } from './types';
// import evalSourceMapMiddleware from 'react-dev-utils/evalSourceMapMiddleware';
// import WatchMissingNodeModulesPlugin from 'react-dev-utils/WatchMissingNodeModulesPlugin';
export function createConfig(options: BundlingOptions): webpack.Configuration {
const paths = resolveBundlingPaths(options);
export function createConfig(
paths: BundlingPaths,
options: BundlingOptions,
): webpack.Configuration {
const { checksEnabled } = options;
const plugins = [
new HtmlWebpackPlugin({
template: paths.targetHtml,
}),
new webpack.HotModuleReplacementPlugin(),
];
if (checksEnabled) {
plugins.push(
new ForkTsCheckerWebpackPlugin({
tsconfig: paths.targetTsConfig,
eslint: true,
eslintOptions: {
parserOptions: {
project: paths.targetTsConfig,
tsconfigRootDir: paths.targetPath,
},
},
reportFiles: ['**', '!**/__tests__/**', '!**/?(*.)(spec|test).*'],
}),
);
}
return {
mode: 'development',
@@ -59,23 +85,7 @@ export function createConfig(options: BundlingOptions): webpack.Configuration {
filename: 'bundle.js',
},
optimization: optimization(),
plugins: [
new HtmlWebpackPlugin({
template: paths.targetHtml,
}),
new ForkTsCheckerWebpackPlugin({
tsconfig: paths.targetTsConfig,
eslint: true,
eslintOptions: {
parserOptions: {
project: paths.targetTsConfig,
tsconfigRootDir: paths.targetPath,
},
},
reportFiles: ['**', '!**/__tests__/**', '!**/?(*.)(spec|test).*'],
}),
new webpack.HotModuleReplacementPlugin(),
],
plugins,
node: {
module: 'empty',
dgram: 'empty',
+2 -1
View File
@@ -14,4 +14,5 @@
* limitations under the License.
*/
export * from './server';
export { bundle } from './bundle';
export { startDevServer } from './server';
+1
View File
@@ -43,6 +43,7 @@ export function resolveBundlingPaths(options: BundlingPathsOptions) {
return {
targetHtml,
targetPath: paths.resolveTarget('.'),
targetDist: paths.resolveTarget('dist'),
targetAssets: paths.resolveTarget('assets'),
targetSrc: paths.resolveTarget('src'),
targetDev: paths.resolveTarget('dev'),
+3 -1
View File
@@ -21,6 +21,7 @@ import openBrowser from 'react-dev-utils/openBrowser';
import { choosePort, prepareUrls } from 'react-dev-utils/WebpackDevServerUtils';
import { createConfig } from './config';
import { BundlingOptions } from './types';
import { resolveBundlingPaths } from './paths';
export async function startDevServer(options: BundlingOptions) {
const host = process.env.HOST ?? '0.0.0.0';
@@ -34,7 +35,8 @@ export async function startDevServer(options: BundlingOptions) {
const protocol = yn(process.env.HTTPS, { default: false }) ? 'https' : 'http';
const urls = prepareUrls(protocol, host, port);
const config = createConfig(options);
const paths = resolveBundlingPaths(options);
const config = createConfig(paths, options);
const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
+3 -1
View File
@@ -16,4 +16,6 @@
import { BundlingPathsOptions } from './paths';
export type BundlingOptions = BundlingPathsOptions & {};
export type BundlingOptions = BundlingPathsOptions & {
checksEnabled: boolean;
};