Skip to content

Commit

Permalink
refactor: 核心部分迁移到纯ESM包格式 (#39)
Browse files Browse the repository at this point in the history
当前完成了最认本部分逻辑的ESM化,剩余的包主要为:

- 各类插件包,这需要先支持`reskript.config.{mjs|ts}`的ESM化
- 几个webpack loader,要等待webpack支持
- eslint插件无法ESM化
- jest配置无法ESM化
- 受前几项影响,`core`和`settings`暂未ESM化

BREAKING CHANGE: 发布的包为纯ESM格式,无法通过CommonJS的`require`引入。参考[sinderesorhus的建议](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)
  • Loading branch information
otakustay committed Jan 20, 2022
1 parent 584b9c6 commit b2dabde
Show file tree
Hide file tree
Showing 227 changed files with 4,470 additions and 4,242 deletions.
20 changes: 12 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node.js CI
name: NodeJS CI

on:
push:
Expand All @@ -11,22 +11,26 @@ jobs:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
- uses: pnpm/action-setup@v2.0.1
with:
version: 6.0.2
version: 6.x
- uses: actions/checkout@v2
- name: CI
- name: Setup Node 16
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
node-version: 16.x
- run: pnpm install
- run: pnpm run build
- run: pnpm run lint
- run: pnpm run test
env:
CI: true
- name: Setup NodeJS 14
uses: actions/setup-node@v1
with:
node-version: 14.x
- name: Unit Test NodeJS 14
run: pnpm run test
env:
CI: true
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
"version": "0.0.1",
"repository": "https://github.com/ecomfe/reskript",
"author": "otakustay <otakustay@gmail.com>",
"engines": {
"node": ">=14.18.0"
},
"license": "MIT",
"scripts": {
"lint": "lerna run --parallel lint",
"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",
"ci": "pnpm install && npm run build-clean && npm run lint && npm run test",
"preversion": "npm run ci",
"prepack": "pnpm install && npm run build-clean && sh scripts/build-check.sh",
"version": "pnpm install && git add pnpm-lock.yaml",
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
8 changes: 0 additions & 8 deletions packages/babel-plugin-add-react-display-name/jest.config.js

This file was deleted.

19 changes: 13 additions & 6 deletions packages/babel-plugin-add-react-display-name/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"name": "@reskript/babel-plugin-add-react-display-name",
"version": "3.0.4",
"main": "dist/index.js",
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js"
},
"engines": {
"node": ">=14.18.0"
},
"files": [
"dist"
],
Expand All @@ -12,19 +18,20 @@
"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",
"@reskript/core": "3.0.4",
"@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,8 +1,10 @@
import {test, expect} from 'vitest';
import {dirFromImportMeta} from '@reskript/core';
import {createTestRunner} from '@reskript/babel-utils';
import plugin from '../index';

const runSuite = createTestRunner(
__dirname,
dirFromImportMeta(import.meta.url),
{test, expect},
{
presets: [
Expand Down
6 changes: 4 additions & 2 deletions packages/babel-plugin-add-react-display-name/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {types, PluginObj} from '@babel/core';
import babel from '@babel/core';
import {isComponentDeclaration} from '@reskript/babel-utils';

export default function addReactDisplayName(): PluginObj {
const {types} = babel;

export default function addReactDisplayName(): babel.PluginObj {
return {
visitor: {
FunctionDeclaration(declaration) {
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

This file was deleted.

20 changes: 14 additions & 6 deletions packages/babel-plugin-debug-react-component-file-name/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "@reskript/babel-plugin-debug-react-component-file-name",
"version": "3.0.4",
"main": "dist/index.js",
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js",
"./useComponentFile": "./dist/useComponentFile.js"
},
"engines": {
"node": ">=14.18.0"
},
"files": [
"dist"
],
Expand All @@ -12,23 +19,24 @@
"scripts": {
"clean": "rm -rf dist",
"build": "tsc -p tsconfig.build.json",
"test": "jest",
"test": "vitest run",
"lint": "eslint --max-warnings=0 src"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.16.5",
"@babel/plugin-transform-classes": "^7.16.5",
"@babel/preset-react": "^7.16.5",
"@reskript/core": "3.0.4",
"@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,17 +1,19 @@
import path from 'path';
import fs from 'fs';
import * as babel from '@babel/core';
import babel from '@babel/core';
import {expect, test} from 'vitest';
import {dirFromImportMeta} from '@reskript/core';
import plugin from '../index';

const HOOK_MODULE = path.join(__dirname, '..', 'useComponentFile.js');
const HOOK_MODULE = path.join(dirFromImportMeta(import.meta.url), '..', 'useComponentFile.js');

const BABEL_OPTIONS = {
presets: ['@babel/preset-react'],
plugins: [
[
plugin,
{
srcDirectory: path.join(__dirname, 'fixtures', 'src'),
srcDirectory: path.join(dirFromImportMeta(import.meta.url), 'fixtures', 'src'),
},
],
'@babel/plugin-proposal-class-properties',
Expand All @@ -20,7 +22,7 @@ const BABEL_OPTIONS = {
};

const testFixture = (name: string, shouldInject: boolean) => {
const filename = path.join(__dirname, 'fixtures', name);
const filename = path.join(dirFromImportMeta(import.meta.url), 'fixtures', name);
const content = fs.readFileSync(filename, 'utf-8');
const result = babel.transformSync(content, {...BABEL_OPTIONS, filename});
expect((result?.code ?? '').includes('useComponentFile("')).toBe(shouldInject);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import path from 'path';
import {types, PluginObj, PluginPass} from '@babel/core';
import babel from '@babel/core';
import {NodePath} from '@babel/traverse';
import {dirFromImportMeta} from '@reskript/core';
import {isComponentDeclaration, findImportStatement, findParentProgram} from '@reskript/babel-utils';

const HOOK_MODULE = path.resolve(__dirname, 'useComponentFile.js');
const {types} = babel;

const insertImportHook = (program: NodePath<types.Program>) => {
const HOOK_MODULE = path.resolve(dirFromImportMeta(import.meta.url), 'useComponentFile.js');

const insertImportHook = (program: NodePath<babel.types.Program>) => {
const expression = types.importDeclaration(
[types.importDefaultSpecifier(types.identifier('useComponentFile'))],
types.stringLiteral(HOOK_MODULE)
Expand All @@ -30,15 +33,15 @@ const prepareHookImport = (current: NodePath): boolean => {
return true;
};

interface PluginState extends PluginPass {
interface PluginState extends babel.PluginPass {
readonly opts: {
srcDirectory: string;
fullPathPrefix?: string;
};
declaredClassNames?: Set<string>;
}

export default function debugReactComponentFileName(): PluginObj<PluginState> {
export default function debugReactComponentFileName(): babel.PluginObj<PluginState> {
return {
visitor: {
ClassDeclaration(declaration, state) {
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.

19 changes: 13 additions & 6 deletions packages/babel-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"name": "@reskript/babel-utils",
"version": "3.0.4",
"main": "dist/index.js",
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js"
},
"engines": {
"node": ">=14.18.0"
},
"files": [
"dist"
],
Expand All @@ -12,21 +18,22 @@
"scripts": {
"clean": "rm -rf dist",
"build": "tsc -p tsconfig.build.json",
"test": "jest",
"test": "vitest run",
"lint": "eslint --max-warnings=0 src"
},
"devDependencies": {
"@babel/core": "^7.16.5",
"@babel/traverse": "^7.16.5",
"@reskript/core": "3.0.4",
"@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
4 changes: 3 additions & 1 deletion packages/babel-utils/src/__tests__/importReact.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {test, expect} from 'vitest';
import {dirFromImportMeta} from '@reskript/core';
import {createTestRunner} from '../testRunner';
import plugin from './plugin';

const runSuite = createTestRunner(
__dirname,
dirFromImportMeta(import.meta.url),
{test, expect},
{
plugins: [plugin],
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-utils/src/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import * as babel from '@babel/core';
import babel from '@babel/core';
import {NodePath, Visitor} from '@babel/traverse';

type FunctionDeclaration = babel.types.FunctionDeclaration;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-utils/src/import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as babel from '@babel/core';
import babel from '@babel/core';
import {NodePath, Visitor} from '@babel/traverse';

type ImportDeclaration = babel.types.ImportDeclaration;
Expand Down
4 changes: 2 additions & 2 deletions 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 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';
Loading

0 comments on commit b2dabde

Please sign in to comment.