packages/cli: make it possible to configure bundle entrypoint

This commit is contained in:
Patrik Oldsberg
2020-05-16 17:49:04 +02:00
parent 989b13ca75
commit afae83ba8d
5 changed files with 37 additions and 14 deletions
+1 -1
View File
@@ -17,7 +17,7 @@
import { startDevServer } from '../../lib/bundle';
export default async () => {
await startDevServer();
await startDevServer({ entry: 'dev/index' });
// Wait for interrupt signal
await new Promise(() => {});
+4 -7
View File
@@ -18,21 +18,18 @@ 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 { BundlingPaths } from './paths';
import { resolveBundlingPaths } from './paths';
import { loaders } from './loaders';
import { optimization } from './optimization';
import { BundlingOptions } from './types';
// import checkRequiredFiles from 'react-dev-utils/checkRequiredFiles';
// import ModuleNotFoundPlugin from 'react-dev-utils/ModuleNotFoundPlugin';
// import errorOverlayMiddleware from 'react-dev-utils/errorOverlayMiddleware';
// import evalSourceMapMiddleware from 'react-dev-utils/evalSourceMapMiddleware';
// import WatchMissingNodeModulesPlugin from 'react-dev-utils/WatchMissingNodeModulesPlugin';
type BundlingOptions = {
paths: BundlingPaths;
};
export function createConfig(options: BundlingOptions): webpack.Configuration {
const { paths } = options;
const paths = resolveBundlingPaths(options);
return {
mode: 'development',
@@ -40,7 +37,7 @@ export function createConfig(options: BundlingOptions): webpack.Configuration {
bail: false,
devtool: 'cheap-module-eval-source-map',
context: paths.targetPath,
entry: [require.resolve('react-hot-loader/patch'), paths.targetDevEntry],
entry: [require.resolve('react-hot-loader/patch'), paths.targetEntry],
resolve: {
extensions: ['.ts', '.tsx', '.mjs', '.js', '.jsx'],
mainFields: ['main:src', 'browser', 'module', 'main'],
+9 -2
View File
@@ -17,7 +17,14 @@
import { existsSync } from 'fs';
import { paths } from '../paths';
export function resolveBundlingPaths() {
export type BundlingPathsOptions = {
// bundle entrypoint, e.g. 'src/index'
entry: string;
};
export function resolveBundlingPaths(options: BundlingPathsOptions) {
const { entry } = options;
const resolveTargetModule = (path: string) => {
for (const ext of ['mjs', 'js', 'ts', 'tsx', 'jsx']) {
const filePath = paths.resolveTarget(`${path}.${ext}`);
@@ -39,7 +46,7 @@ export function resolveBundlingPaths() {
targetAssets: paths.resolveTarget('assets'),
targetSrc: paths.resolveTarget('src'),
targetDev: paths.resolveTarget('dev'),
targetDevEntry: resolveTargetModule('dev/index'),
targetEntry: resolveTargetModule(entry),
targetTsConfig: paths.resolveTarget('tsconfig.json'),
targetNodeModules: paths.resolveTarget('node_modules'),
targetPackageJson: paths.resolveTarget('package.json'),
+4 -4
View File
@@ -19,10 +19,10 @@ import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import openBrowser from 'react-dev-utils/openBrowser';
import { choosePort, prepareUrls } from 'react-dev-utils/WebpackDevServerUtils';
import { resolveBundlingPaths } from './paths';
import { createConfig } from './config';
import { BundlingOptions } from './types';
export async function startDevServer() {
export async function startDevServer(options: BundlingOptions) {
const host = process.env.HOST ?? '0.0.0.0';
const defaultPort = parseInt(process.env.PORT ?? '', 10) || 3000;
@@ -34,9 +34,9 @@ export async function startDevServer() {
const protocol = yn(process.env.HTTPS, { default: false }) ? 'https' : 'http';
const urls = prepareUrls(protocol, host, port);
const paths = resolveBundlingPaths();
const config = createConfig({ paths });
const config = createConfig(options);
const compiler = webpack(config);
const server = new WebpackDevServer(compiler, {
hot: true,
publicPath: '/',
+19
View File
@@ -0,0 +1,19 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BundlingPathsOptions } from './paths';
export type BundlingOptions = BundlingPathsOptions & {};