Skip to content

Commit

Permalink
feature(webpack): add custom webpack config (#7)
Browse files Browse the repository at this point in the history
* fix: fix issue

* chore: update version

* fix: fix issue
  • Loading branch information
Caedman Ziwen Lan authored Nov 1, 2023
1 parent dbe0d4a commit e4d1936
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
15 changes: 15 additions & 0 deletions src/utils/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
8 changes: 6 additions & 2 deletions src/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -145,4 +147,6 @@ export const getWebpackConfig = (
new CleanWebpackPlugin()
],
};
const result = merge(webpackConfig, webpackConfigFromJson)
return result
};

0 comments on commit e4d1936

Please sign in to comment.