-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: [email protected] [email protected] [skip ci] --------- Co-authored-by: jialan <[email protected]>
- Loading branch information
Showing
12 changed files
with
316 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/ko/src/webpack/custom-plugins/purge-cache-webpack-plugin/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Compiler } from 'webpack'; | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
|
||
class PurgeCacheWebpackPlugin { | ||
WEBPACK_PLUGIN_NAME = 'PurgeCacheWebpackPlugin'; | ||
|
||
purgeCacheFiles( | ||
directory: string, | ||
maxAge: number, | ||
callback: (errors?: Error[]) => void | ||
) { | ||
fs.readdir(directory) | ||
.then(files => { | ||
const expiredPacks = []; | ||
if (files.length === 0) return callback(); | ||
|
||
for (const file of files) { | ||
const pack = new Promise((resolve, reject) => { | ||
const filePath = path.join(directory, file); | ||
fs.stat(filePath) | ||
.then(stats => { | ||
if (stats.mtime.getTime() + maxAge < Date.now()) { | ||
fs.unlink(filePath) | ||
.then(() => { | ||
resolve(true); | ||
}) | ||
.catch(err => reject(err)); | ||
} else { | ||
resolve(true); | ||
} | ||
}) | ||
.catch(err => reject(err)); | ||
}); | ||
|
||
expiredPacks.push(pack); | ||
} | ||
|
||
Promise.allSettled(expiredPacks).then(results => { | ||
const errors = results | ||
.filter(result => result.status === 'rejected') | ||
.map((result: any) => result.reason); | ||
callback(errors); | ||
}); | ||
}) | ||
.catch(err => { | ||
callback([err]); | ||
}); | ||
} | ||
|
||
apply(compiler: Compiler) { | ||
compiler.hooks.done.tapAsync( | ||
{ name: this.WEBPACK_PLUGIN_NAME }, | ||
(_, callback) => { | ||
const { type, maxAge, cacheLocation } = compiler.options.cache as any; | ||
if (type === 'filesystem') { | ||
const logger = compiler.getInfrastructureLogger( | ||
this.WEBPACK_PLUGIN_NAME | ||
); | ||
this.purgeCacheFiles(cacheLocation, maxAge, errors => { | ||
if (errors?.length) { | ||
errors.forEach(err => logger.warn(err.message)); | ||
} else { | ||
logger.info(`purge expired cache files completed`); | ||
} | ||
callback(); | ||
}); | ||
} else { | ||
callback(); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
|
||
export default PurgeCacheWebpackPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
"strict": true, | ||
"strictPropertyInitialization": false, | ||
"outDir": "lib", | ||
"declaration": true, | ||
}, | ||
} |
Oops, something went wrong.