Skip to content

Commit

Permalink
type: fix type issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed May 28, 2023
1 parent 5f354a8 commit 0e5e56d
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 42 deletions.
4 changes: 2 additions & 2 deletions core/package.json
Expand Up @@ -7,8 +7,8 @@
"types": "lib/index.d.ts",
"scripts": {
"start": "npm run watch",
"build": "tsbb build --file-names src/bin/kkt.ts --file-names src/scripts/build.ts --file-names src/scripts/doc.ts --file-names src/scripts/start.ts --file-names src/scripts/testk.ts --no-esm",
"watch": "tsbb watch --file-names src/bin/kkt.ts --file-names src/scripts/build.ts --file-names src/scripts/doc.ts --file-names src/scripts/start.ts --file-names src/scripts/testk.ts --no-esm",
"build": "tsbb build src/*.ts --use-babel --no-esm",
"watch": "tsbb watch src/*.ts --use-babel --no-esm",
"test": "tsbb test",
"coverage": "tsbb test --coverage"
},
Expand Down
7 changes: 7 additions & 0 deletions core/src/global.d.ts
@@ -0,0 +1,7 @@
declare const require: {
(u: string): any;
cache: {
[key: string]: any;
};
resolve(path: string): string;
};
16 changes: 9 additions & 7 deletions core/src/overrides/checkRequired.ts
Expand Up @@ -10,7 +10,7 @@ import { OverridePaths } from './paths';
*/
export function checkRequiredFiles(paths: OverridePaths, isNotCheckHTML: boolean) {
const checkRequiredFilesPath = `${reactDevUtils}/checkRequiredFiles`;
require.cache[require.resolve(checkRequiredFilesPath)].exports = (files: fs.PathLike[]) => {
require.cache[require.resolve(checkRequiredFilesPath)].exports = (files: (fs.PathLike | undefined)[] = []) => {
files = files
.map((item) => {
if (/(\.html)$/.test(item as string) && isNotCheckHTML) {
Expand All @@ -26,15 +26,17 @@ export function checkRequiredFiles(paths: OverridePaths, isNotCheckHTML: boolean
try {
files.forEach((filePath) => {
currentFilePath = filePath;
fs.accessSync(filePath, fs.constants.F_OK);
filePath && fs.accessSync(filePath, fs.constants.F_OK);
});
return true;
} catch (err) {
const dirName = path.dirname(currentFilePath);
const fileName = path.basename(currentFilePath);
console.log('\x1b[1;31m Could not find a required file. \x1b[0m');
console.log(`\x1b[1;31m Name: \x1b[0m ${fileName}`);
console.log(`\x1b[1;31m Searched in: \x1b[0m \x1b[1;36m${dirName}\x1b[0m`);
if (currentFilePath) {
const dirName = path.dirname(currentFilePath);
const fileName = path.basename(currentFilePath);
console.log('\x1b[1;31m Could not find a required file. \x1b[0m');
console.log(`\x1b[1;31m Name: \x1b[0m ${fileName}`);
console.log(`\x1b[1;31m Searched in: \x1b[0m \x1b[1;36m${dirName}\x1b[0m`);
}
return false;
}
};
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/miniCssExtractPlugin.ts
Expand Up @@ -23,7 +23,7 @@ export const miniCssExtractPlugin = (
options: MiniCssExtractPlugin.PluginOptions = {},
) => {
const regexp = /(MiniCssExtractPlugin)/;
conf.plugins = conf.plugins
conf.plugins = (conf.plugins || [])
.map((item) => {
if (item.constructor && item.constructor.name && regexp.test(item.constructor.name)) {
return new MiniCssExtractPlugin({
Expand Down
18 changes: 10 additions & 8 deletions core/src/plugins/staticDoc.ts
Expand Up @@ -41,25 +41,25 @@ export function getDocsData(str: string = '', route = '/_doc'): DocsDataResult {
result.dirPath = arr[0];
result.route = arr[1] || route;
}
if (!result.route.startsWith('/')) {
if (result.route && !result.route.startsWith('/')) {
result.route = '/' + result.route;
}
// relative directory
if (fs.existsSync(path.resolve(dirPath))) {
result.docRoot = path.resolve(dirPath);
result.pkgPath = resolvePackagePath(process.cwd(), process.cwd());
const pkg = fs.readJSONSync(result.pkgPath);
result.pkgPath = resolvePackagePath(process.cwd(), process.cwd()) || '';
const pkg = fs.readJSONSync(result.pkgPath!);
result.name = pkg.name;
result.route = '/';
return result;
}

const [_, name] = dirPath.match(/^([a-zA-Z\-]+|@[a-zA-Z\-]+\/[a-zA-Z\-]+)\/?/i);
const [_, name] = dirPath.match(/^([a-zA-Z\-]+|@[a-zA-Z\-]+\/[a-zA-Z\-]+)\/?/i) || [];
result.name = name;
result.pkgPath = resolvePackagePath(name, process.cwd());
result.root = path.dirname(result.pkgPath).replace(new RegExp(`${name.replace('/', path.sep)}$`, 'ig'), '');
result.pkgPath = resolvePackagePath(name, process.cwd()) || '';
result.root = path.dirname(result.pkgPath!).replace(new RegExp(`${name.replace('/', path.sep)}$`, 'ig'), '');
const [repath] = str.replace(name, '').split(':');
result.docRoot = path.resolve(path.dirname(result.pkgPath) + repath);
result.docRoot = path.resolve(path.dirname(result.pkgPath!) + repath);
return { ...result };
}

Expand All @@ -78,7 +78,9 @@ export const staticDocSetupMiddlewares = (
if (options.config?.output?.publicPath && typeof options.config.output.publicPath === 'string') {
routePath = options.config.output.publicPath.replace(/\/+$/, '') + route;
}
devServer.app.use(routePath, express.static(docRoot));
if (routePath && docRoot) {
devServer.app && devServer.app.use(routePath, express.static(docRoot));
}
}
return middlewares;
};
2 changes: 1 addition & 1 deletion core/src/scripts/build.ts
Expand Up @@ -50,7 +50,7 @@ export default async function build(argvs: BuildArgs) {
// run original script
await require(`${reactScripts}/scripts/build`);
} catch (error) {
const message = error && error.message ? error.message : '';
const message = error && error instanceof Error && error.message ? error.message : '';
console.log('\x1b[31;1m KKT:BUILD:ERROR: \x1b[0m\n', error);
new Error(`KKT:BUILD:ERROR: \n ${message}`);
process.exit(1);
Expand Down
16 changes: 9 additions & 7 deletions core/src/scripts/doc.ts
Expand Up @@ -18,12 +18,14 @@ export interface DocsArgs extends BuildArgs {
export default async function docs(argv: DocsArgs) {
try {
const { route, docRoot, dirPath, ...other } = getDocsData(argv.path, '/');
app.use(route, express.static(docRoot), (req, res, next) => {
const content = fs.readFileSync(path.resolve(docRoot, './index.html'));
res.send(content.toString());
next();
});
const DEFAULT_PORT = parseInt(process.env.DOC_PORT, 10) || argv.port || 3002;
if (route && docRoot) {
app.use(route, express.static(docRoot), (req, res, next) => {
const content = fs.readFileSync(path.resolve(docRoot, './index.html'));
res.send(content.toString());
next();
});
}
const DEFAULT_PORT = (process.env.DOC_PORT && parseInt(process.env.DOC_PORT, 10)) || argv.port || 3002;
const port = await choosePort(HOST, DEFAULT_PORT);
app.listen(port, () => {
const urls = prepareUrls(
Expand All @@ -38,7 +40,7 @@ export default async function docs(argv: DocsArgs) {
console.log(` On Your Network: ${urls.localUrlForTerminal}`);
});
} catch (error) {
const message = error && error.message ? error.message : '';
const message = error && error instanceof Error && error.message ? error.message : '';
console.log('\x1b[31;1m KKT:DOC:ERROR: \x1b[0m\n', error);
new Error(`KKT:BUILD:ERROR: \n ${message}`);
process.exit(1);
Expand Down
10 changes: 5 additions & 5 deletions core/src/scripts/start.ts
Expand Up @@ -108,7 +108,7 @@ export default async function start(argvs: StartArgs) {
// Keep `evalSourceMapMiddleware`
// middlewares before `redirectServedPath` otherwise will not have any effect
// This lets us fetch source contents from webpack for the error overlay
devServer.app.use(evalSourceMapMiddleware(devServer));
devServer.app && devServer.app.use(evalSourceMapMiddleware(devServer));
if (fs.existsSync(paths.proxySetup)) {
// This registers user provided middleware for proxy reasons
require(paths.proxySetup)(devServer.app);
Expand All @@ -119,14 +119,14 @@ export default async function start(argvs: StartArgs) {
}

// Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
devServer.app.use(redirectServedPath(paths.publicUrlOrPath));
devServer.app && devServer.app.use(redirectServedPath(paths.publicUrlOrPath));

// This service worker file is effectively a 'no-op' that will reset any
// previous service worker registered for the same host:port combination.
// We do this in development to avoid hitting the production cache if
// it used the same host and port.
// https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
devServer.app && devServer.app.use(noopServiceWorkerMiddleware(paths.publicUrlOrPath));
const mds = setupMiddlewares ? setupMiddlewares(middlewares, devServer) : middlewares;
return staticDocSetupMiddlewares(mds, devServer, { ...argvs, paths, config: webpackConf });
};
Expand All @@ -142,7 +142,7 @@ export default async function start(argvs: StartArgs) {
console.log('❌ KKT:\x1b[31;1mERR\x1b[0m:', err);
return;
}
if (stats.hasErrors()) {
if (stats && stats.hasErrors()) {
clearConsole();
console.log(`❌ KKT:\x1b[31;1mERR\x1b[0m: \x1b[35;1m${today()}\x1b[0m\n`, stats.toString());
return;
Expand All @@ -156,7 +156,7 @@ export default async function start(argvs: StartArgs) {
require(`${reactScripts}/scripts/start`);
}
} catch (error) {
const message = error && error.message ? error.message : '';
const message = error && error instanceof Error && error.message ? error.message : '';
console.log('\x1b[31;1m KKT:START:ERROR: \x1b[0m\n', error);
new Error(`KKT:START:ERROR: \n ${message}`);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion core/src/scripts/testk.ts
Expand Up @@ -15,7 +15,7 @@ export default async function test(argvs: TestArgs) {
// run original script
require(`${reactScripts}/scripts/test`);
} catch (error) {
const message = error && error.message ? error.message : '';
const message = error && error instanceof Error && error.message ? error.message : '';
console.log('\x1b[31;1m KKT:TEST:ERROR: \x1b[0m\n', error);
new Error(`KKT:TEST:ERROR: \n ${message}`);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion core/src/utils/loaderConf.ts
Expand Up @@ -95,7 +95,7 @@ export async function loaderConf(rcPath: string): Promise<KKTRC> {
}
return kktrc;
} catch (error) {
const message = error && error.message ? error.message : '';
const message = error && error instanceof Error && error.message ? error.message : '';
console.log('Invalid \x1b[31;1m .kktrc.js \x1b[0m file.\n', error);
new Error(`Invalid .kktrc.js file. \n ${message}`);
process.exit(1);
Expand Down
12 changes: 6 additions & 6 deletions core/tsconfig.json
@@ -1,16 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"skipLibCheck": true,
"target": "ES5",
"strict": true,
"declaration": true,
"target": "es2017",
"noImplicitAny": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"sourceMap": true,
"skipLibCheck": true,
"noImplicitAny": true,
"outDir": "lib",
"baseUrl": "."
},
"include": ["src/**/*", "src/*"]
"include": ["src/**/*"]
}
1 change: 1 addition & 0 deletions packages/less-modules/package.json
Expand Up @@ -18,6 +18,7 @@
"src"
],
"scripts": {
"build": "tsbb build --no-esm",
"watch": "tsbb watch --no-esm"
},
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/less-modules/src/index.ts
Expand Up @@ -104,7 +104,7 @@ const createLessModule = (lessLoaderOptions = {} as LessLoaderOptions) => {
/**
* Use create-react-app to build react libraries. Support for regular less files and *.module.less files.
*/
const module = createLessModule();
(module as any).withLoaderOptions = createLessModule;
const lessmodule = createLessModule();
(lessmodule as any).withLoaderOptions = createLessModule;

export default module;
export default lessmodule;
1 change: 1 addition & 0 deletions packages/resolve-fallback/package.json
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"assert": "^2.0.0",
"crypto-browserify": "^3.12.0",
"path-browserify": "^1.0.1",
"https-browserify": "^1.0.0",
"os": "^0.1.2",
"os-browserify": "^0.3.0",
Expand Down

0 comments on commit 0e5e56d

Please sign in to comment.