Skip to content

Commit

Permalink
refactor: 迁移到纯ESM包格式 (#39)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 发布的包为纯ESM格式,无法通过CommonJS的`require`引入。参考[sinderesorhus的建议](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)
  • Loading branch information
otakustay committed Jan 6, 2022
1 parent 4509c7f commit e9dfd1c
Show file tree
Hide file tree
Showing 201 changed files with 3,343 additions and 3,293 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"test": "lerna run --concurrency=1 test",
"build": "lerna run build",
"build-clean": "lerna run --parallel clean && npm run build",
"build-showcase": "cd showcase/todo && npm run build",
"release": "lerna version --conventional-commits --no-push --force-publish",
"release-beta": "npm run release -- --conventional-prerelease --preid=beta",
"ci": "pnpm install && npm run build-clean && npm run lint && npm run test && npm run build-showcase",
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-add-react-display-name/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('../config-lint/dist/patch');
require('../config-lint/config/patch.cjs');

module.exports = {
extends: '../config-lint/config/eslint.js',
extends: '../config-lint/config/eslint.cjs',
ignorePatterns: [
'**/fixtures/**',
],
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-plugin-add-react-display-name/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"scripts": {
"clean": "rm -rf dist",
"build": "tsc -p tsconfig.build.json",
"test": "jest",
"test": "vitest run",
"lint": "eslint --max-warnings=0 src"
},
"devDependencies": {
"@babel/preset-react": "^7.16.5",
"@types/babel__core": "^7.1.17",
"@types/babel__traverse": "^7.14.2",
"@types/jest": "^27.0.3",
"@types/node": "^17.0.4",
"c8": "^7.10.0",
"eslint": "^8.6.0",
"jest": "^27.4.5",
"ts-jest": "^27.1.2",
"typescript": "^4.5.4"
"typescript": "4.6.0-dev.20220105",
"vite": "^2.7.7",
"vitest": "^0.0.115"
},
"dependencies": {
"@babel/core": "^7.16.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1

exports[`export default a render function 1`] = `
"// export default a render function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {test, expect} from 'vitest';
import {createTestRunner} from '@reskript/babel-utils';
import plugin from '../index';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('../config-lint/dist/patch');
require('../config-lint/config/patch.cjs');

module.exports = {
extends: '../config-lint/config/eslint.js',
extends: '../config-lint/config/eslint.cjs',
ignorePatterns: [
'**/fixtures/**',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"clean": "rm -rf dist",
"build": "tsc -p tsconfig.build.json",
"test": "jest",
"test": "vitest run",
"lint": "eslint --max-warnings=0 src"
},
"devDependencies": {
Expand All @@ -21,14 +21,14 @@
"@babel/preset-react": "^7.16.5",
"@types/babel__core": "^7.1.17",
"@types/babel__traverse": "^7.14.2",
"@types/jest": "^27.0.3",
"@types/node": "^17.0.4",
"@types/react": "^17.0.38",
"c8": "^7.10.0",
"eslint": "^8.6.0",
"jest": "^27.4.5",
"react": "^17.0.2",
"ts-jest": "^27.1.2",
"typescript": "^4.5.4"
"typescript": "4.6.0-dev.20220105",
"vite": "^2.7.7",
"vitest": "^0.0.115"
},
"dependencies": {
"@babel/core": "^7.16.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import fs from 'fs';
import * as babel from '@babel/core';
import {expect, test} from 'vitest';
import plugin from '../index';

const HOOK_MODULE = path.join(__dirname, '..', 'useComponentFile.js');
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-utils/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('../config-lint/dist/patch');
require('../config-lint/config/patch.cjs');

module.exports = {
extends: '../config-lint/config/eslint.js',
extends: '../config-lint/config/eslint.cjs',
ignorePatterns: [
'**/fixtures/**',
],
Expand Down
8 changes: 0 additions & 8 deletions packages/babel-utils/jest.config.js

This file was deleted.

10 changes: 5 additions & 5 deletions packages/babel-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"clean": "rm -rf dist",
"build": "tsc -p tsconfig.build.json",
"test": "jest",
"test": "vitest run",
"lint": "eslint --max-warnings=0 src"
},
"devDependencies": {
Expand All @@ -21,12 +21,12 @@
"@types/babel__core": "^7.1.17",
"@types/babel__traverse": "^7.14.2",
"@types/glob": "^7.2.0",
"@types/jest": "^27.0.3",
"@types/node": "^17.0.4",
"c8": "^7.10.0",
"eslint": "^8.6.0",
"jest": "^27.4.5",
"ts-jest": "^27.1.2",
"typescript": "^4.5.4"
"typescript": "4.6.0-dev.20220105",
"vite": "^2.7.7",
"vitest": "^0.0.115"
},
"peerDependencies": {
"@babel/core": "^7.14.6",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1

exports[`already imported 1`] = `
"// already imported
Expand Down
1 change: 1 addition & 0 deletions packages/babel-utils/src/__tests__/importReact.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {test, expect} from 'vitest';
import {createTestRunner} from '../testRunner';
import plugin from './plugin';

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-utils/src/importReact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as babel from '@babel/core';
import {NodePath} from '@babel/traverse';
import {findImportStatement} from './import';
import {findImportStatement} from './import.js';

type ImportDeclaration = babel.types.ImportDeclaration;
type MemberExpression = babel.types.MemberExpression;
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './importReact';
export * from './testRunner';
export * from './component';
export * from './import';
export * from './traverse';
export {prepareReactImport} from './importReact.js';
export {createTestRunner} from './testRunner.js';
export {isComponentDeclaration, resolveComponentName} from './component.js';
export {findImportStatement} from './import.js';
export {findParentProgram} from './traverse.js';
4 changes: 2 additions & 2 deletions packages/cli-babel/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('../config-lint/dist/patch');
require('../config-lint/config/patch.cjs');

module.exports = {
extends: '../config-lint/config/eslint.js',
extends: '../config-lint/config/eslint.cjs',
};
12 changes: 8 additions & 4 deletions packages/cli-babel/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name": "@reskript/cli-babel",
"version": "3.0.1",
"main": "dist/index.js",
"type": "module",
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js"
},
"license": "MIT",
"files": [
"dist"
Expand All @@ -20,15 +24,15 @@
"@types/jest": "^27.0.3",
"@types/node": "^17.0.4",
"eslint": "^8.6.0",
"typescript": "^4.5.4"
"typescript": "4.6.0-dev.20220105"
},
"dependencies": {
"@babel/core": "^7.16.5",
"@reskript/config-babel": "3.0.1",
"@reskript/core": "3.0.1",
"cli-highlight": "^2.1.11",
"globby": "^11.0.4",
"throat": "^6.0.1"
"globby": "^12.0.2",
"p-limit": "^4.0.0"
},
"peerDependencies": {
"core-js": "3.x"
Expand Down
13 changes: 8 additions & 5 deletions packages/cli-babel/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import path from 'path';
import {promises as fs} from 'fs';
import globby from 'globby';
import throat from 'throat';
import highlight from 'cli-highlight';
import pLimit from 'p-limit';
import {highlight} from 'cli-highlight';
import {transformFileAsync, TransformOptions} from '@babel/core';
import {logger} from '@reskript/core';
import {getTransformBabelConfig, BabelConfigOptions} from '@reskript/config-babel';
import {BabelCommandLineArgs} from './interface';
import {BabelCommandLineArgs} from './interface.js';

export {BabelCommandLineArgs};

Expand Down Expand Up @@ -45,6 +44,7 @@ const transformFile = async (file: string, baseIn: string, baseOut: string, opti
};

const transformDirectory = async (dir: string, out: string, options: TransformOptions) => {
const {globby} = await import('globby');
const files = await globby(`${dir.replace(/\/$/, '')}/**/*.{js,jsx,ts,tsx}`);
await Promise.all(files.map(f => transformFile(f, dir, out, options)));
};
Expand All @@ -67,6 +67,8 @@ const printInConsole = (code: string | null | undefined) => {
};

export const run = async (cmd: BabelCommandLineArgs, file: string): Promise<void> => {
const {globby} = await import('globby');

if (!file) {
return;
}
Expand Down Expand Up @@ -112,7 +114,8 @@ export const run = async (cmd: BabelCommandLineArgs, file: string): Promise<void

if (copy) {
const files = await globby([`${file}/**`, `!${file}/**/*.{ts,js,tsx,jsx}`]);
await Promise.all(files.map(throat(2, f => copyFile(f, file, outDirectory))));
const limit = pLimit(2);
await Promise.all(files.map(v => limit(copyFile, v, file, outDirectory)));
}
}
};
4 changes: 2 additions & 2 deletions packages/cli-build/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('../config-lint/dist/patch');
require('../config-lint/config/patch.cjs');

module.exports = {
extends: '../config-lint/config/eslint.js',
extends: '../config-lint/config/eslint.cjs',
};
16 changes: 10 additions & 6 deletions packages/cli-build/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name": "@reskript/cli-build",
"version": "3.0.1",
"main": "dist/index.js",
"type": "module",
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js"
},
"license": "MIT",
"files": [
"dist"
Expand All @@ -17,20 +21,20 @@
},
"devDependencies": {
"@types/jest": "^27.0.3",
"@types/lodash": "^4.14.178",
"@types/node": "^17.0.4",
"typescript": "^4.5.4",
"@types/ramda": "^0.27.62",
"typescript": "4.6.0-dev.20220105",
"webpack": "^5.65.0"
},
"dependencies": {
"@reskript/config-webpack": "3.0.1",
"@reskript/core": "3.0.1",
"@reskript/settings": "3.0.1",
"chalk": "^4.1.2",
"eslint": "^8.6.0",
"lodash": "^4.17.21",
"matcher": "^4.0.0",
"kolorist": "^1.5.1",
"matcher": "^5.0.0",
"pretty-bytes": "^5.6.0",
"ramda": "^0.27.1",
"tty-table": "^4.1.5",
"webpack-bundle-analyzer": "^4.5.0"
},
Expand Down
16 changes: 8 additions & 8 deletions packages/cli-build/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs/promises';
import {compact, difference, uniq} from 'lodash';
import {reject, isNil, difference} from 'ramda';
import webpack, {Configuration, Stats} from 'webpack';
import {logger, pMap, prepareEnvironment, readPackageConfig} from '@reskript/core';
import {
Expand All @@ -12,10 +12,10 @@ import {
EntryLocation,
} from '@reskript/config-webpack';
import {readProjectSettings, BuildEnv, ProjectSettings, strictCheckRequiredDependency} from '@reskript/settings';
import * as partials from './partial';
import {BuildCommandLineArgs} from './interface';
import {drawFeatureMatrix, drawBuildReport, printWebpackResult, WebpackResult} from './report';
import inspect from './inspect';
import * as partials from './partial.js';
import {BuildCommandLineArgs} from './interface.js';
import {drawFeatureMatrix, drawBuildReport, printWebpackResult, WebpackResult} from './report.js';
import inspect from './inspect/index.js';

export {BuildCommandLineArgs};

Expand All @@ -36,10 +36,10 @@ const build = (configuration: Configuration | Configuration[]): Promise<Stats> =
const toJsonOptions = {all: false, errors: true, warnings: true, assets: true};
// webpack的`toJson`的定义是错的
const {errors, warnings} = stats.toJson(toJsonOptions);
for (const error of uniq(errors)) {
for (const error of reject(isNil, errors ?? [])) {
printWebpackResult('error', error as unknown as WebpackResult);
}
for (const warning of uniq(warnings)) {
for (const warning of reject(isNil, warnings ?? [])) {
printWebpackResult('warn', warning as unknown as WebpackResult);
}

Expand Down Expand Up @@ -111,7 +111,7 @@ const createConfigurations = async (cmd: BuildCommandLineArgs, projectSettings:
caseSensitiveModuleSource: cmd.strict,
typeCheck: cmd.strict,
},
extras: compact(extras),
extras: reject((v: false | Configuration): v is false => !v, extras),
}
);
};
Expand Down
8 changes: 4 additions & 4 deletions packages/cli-build/src/inspect/duplicatePackages.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {StatsCompilation} from 'webpack';
import {compact, flatMap, uniq} from 'lodash';
import {uniq, reject, isNil} from 'ramda';
import {readPackageConfig} from '@reskript/core';
import {BuildInspectSettings, SourceFilter} from '@reskript/settings';
import {RuleProcessor, isIncluded} from './utils';
import {RuleProcessor, isIncluded} from './utils.js';

const extractUniqueModules = (compilations: StatsCompilation[]): string[] => {
const modules = flatMap(compilations, c => c.modules);
const modules = compilations.flatMap(c => c.modules);
const names = modules.map(m => m?.nameForCondition);
return uniq(compact(names));
return uniq(reject(isNil, names));
};

interface LibraryInfo {
Expand Down
10 changes: 5 additions & 5 deletions packages/cli-build/src/inspect/htmlImportable.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import path from 'path';
import fs from 'fs/promises';
import {uniq, compact, flatMap, last} from 'lodash';
import {uniq, last, reject, isNil} from 'ramda';
import {StatsCompilation} from 'webpack';
import {BuildInspectSettings, SourceFilter} from '@reskript/settings';
import {RuleProcessor, isIncluded} from './utils';
import {RuleProcessor, isIncluded} from './utils.js';

const extractScriptEntries = (compilations: StatsCompilation[]): string[] => {
const entries = flatMap(compilations, c => Object.values(c.entrypoints ?? {}));
const entries = compilations.flatMap(c => Object.values(c.entrypoints ?? {}));
// 这里的`assets`是有顺序的(大概),被别人依赖的在前面(大概),真正的入口是最后一个(大概)
return uniq(compact(entries.map(v => last(v.assets)?.name)));
return uniq(reject(isNil, entries.map(v => last(v.assets ?? [])?.name)));
};

const extractHTMLEntries = (compilations: StatsCompilation[]): string[] => {
const assets = flatMap(compilations, c => c.assets ?? []);
const assets = compilations.flatMap(c => c.assets ?? []);
const files = assets.filter(v => /^\.\.\/.+\.html$/.test(v.name)).map(v => v.name);
return files;
};
Expand Down
Loading

0 comments on commit e9dfd1c

Please sign in to comment.