@modern-js/server-utils 1.1.1 → 1.2.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,52 @@
1
1
  # @modern-js/server-utils
2
2
 
3
+ ## 1.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+ - Updated dependencies [83166714]
9
+ - @modern-js/babel-preset-lib@1.2.1
10
+ - @modern-js/plugin@1.2.1
11
+ - @modern-js/utils@1.2.2
12
+
13
+ ## 1.2.0
14
+
15
+ ### Minor Changes
16
+
17
+ - cfe11628: Make Modern.js self bootstraping
18
+
19
+ ### Patch Changes
20
+
21
+ - 146dcd85: modify server framework plugin hook types and hook context
22
+ - 146dcd85: modify server framework plugin hook types
23
+ - 146dcd85: fix test case in babel compiler
24
+ - 1ebc7ee2: fix: @babel/core version
25
+ - Updated dependencies [2da09c69]
26
+ - Updated dependencies [c3d46ee4]
27
+ - Updated dependencies [cfe11628]
28
+ - Updated dependencies [1ebc7ee2]
29
+ - @modern-js/utils@1.2.0
30
+ - @modern-js/babel-preset-lib@1.2.0
31
+ - @modern-js/plugin@1.2.0
32
+
33
+ ## 1.1.2
34
+
35
+ ### Patch Changes
36
+
37
+ - 085a6a58: refactor server plugin
38
+ - 085a6a58: refactor server plugin
39
+ - 085a6a58: refactor server conifg
40
+ - 085a6a58: support server runtime
41
+ - 085a6a58: feat: refactor server plugin
42
+ - Updated dependencies [085a6a58]
43
+ - Updated dependencies [085a6a58]
44
+ - Updated dependencies [085a6a58]
45
+ - Updated dependencies [d280ea33]
46
+ - Updated dependencies [085a6a58]
47
+ - Updated dependencies [085a6a58]
48
+ - @modern-js/utils@1.1.3
49
+
3
50
  ## 1.1.1
4
51
 
5
52
  ### Patch Changes
@@ -1,6 +1,6 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
2
 
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
4
 
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
@@ -1,3 +1 @@
1
- export * from "./middleware";
2
- export * from "./gather";
3
1
  export * from "./babel";
@@ -33,9 +33,9 @@ Object.keys(_core).forEach(function (key) {
33
33
 
34
34
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
35
 
36
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
36
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
37
37
 
38
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
38
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
39
39
 
40
40
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
41
41
 
@@ -4,32 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
 
7
- var _middleware = require("./middleware");
8
-
9
- Object.keys(_middleware).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- if (key in exports && exports[key] === _middleware[key]) return;
12
- Object.defineProperty(exports, key, {
13
- enumerable: true,
14
- get: function () {
15
- return _middleware[key];
16
- }
17
- });
18
- });
19
-
20
- var _gather = require("./gather");
21
-
22
- Object.keys(_gather).forEach(function (key) {
23
- if (key === "default" || key === "__esModule") return;
24
- if (key in exports && exports[key] === _gather[key]) return;
25
- Object.defineProperty(exports, key, {
26
- enumerable: true,
27
- get: function () {
28
- return _gather[key];
29
- }
30
- });
31
- });
32
-
33
7
  var _babel = require("./babel");
34
8
 
35
9
  Object.keys(_babel).forEach(function (key) {
@@ -1,6 +1,6 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
2
 
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
4
 
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
@@ -1,3 +1 @@
1
- export * from "./middleware";
2
- export * from "./gather";
3
1
  export * from "./babel";
@@ -1,3 +1 @@
1
- export * from './middleware';
2
- export * from './gather';
3
1
  export * from './babel';
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
+ ...sharedConfig,
7
+ rootDir: __dirname,
8
+ };
package/modern.config.js CHANGED
@@ -1,15 +1,2 @@
1
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
- };
2
+ module.exports = {};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.1",
14
+ "version": "1.2.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -20,6 +20,7 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "node": {
23
+ "jsnext:source": "./src/index.ts",
23
24
  "import": "./dist/js/modern/index.js",
24
25
  "require": "./dist/js/node/index.js"
25
26
  },
@@ -28,21 +29,21 @@
28
29
  },
29
30
  "dependencies": {
30
31
  "@babel/compat-data": "^7.15.0",
31
- "@babel/core": "^7.15.0",
32
+ "@babel/core": "7.16.7",
32
33
  "@babel/plugin-proposal-class-properties": "^7.14.5",
33
34
  "@babel/plugin-proposal-decorators": "^7.15.4",
34
35
  "@babel/preset-env": "^7.15.0",
35
36
  "@babel/preset-typescript": "^7.15.0",
36
37
  "@babel/runtime": "^7",
37
- "@modern-js/babel-preset-lib": "^1.1.1",
38
- "@modern-js/plugin": "^1.1.2",
39
- "@modern-js/utils": "^1.1.2",
38
+ "@modern-js/babel-preset-lib": "^1.2.1",
39
+ "@modern-js/plugin": "^1.2.1",
40
+ "@modern-js/utils": "^1.2.2",
40
41
  "babel-plugin-module-resolver": "^4.1.0",
41
42
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
42
43
  "json5": "^2.2.0"
43
44
  },
44
45
  "devDependencies": {
45
- "@modern-js/core": "^1.1.2",
46
+ "@modern-js/core": "^1.3.2",
46
47
  "@types/babel__core": "^7.1.15",
47
48
  "@types/jest": "^26",
48
49
  "@types/node": "^14",
@@ -50,17 +51,20 @@
50
51
  "@types/react-dom": "^17",
51
52
  "ts-jest": "^27.0.4",
52
53
  "typescript": "^4",
53
- "@modern-js/plugin-testing": "^1.1.0",
54
- "@modern-js/module-tools": "^1.1.0"
54
+ "@scripts/build": "0.0.0",
55
+ "jest": "^27",
56
+ "@scripts/jest-config": "0.0.0"
55
57
  },
56
58
  "sideEffects": false,
57
59
  "publishConfig": {
58
60
  "registry": "https://registry.npmjs.org/",
59
- "access": "public"
61
+ "access": "public",
62
+ "types": "./dist/types/index.d.ts"
60
63
  },
61
64
  "scripts": {
62
65
  "new": "modern new",
63
66
  "build": "modern build",
64
- "test": "modern test --passWithNoTests"
65
- }
67
+ "test": "jest --passWithNoTests"
68
+ },
69
+ "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
66
70
  }
@@ -63,10 +63,10 @@ Object {
63
63
  "/@babel/plugin-transform-runtime/lib/index.js",
64
64
  Object {
65
65
  "corejs": false,
66
- "helpers": true,
66
+ "helpers": false,
67
67
  "regenerator": true,
68
68
  "useESModules": false,
69
- "version": "7.16.3",
69
+ "version": "7.16.7",
70
70
  },
71
71
  ],
72
72
  Array [
@@ -166,6 +166,7 @@ Object {
166
166
  "allExtensions": true,
167
167
  "allowDeclareFields": true,
168
168
  "allowNamespaces": true,
169
+ "isTSX": true,
169
170
  },
170
171
  ],
171
172
  ],
@@ -1,6 +1,6 @@
1
1
  import path from 'path';
2
+ import { resolveBabelConfig } from '../src';
2
3
  import { defaults } from './helpers';
3
- import { resolveBabelConfig } from '@/index';
4
4
 
5
5
  describe('babel', () => {
6
6
  it('resolveBabelConfig', () => {
@@ -1,4 +1,4 @@
1
- import { hook } from '../../..';
1
+ import { hook } from '../../../src';
2
2
 
3
3
  export default hook(({ addMiddleware }) => {
4
4
  addMiddleware('@koa/api');
@@ -1,4 +1,4 @@
1
- import { hook } from '../../..';
1
+ import { hook } from '../../../src';
2
2
 
3
3
  export default hook(({ addMiddleware }) => {
4
4
  addMiddleware('@koa/web');
@@ -5,9 +5,7 @@
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
7
  "isolatedModules": true,
8
- "paths": {
9
- "@/*": ["../src/*"]
10
- },
8
+ "paths": {},
11
9
  "types": ["node", "jest"]
12
10
  }
13
11
  }
package/tsconfig.json CHANGED
@@ -5,9 +5,7 @@
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
7
  "isolatedModules": true,
8
- "paths": {
9
- "@/*": ["./src/*"]
10
- },
8
+ "paths": {},
11
9
  "types": ["node", "jest"]
12
10
  },
13
11
  "include": ["src"]
@@ -1,35 +0,0 @@
1
- import path from 'path';
2
- import { createMiddlewareCollecter } from "./middleware";
3
- import { requireModule } from "./requireModule";
4
- const API_DIR_PATH = 'api';
5
- const API_APP_NAME = '_app';
6
- const SERVER_DIR_PATH = 'server';
7
- const WEB_APP_NAME = 'index';
8
- export const gather = pwd => {
9
- const {
10
- getMiddlewares,
11
- addAPIMiddleware,
12
- addWebMiddleware
13
- } = createMiddlewareCollecter();
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
- const apiAttacher = requireModule(apiAppPath);
19
-
20
- if (apiAttacher) {
21
- apiAttacher({
22
- addMiddleware: addAPIMiddleware
23
- });
24
- }
25
-
26
- const webAttacher = requireModule(webAppPath);
27
-
28
- if (webAttacher) {
29
- webAttacher({
30
- addMiddleware: addWebMiddleware
31
- });
32
- }
33
-
34
- return getMiddlewares();
35
- };
@@ -1,31 +0,0 @@
1
- export const createMiddlewareCollecter = () => {
2
- const webMiddlewares = [];
3
- const apiMiddlewares = [];
4
- const ssrMiddlewares = [];
5
-
6
- const addWebMiddleware = input => {
7
- webMiddlewares.push(input);
8
- };
9
-
10
- const addAPIMiddleware = input => {
11
- apiMiddlewares.push(input);
12
- };
13
-
14
- const addSSRMiddleware = input => {
15
- ssrMiddlewares.push(input);
16
- };
17
-
18
- const getMiddlewares = () => ({
19
- web: webMiddlewares,
20
- api: apiMiddlewares,
21
- ssr: ssrMiddlewares
22
- });
23
-
24
- return {
25
- getMiddlewares,
26
- addWebMiddleware,
27
- addAPIMiddleware,
28
- addSSRMiddleware
29
- };
30
- };
31
- export const hook = attacher => attacher;
@@ -1,24 +0,0 @@
1
- import { findExists } from '@modern-js/utils';
2
- const FILE_EXTENSIONS = ['.ts', '.js'];
3
- export const requireModule = filename => {
4
- const exist = findExists(FILE_EXTENSIONS.map(ext => `${filename}${ext}`));
5
-
6
- if (!exist) {
7
- return null;
8
- } // throw errors directly if require fail
9
-
10
-
11
- const mod = require(exist);
12
-
13
- return interopRequire(mod);
14
- };
15
-
16
- function interopRequire(obj) {
17
- return interopRequireDefault(obj).default;
18
- }
19
-
20
- function interopRequireDefault(obj) {
21
- return obj !== null && obj !== void 0 && obj.__esModule ? obj : {
22
- default: obj
23
- };
24
- }
@@ -1,55 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.gather = void 0;
7
-
8
- var _path = _interopRequireDefault(require("path"));
9
-
10
- var _middleware = require("./middleware");
11
-
12
- var _requireModule = require("./requireModule");
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- const API_DIR_PATH = 'api';
17
- const API_APP_NAME = '_app';
18
- const SERVER_DIR_PATH = 'server';
19
- const WEB_APP_NAME = 'index';
20
-
21
- const gather = pwd => {
22
- const {
23
- getMiddlewares,
24
- addAPIMiddleware,
25
- addWebMiddleware
26
- } = (0, _middleware.createMiddlewareCollecter)();
27
-
28
- const apiPath = _path.default.resolve(pwd, API_DIR_PATH);
29
-
30
- const apiAppPath = _path.default.resolve(apiPath, API_APP_NAME);
31
-
32
- const serverPath = _path.default.resolve(pwd, SERVER_DIR_PATH);
33
-
34
- const webAppPath = _path.default.resolve(serverPath, WEB_APP_NAME);
35
-
36
- const apiAttacher = (0, _requireModule.requireModule)(apiAppPath);
37
-
38
- if (apiAttacher) {
39
- apiAttacher({
40
- addMiddleware: addAPIMiddleware
41
- });
42
- }
43
-
44
- const webAttacher = (0, _requireModule.requireModule)(webAppPath);
45
-
46
- if (webAttacher) {
47
- webAttacher({
48
- addMiddleware: addWebMiddleware
49
- });
50
- }
51
-
52
- return getMiddlewares();
53
- };
54
-
55
- exports.gather = gather;
@@ -1,43 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.hook = exports.createMiddlewareCollecter = void 0;
7
-
8
- const createMiddlewareCollecter = () => {
9
- const webMiddlewares = [];
10
- const apiMiddlewares = [];
11
- const ssrMiddlewares = [];
12
-
13
- const addWebMiddleware = input => {
14
- webMiddlewares.push(input);
15
- };
16
-
17
- const addAPIMiddleware = input => {
18
- apiMiddlewares.push(input);
19
- };
20
-
21
- const addSSRMiddleware = input => {
22
- ssrMiddlewares.push(input);
23
- };
24
-
25
- const getMiddlewares = () => ({
26
- web: webMiddlewares,
27
- api: apiMiddlewares,
28
- ssr: ssrMiddlewares
29
- });
30
-
31
- return {
32
- getMiddlewares,
33
- addWebMiddleware,
34
- addAPIMiddleware,
35
- addSSRMiddleware
36
- };
37
- };
38
-
39
- exports.createMiddlewareCollecter = createMiddlewareCollecter;
40
-
41
- const hook = attacher => attacher;
42
-
43
- exports.hook = hook;
@@ -1,35 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.requireModule = void 0;
7
-
8
- var _utils = require("@modern-js/utils");
9
-
10
- const FILE_EXTENSIONS = ['.ts', '.js'];
11
-
12
- const requireModule = filename => {
13
- const exist = (0, _utils.findExists)(FILE_EXTENSIONS.map(ext => `${filename}${ext}`));
14
-
15
- if (!exist) {
16
- return null;
17
- } // throw errors directly if require fail
18
-
19
-
20
- const mod = require(exist);
21
-
22
- return interopRequire(mod);
23
- };
24
-
25
- exports.requireModule = requireModule;
26
-
27
- function interopRequire(obj) {
28
- return interopRequireDefault(obj).default;
29
- }
30
-
31
- function interopRequireDefault(obj) {
32
- return obj !== null && obj !== void 0 && obj.__esModule ? obj : {
33
- default: obj
34
- };
35
- }
@@ -1,35 +0,0 @@
1
- import path from 'path';
2
- import { createMiddlewareCollecter } from "./middleware";
3
- import { requireModule } from "./requireModule";
4
- var API_DIR_PATH = 'api';
5
- var API_APP_NAME = '_app';
6
- var SERVER_DIR_PATH = 'server';
7
- var WEB_APP_NAME = 'index';
8
- export var gather = function gather(pwd) {
9
- var _createMiddlewareColl = createMiddlewareCollecter(),
10
- getMiddlewares = _createMiddlewareColl.getMiddlewares,
11
- addAPIMiddleware = _createMiddlewareColl.addAPIMiddleware,
12
- addWebMiddleware = _createMiddlewareColl.addWebMiddleware;
13
-
14
- var apiPath = path.resolve(pwd, API_DIR_PATH);
15
- var apiAppPath = path.resolve(apiPath, API_APP_NAME);
16
- var serverPath = path.resolve(pwd, SERVER_DIR_PATH);
17
- var webAppPath = path.resolve(serverPath, WEB_APP_NAME);
18
- var apiAttacher = requireModule(apiAppPath);
19
-
20
- if (apiAttacher) {
21
- apiAttacher({
22
- addMiddleware: addAPIMiddleware
23
- });
24
- }
25
-
26
- var webAttacher = requireModule(webAppPath);
27
-
28
- if (webAttacher) {
29
- webAttacher({
30
- addMiddleware: addWebMiddleware
31
- });
32
- }
33
-
34
- return getMiddlewares();
35
- };
@@ -1,35 +0,0 @@
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
- };
@@ -1,26 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export declare const gather: (pwd: string) => import("./middleware").CollectMiddlewaresResult;
@@ -1,16 +0,0 @@
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;
@@ -1 +0,0 @@
1
- export declare const requireModule: (filename: string) => any;
package/src/babel.ts DELETED
@@ -1,151 +0,0 @@
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.plugin('babel-plugin-transform-typescript-metadata').use(
123
- require.resolve('babel-plugin-transform-typescript-metadata'),
124
-
125
- [],
126
- );
127
-
128
- babelChain
129
- .plugin('@babel/plugin-proposal-decorators')
130
- .use(require.resolve('@babel/plugin-proposal-decorators'), [
131
- { legacy: true },
132
- ]);
133
-
134
- babelChain.plugin('@babel/plugin-proposal-class-properties').use(
135
- require.resolve('@babel/plugin-proposal-class-properties'),
136
-
137
- [{ loose: true }],
138
- );
139
-
140
- const internalBabelConfig = { ...babelChain.toJSON() };
141
-
142
- const userBabelConfig = modernConfig.tools.babel;
143
- applyOptionsChain(
144
- internalBabelConfig,
145
- // TODO: 感觉 userBabelConfig 的类型应该是TransformOptions
146
- userBabelConfig as any,
147
- { chain: babelChain },
148
- );
149
-
150
- return internalBabelConfig;
151
- };
package/src/gather.ts DELETED
@@ -1,30 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- export * from './middleware';
2
- export * from './gather';
3
- export * from './babel';
package/src/middleware.ts DELETED
@@ -1,43 +0,0 @@
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;
@@ -1,23 +0,0 @@
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
- }
@@ -1,22 +0,0 @@
1
- import path from 'path';
2
- import { gather } from '@/index';
3
-
4
- describe('@modern-js/server-utils', () => {
5
- it('should work well', () => {
6
- const pwd = path.resolve(__dirname, './fixtures');
7
- const result = gather(pwd);
8
-
9
- expect(result.api.length).toBe(1);
10
- expect(result.api[0]).toBe('@koa/api');
11
- expect(result.web.length).toBe(1);
12
- expect(result.web[0]).toBe('@koa/web');
13
- });
14
-
15
- it('should get empty when pass a empty dir', () => {
16
- const pwd = path.resolve(__dirname, './fixtures/empty');
17
- const result = gather(pwd);
18
-
19
- expect(result.api.length).toBe(0);
20
- expect(result.web.length).toBe(0);
21
- });
22
- });