@modern-js/server-utils 1.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ export var createMiddlewareCollecter = function createMiddlewareCollecter() {
2
+ var webMiddlewares = [];
3
+ var apiMiddlewares = [];
4
+ var ssrMiddlewares = [];
5
+
6
+ var addWebMiddleware = function addWebMiddleware(input) {
7
+ webMiddlewares.push(input);
8
+ };
9
+
10
+ var addAPIMiddleware = function addAPIMiddleware(input) {
11
+ apiMiddlewares.push(input);
12
+ };
13
+
14
+ var addSSRMiddleware = function addSSRMiddleware(input) {
15
+ ssrMiddlewares.push(input);
16
+ };
17
+
18
+ var getMiddlewares = function getMiddlewares() {
19
+ return {
20
+ web: webMiddlewares,
21
+ api: apiMiddlewares,
22
+ ssr: ssrMiddlewares
23
+ };
24
+ };
25
+
26
+ return {
27
+ getMiddlewares: getMiddlewares,
28
+ addWebMiddleware: addWebMiddleware,
29
+ addAPIMiddleware: addAPIMiddleware,
30
+ addSSRMiddleware: addSSRMiddleware
31
+ };
32
+ };
33
+ export var hook = function hook(attacher) {
34
+ return attacher;
35
+ };
@@ -0,0 +1,26 @@
1
+ import { findExists } from '@modern-js/utils';
2
+ var FILE_EXTENSIONS = ['.ts', '.js'];
3
+ export var requireModule = function requireModule(filename) {
4
+ var exist = findExists(FILE_EXTENSIONS.map(function (ext) {
5
+ return "".concat(filename).concat(ext);
6
+ }));
7
+
8
+ if (!exist) {
9
+ return null;
10
+ } // throw errors directly if require fail
11
+
12
+
13
+ var mod = require(exist);
14
+
15
+ return interopRequire(mod);
16
+ };
17
+
18
+ function interopRequire(obj) {
19
+ return interopRequireDefault(obj)["default"];
20
+ }
21
+
22
+ function interopRequireDefault(obj) {
23
+ return obj !== null && obj !== void 0 && obj.__esModule ? obj : {
24
+ "default": obj
25
+ };
26
+ }
@@ -0,0 +1,27 @@
1
+ import { ILibPresetOption, ISyntaxOption } from '@modern-js/babel-preset-lib';
2
+ import { TransformOptions } from '@babel/core';
3
+ import type { NormalizedConfig } from '@modern-js/core';
4
+ export * from '@babel/core';
5
+ export interface ITsconfig {
6
+ compilerOptions?: {
7
+ rootDir?: string;
8
+ baseUrl?: string;
9
+ declaration?: boolean;
10
+ emitDeclarationOnly?: boolean;
11
+ isolatedModules?: boolean;
12
+ allowJs?: boolean;
13
+ outDir?: string;
14
+ paths?: Record<string, string[]>;
15
+ } | undefined;
16
+ include?: string[];
17
+ exclude?: string[];
18
+ }
19
+ export declare const readTsConfig: <T extends ITsconfig | null>(tsconfigPath: string, noExistReturn?: T) => ITsconfig | T;
20
+ export declare const existTsConfigFile: (tsconfigAbsolutePath: string) => boolean;
21
+ export declare const getBabelConfig: (libPresetOption: ILibPresetOption, syntaxOption: ISyntaxOption) => TransformOptions;
22
+ export interface IPackageModeValue {
23
+ type: 'module' | 'commonjs';
24
+ syntax: 'es5' | 'es6+';
25
+ tsconfigPath: string;
26
+ }
27
+ export declare const resolveBabelConfig: (appDirectory: string, modernConfig: NormalizedConfig, option: IPackageModeValue) => any;
@@ -0,0 +1 @@
1
+ export declare const gather: (pwd: string) => import("./middleware").CollectMiddlewaresResult;
@@ -0,0 +1,3 @@
1
+ export * from './middleware';
2
+ export * from './gather';
3
+ export * from './babel';
@@ -0,0 +1,16 @@
1
+ export declare type CollectMiddlewaresResult = {
2
+ web: any[];
3
+ api: any[];
4
+ ssr: any[];
5
+ };
6
+ export declare const createMiddlewareCollecter: () => {
7
+ getMiddlewares: () => CollectMiddlewaresResult;
8
+ addWebMiddleware: (input: any) => void;
9
+ addAPIMiddleware: (input: any) => void;
10
+ addSSRMiddleware: (input: any) => void;
11
+ };
12
+ export declare type AttacherOptions = {
13
+ addMiddleware: (...input: any[]) => void;
14
+ };
15
+ export declare type Attacher = (options: AttacherOptions) => void;
16
+ export declare const hook: (attacher: Attacher) => Attacher;
@@ -0,0 +1 @@
1
+ export declare const requireModule: (filename: string) => any;
@@ -0,0 +1,15 @@
1
+ /** @type {import('@modern-js/module-tools').UserConfig} */
2
+ module.exports = {
3
+ testing: {
4
+ jest: {
5
+ collectCoverage: true,
6
+ collectCoverageFrom: ['./src/**/*.ts'],
7
+ coveragePathIgnorePatterns: ['/node_modules/'],
8
+ testEnvironment: 'jsdom',
9
+ moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
10
+ transform: {
11
+ '^.+\\.tsx?$': 'ts-jest',
12
+ },
13
+ },
14
+ },
15
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@modern-js/server-utils",
3
+ "version": "1.0.0-rc.2",
4
+ "jsnext:source": "./src/index.ts",
5
+ "types": "./dist/types/index.d.ts",
6
+ "main": "./dist/js/node/index.js",
7
+ "module": "./dist/js/treeshaking/index.js",
8
+ "jsnext:modern": "./dist/js/modern/index.js",
9
+ "exports": {
10
+ ".": {
11
+ "node": {
12
+ "import": "./dist/js/modern/index.js",
13
+ "require": "./dist/js/node/index.js"
14
+ },
15
+ "default": "./dist/js/treeshaking/index.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@babel/compat-data": "^7.15.0",
20
+ "@babel/core": "^7.15.0",
21
+ "@babel/plugin-proposal-class-properties": "^7.14.5",
22
+ "@babel/plugin-proposal-decorators": "^7.15.4",
23
+ "@babel/preset-env": "^7.15.0",
24
+ "@babel/preset-typescript": "^7.15.0",
25
+ "@babel/runtime": "^7",
26
+ "@modern-js/babel-preset-lib": "^1.0.0-rc.2",
27
+ "@modern-js/plugin": "^1.0.0-rc.2",
28
+ "@modern-js/utils": "^1.0.0-rc.2",
29
+ "babel-plugin-module-resolver": "^4.1.0",
30
+ "babel-plugin-transform-typescript-metadata": "^0.3.2",
31
+ "json5": "^2.2.0"
32
+ },
33
+ "devDependencies": {
34
+ "@modern-js/core": "^1.0.0-rc.2",
35
+ "@types/babel__core": "^7.1.15",
36
+ "@types/jest": "^26",
37
+ "@types/node": "^14",
38
+ "@types/react": "^17",
39
+ "@types/react-dom": "^17",
40
+ "ts-jest": "^27.0.4",
41
+ "typescript": "^4",
42
+ "@modern-js/plugin-testing": "^1.0.0-rc.2",
43
+ "@modern-js/module-tools": "^1.0.0-rc.2"
44
+ },
45
+ "sideEffects": false,
46
+ "scripts": {
47
+ "new": "modern new",
48
+ "build": "modern build",
49
+ "test": "modern test --passWithNoTests"
50
+ }
51
+ }
package/src/babel.ts ADDED
@@ -0,0 +1,149 @@
1
+ import {
2
+ getBabelChain,
3
+ ILibPresetOption,
4
+ ISyntaxOption,
5
+ } from '@modern-js/babel-preset-lib';
6
+ import { TransformOptions } from '@babel/core';
7
+ import { applyOptionsChain, fs, getAlias } from '@modern-js/utils';
8
+ import type { NormalizedConfig } from '@modern-js/core';
9
+ import json5 from 'json5';
10
+
11
+ export * from '@babel/core';
12
+
13
+ export interface ITsconfig {
14
+ compilerOptions?:
15
+ | {
16
+ rootDir?: string;
17
+ baseUrl?: string;
18
+ declaration?: boolean;
19
+ emitDeclarationOnly?: boolean;
20
+ isolatedModules?: boolean;
21
+ allowJs?: boolean;
22
+ outDir?: string;
23
+ paths?: Record<string, string[]>;
24
+ }
25
+ | undefined;
26
+ include?: string[];
27
+ exclude?: string[];
28
+ }
29
+
30
+ export const readTsConfig = <T extends null | ITsconfig>(
31
+ tsconfigPath: string,
32
+ noExistReturn: T = null as T,
33
+ ): ITsconfig | T => {
34
+ // 如果不存在,则返回 noExistReturn
35
+ if (!fs.existsSync(tsconfigPath)) {
36
+ return noExistReturn;
37
+ }
38
+ const content = fs.readFileSync(tsconfigPath, 'utf-8');
39
+ return json5.parse(content);
40
+ };
41
+
42
+ export const existTsConfigFile = (tsconfigAbsolutePath: string) => {
43
+ const tsconfig = readTsConfig(tsconfigAbsolutePath);
44
+ return Boolean(tsconfig);
45
+ };
46
+
47
+ export const getBabelConfig = (
48
+ libPresetOption: ILibPresetOption,
49
+ syntaxOption: ISyntaxOption,
50
+ ): TransformOptions => {
51
+ const chain = getBabelChain(libPresetOption, syntaxOption);
52
+
53
+ return {
54
+ sourceType: 'unambiguous',
55
+ ...chain.toJSON(),
56
+ };
57
+ };
58
+
59
+ export interface IPackageModeValue {
60
+ type: 'module' | 'commonjs';
61
+ syntax: 'es5' | 'es6+';
62
+ tsconfigPath: string;
63
+ }
64
+
65
+ export const resolveBabelConfig = (
66
+ appDirectory: string,
67
+ modernConfig: NormalizedConfig,
68
+ option: IPackageModeValue,
69
+ // FIXME: babel type can't pass type checking
70
+ ): any => {
71
+ const {
72
+ source: {
73
+ envVars,
74
+ globalVars,
75
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
76
+ // @ts-expect-error
77
+ jsxTransformRuntime = 'automatic',
78
+ },
79
+ tools: { lodash: userLodashOption },
80
+ } = modernConfig;
81
+
82
+ // alias config
83
+ const aliasConfig = getAlias(modernConfig.source.alias, {
84
+ appDirectory,
85
+ ...option,
86
+ });
87
+
88
+ // lodash config
89
+ const lodashOptions = applyOptionsChain(
90
+ { id: ['lodash', 'ramda'] },
91
+ // TODO: 需要处理类型问题
92
+ userLodashOption as any,
93
+ );
94
+ // babel config
95
+ const babelChain = getBabelChain(
96
+ {
97
+ appDirectory,
98
+ enableReactPreset: true,
99
+ enableTypescriptPreset: true,
100
+ alias: aliasConfig,
101
+ envVars,
102
+ globalVars,
103
+ lodashOptions,
104
+ jsxTransformRuntime,
105
+ },
106
+ {
107
+ type: option.type,
108
+ syntax: option.syntax,
109
+ },
110
+ );
111
+
112
+ const envOptions = babelChain.preset('@babel/preset-env').options();
113
+ babelChain
114
+ .preset('@babel/preset-env')
115
+ .use(require.resolve('@babel/preset-env'), [
116
+ {
117
+ ...envOptions[0],
118
+ loose: true,
119
+ },
120
+ ]);
121
+
122
+ babelChain
123
+ .plugin('babel-plugin-transform-typescript-metadata')
124
+ .use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
125
+
126
+ babelChain
127
+ .plugin('@babel/plugin-proposal-decorators')
128
+ .use(require.resolve('@babel/plugin-proposal-decorators'), [
129
+ { legacy: true },
130
+ ]);
131
+
132
+ babelChain
133
+ .plugin('@babel/plugin-proposal-class-properties')
134
+ .use(require.resolve('@babel/plugin-proposal-class-properties'), [
135
+ { loose: true },
136
+ ]);
137
+
138
+ const internalBabelConfig = { ...babelChain.toJSON() };
139
+
140
+ const userBabelConfig = modernConfig.tools.babel;
141
+ applyOptionsChain(
142
+ internalBabelConfig,
143
+ // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
144
+ userBabelConfig as any,
145
+ { chain: babelChain },
146
+ );
147
+
148
+ return internalBabelConfig;
149
+ };
package/src/gather.ts ADDED
@@ -0,0 +1,30 @@
1
+ import path from 'path';
2
+ import { createMiddlewareCollecter } from './middleware';
3
+ import { requireModule } from './requireModule';
4
+
5
+ const API_DIR_PATH = 'api';
6
+ const API_APP_NAME = '_app';
7
+ const SERVER_DIR_PATH = 'server';
8
+ const WEB_APP_NAME = 'index';
9
+
10
+ export const gather = (pwd: string) => {
11
+ const { getMiddlewares, addAPIMiddleware, addWebMiddleware } =
12
+ createMiddlewareCollecter();
13
+
14
+ const apiPath = path.resolve(pwd, API_DIR_PATH);
15
+ const apiAppPath = path.resolve(apiPath, API_APP_NAME);
16
+ const serverPath = path.resolve(pwd, SERVER_DIR_PATH);
17
+ const webAppPath = path.resolve(serverPath, WEB_APP_NAME);
18
+
19
+ const apiAttacher = requireModule(apiAppPath);
20
+ if (apiAttacher) {
21
+ apiAttacher({ addMiddleware: addAPIMiddleware });
22
+ }
23
+
24
+ const webAttacher = requireModule(webAppPath);
25
+ if (webAttacher) {
26
+ webAttacher({ addMiddleware: addWebMiddleware });
27
+ }
28
+
29
+ return getMiddlewares();
30
+ };
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './middleware';
2
+ export * from './gather';
3
+ export * from './babel';
@@ -0,0 +1,43 @@
1
+ export type CollectMiddlewaresResult = {
2
+ web: any[];
3
+ api: any[];
4
+ ssr: any[];
5
+ };
6
+
7
+ export const createMiddlewareCollecter = () => {
8
+ const webMiddlewares: any[] = [];
9
+ const apiMiddlewares: any[] = [];
10
+ const ssrMiddlewares: any[] = [];
11
+
12
+ const addWebMiddleware = (input: any) => {
13
+ webMiddlewares.push(input);
14
+ };
15
+
16
+ const addAPIMiddleware = (input: any) => {
17
+ apiMiddlewares.push(input);
18
+ };
19
+
20
+ const addSSRMiddleware = (input: any) => {
21
+ ssrMiddlewares.push(input);
22
+ };
23
+
24
+ const getMiddlewares = (): CollectMiddlewaresResult => ({
25
+ web: webMiddlewares,
26
+ api: apiMiddlewares,
27
+ ssr: ssrMiddlewares,
28
+ });
29
+ return {
30
+ getMiddlewares,
31
+ addWebMiddleware,
32
+ addAPIMiddleware,
33
+ addSSRMiddleware,
34
+ };
35
+ };
36
+
37
+ export type AttacherOptions = {
38
+ addMiddleware: (...input: any[]) => void;
39
+ };
40
+
41
+ export type Attacher = (options: AttacherOptions) => void;
42
+
43
+ export const hook = (attacher: Attacher) => attacher;
@@ -0,0 +1,23 @@
1
+ import { findExists } from '@modern-js/utils';
2
+
3
+ const FILE_EXTENSIONS = ['.ts', '.js'];
4
+
5
+ export const requireModule = (filename: string) => {
6
+ const exist = findExists(FILE_EXTENSIONS.map(ext => `${filename}${ext}`));
7
+
8
+ if (!exist) {
9
+ return null;
10
+ }
11
+
12
+ // throw errors directly if require fail
13
+ const mod = require(exist);
14
+ return interopRequire(mod);
15
+ };
16
+
17
+ function interopRequire(obj: any) {
18
+ return interopRequireDefault(obj).default;
19
+ }
20
+
21
+ function interopRequireDefault(obj: any) {
22
+ return obj?.__esModule ? obj : { default: obj };
23
+ }
@@ -0,0 +1,172 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`babel resolveBabelConfig 1`] = `
4
+ Object {
5
+ "plugins": Array [
6
+ Array [
7
+ "/babel-plugin-macros/dist/index.js",
8
+ Object {
9
+ "twin": Object {
10
+ "preset": "styled-components",
11
+ },
12
+ },
13
+ ],
14
+ Array [
15
+ "/babel-plugin-import/lib/index.js",
16
+ Object {
17
+ "libraryDirectory": "es",
18
+ "libraryName": "antd",
19
+ "style": true,
20
+ },
21
+ "import-antd",
22
+ ],
23
+ Array [
24
+ "/babel-plugin-lodash/lib/index.js",
25
+ Object {
26
+ "id": Array [
27
+ "lodash",
28
+ "ramda",
29
+ ],
30
+ },
31
+ ],
32
+ Array [
33
+ "/@babel/plugin-proposal-decorators/lib/index.js",
34
+ Object {
35
+ "legacy": true,
36
+ },
37
+ ],
38
+ Array [
39
+ "/@babel/plugin-proposal-class-properties/lib/index.js",
40
+ Object {
41
+ "loose": true,
42
+ },
43
+ ],
44
+ Array [
45
+ "/@babel/plugin-proposal-private-methods/lib/index.js",
46
+ Object {
47
+ "loose": true,
48
+ },
49
+ ],
50
+ Array [
51
+ "/@babel/plugin-proposal-private-property-in-object/lib/index.js",
52
+ Object {
53
+ "loose": true,
54
+ },
55
+ ],
56
+ Array [
57
+ "/@babel/plugin-proposal-object-rest-spread/lib/index.js",
58
+ Object {
59
+ "useBuiltIns": true,
60
+ },
61
+ ],
62
+ Array [
63
+ "/@babel/plugin-transform-runtime/lib/index.js",
64
+ Object {
65
+ "corejs": false,
66
+ "helpers": false,
67
+ "regenerator": false,
68
+ "useESModules": false,
69
+ "version": "7.15.4",
70
+ },
71
+ ],
72
+ Array [
73
+ "/babel-plugin-transform-react-remove-prop-types/lib/index.js",
74
+ Object {
75
+ "removeImport": true,
76
+ },
77
+ ],
78
+ Array [
79
+ "/@babel/plugin-proposal-function-bind/lib/index.js",
80
+ ],
81
+ Array [
82
+ "/@babel/plugin-proposal-export-default-from/lib/index.js",
83
+ ],
84
+ Array [
85
+ "/@babel/plugin-proposal-export-namespace-from/lib/index.js",
86
+ ],
87
+ Array [
88
+ "/@babel/plugin-proposal-optional-chaining/lib/index.js",
89
+ ],
90
+ Array [
91
+ "/@babel/plugin-proposal-numeric-separator/lib/index.js",
92
+ ],
93
+ Array [
94
+ "/@babel/plugin-proposal-pipeline-operator/lib/index.js",
95
+ Object {
96
+ "proposal": "minimal",
97
+ },
98
+ ],
99
+ Array [
100
+ "/@babel/plugin-proposal-partial-application/lib/index.js",
101
+ ],
102
+ Array [
103
+ "/@babel/plugin-proposal-nullish-coalescing-operator/lib/index.js",
104
+ ],
105
+ Array [
106
+ "/babel-plugin-styled-components/lib/index.js",
107
+ Object {},
108
+ "styled-components",
109
+ ],
110
+ Array [
111
+ "/babel-plugin-transform-inline-environment-variables/lib/index.js",
112
+ Object {
113
+ "include": Array [],
114
+ },
115
+ ],
116
+ Array [
117
+ "/babel-plugin-module-resolver/lib/index.js",
118
+ Object {
119
+ "alias": Object {
120
+ "@/*": Array [
121
+ "./src/*",
122
+ ],
123
+ },
124
+ "extensions": Array [
125
+ ".ts",
126
+ ".tsx",
127
+ ".js",
128
+ ".jsx",
129
+ ".es",
130
+ ".es6",
131
+ ".mjs",
132
+ ],
133
+ "resolvePath": [Function],
134
+ "root": "/packages/server/utils/tests/fixtures/",
135
+ },
136
+ ],
137
+ Array [
138
+ "/babel-plugin-transform-typescript-metadata/lib/plugin.js",
139
+ ],
140
+ ],
141
+ "presets": Array [
142
+ Array [
143
+ "/@babel/preset-env/lib/index.js",
144
+ Object {
145
+ "bugfixes": true,
146
+ "modules": "commonjs",
147
+ "shippedProposals": false,
148
+ "targets": Array [
149
+ "chrome > 61",
150
+ "edge > 16",
151
+ "firefox > 60",
152
+ "safari > 11",
153
+ "ios_saf > 11",
154
+ ],
155
+ },
156
+ ],
157
+ Array [
158
+ "/@babel/preset-react/lib/index.js",
159
+ Object {
160
+ "runtime": "automatic",
161
+ },
162
+ ],
163
+ Array [
164
+ "/@babel/preset-typescript/lib/index.js",
165
+ Object {
166
+ "allowDeclareFields": true,
167
+ "allowNamespaces": true,
168
+ },
169
+ ],
170
+ ],
171
+ }
172
+ `;
@@ -0,0 +1,31 @@
1
+ import path from 'path';
2
+ import { defaults } from './helpers';
3
+ import { resolveBabelConfig } from '@/index';
4
+
5
+ describe('babel', () => {
6
+ it('resolveBabelConfig', () => {
7
+ const pwd = path.resolve(__dirname, './fixtures');
8
+ const tsconfigPath = path.resolve(
9
+ __dirname,
10
+ './fixtures/api/tsconfig.json',
11
+ );
12
+ const config = resolveBabelConfig(pwd, defaults as any, {
13
+ type: 'commonjs',
14
+ syntax: 'es6+',
15
+ tsconfigPath,
16
+ });
17
+
18
+ expect.addSnapshotSerializer({
19
+ test: val => typeof val === 'string' && val.includes('modern-js'),
20
+ print: val =>
21
+ // eslint-disable-next-line no-nested-ternary
22
+ typeof val === 'string'
23
+ ? val.includes('node_modules')
24
+ ? `"${val.replace(/.+node_modules/, '')}"`
25
+ : `"${val.replace(/.+modern-js/, '')}"`
26
+ : (val as string),
27
+ });
28
+
29
+ expect(config).toMatchSnapshot();
30
+ });
31
+ });
@@ -0,0 +1,5 @@
1
+ import { hook } from '../../..';
2
+
3
+ export default hook(({ addMiddleware }) => {
4
+ addMiddleware('@koa/api');
5
+ });
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": false,
4
+ "jsx": "preserve",
5
+ "baseUrl": "./",
6
+ "isolatedModules": true,
7
+ "experimentalDecorators": true,
8
+ "esModuleInterop": true,
9
+ "paths": {
10
+ "@/*": ["./src/*"]
11
+ }
12
+ },
13
+ "include": ["src", "api", "server", "electron"]
14
+ }
@@ -0,0 +1,5 @@
1
+ import { hook } from '../../..';
2
+
3
+ export default hook(({ addMiddleware }) => {
4
+ addMiddleware('@koa/web');
5
+ });