From afae83ba8d3a536ccc6e32cfb9d0071684f3d211 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 16 May 2020 17:49:04 +0200 Subject: [PATCH] packages/cli: make it possible to configure bundle entrypoint --- packages/cli/src/commands/plugin/serve.ts | 2 +- packages/cli/src/lib/bundle/config.ts | 11 ++++------- packages/cli/src/lib/bundle/paths.ts | 11 +++++++++-- packages/cli/src/lib/bundle/server.ts | 8 ++++---- packages/cli/src/lib/bundle/types.ts | 19 +++++++++++++++++++ 5 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 packages/cli/src/lib/bundle/types.ts diff --git a/packages/cli/src/commands/plugin/serve.ts b/packages/cli/src/commands/plugin/serve.ts index b06871ce9d..1c732cfc9a 100644 --- a/packages/cli/src/commands/plugin/serve.ts +++ b/packages/cli/src/commands/plugin/serve.ts @@ -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(() => {}); diff --git a/packages/cli/src/lib/bundle/config.ts b/packages/cli/src/lib/bundle/config.ts index 2d39570339..f7ef31dcba 100644 --- a/packages/cli/src/lib/bundle/config.ts +++ b/packages/cli/src/lib/bundle/config.ts @@ -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'], diff --git a/packages/cli/src/lib/bundle/paths.ts b/packages/cli/src/lib/bundle/paths.ts index d98f7ed5de..9a030d21c1 100644 --- a/packages/cli/src/lib/bundle/paths.ts +++ b/packages/cli/src/lib/bundle/paths.ts @@ -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'), diff --git a/packages/cli/src/lib/bundle/server.ts b/packages/cli/src/lib/bundle/server.ts index 651c9fa236..64960fa3a6 100644 --- a/packages/cli/src/lib/bundle/server.ts +++ b/packages/cli/src/lib/bundle/server.ts @@ -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: '/', diff --git a/packages/cli/src/lib/bundle/types.ts b/packages/cli/src/lib/bundle/types.ts new file mode 100644 index 0000000000..8a01d74402 --- /dev/null +++ b/packages/cli/src/lib/bundle/types.ts @@ -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 & {};