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 21, 2022
1 parent ae52255 commit 379f633
Show file tree
Hide file tree
Showing 231 changed files with 4,582 additions and 4,347 deletions.
20 changes: 12 additions & 8 deletions .github/workflows/ci.yml
@@ -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
10 changes: 6 additions & 4 deletions package.json
Expand Up @@ -4,19 +4,21 @@
"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 --frozen-lockfile && 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",
"prepack": "pnpm install --frozen-lockfile && npm run build-clean && sh scripts/build-check.sh",
"version": "pnpm install --frozen-lockfile && git add pnpm-lock.yaml",
"deploy": "lerna publish from-package",
"deploy-next": "npm run deploy -- --dist-tag=next --pre-dist-tag=next",
"prepare": "husky install"
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-add-react-display-name/.eslintrc.cjs
@@ -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
@@ -1,8 +1,14 @@
{
"name": "@reskript/babel-plugin-add-react-display-name",
"version": "3.0.6",
"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.6",
"@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
@@ -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
@@ -1,8 +1,10 @@
import {test, expect} from 'vitest';
import {dirFromImportMeta} from '@reskript/core';
import {createTestRunner} from '@reskript/babel-utils';
import plugin from '../index';
import plugin from '../index.js';

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
@@ -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
@@ -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
@@ -1,8 +1,15 @@
{
"name": "@reskript/babel-plugin-debug-react-component-file-name",
"version": "3.0.6",
"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.6",
"@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
@@ -1,17 +1,19 @@
import path from 'path';
import fs from 'fs';
import * as babel from '@babel/core';
import plugin from '../index';
import babel from '@babel/core';
import {expect, test} from 'vitest';
import {dirFromImportMeta} from '@reskript/core';
import plugin from '../index.js';

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
@@ -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
@@ -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
@@ -1,8 +1,14 @@
{
"name": "@reskript/babel-utils",
"version": "3.0.6",
"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.6",
"@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
@@ -1,4 +1,4 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1

exports[`already imported 1`] = `
"// already imported
Expand Down
8 changes: 5 additions & 3 deletions packages/babel-utils/src/__tests__/importReact.test.ts
@@ -1,8 +1,10 @@
import {createTestRunner} from '../testRunner';
import plugin from './plugin';
import {test, expect} from 'vitest';
import {dirFromImportMeta} from '@reskript/core';
import {createTestRunner} from '../testRunner.js';
import plugin from './plugin.js';

const runSuite = createTestRunner(
__dirname,
dirFromImportMeta(import.meta.url),
{test, expect},
{
plugins: [plugin],
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-utils/src/__tests__/plugin.ts
@@ -1,5 +1,5 @@
import {PluginObj} from '@babel/core';
import {prepareReactImport} from '../importReact';
import {prepareReactImport} from '../importReact.js';

const plugin = (): PluginObj => {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-utils/src/component.ts
@@ -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
@@ -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

0 comments on commit 379f633

Please sign in to comment.