From e4d193673456436862d56c8d7cda6e185a2fafd3 Mon Sep 17 00:00:00 2001 From: Caedman Ziwen Lan Date: Wed, 1 Nov 2023 09:17:53 +0800 Subject: [PATCH] feature(webpack): add custom webpack config (#7) * fix: fix issue * chore: update version * fix: fix issue --- package.json | 3 ++- src/config.ts | 1 + src/utils/project.ts | 15 +++++++++++++++ src/webpack.config.ts | 8 ++++++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ee83132..bef4c3a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@apitable/widget-cli", "description": "help you to build awesome apitable widget", - "version": "1.0.4", + "version": "1.0.8", "author": "APITable PTE. LTD.", "bin": { "widget-cli": "./bin/run" @@ -55,6 +55,7 @@ "ts-loader": "^9.2.3", "tslib": "^1", "typescript": "4.1.2", + "webpack-merge": "^5", "webpack": "^5.45.1", "yaml": "^1.10.2" }, diff --git a/src/config.ts b/src/config.ts index 6175750..a166dce 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,6 @@ export default { widgetConfigFileName: 'widget.config.json', + webpackConfigFileName: 'webpack.config.js', widgetYamlFileName: '.apitable.yml', releaseCodeName: 'widget_bundle.js', releaseCodeProdName: 'widget_bundle.min.js', diff --git a/src/utils/project.ts b/src/utils/project.ts index b6d8e60..5913ca1 100644 --- a/src/utils/project.ts +++ b/src/utils/project.ts @@ -14,6 +14,21 @@ export function getWidgetConfig(rootDir?: string): IWidgetConfig { return JSON.parse(fse.readFileSync(path.join(rootDir, Config.widgetConfigFileName), 'utf8')); } +export function getWebpackCustomConfig(rootDir?: string): any { + rootDir = rootDir ?? findWidgetRootDir(); + const checkExist = fse.existsSync(path.join(rootDir, Config.webpackConfigFileName)) + if(checkExist) { + try { + const common = require(path.join(rootDir, Config.webpackConfigFileName)); + return common; + } catch (e) { + console.error(e) + return {} + } + } + return {} +} + export function getPackageJSON(rootDir?: string) { rootDir = rootDir ?? findWidgetRootDir(); return require(path.join(rootDir, 'package.json')); diff --git a/src/webpack.config.ts b/src/webpack.config.ts index 3fe19c8..250ba23 100644 --- a/src/webpack.config.ts +++ b/src/webpack.config.ts @@ -4,16 +4,18 @@ import Config from './config'; import { CleanWebpackPlugin } from 'clean-webpack-plugin'; import { getAssetsType, viaFileLoader } from './utils/file'; import { IWebpackConfig } from './interface/webpack'; -import { getWidgetConfig } from './utils/project'; +import { getWidgetConfig, getWebpackCustomConfig } from './utils/project'; +const { merge } = require('webpack-merge'); export const getWebpackConfig = ( { dir, mode, globalFlag, config, onSucceed }: {dir: string; globalFlag: boolean | undefined, mode: 'dev' | 'prod'; config: IWebpackConfig; onSucceed: () => void} ): webpack.Configuration => { const widgetConfig = getWidgetConfig(); + const webpackConfigFromJson = getWebpackCustomConfig() as webpack.Configuration ; const packageId = (globalFlag ? widgetConfig.globalPackageId : widgetConfig.packageId) || 'wpkDeveloper'; - return { + const webpackConfig = { context: path.resolve(__dirname), entry: { bundle: path.join(dir, config.entry), @@ -145,4 +147,6 @@ export const getWebpackConfig = ( new CleanWebpackPlugin() ], }; + const result = merge(webpackConfig, webpackConfigFromJson) + return result };