diff --git a/core/package.json b/core/package.json index 2f204067f..e531f2a7e 100644 --- a/core/package.json +++ b/core/package.json @@ -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" }, diff --git a/core/src/global.d.ts b/core/src/global.d.ts new file mode 100644 index 000000000..2805a4a37 --- /dev/null +++ b/core/src/global.d.ts @@ -0,0 +1,7 @@ +declare const require: { + (u: string): any; + cache: { + [key: string]: any; + }; + resolve(path: string): string; +}; diff --git a/core/src/overrides/checkRequired.ts b/core/src/overrides/checkRequired.ts index 5c9bcade1..d435cb43d 100644 --- a/core/src/overrides/checkRequired.ts +++ b/core/src/overrides/checkRequired.ts @@ -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) { @@ -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; } }; diff --git a/core/src/plugins/miniCssExtractPlugin.ts b/core/src/plugins/miniCssExtractPlugin.ts index 7ea4b3918..f7c12dfac 100644 --- a/core/src/plugins/miniCssExtractPlugin.ts +++ b/core/src/plugins/miniCssExtractPlugin.ts @@ -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({ diff --git a/core/src/plugins/staticDoc.ts b/core/src/plugins/staticDoc.ts index ef0aad1e0..6085281b9 100644 --- a/core/src/plugins/staticDoc.ts +++ b/core/src/plugins/staticDoc.ts @@ -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 }; } @@ -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; }; diff --git a/core/src/scripts/build.ts b/core/src/scripts/build.ts index b6b0a1287..4cf6d6e1b 100644 --- a/core/src/scripts/build.ts +++ b/core/src/scripts/build.ts @@ -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); diff --git a/core/src/scripts/doc.ts b/core/src/scripts/doc.ts index 75d8d16b0..a9ed39750 100644 --- a/core/src/scripts/doc.ts +++ b/core/src/scripts/doc.ts @@ -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( @@ -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); diff --git a/core/src/scripts/start.ts b/core/src/scripts/start.ts index f09f86274..4faf1f8fd 100644 --- a/core/src/scripts/start.ts +++ b/core/src/scripts/start.ts @@ -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); @@ -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 }); }; @@ -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; @@ -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); diff --git a/core/src/scripts/testk.ts b/core/src/scripts/testk.ts index 9ad8a82c8..31bdc7c4a 100644 --- a/core/src/scripts/testk.ts +++ b/core/src/scripts/testk.ts @@ -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); diff --git a/core/src/utils/loaderConf.ts b/core/src/utils/loaderConf.ts index 371327ef7..e5f2e20db 100644 --- a/core/src/utils/loaderConf.ts +++ b/core/src/utils/loaderConf.ts @@ -95,7 +95,7 @@ export async function loaderConf(rcPath: string): Promise { } 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); diff --git a/core/tsconfig.json b/core/tsconfig.json index ed061fa2e..b5a8bfd31 100644 --- a/core/tsconfig.json +++ b/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/**/*"] } diff --git a/packages/less-modules/package.json b/packages/less-modules/package.json index 95943e184..a8991ac66 100644 --- a/packages/less-modules/package.json +++ b/packages/less-modules/package.json @@ -18,6 +18,7 @@ "src" ], "scripts": { + "build": "tsbb build --no-esm", "watch": "tsbb watch --no-esm" }, "dependencies": { diff --git a/packages/less-modules/src/index.ts b/packages/less-modules/src/index.ts index 7eb38bac8..425788b1a 100644 --- a/packages/less-modules/src/index.ts +++ b/packages/less-modules/src/index.ts @@ -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; diff --git a/packages/resolve-fallback/package.json b/packages/resolve-fallback/package.json index 28da9735d..e5df073dd 100644 --- a/packages/resolve-fallback/package.json +++ b/packages/resolve-fallback/package.json @@ -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",