From d88e73fc0fd06071b1a815da36b1ba6ea97c7968 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Tue, 29 Jun 2021 15:50:46 +0800 Subject: [PATCH] fix: fix transform webpack --- src/config/webpack.ts | 4 +++- src/transform/transformWebpack.ts | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/config/webpack.ts b/src/config/webpack.ts index e9d11c8..196c689 100644 --- a/src/config/webpack.ts +++ b/src/config/webpack.ts @@ -23,9 +23,11 @@ export interface Resolve { alias: Record; } +export type Entry = string | string[] | { [entryAlias: string]: string } | any; + export interface WebpackConfig extends Config { mode?: string; - entry?: string | string[] | { [entryAlias: string]: string } | Function; + entry?: Entry; output?: Output; module?: Module; resolve?: Resolve; diff --git a/src/transform/transformWebpack.ts b/src/transform/transformWebpack.ts index a534441..148d90e 100644 --- a/src/transform/transformWebpack.ts +++ b/src/transform/transformWebpack.ts @@ -4,6 +4,7 @@ import { TransformContext } from './context' import { initViteConfig, Transformer, transformImporters } from './transformer' import path from 'path' import { DEFAULT_VUE_VERSION } from '../constants/constants' +import { Entry } from '../config/webpack' // convert webpack.config.js => vite.config.js export class WebpackTransformer implements Transformer { @@ -19,7 +20,6 @@ export class WebpackTransformer implements Transformer { const config = this.context.config // convert base config - // TODO: convert entry // webpack may have multiple entry files, e.g. // 1. one entry, with one entry file : e.g. entry: './app/index.js' // 2. one entry, with multiple entry files: e.g. entry: ['./pc/index.js','./wap/index.js'] @@ -34,7 +34,7 @@ export class WebpackTransformer implements Transformer { if (webpackConfig.entry !== '' && webpackConfig.entry !== null) { config.build.rollupOptions = {} if (isObject(webpackConfig.entry)) { - webpackConfig.entry = suitableFormat(webpackConfig.entry) + config.build.rollupOptions.input = suitableFormat(webpackConfig.entry) } else if (typeof webpackConfig.entry === 'function') { config.build.rollupOptions.input = webpackConfig.entry() } else { @@ -79,8 +79,8 @@ function isObject (value : any) : boolean { return Object.prototype.toString.call(value) === '[object Object]'; } -function suitableFormat (entry: Object) : { [entryAlias: string]: string } { - const res : { [entryAlias: string]: string } = {} +function suitableFormat (entry: Entry) : Entry { + const res : Entry = {} Object.keys(entry).forEach(function (name) { if (!Array.isArray(entry[name])) { res[name] = entry[name]