@modern-js/server-utils 2.25.2 → 2.26.0

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,15 @@
1
1
  # @modern-js/server-utils
2
2
 
3
+ ## 2.26.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 1fb5804: fix: should load typescript from direct dependencies on demand
8
+ fix: 按需从直接依赖中加载 typescript
9
+ - @modern-js/babel-preset-base@2.26.0
10
+ - @modern-js/babel-compiler@2.26.0
11
+ - @modern-js/utils@2.26.0
12
+
3
13
  ## 2.25.2
4
14
 
5
15
  ### Patch Changes
@@ -20,8 +20,6 @@ _export(exports, {
20
20
  const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
21
21
  const _path = /* @__PURE__ */ _interop_require_wildcard._(require("path"));
22
22
  const _utils = require("@modern-js/utils");
23
- const _typescript = require("../compilers/typescript");
24
- const _babel = require("../compilers/babel");
25
23
  const FILE_EXTENSIONS = [
26
24
  ".js",
27
25
  ".ts",
@@ -44,8 +42,10 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
44
42
  const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_server = modernConfig.server) === null || _modernConfig_server === void 0 ? void 0 : _modernConfig_server.compiler;
45
43
  const isTsProject = tsconfigPath && await _utils.fs.pathExists(tsconfigPath);
46
44
  if (!isTsProject || compiler === "babel") {
47
- await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
45
+ const { compileByBabel } = await Promise.resolve().then(() => /* @__PURE__ */ _interop_require_wildcard._(require("../compilers/babel")));
46
+ await compileByBabel(appDirectory, modernConfig, compileOptions);
48
47
  } else {
49
- await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
48
+ const { compileByTs } = await Promise.resolve().then(() => /* @__PURE__ */ _interop_require_wildcard._(require("../compilers/typescript")));
49
+ await compileByTs(appDirectory, modernConfig, compileOptions);
50
50
  }
51
51
  };
@@ -11,11 +11,10 @@ Object.defineProperty(exports, "compileByTs", {
11
11
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
12
12
  const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
13
13
  const _utils = require("@modern-js/utils");
14
- const _typescript = /* @__PURE__ */ _interop_require_default._(require("typescript"));
15
14
  const _typescriptLoader = require("./typescriptLoader");
16
15
  const _tsconfigPathsPlugin = require("./tsconfigPathsPlugin");
17
- const readTsConfigByFile = (tsConfigFile) => {
18
- const parsedCmd = _typescript.default.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, _typescript.default.sys);
16
+ const readTsConfigByFile = (tsConfigFile, tsInstance) => {
17
+ const parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
19
18
  const { options, fileNames, projectReferences } = parsedCmd;
20
19
  return {
21
20
  options,
@@ -51,7 +50,7 @@ const compileByTs = async (appDirectory, config, compileOptions) => {
51
50
  tsconfigPath
52
51
  });
53
52
  const { paths = {}, absoluteBaseUrl = "./" } = aliasOption;
54
- const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath);
53
+ const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath, ts);
55
54
  const sourcePosixPaths = sourceDirs.map((sourceDir) => sourceDir.split(_path.default.sep).join(_path.default.posix.sep));
56
55
  const rootNames = fileNames.filter((fileName) => {
57
56
  return fileName.endsWith(".d.ts") || sourcePosixPaths.some((sourceDir) => {
@@ -2,8 +2,6 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import * as path from "path";
4
4
  import { fs } from "@modern-js/utils";
5
- import { compileByTs } from "../compilers/typescript";
6
- import { compileByBabel } from "../compilers/babel";
7
5
  export var FILE_EXTENSIONS = [
8
6
  ".js",
9
7
  ".ts",
@@ -22,7 +20,7 @@ var validateAbsolutePaths = function(filenames, messageFunc) {
22
20
  };
23
21
  export var compile = function() {
24
22
  var _ref = _async_to_generator(function(appDirectory, modernConfig, compileOptions) {
25
- var _modernConfig_server, sourceDirs, distDir, tsconfigPath, compiler, isTsProject, _tmp;
23
+ var _modernConfig_server, sourceDirs, distDir, tsconfigPath, compiler, isTsProject, _tmp, compileByBabel, compileByTs;
26
24
  return _ts_generator(this, function(_state) {
27
25
  switch (_state.label) {
28
26
  case 0:
@@ -50,27 +48,39 @@ export var compile = function() {
50
48
  if (!(!isTsProject || compiler === "babel"))
51
49
  return [
52
50
  3,
53
- 4
51
+ 5
54
52
  ];
55
53
  return [
56
54
  4,
57
- compileByBabel(appDirectory, modernConfig, compileOptions)
55
+ import("../compilers/babel")
58
56
  ];
59
57
  case 3:
58
+ compileByBabel = _state.sent().compileByBabel;
59
+ return [
60
+ 4,
61
+ compileByBabel(appDirectory, modernConfig, compileOptions)
62
+ ];
63
+ case 4:
60
64
  _state.sent();
61
65
  return [
62
66
  3,
63
- 6
67
+ 8
64
68
  ];
65
- case 4:
69
+ case 5:
70
+ return [
71
+ 4,
72
+ import("../compilers/typescript")
73
+ ];
74
+ case 6:
75
+ compileByTs = _state.sent().compileByTs;
66
76
  return [
67
77
  4,
68
78
  compileByTs(appDirectory, modernConfig, compileOptions)
69
79
  ];
70
- case 5:
80
+ case 7:
71
81
  _state.sent();
72
- _state.label = 6;
73
- case 6:
82
+ _state.label = 8;
83
+ case 8:
74
84
  return [
75
85
  2
76
86
  ];
@@ -4,11 +4,10 @@ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
5
5
  import path from "path";
6
6
  import { logger, getAliasConfig, fs } from "@modern-js/utils";
7
- import ts from "typescript";
8
7
  import { TypescriptLoader } from "./typescriptLoader";
9
8
  import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin";
10
- var readTsConfigByFile = function(tsConfigFile) {
11
- var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, ts.sys);
9
+ var readTsConfigByFile = function(tsConfigFile, tsInstance) {
10
+ var parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
12
11
  var options = parsedCmd.options, fileNames = parsedCmd.fileNames, projectReferences = parsedCmd.projectReferences;
13
12
  return {
14
13
  options: options,
@@ -60,7 +59,7 @@ var copyFiles = function() {
60
59
  }();
61
60
  export var compileByTs = function() {
62
61
  var _ref = _async_to_generator(function(appDirectory, config, compileOptions) {
63
- var sourceDirs, distDir, tsconfigPath, ts2, createProgram, formatHost, alias, aliasOption, _aliasOption_paths, paths, _aliasOption_absoluteBaseUrl, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, noEmitOnError, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
62
+ var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption_paths, paths, _aliasOption_absoluteBaseUrl, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, noEmitOnError, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
64
63
  return _ts_generator(this, function(_state) {
65
64
  switch (_state.label) {
66
65
  case 0:
@@ -71,18 +70,18 @@ export var compileByTs = function() {
71
70
  2
72
71
  ];
73
72
  }
74
- ts2 = new TypescriptLoader({
73
+ ts = new TypescriptLoader({
75
74
  appDirectory: appDirectory
76
75
  }).load();
77
- createProgram = ts2.createIncrementalProgram || ts2.createProgram;
78
- formatHost = getFormatHost(ts2);
76
+ createProgram = ts.createIncrementalProgram || ts.createProgram;
77
+ formatHost = getFormatHost(ts);
79
78
  alias = config.alias;
80
79
  aliasOption = getAliasConfig(alias, {
81
80
  appDirectory: appDirectory,
82
81
  tsconfigPath: tsconfigPath
83
82
  });
84
83
  _aliasOption_paths = aliasOption.paths, paths = _aliasOption_paths === void 0 ? {} : _aliasOption_paths, _aliasOption_absoluteBaseUrl = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption_absoluteBaseUrl === void 0 ? "./" : _aliasOption_absoluteBaseUrl;
85
- _readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
84
+ _readTsConfigByFile = readTsConfigByFile(tsconfigPath, ts), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
86
85
  sourcePosixPaths = sourceDirs.map(function(sourceDir) {
87
86
  return sourceDir.split(path.sep).join(path.posix.sep);
88
87
  });
@@ -91,7 +90,7 @@ export var compileByTs = function() {
91
90
  return fileName.includes(sourceDir);
92
91
  });
93
92
  });
94
- program = createProgram.call(ts2, {
93
+ program = createProgram.call(ts, {
95
94
  rootNames: rootNames,
96
95
  projectReferences: projectReferences,
97
96
  options: _object_spread({
@@ -99,16 +98,16 @@ export var compileByTs = function() {
99
98
  outDir: distDir
100
99
  }, options)
101
100
  });
102
- tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts2, absoluteBaseUrl, paths);
101
+ tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
103
102
  emitResult = program.emit(void 0, void 0, void 0, void 0, {
104
103
  before: [
105
104
  tsconfigPathsPlugin
106
105
  ]
107
106
  });
108
- allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
107
+ allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
109
108
  noEmitOnError = options.noEmitOnError;
110
109
  if (allDiagnostics.length > 0) {
111
- logger.error(ts2.formatDiagnosticsWithColorAndContext(_to_consumable_array(new Set(allDiagnostics)), formatHost));
110
+ logger.error(ts.formatDiagnosticsWithColorAndContext(_to_consumable_array(new Set(allDiagnostics)), formatHost));
112
111
  if (typeof noEmitOnError === "undefined" || noEmitOnError === true) {
113
112
  process.exit(1);
114
113
  }
@@ -182,14 +181,14 @@ export var compileByTs = function() {
182
181
  return _ref.apply(this, arguments);
183
182
  };
184
183
  }();
185
- var getFormatHost = function(ts2) {
184
+ var getFormatHost = function(ts) {
186
185
  return {
187
186
  getCanonicalFileName: function(path2) {
188
187
  return path2;
189
188
  },
190
- getCurrentDirectory: ts2.sys.getCurrentDirectory,
189
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
191
190
  getNewLine: function() {
192
- return ts2.sys.newLine;
191
+ return ts.sys.newLine;
193
192
  }
194
193
  };
195
194
  };
@@ -1,7 +1,5 @@
1
1
  import * as path from "path";
2
2
  import { fs } from "@modern-js/utils";
3
- import { compileByTs } from "../compilers/typescript";
4
- import { compileByBabel } from "../compilers/babel";
5
3
  export const FILE_EXTENSIONS = [
6
4
  ".js",
7
5
  ".ts",
@@ -24,8 +22,10 @@ export const compile = async (appDirectory, modernConfig, compileOptions) => {
24
22
  const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_server = modernConfig.server) === null || _modernConfig_server === void 0 ? void 0 : _modernConfig_server.compiler;
25
23
  const isTsProject = tsconfigPath && await fs.pathExists(tsconfigPath);
26
24
  if (!isTsProject || compiler === "babel") {
25
+ const { compileByBabel } = await import("../compilers/babel");
27
26
  await compileByBabel(appDirectory, modernConfig, compileOptions);
28
27
  } else {
28
+ const { compileByTs } = await import("../compilers/typescript");
29
29
  await compileByTs(appDirectory, modernConfig, compileOptions);
30
30
  }
31
31
  };
@@ -1,10 +1,9 @@
1
1
  import path from "path";
2
2
  import { logger, getAliasConfig, fs } from "@modern-js/utils";
3
- import ts from "typescript";
4
3
  import { TypescriptLoader } from "./typescriptLoader";
5
4
  import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin";
6
- const readTsConfigByFile = (tsConfigFile) => {
7
- const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, ts.sys);
5
+ const readTsConfigByFile = (tsConfigFile, tsInstance) => {
6
+ const parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
8
7
  const { options, fileNames, projectReferences } = parsedCmd;
9
8
  return {
10
9
  options,
@@ -29,25 +28,25 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
29
28
  if (!tsconfigPath) {
30
29
  return;
31
30
  }
32
- const ts2 = new TypescriptLoader({
31
+ const ts = new TypescriptLoader({
33
32
  appDirectory
34
33
  }).load();
35
- const createProgram = ts2.createIncrementalProgram || ts2.createProgram;
36
- const formatHost = getFormatHost(ts2);
34
+ const createProgram = ts.createIncrementalProgram || ts.createProgram;
35
+ const formatHost = getFormatHost(ts);
37
36
  const { alias } = config;
38
37
  const aliasOption = getAliasConfig(alias, {
39
38
  appDirectory,
40
39
  tsconfigPath
41
40
  });
42
41
  const { paths = {}, absoluteBaseUrl = "./" } = aliasOption;
43
- const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath);
42
+ const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath, ts);
44
43
  const sourcePosixPaths = sourceDirs.map((sourceDir) => sourceDir.split(path.sep).join(path.posix.sep));
45
44
  const rootNames = fileNames.filter((fileName) => {
46
45
  return fileName.endsWith(".d.ts") || sourcePosixPaths.some((sourceDir) => {
47
46
  return fileName.includes(sourceDir);
48
47
  });
49
48
  });
50
- const program = createProgram.call(ts2, {
49
+ const program = createProgram.call(ts, {
51
50
  rootNames,
52
51
  projectReferences,
53
52
  options: {
@@ -56,16 +55,16 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
56
55
  ...options
57
56
  }
58
57
  });
59
- const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts2, absoluteBaseUrl, paths);
58
+ const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
60
59
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
61
60
  before: [
62
61
  tsconfigPathsPlugin
63
62
  ]
64
63
  });
65
- const allDiagnostics = ts2.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
64
+ const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
66
65
  const { noEmitOnError } = options;
67
66
  if (allDiagnostics.length > 0) {
68
- logger.error(ts2.formatDiagnosticsWithColorAndContext([
67
+ logger.error(ts.formatDiagnosticsWithColorAndContext([
69
68
  ...new Set(allDiagnostics)
70
69
  ], formatHost));
71
70
  if (typeof noEmitOnError === "undefined" || noEmitOnError === true) {
@@ -77,10 +76,10 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
77
76
  }
78
77
  logger.info(`Ts compile succeed`);
79
78
  };
80
- const getFormatHost = (ts2) => {
79
+ const getFormatHost = (ts) => {
81
80
  return {
82
81
  getCanonicalFileName: (path2) => path2,
83
- getCurrentDirectory: ts2.sys.getCurrentDirectory,
84
- getNewLine: () => ts2.sys.newLine
82
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
83
+ getNewLine: () => ts.sys.newLine
85
84
  };
86
85
  };
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.25.2",
18
+ "version": "2.26.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -39,9 +39,9 @@
39
39
  "babel-plugin-module-resolver": "^4.1.0",
40
40
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
41
41
  "@swc/helpers": "0.5.1",
42
- "@modern-js/babel-compiler": "2.25.2",
43
- "@modern-js/utils": "2.25.2",
44
- "@modern-js/babel-preset-base": "2.25.2"
42
+ "@modern-js/babel-compiler": "2.26.0",
43
+ "@modern-js/babel-preset-base": "2.26.0",
44
+ "@modern-js/utils": "2.26.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/babel__core": "^7.20.0",
@@ -50,9 +50,9 @@
50
50
  "jest": "^29",
51
51
  "ts-jest": "^29.1.0",
52
52
  "typescript": "^5",
53
- "@modern-js/server-core": "2.25.2",
54
- "@scripts/build": "2.25.2",
55
- "@scripts/jest-config": "2.25.2"
53
+ "@modern-js/server-core": "2.26.0",
54
+ "@scripts/build": "2.26.0",
55
+ "@scripts/jest-config": "2.26.0"
56
56
  },
57
57
  "sideEffects": false,
58
58
  "publishConfig": {