Skip to content

Commit c189db4

Browse files
LuckyFBBjialan
and
jialan
authored
feat(ko): remove cache and add jsdoc (#162)
* Feat 6.6.0 jsdoc (#1) * feat(ko): add jsdoc description and export .d.ts file * fix(create-ko): update jsdoc for template * docs(changeset): update jsdoc for create-ko template add jsdoc description and export .d.ts file * Feat 6.6.0 cache (#2) * feat(ko): auto purge cache when cache pack expired by maxAge and remove useless async * docs(changeset): support auto purge cache when cache pack expired by maxAge * Fix 6.6.0 dev serve (#3) * chore(ko): update webpack-dev-server to 4.14.0 * docs(changeset): update webpack-dev-server to 4.14.0 and remove error-overlay-webpack-plugin * RELEASING: Releasing 2 package(s) Releases: ko@6.6.0 create-ko@1.0.2 [skip ci] --------- Co-authored-by: jialan <jialan@dtstack.com>
1 parent d1f527c commit c189db4

File tree

12 files changed

+316
-151
lines changed

12 files changed

+316
-151
lines changed

packages/create-ko/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# create-ko
22

3+
## 1.0.2
4+
5+
### Patch Changes
6+
7+
- dd8e687d7: update jsdoc for create-ko template
8+
39
## 1.0.1
410

511
### Patch Changes

packages/create-ko/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-ko",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "create react project with ko",
55
"repository": {
66
"type": "git",

packages/create-ko/template/ko.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const finalKoConfig = (function () {
55
const APP_PATH = path.resolve(ROOT_PATH, 'src');
66
const BUILD_PATH = path.resolve(ROOT_PATH, `dist`);
77
const WEB_PUBLIC = path.resolve(APP_PATH, 'public');
8+
/**
9+
* @type {import("ko/lib/types").IOptions}
10+
*/
811
const baseKoConfig = {
912
entry: path.join(APP_PATH, './main.tsx'),
1013
outputPath: BUILD_PATH,

packages/ko/CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# ko
22

3+
## 6.6.0
4+
5+
### Minor Changes
6+
7+
- dd8e687d7: add jsdoc description and export .d.ts file
8+
- b1a004f68: support auto purge cache when cache pack expired by maxAge
9+
10+
### Patch Changes
11+
12+
- 4d4071220: update webpack-dev-server to 4.14.0 and remove error-overlay-webpack-plugin
13+
314
## 6.5.12
415

516
### Patch Changes
@@ -12,6 +23,7 @@
1223

1324
- 22089fe9: scriptLoader with antlr4ng
1425

26+
1527
## 6.5.10
1628

1729
### Patch Changes

packages/ko/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ko",
3-
"version": "6.5.12",
3+
"version": "6.6.0",
44
"description": "build & lint library",
55
"keywords": [
66
"ko",
@@ -61,7 +61,6 @@
6161
"css-minimizer-webpack-plugin": "^4.0.0",
6262
"detect-port": "^1.3.0",
6363
"dynamic-resolve-webpack-plugin": "workspace:^2.0.0",
64-
"error-overlay-webpack-plugin": "^1.1.1",
6564
"esbuild-loader": "^2.19.0",
6665
"html-webpack-plugin": "^5.5.0",
6766
"inquirer": "^8.2.2",
@@ -86,7 +85,7 @@
8685
"thread-loader": "^3.0.4",
8786
"webpack": "^5.72.1",
8887
"webpack-bundle-analyzer": "^4.5.0",
89-
"webpack-dev-server": "4.9.0",
88+
"webpack-dev-server": "4.14.0",
9089
"webpackbar": "^5.0.2",
9190
"worker-loader": "^3.0.8"
9291
},

packages/ko/src/actions/dev.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ class Dev extends ActionFactory {
3131
setupExitSignals: false,
3232
allowedHosts: 'all',
3333
client: {
34-
overlay: false,
34+
overlay: {
35+
errors: true,
36+
warnings: false,
37+
runtimeErrors: true,
38+
},
3539
},
3640
historyApiFallback,
3741
};

packages/ko/src/types.ts

+72
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,89 @@ import { IOpts as AutoPolyfillsWebpackPluginOptions } from '@dtinsight/auto-poly
55

66
export type IOptions = {
77
//common configs
8+
/**
9+
* The current working directory.
10+
* @param {string}
11+
*/
812
cwd: string;
13+
/**
14+
* An object mapping module names to file paths or directories.
15+
* @param {Record<string, string>}
16+
*/
917
alias?: Record<string, string>;
18+
/**
19+
* An array of patterns specifying files to copy to the output directory.
20+
* @param {Pattern[]}
21+
*/
1022
copy?: Pattern[];
23+
/**
24+
* The entry point of the application.
25+
* @param {string}
26+
*/
1127
entry?: string;
28+
/**
29+
* The path to the output directory.
30+
* @param {string}
31+
*/
1232
outputPath?: string;
33+
/**
34+
* The public path of the application.
35+
* @param {string}
36+
*/
1337
publicPath?: string;
38+
/**
39+
* Whether to append a hash to the output file name for cache busting.
40+
* @param {boolean}
41+
*/
1442
hash?: boolean;
43+
/**
44+
* An object mapping module names to global variables.
45+
* @param {Record<string, string>}
46+
*/
1547
externals?: Record<string, string>;
48+
/**
49+
* An array of plugin configurations.
50+
* @param {HookOptions[]}
51+
*/
1652
plugins?: HookOptions[];
53+
/**
54+
* The path to the HTML template to use for the application.
55+
* @param {string}
56+
*/
1757
htmlTemplate?: string;
1858
htmlChunks?: 'all' | string[];
1959
// style configs
60+
/**
61+
* Whether to enable the bundle analyzer plugin.
62+
* @param {boolean}
63+
*/
2064
analyzer?: boolean;
65+
/**
66+
* An array of additional PostCSS plugins to use.
67+
* @param {Plugin[]}
68+
*/
2169
extraPostCSSPlugins?: Plugin[];
70+
/**
71+
* Options to pass to the Less compiler.
72+
* @param {*}
73+
*/
2274
lessOptions?: any;
2375
// integrated plugins options
76+
/**
77+
* A function to dynamically resolve module requests.
78+
* @param {Function}
79+
*/
2480
dynamicResolve?: <T extends any>(request: T) => T;
81+
/**
82+
* Whether to enable the auto-polyfills plugin, or an options object for the plugin.
83+
* @param {string}
84+
*/
2585
autoPolyfills: boolean | AutoPolyfillsWebpackPluginOptions;
2686
// dev, or serve configs
87+
/**
88+
* Options for the development server.
89+
* @param {{proxy?: Record<string, any>, host: string, port: number, staticPath?: string, historyApiFallback?: any, compilationSuccessInfo?: { messages: string[]; notes?: string[] }}}
90+
*/
2791
serve: {
2892
proxy?: Record<string, any>;
2993
host: string;
@@ -33,6 +97,10 @@ export type IOptions = {
3397
compilationSuccessInfo?: { messages: string[]; notes?: string[] };
3498
};
3599
// experimental features
100+
/**
101+
* Experimental features to enable.
102+
* @param {{speedUp?: boolean, minimizer?: boolean, disableLazyImports?: boolean, enableCssModule?: boolean, compress?: any}}
103+
*/
36104
experiment?: {
37105
speedUp?: boolean;
38106
minimizer?: boolean;
@@ -41,6 +109,10 @@ export type IOptions = {
41109
compress?: any;
42110
};
43111
//misc
112+
/**
113+
* Options for the linter plugins.
114+
* @param {Record<IKeys, Omit<IOpts, 'write'>>}
115+
*/
44116
lints?: Record<IKeys, Omit<IOpts, 'write'>>;
45117
};
46118

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Compiler } from 'webpack';
2+
import fs from 'fs/promises';
3+
import path from 'path';
4+
5+
class PurgeCacheWebpackPlugin {
6+
WEBPACK_PLUGIN_NAME = 'PurgeCacheWebpackPlugin';
7+
8+
purgeCacheFiles(
9+
directory: string,
10+
maxAge: number,
11+
callback: (errors?: Error[]) => void
12+
) {
13+
fs.readdir(directory)
14+
.then(files => {
15+
const expiredPacks = [];
16+
if (files.length === 0) return callback();
17+
18+
for (const file of files) {
19+
const pack = new Promise((resolve, reject) => {
20+
const filePath = path.join(directory, file);
21+
fs.stat(filePath)
22+
.then(stats => {
23+
if (stats.mtime.getTime() + maxAge < Date.now()) {
24+
fs.unlink(filePath)
25+
.then(() => {
26+
resolve(true);
27+
})
28+
.catch(err => reject(err));
29+
} else {
30+
resolve(true);
31+
}
32+
})
33+
.catch(err => reject(err));
34+
});
35+
36+
expiredPacks.push(pack);
37+
}
38+
39+
Promise.allSettled(expiredPacks).then(results => {
40+
const errors = results
41+
.filter(result => result.status === 'rejected')
42+
.map((result: any) => result.reason);
43+
callback(errors);
44+
});
45+
})
46+
.catch(err => {
47+
callback([err]);
48+
});
49+
}
50+
51+
apply(compiler: Compiler) {
52+
compiler.hooks.done.tapAsync(
53+
{ name: this.WEBPACK_PLUGIN_NAME },
54+
(_, callback) => {
55+
const { type, maxAge, cacheLocation } = compiler.options.cache as any;
56+
if (type === 'filesystem') {
57+
const logger = compiler.getInfrastructureLogger(
58+
this.WEBPACK_PLUGIN_NAME
59+
);
60+
this.purgeCacheFiles(cacheLocation, maxAge, errors => {
61+
if (errors?.length) {
62+
errors.forEach(err => logger.warn(err.message));
63+
} else {
64+
logger.info(`purge expired cache files completed`);
65+
}
66+
callback();
67+
});
68+
} else {
69+
callback();
70+
}
71+
}
72+
);
73+
}
74+
}
75+
76+
export default PurgeCacheWebpackPlugin;

packages/ko/src/webpack/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class WebpackConfig {
3434
return merge(this.base, ...opts);
3535
}
3636

37-
get cache() {
37+
get cache(): FileCacheOptions {
3838
const { experiment } = this.opts;
3939
const type = experiment?.speedUp
4040
? 'filesystem'
@@ -46,6 +46,7 @@ class WebpackConfig {
4646
};
4747
if (type === 'filesystem') {
4848
(cache as FileCacheOptions).version = this.projectVersion;
49+
(cache as FileCacheOptions).maxAge = 7 * 24 * 3600000;
4950
} else {
5051
(
5152
cache as Exclude<
@@ -54,7 +55,7 @@ class WebpackConfig {
5455
>
5556
).maxGenerations = 1;
5657
}
57-
return cache;
58+
return cache as FileCacheOptions;
5859
}
5960

6061
get projectVersion(): string {

packages/ko/src/webpack/plugins.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
99
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
1010
import AutoPolyfillsWebpackPlugin from '@dtinsight/auto-polyfills-webpack-plugin';
1111
import FriendlyErrorsWebpackPlugin from '@nuxt/friendly-errors-webpack-plugin';
12-
import ErrorOverlayPlugin from 'error-overlay-webpack-plugin';
12+
import PurgeCacheWebpackPlugin from './custom-plugins/purge-cache-webpack-plugin';
1313
import { IWebpackOptions } from '../types';
1414

1515
function getPlugins(opts: IWebpackOptions) {
@@ -99,7 +99,7 @@ function getPlugins(opts: IWebpackOptions) {
9999
notes: [],
100100
},
101101
}),
102-
!isProd && new ErrorOverlayPlugin(),
102+
!isProd && new PurgeCacheWebpackPlugin(),
103103
isProd &&
104104
autoPolyfills &&
105105
(typeof autoPolyfills === 'boolean'

packages/ko/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"strict": true,
1010
"strictPropertyInitialization": false,
1111
"outDir": "lib",
12+
"declaration": true,
1213
},
1314
}

0 commit comments

Comments
 (0)