@modern-js/server-utils 1.2.12-beta.0 → 1.15.1-beta.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.
Files changed (27) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/js/modern/common/index.js +32 -0
  3. package/dist/js/modern/{babel.js → compilers/babel/index.js} +40 -11
  4. package/dist/js/modern/compilers/typescript/index.js +101 -0
  5. package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +138 -0
  6. package/dist/js/modern/compilers/typescript/typescript-loader.js +32 -0
  7. package/dist/js/modern/index.js +2 -1
  8. package/dist/js/node/common/index.js +50 -0
  9. package/dist/js/node/{babel.js → compilers/babel/index.js} +54 -14
  10. package/dist/js/node/compilers/typescript/index.js +123 -0
  11. package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +152 -0
  12. package/dist/js/node/compilers/typescript/typescript-loader.js +41 -0
  13. package/dist/js/node/index.js +14 -2
  14. package/dist/js/treeshaking/common/index.js +76 -0
  15. package/dist/js/treeshaking/compilers/babel/index.js +159 -0
  16. package/dist/js/treeshaking/compilers/typescript/index.js +176 -0
  17. package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +155 -0
  18. package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +39 -0
  19. package/dist/js/treeshaking/index.js +2 -1
  20. package/dist/types/common/index.d.ts +14 -0
  21. package/dist/types/{babel.d.ts → compilers/babel/index.d.ts} +3 -1
  22. package/dist/types/compilers/typescript/index.d.ts +2 -0
  23. package/dist/types/compilers/typescript/tsconfig-paths-plugin.d.ts +2 -0
  24. package/dist/types/compilers/typescript/typescript-loader.d.ts +11 -0
  25. package/dist/types/index.d.ts +2 -1
  26. package/package.json +13 -8
  27. package/dist/js/treeshaking/babel.js +0 -82
@@ -0,0 +1,155 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
4
+ import * as os from 'os';
5
+ import path, { dirname, posix } from 'path';
6
+ import { createMatchPath } from 'tsconfig-paths';
7
+
8
+ var isRegExpKey = function isRegExpKey(str) {
9
+ return str.startsWith('^') || str.endsWith('$');
10
+ };
11
+
12
+ var resolveAliasPath = function resolveAliasPath(baseUrl, filePath) {
13
+ if (path.isAbsolute(filePath)) {
14
+ return filePath;
15
+ }
16
+
17
+ return path.resolve(baseUrl, filePath);
18
+ };
19
+
20
+ var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
21
+ var aliasPairs = Object.keys(alias).reduce(function (o, key) {
22
+ if (isRegExpKey(key)) {
23
+ var regexp = new RegExp(key);
24
+ var aliasPath = resolveAliasPath(baseUrl, alias[key]);
25
+ o.push([regexp, aliasPath]);
26
+ } else {
27
+ var _aliasPath = resolveAliasPath(baseUrl, alias[key]);
28
+
29
+ o.push([key, _aliasPath]);
30
+ }
31
+
32
+ return o;
33
+ }, []);
34
+ return function (requestedModule) {
35
+ var _iterator = _createForOfIteratorHelper(aliasPairs),
36
+ _step;
37
+
38
+ try {
39
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
40
+ var _step$value = _slicedToArray(_step.value, 2),
41
+ key = _step$value[0],
42
+ value = _step$value[1];
43
+
44
+ if (key instanceof RegExp) {
45
+ if (key.test(requestedModule)) {
46
+ return value;
47
+ }
48
+ }
49
+
50
+ if (requestedModule === key) {
51
+ return value;
52
+ }
53
+ }
54
+ } catch (err) {
55
+ _iterator.e(err);
56
+ } finally {
57
+ _iterator.f();
58
+ }
59
+ };
60
+ };
61
+
62
+ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
63
+ var tsPaths = {};
64
+ var alias = {};
65
+ Object.keys(paths).forEach(function (key) {
66
+ if (Array.isArray(paths[key])) {
67
+ tsPaths[key] = paths[key];
68
+ } else {
69
+ alias[key] = paths[key];
70
+ }
71
+ });
72
+ var matchAliasPath = createAliasMatcher(baseUrl, alias);
73
+ var matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
74
+
75
+ var matchPath = function matchPath(requestedModule, readJSONSync, fileExists, extensions) {
76
+ var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
77
+
78
+ if (result) {
79
+ return result;
80
+ }
81
+
82
+ return matchAliasPath(requestedModule);
83
+ };
84
+
85
+ if (Object.keys(paths).length === 0) {
86
+ return undefined;
87
+ }
88
+
89
+ return function (ctx) {
90
+ return function (sf) {
91
+ var visitNode = function visitNode(node) {
92
+ if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
93
+ try {
94
+ var importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
95
+
96
+ if (!importPathWithQuotes) {
97
+ return node;
98
+ }
99
+
100
+ var text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
101
+ var result = getNotAliasedPath(sf, matchPath, text);
102
+
103
+ if (!result) {
104
+ return node;
105
+ }
106
+
107
+ var moduleSpecifier = tsBinary.factory.createStringLiteral(result);
108
+ moduleSpecifier.parent = node.moduleSpecifier.parent;
109
+
110
+ if (tsBinary.isImportDeclaration(node)) {
111
+ return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
112
+ } else {
113
+ return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
114
+ }
115
+ } catch (_unused) {
116
+ return node;
117
+ }
118
+ }
119
+
120
+ return tsBinary.visitEachChild(node, visitNode, ctx);
121
+ };
122
+
123
+ return tsBinary.visitNode(sf, visitNode);
124
+ };
125
+ };
126
+ } // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
127
+
128
+ function getNotAliasedPath(sf, matcher, text) {
129
+ var result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
130
+
131
+ if (!result) {
132
+ return;
133
+ }
134
+
135
+ if (os.platform() === 'win32') {
136
+ result = result.replace(/\\/g, '/');
137
+ }
138
+
139
+ if (!path.isAbsolute(result)) {
140
+ try {
141
+ // Installed packages (node modules) should take precedence over root files with the same name.
142
+ // Ref: https://github.com/nestjs/nest-cli/issues/838
143
+ var packagePath = require.resolve(text, {
144
+ paths: [process.cwd()].concat(_toConsumableArray(module.paths))
145
+ });
146
+
147
+ if (packagePath) {
148
+ return text;
149
+ }
150
+ } catch (_unused2) {}
151
+ }
152
+
153
+ var resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
154
+ return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
155
+ }
@@ -0,0 +1,39 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
+ export var TypescriptLoader = /*#__PURE__*/function () {
5
+ function TypescriptLoader(_ref) {
6
+ var appDirectory = _ref.appDirectory;
7
+
8
+ _classCallCheck(this, TypescriptLoader);
9
+
10
+ _defineProperty(this, "tsBinary", void 0);
11
+
12
+ _defineProperty(this, "appDirectory", void 0);
13
+
14
+ this.appDirectory = appDirectory;
15
+ }
16
+
17
+ _createClass(TypescriptLoader, [{
18
+ key: "load",
19
+ value: function load() {
20
+ if (this.tsBinary) {
21
+ return this.tsBinary;
22
+ }
23
+
24
+ try {
25
+ var tsPath = require.resolve('typescript', {
26
+ paths: [this.appDirectory || process.cwd()]
27
+ });
28
+
29
+ var _ts = require(tsPath);
30
+
31
+ return _ts;
32
+ } catch (error) {
33
+ throw new Error('TypeScript could not be found! Please, install "typescript" package.');
34
+ }
35
+ }
36
+ }]);
37
+
38
+ return TypescriptLoader;
39
+ }();
@@ -1 +1,2 @@
1
- export * from "./babel";
1
+ export * from "./compilers/babel";
2
+ export { compile } from "./common";
@@ -0,0 +1,14 @@
1
+ import type { NormalizedConfig } from '@modern-js/core';
2
+ export interface Pattern {
3
+ from: string;
4
+ to: string;
5
+ tsconfigPath?: string;
6
+ }
7
+ export interface CompileOptions {
8
+ sourceDirs: string[];
9
+ distDir: string;
10
+ tsconfigPath?: string;
11
+ }
12
+ export declare type CompileFunc = (appDirectory: string, modernConfig: NormalizedConfig, compileOptions: CompileOptions) => Promise<void>;
13
+ export declare const FILE_EXTENSIONS: string[];
14
+ export declare const compile: CompileFunc;
@@ -1,6 +1,7 @@
1
1
  import { ILibPresetOption, ISyntaxOption } from '@modern-js/babel-preset-lib';
2
2
  import { TransformOptions } from '@babel/core';
3
3
  import type { NormalizedConfig } from '@modern-js/core';
4
+ import { CompileFunc } from '../../common';
4
5
  export * from '@babel/core';
5
6
  export interface ITsconfig {
6
7
  compilerOptions?: {
@@ -24,4 +25,5 @@ export interface IPackageModeValue {
24
25
  syntax: 'es5' | 'es6+';
25
26
  tsconfigPath: string;
26
27
  }
27
- export declare const resolveBabelConfig: (appDirectory: string, modernConfig: NormalizedConfig, option: IPackageModeValue) => any;
28
+ export declare const resolveBabelConfig: (appDirectory: string, modernConfig: NormalizedConfig, option: IPackageModeValue) => any;
29
+ export declare const compileByBabel: CompileFunc;
@@ -0,0 +1,2 @@
1
+ import type { CompileFunc } from '../../common';
2
+ export declare const compileByTs: CompileFunc;
@@ -0,0 +1,2 @@
1
+ import * as ts from 'typescript';
2
+ export declare function tsconfigPathsBeforeHookFactory(tsBinary: typeof ts, baseUrl: string, paths: Record<string, string[] | string>): ((ctx: ts.TransformationContext) => ts.Transformer<any>) | undefined;
@@ -0,0 +1,11 @@
1
+ import type ts from 'typescript';
2
+ export declare class TypescriptLoader {
3
+ private tsBinary?;
4
+ private appDirectory?;
5
+ constructor({
6
+ appDirectory
7
+ }: {
8
+ appDirectory: string;
9
+ });
10
+ load(): typeof ts;
11
+ }
@@ -1 +1,2 @@
1
- export * from './babel';
1
+ export * from './compilers/babel';
2
+ export { compile } from './common';
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.12-beta.0",
14
+ "version": "1.15.1-beta.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -35,21 +35,25 @@
35
35
  "@babel/preset-env": "^7.18.0",
36
36
  "@babel/preset-typescript": "^7.17.12",
37
37
  "@babel/runtime": "^7.18.0",
38
- "@modern-js/babel-preset-lib": "^1.3.1",
39
- "@modern-js/plugin": "^1.3.7",
40
- "@modern-js/utils": "^1.7.7",
38
+ "@modern-js/babel-compiler": "^1.15.0",
39
+ "@modern-js/babel-preset-lib": "1.15.0",
40
+ "@modern-js/plugin": "1.15.0",
41
+ "@modern-js/utils": "1.15.0",
41
42
  "babel-plugin-module-resolver": "^4.1.0",
42
- "babel-plugin-transform-typescript-metadata": "^0.3.2"
43
+ "babel-plugin-transform-typescript-metadata": "^0.3.2",
44
+ "recursive-readdir": "2.2.2",
45
+ "tsconfig-paths": "3.14.1"
43
46
  },
44
47
  "devDependencies": {
45
- "@modern-js/core": "1.12.0",
46
- "@scripts/build": "0.0.0",
47
- "@scripts/jest-config": "0.0.0",
48
+ "@modern-js/core": "1.15.0",
49
+ "@scripts/build": "1.15.0",
50
+ "@scripts/jest-config": "1.15.0",
48
51
  "@types/babel__core": "^7.1.15",
49
52
  "@types/jest": "^27",
50
53
  "@types/node": "^14",
51
54
  "@types/react": "^17",
52
55
  "@types/react-dom": "^17",
56
+ "@types/recursive-readdir": "2.2.0",
53
57
  "jest": "^27",
54
58
  "ts-jest": "^27.0.4",
55
59
  "typescript": "^4"
@@ -85,6 +89,7 @@
85
89
  },
86
90
  "scripts": {
87
91
  "new": "modern new",
92
+ "dev": "modern build --watch",
88
93
  "build": "wireit",
89
94
  "test": "wireit"
90
95
  },
@@ -1,82 +0,0 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
3
- import { fs, json5, getAlias, applyOptionsChain } from '@modern-js/utils';
4
- export * from '@babel/core';
5
- export var readTsConfig = function readTsConfig(tsconfigPath) {
6
- var noExistReturn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
7
-
8
- // 如果不存在,则返回 noExistReturn
9
- if (!fs.existsSync(tsconfigPath)) {
10
- return noExistReturn;
11
- }
12
-
13
- var content = fs.readFileSync(tsconfigPath, 'utf-8');
14
- return json5.parse(content);
15
- };
16
- export var existTsConfigFile = function existTsConfigFile(tsconfigAbsolutePath) {
17
- var tsconfig = readTsConfig(tsconfigAbsolutePath);
18
- return Boolean(tsconfig);
19
- };
20
- export var getBabelConfig = function getBabelConfig(libPresetOption, syntaxOption) {
21
- var chain = getBabelChain(libPresetOption, syntaxOption);
22
- return _objectSpread({
23
- sourceType: 'unambiguous'
24
- }, chain.toJSON());
25
- };
26
- export var resolveBabelConfig = function resolveBabelConfig(appDirectory, modernConfig, option // FIXME: babel type can't pass type checking
27
- ) {
28
- var _modernConfig$source = modernConfig.source,
29
- envVars = _modernConfig$source.envVars,
30
- globalVars = _modernConfig$source.globalVars,
31
- _modernConfig$source$ = _modernConfig$source.jsxTransformRuntime,
32
- jsxTransformRuntime = _modernConfig$source$ === void 0 ? 'automatic' : _modernConfig$source$,
33
- userLodashOption = modernConfig.tools.lodash; // alias config
34
-
35
- var aliasConfig = getAlias(modernConfig.source.alias, _objectSpread({
36
- appDirectory: appDirectory
37
- }, option)); // lodash config
38
-
39
- var lodashOptions = applyOptionsChain({
40
- id: ['lodash', 'ramda']
41
- }, // TODO: 需要处理类型问题
42
- userLodashOption); // babel config
43
-
44
- var babelChain = getBabelChain({
45
- appDirectory: appDirectory,
46
- enableReactPreset: true,
47
- enableTypescriptPreset: true,
48
- alias: aliasConfig,
49
- envVars: envVars,
50
- globalVars: globalVars,
51
- lodashOptions: lodashOptions,
52
- jsxTransformRuntime: jsxTransformRuntime
53
- }, {
54
- type: option.type,
55
- syntax: option.syntax
56
- });
57
- var envOptions = babelChain.preset('@babel/preset-env').options();
58
- babelChain.preset('@babel/preset-env').use(require.resolve('@babel/preset-env'), [_objectSpread(_objectSpread({}, envOptions[0]), {}, {
59
- targets: {
60
- node: '12.18.3'
61
- },
62
- loose: true
63
- })]);
64
- babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
65
- babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
66
- legacy: true
67
- }]); // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
68
- // babelChain
69
- // .plugin('@babel/plugin-proposal-class-properties')
70
- // .use(require.resolve('@babel/plugin-proposal-class-properties'), [
71
- // {
72
- // loose: true,
73
- // },
74
- // ]);
75
-
76
- babelChain.plugin('@babel/plugin-syntax-class-properties').use(require.resolve('@babel/plugin-syntax-class-properties'), []);
77
-
78
- var internalBabelConfig = _objectSpread({}, babelChain.toJSON());
79
-
80
- var userBabelConfig = modernConfig.tools.babel;
81
- return applyUserBabelConfig(internalBabelConfig, userBabelConfig);
82
- };