Skip to content

Commit

Permalink
refactor: core包迁移至ESM (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Jan 29, 2022
1 parent ff3b960 commit f2e3100
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 27 deletions.
2 changes: 1 addition & 1 deletion 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';
Expand Down
2 changes: 1 addition & 1 deletion 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';
Expand All @@ -24,7 +25,6 @@ const filterUnwantedReports = (report: LintResult[], cmd: LintCommandLineArgs):
};

export const run = async (cmd: LintCommandLineArgs, files: string[]): Promise<void> => {
const {execa} = await import('execa');
const gitRoot = await findGitRoot() || process.cwd();
const status = await gitStatus(process.cwd());
const options: ResolveOptions = {...cmd, gitRoot, gitStatus: status};
Expand Down
3 changes: 1 addition & 2 deletions 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';
}
Expand Down
13 changes: 2 additions & 11 deletions packages/cli/src/DynamicImportCommand.ts
Expand Up @@ -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;
Expand All @@ -27,8 +28,6 @@ export default abstract class DynamicImportCommand<A> 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);
Expand All @@ -52,7 +51,6 @@ export default abstract class DynamicImportCommand<A> extends Command {
}

private async importCommandPackage() {
const {logger, resolveFrom} = await import('@reskript/core');
const resolve = resolveFrom(process.cwd());

const dynamicImport = async () => {
Expand Down Expand Up @@ -111,9 +109,6 @@ export default abstract class DynamicImportCommand<A> extends Command {
}

private async canAutoInstall() {
const {readPackageConfig} = await import('@reskript/core');
const {packageDirectory} = await import('pkg-dir');

const packageRoot = await packageDirectory();

if (!packageRoot) {
Expand All @@ -128,8 +123,6 @@ export default abstract class DynamicImportCommand<A> extends Command {
}

private async detectPackageManager(): Promise<PackageManager | null> {
const {findGitRoot} = await import('@reskript/core');

const gitRoot = await findGitRoot();

if (!gitRoot) {
Expand All @@ -150,8 +143,6 @@ export default abstract class DynamicImportCommand<A> extends Command {
}

private async installCommandPackage(): Promise<InstallReuslt> {
const {logger} = await import('@reskript/core');

const question = {
type: 'confirm',
name: 'ok',
Expand Down
1 change: 1 addition & 0 deletions 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';
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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<string, any> = {
Expand Down
2 changes: 1 addition & 1 deletion 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<string> => {
const name = path.join('node_modules', ...segments);
Expand Down
10 changes: 7 additions & 3 deletions packages/core/package.json
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions 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);
})();
1 change: 1 addition & 0 deletions packages/core/src/internal.ts
@@ -0,0 +1 @@
export {resolveSync} from './resolve.js';
1 change: 1 addition & 0 deletions 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';

Expand Down
7 changes: 3 additions & 4 deletions 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<string> => {
const {packageDirectory} = await import('pkg-dir');
const root = await packageDirectory();

if (!root) {
Expand All @@ -21,7 +23,6 @@ export const readPackageConfig = async (cwd: string): Promise<PackageInfo> => {
};

export const findGitRoot = async (cwd?: string): Promise<string | undefined> => {
const {findUp} = await import('find-up');
const gitDirectory = await findUp('.git', {cwd, type: 'directory'});
return gitDirectory && path.dirname(gitDirectory);
};
Expand All @@ -36,7 +37,6 @@ export const isMonorepo = async (cwd: string): Promise<boolean> => {
};

export const resolveMonorepoPackageDirectories = async (cwd: string): Promise<string[]> => {
const {globby} = await import('globby');
const packageInfo = await readPackageConfig(cwd);
const packages = packageInfo.workspaces
? (Array.isArray(packageInfo.workspaces) ? packageInfo.workspaces : packageInfo.workspaces.packages)
Expand All @@ -46,7 +46,6 @@ export const resolveMonorepoPackageDirectories = async (cwd: string): Promise<st
};

export const findMonorepoRoot = async (cwd: string): Promise<string> => {
const {findUp} = await import('find-up');
const dir = await findUp('packages', {cwd, type: 'directory'});

if (!dir) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/resolve.ts
Expand Up @@ -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) => {
Expand Down
61 changes: 61 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions site/docs/app/quick-start.md
Expand Up @@ -71,8 +71,8 @@ module.exports = {
"./*.ts"
],
"compilerOptions": {
"module": "CommonJS",
"target": "es2018",
"module": "esnext",
"target": "esnext",
"outDir": "dist",
"noEmit": true,
"jsx": "react-jsx",
Expand Down

0 comments on commit f2e3100

Please sign in to comment.