From f2e31003c8e42909a6fbd2c1c1e7c331e5d89712 Mon Sep 17 00:00:00 2001 From: otakustay Date: Sat, 15 Jan 2022 14:49:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20core=E5=8C=85=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E8=87=B3ESM=20(#39)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli-dev/src/utils.ts | 2 +- packages/cli-lint/src/index.ts | 2 +- packages/cli-play/src/utils/host.ts | 3 +- packages/cli/src/DynamicImportCommand.ts | 13 +--- packages/config-jest/src/interface.ts | 1 + packages/config-lint/src/rules/eslint.ts | 2 +- packages/config-lint/src/rules/stylelint.ts | 2 +- .../config-webpack/src/partials/production.ts | 2 +- packages/core/package.json | 10 ++- packages/core/scripts/post-build.js | 36 +++++++++++ packages/core/src/internal.ts | 1 + packages/core/src/logger.ts | 1 + packages/core/src/project.ts | 7 +-- packages/core/src/resolve.ts | 1 + pnpm-lock.yaml | 61 +++++++++++++++++++ site/docs/app/quick-start.md | 4 +- 16 files changed, 121 insertions(+), 27 deletions(-) create mode 100644 packages/core/scripts/post-build.js create mode 100644 packages/core/src/internal.ts diff --git a/packages/cli-dev/src/utils.ts b/packages/cli-dev/src/utils.ts index 9b03a179..2504571c 100644 --- a/packages/cli-dev/src/utils.ts +++ b/packages/cli-dev/src/utils.ts @@ -1,11 +1,11 @@ import WebpackDevServer from 'webpack-dev-server'; +import {internalIpV4} from 'internal-ip'; import {readProjectSettings, BuildEnv, strictCheckRequiredDependency} from '@reskript/settings'; import {BuildContext, collectEntries, createRuntimeBuildEnv, EntryLocation} from '@reskript/config-webpack'; import {logger, readPackageConfig} from '@reskript/core'; import {DevCommandLineArgs} from './interface.js'; export const resolveHost = async (hostType: DevCommandLineArgs['host']) => { - const {internalIpV4} = await import('internal-ip'); if (!hostType) { return 'localhost'; diff --git a/packages/cli-lint/src/index.ts b/packages/cli-lint/src/index.ts index ebc9603f..5e27f6a2 100644 --- a/packages/cli-lint/src/index.ts +++ b/packages/cli-lint/src/index.ts @@ -1,5 +1,6 @@ import eslintPrettyFormatter from 'eslint-formatter-pretty'; import {Linter, ESLint} from 'eslint'; +import {execa} from 'execa'; import {logger, gitStatus, findGitRoot} from '@reskript/core'; import {LintCommandLineArgs, ResolveOptions} from './interface.js'; import lintScripts from './script.js'; @@ -24,7 +25,6 @@ const filterUnwantedReports = (report: LintResult[], cmd: LintCommandLineArgs): }; export const run = async (cmd: LintCommandLineArgs, files: string[]): Promise => { - const {execa} = await import('execa'); const gitRoot = await findGitRoot() || process.cwd(); const status = await gitStatus(process.cwd()); const options: ResolveOptions = {...cmd, gitRoot, gitStatus: status}; diff --git a/packages/cli-play/src/utils/host.ts b/packages/cli-play/src/utils/host.ts index 4ad2eaa4..4f52c3a4 100644 --- a/packages/cli-play/src/utils/host.ts +++ b/packages/cli-play/src/utils/host.ts @@ -1,8 +1,7 @@ +import {internalIpV4} from 'internal-ip'; import {PlayCommandLineArgs} from '../interface.js'; export const resolveHost = async (hostType: PlayCommandLineArgs['host']) => { - const {internalIpV4} = await import('internal-ip'); - if (!hostType) { return 'localhost'; } diff --git a/packages/cli/src/DynamicImportCommand.ts b/packages/cli/src/DynamicImportCommand.ts index e9c41ce7..3087bf78 100644 --- a/packages/cli/src/DynamicImportCommand.ts +++ b/packages/cli/src/DynamicImportCommand.ts @@ -2,10 +2,11 @@ import path from 'path'; import {existsSync} from 'fs'; import childProcess from 'child_process'; import {promisify} from 'util'; +import {packageDirectory} from 'pkg-dir'; import enquirer from 'enquirer'; // @ts-expect-error import {Command} from 'clipanion'; -import type {CommandDefinition} from '@reskript/core'; +import {CommandDefinition, findGitRoot, logger, readPackageConfig, resolveFrom} from '@reskript/core'; const isErrorWithCode = (error: any): error is NodeJS.ErrnoException => { return 'message' in error && 'code' in error; @@ -27,8 +28,6 @@ export default abstract class DynamicImportCommand extends Command { protected readonly packageName: string = ''; async execute() { - const {logger} = await import('@reskript/core'); - if (!this.packageName) { logger.error('No command package defined'); process.exit(11); @@ -52,7 +51,6 @@ export default abstract class DynamicImportCommand extends Command { } private async importCommandPackage() { - const {logger, resolveFrom} = await import('@reskript/core'); const resolve = resolveFrom(process.cwd()); const dynamicImport = async () => { @@ -111,9 +109,6 @@ export default abstract class DynamicImportCommand extends Command { } private async canAutoInstall() { - const {readPackageConfig} = await import('@reskript/core'); - const {packageDirectory} = await import('pkg-dir'); - const packageRoot = await packageDirectory(); if (!packageRoot) { @@ -128,8 +123,6 @@ export default abstract class DynamicImportCommand extends Command { } private async detectPackageManager(): Promise { - const {findGitRoot} = await import('@reskript/core'); - const gitRoot = await findGitRoot(); if (!gitRoot) { @@ -150,8 +143,6 @@ export default abstract class DynamicImportCommand extends Command { } private async installCommandPackage(): Promise { - const {logger} = await import('@reskript/core'); - const question = { type: 'confirm', name: 'ok', diff --git a/packages/config-jest/src/interface.ts b/packages/config-jest/src/interface.ts index bb3da8ea..2f5b0fde 100644 --- a/packages/config-jest/src/interface.ts +++ b/packages/config-jest/src/interface.ts @@ -1,3 +1,4 @@ +// @ts-expect-error import type {ProjectAware} from '@reskript/core'; // @ts-expect-error import type {FeatureMatrix} from '@reskript/settings'; diff --git a/packages/config-lint/src/rules/eslint.ts b/packages/config-lint/src/rules/eslint.ts index d8fa73e6..9a89a8b2 100644 --- a/packages/config-lint/src/rules/eslint.ts +++ b/packages/config-lint/src/rules/eslint.ts @@ -1,4 +1,4 @@ -import {resolveSync} from '@reskript/core'; +import {resolveSync} from '@reskript/core/willBreakingInternalUseOnly'; import {getParseOnlyBabelConfig} from '@reskript/config-babel/willBreakingInternalUseOnly'; // 为了支持 IDE 和代码编辑工具使用 eslint 的配置,在 build 阶段会生成一份 .eslintrc 文件。参阅 scripts/post-build.js diff --git a/packages/config-lint/src/rules/stylelint.ts b/packages/config-lint/src/rules/stylelint.ts index a15e0292..1a7a4d8e 100644 --- a/packages/config-lint/src/rules/stylelint.ts +++ b/packages/config-lint/src/rules/stylelint.ts @@ -1,4 +1,4 @@ -import {resolveSync} from '@reskript/core'; +import {resolveSync} from '@reskript/core/willBreakingInternalUseOnly'; import lessSyntax from 'postcss-less'; const config: Record = { diff --git a/packages/config-webpack/src/partials/production.ts b/packages/config-webpack/src/partials/production.ts index 283c2afa..52d92436 100644 --- a/packages/config-webpack/src/partials/production.ts +++ b/packages/config-webpack/src/partials/production.ts @@ -1,10 +1,10 @@ import path from 'path'; import {Configuration} from 'webpack'; +import {findUp} from 'find-up'; import {filter} from 'ramda'; import {ConfigurationFactory} from '../interface.js'; const factory: ConfigurationFactory = async entry => { - const {findUp} = await import('find-up'); const {cwd} = entry; const nodeModule = async (...segments: string[]): Promise => { const name = path.join('node_modules', ...segments); diff --git a/packages/core/package.json b/packages/core/package.json index ffe13408..3be0bea7 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -2,11 +2,12 @@ "name": "@reskript/core", "version": "3.0.6", "license": "MIT", - "type": "commonjs", + "type": "module", "main": "./dist/index.js", "types": "./dist/index.d.ts", "exports": { - ".": "./dist/index.js" + ".": "./dist/index.js", + "./willBreakingInternalUseOnly": "./dist/internal.cjs" }, "engines": { "node": ">=14.18.0" @@ -19,11 +20,13 @@ }, "scripts": { "clean": "rm -rf dist", - "build": "tsc -p tsconfig.build.json", + "build": "tsc -p tsconfig.build.json && node scripts/post-build.js && cp dist/internal.d.ts dist/internal.d.cts", "test": "vitest run", "lint": "eslint --max-warnings=0 src" }, "devDependencies": { + "@babel/plugin-transform-typescript": "^7.16.7", + "@rollup/plugin-babel": "^5.3.0", "@types/caller": "^1.0.0", "@types/dedent": "^0.7.0", "@types/node": "^17.0.4", @@ -32,6 +35,7 @@ "@types/unixify": "^1.0.0", "c8": "^7.10.0", "eslint": "^8.6.0", + "rollup": "^2.62.0", "typescript": "4.6.0-dev.20220105", "vite": "^2.7.7", "vitest": "^0.0.115" diff --git a/packages/core/scripts/post-build.js b/packages/core/scripts/post-build.js new file mode 100644 index 00000000..427f7e66 --- /dev/null +++ b/packages/core/scripts/post-build.js @@ -0,0 +1,36 @@ +import path from 'path'; +import {fileURLToPath} from 'url'; +import {rollup} from 'rollup'; +import {babel} from '@rollup/plugin-babel'; + +(async () => { + const project = path.join(fileURLToPath(import.meta.url), '..', '..'); + const config = { + input: path.join(project, 'src', 'internal.ts'), + external: id => { + if (id.startsWith('.')) { + return false; + } + + return path.relative(path.join(project, 'src'), id).startsWith('..'); + }, + plugins: [ + { + transform(code, id) { + if (id.endsWith('.ts')) { + return code.replace(/\.js';/g, '.ts\';'); + } + }, + }, + babel({plugins: ['@babel/plugin-transform-typescript'], extensions: ['.ts'], babelHelpers: 'bundled'}), + ], + }; + const bundle = await rollup(config); + + const outuput = { + file: path.join(project, 'dist', 'internal.cjs'), + format: 'cjs', + exports: 'named', + }; + await bundle.write(outuput); +})(); diff --git a/packages/core/src/internal.ts b/packages/core/src/internal.ts new file mode 100644 index 00000000..3e2ef165 --- /dev/null +++ b/packages/core/src/internal.ts @@ -0,0 +1 @@ +export {resolveSync} from './resolve.js'; diff --git a/packages/core/src/logger.ts b/packages/core/src/logger.ts index 664ac284..ef18cbee 100644 --- a/packages/core/src/logger.ts +++ b/packages/core/src/logger.ts @@ -1,5 +1,6 @@ // @ts-expect-error import * as kolorist from 'kolorist'; +// @ts-expect-error import dedentString from 'dedent'; import {isInDebugMode} from './flag.js'; diff --git a/packages/core/src/project.ts b/packages/core/src/project.ts index 53622069..9d3df0aa 100644 --- a/packages/core/src/project.ts +++ b/packages/core/src/project.ts @@ -1,10 +1,12 @@ import path from 'path'; import {existsSync} from 'fs'; import fs from 'fs/promises'; +import {packageDirectory} from 'pkg-dir'; +import {findUp} from 'find-up'; +import {globby} from 'globby'; import {PackageInfo} from './interface.js'; export const resolveCacheLocation = async (name: string): Promise => { - const {packageDirectory} = await import('pkg-dir'); const root = await packageDirectory(); if (!root) { @@ -21,7 +23,6 @@ export const readPackageConfig = async (cwd: string): Promise => { }; export const findGitRoot = async (cwd?: string): Promise => { - const {findUp} = await import('find-up'); const gitDirectory = await findUp('.git', {cwd, type: 'directory'}); return gitDirectory && path.dirname(gitDirectory); }; @@ -36,7 +37,6 @@ export const isMonorepo = async (cwd: string): Promise => { }; export const resolveMonorepoPackageDirectories = async (cwd: string): Promise => { - const {globby} = await import('globby'); const packageInfo = await readPackageConfig(cwd); const packages = packageInfo.workspaces ? (Array.isArray(packageInfo.workspaces) ? packageInfo.workspaces : packageInfo.workspaces.packages) @@ -46,7 +46,6 @@ export const resolveMonorepoPackageDirectories = async (cwd: string): Promise => { - const {findUp} = await import('find-up'); const dir = await findUp('packages', {cwd, type: 'directory'}); if (!dir) { diff --git a/packages/core/src/resolve.ts b/packages/core/src/resolve.ts index b04a17f2..14ff0324 100644 --- a/packages/core/src/resolve.ts +++ b/packages/core/src/resolve.ts @@ -3,6 +3,7 @@ import fs from 'fs'; import {fileURLToPath} from 'url'; import {bundleRequire} from 'bundle-require'; import resolveCore from 'resolve'; +// @ts-expect-error import caller from 'caller'; export const resolveFrom = (base: string) => (id: string) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd7fa837..2333bde6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -784,6 +784,8 @@ importers: packages/core: specifiers: + '@babel/plugin-transform-typescript': ^7.16.7 + '@rollup/plugin-babel': ^5.3.0 '@types/caller': ^1.0.0 '@types/dedent': ^0.7.0 '@types/node': ^17.0.4 @@ -805,6 +807,7 @@ importers: pkg-dir: ^6.0.1 ramda: ^0.27.1 resolve: ^1.20.0 + rollup: ^2.62.0 typescript: 4.6.0-dev.20220105 vite: ^2.7.7 vitest: ^0.0.115 @@ -823,6 +826,8 @@ importers: ramda: 0.27.2 resolve: 1.21.0 devDependencies: + '@babel/plugin-transform-typescript': 7.16.8 + '@rollup/plugin-babel': 5.3.0_rollup@2.64.0 '@types/caller': 1.0.0 '@types/dedent': 0.7.0 '@types/node': 17.0.10 @@ -831,6 +836,7 @@ importers: '@types/unixify': 1.0.0 c8: 7.11.0 eslint: 8.7.0 + rollup: 2.64.0 typescript: 4.6.0-dev.20220105 vite: 2.7.13 vitest: 0.0.115_c8@7.11.0+vite@2.7.13 @@ -1312,6 +1318,23 @@ packages: browserslist: 4.19.1 semver: 6.3.0 + /@babel/helper-create-class-features-plugin/7.16.10: + resolution: {integrity: sha512-wDeej0pu3WN/ffTxMNCPW5UCiOav8IcLRxSIyp/9+IF2xJUM9h/OYjg0IJLHaL6F8oU8kqMz9nc1vryXhMsgXg==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0 + dependencies: + '@babel/helper-annotate-as-pure': 7.16.7 + '@babel/helper-environment-visitor': 7.16.7 + '@babel/helper-function-name': 7.16.7 + '@babel/helper-member-expression-to-functions': 7.16.7 + '@babel/helper-optimise-call-expression': 7.16.7 + '@babel/helper-replace-supers': 7.16.7 + '@babel/helper-split-export-declaration': 7.16.7 + transitivePeerDependencies: + - supports-color + dev: true + /@babel/helper-create-class-features-plugin/7.16.10_@babel+core@7.16.10: resolution: {integrity: sha512-wDeej0pu3WN/ffTxMNCPW5UCiOav8IcLRxSIyp/9+IF2xJUM9h/OYjg0IJLHaL6F8oU8kqMz9nc1vryXhMsgXg==} engines: {node: '>=6.9.0'} @@ -1948,6 +1971,15 @@ packages: '@babel/core': 7.16.10 '@babel/helper-plugin-utils': 7.16.7 + /@babel/plugin-syntax-typescript/7.16.7: + resolution: {integrity: sha512-YhUIJHHGkqPgEcMYkPCKTyGUdoGKWtopIycQyjJH8OjvRgOYsXsaKehLVPScKJWAULPxMa4N1vCe6szREFlZ7A==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0-0 + dependencies: + '@babel/helper-plugin-utils': 7.16.7 + dev: true + /@babel/plugin-syntax-typescript/7.16.7_@babel+core@7.16.10: resolution: {integrity: sha512-YhUIJHHGkqPgEcMYkPCKTyGUdoGKWtopIycQyjJH8OjvRgOYsXsaKehLVPScKJWAULPxMa4N1vCe6szREFlZ7A==} engines: {node: '>=6.9.0'} @@ -2336,6 +2368,19 @@ packages: '@babel/helper-plugin-utils': 7.16.7 dev: false + /@babel/plugin-transform-typescript/7.16.8: + resolution: {integrity: sha512-bHdQ9k7YpBDO2d0NVfkj51DpQcvwIzIusJ7mEUaMlbZq3Kt/U47j24inXZHQ5MDiYpCs+oZiwnXyKedE8+q7AQ==} + engines: {node: '>=6.9.0'} + peerDependencies: + '@babel/core': ^7.0.0-0 + dependencies: + '@babel/helper-create-class-features-plugin': 7.16.10 + '@babel/helper-plugin-utils': 7.16.7 + '@babel/plugin-syntax-typescript': 7.16.7 + transitivePeerDependencies: + - supports-color + dev: true + /@babel/plugin-transform-typescript/7.16.8_@babel+core@7.16.10: resolution: {integrity: sha512-bHdQ9k7YpBDO2d0NVfkj51DpQcvwIzIusJ7mEUaMlbZq3Kt/U47j24inXZHQ5MDiYpCs+oZiwnXyKedE8+q7AQ==} engines: {node: '>=6.9.0'} @@ -4054,6 +4099,22 @@ packages: '@rollup/pluginutils': 3.1.0_rollup@2.64.0 rollup: 2.64.0 + /@rollup/plugin-babel/5.3.0_rollup@2.64.0: + resolution: {integrity: sha512-9uIC8HZOnVLrLHxayq/PTzw+uS25E14KPUBh5ktF+18Mjo5yK0ToMMx6epY0uEgkjwJw0aBW4x2horYXh8juWw==} + engines: {node: '>= 10.0.0'} + peerDependencies: + '@babel/core': ^7.0.0 + '@types/babel__core': ^7.1.9 + rollup: ^1.20.0||^2.0.0 + peerDependenciesMeta: + '@types/babel__core': + optional: true + dependencies: + '@babel/helper-module-imports': 7.16.7 + '@rollup/pluginutils': 3.1.0_rollup@2.64.0 + rollup: 2.64.0 + dev: true + /@rollup/plugin-node-resolve/11.2.1_rollup@2.64.0: resolution: {integrity: sha512-yc2n43jcqVyGE2sqV5/YCmocy9ArjVAP/BeXyTtADTBBX6V0e5UMqwO8CdQ0kzjb6zu5P1qMzsScCMRvE9OlVg==} engines: {node: '>= 10.0.0'} diff --git a/site/docs/app/quick-start.md b/site/docs/app/quick-start.md index fdc28914..275fd875 100644 --- a/site/docs/app/quick-start.md +++ b/site/docs/app/quick-start.md @@ -71,8 +71,8 @@ module.exports = { "./*.ts" ], "compilerOptions": { - "module": "CommonJS", - "target": "es2018", + "module": "esnext", + "target": "esnext", "outDir": "dist", "noEmit": true, "jsx": "react-jsx",