@modern-js/module-tools 2.42.0 → 2.42.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.
@@ -23,6 +23,7 @@ __export(clear_exports, {
23
23
  module.exports = __toCommonJS(clear_exports);
24
24
  var import_utils = require("@modern-js/utils");
25
25
  var import_locale = require("../locale");
26
+ var import_utils2 = require("../utils");
26
27
  const clearBuildConfigPaths = async (configs, projectAbsRootPath) => {
27
28
  for (const config of configs) {
28
29
  if (projectAbsRootPath === config.outDir) {
@@ -30,6 +31,22 @@ const clearBuildConfigPaths = async (configs, projectAbsRootPath) => {
30
31
  } else {
31
32
  await import_utils.fs.remove(config.outDir);
32
33
  }
34
+ if (config.buildType === "bundleless" && config.dts && // keep it same as https://github.com/web-infra-dev/modern.js/blob/main/packages/solutions/module-tools/src/builder/build.ts#L37
35
+ await import_utils.fs.pathExists(config.tsconfig)) {
36
+ const tscBinFile = await (0, import_utils2.getTscBinPath)(projectAbsRootPath);
37
+ const childProgress = (0, import_utils.execa)(tscBinFile, [
38
+ "--build",
39
+ "--clean"
40
+ ], {
41
+ stdio: "pipe",
42
+ cwd: projectAbsRootPath
43
+ });
44
+ try {
45
+ await childProgress;
46
+ } catch (e) {
47
+ import_utils.logger.error(e);
48
+ }
49
+ }
33
50
  }
34
51
  };
35
52
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var tsc_exports = {};
20
30
  __export(tsc_exports, {
@@ -22,6 +32,7 @@ __export(tsc_exports, {
22
32
  runTsc: () => runTsc
23
33
  });
24
34
  module.exports = __toCommonJS(tsc_exports);
35
+ var import_path = __toESM(require("path"));
25
36
  var import_utils = require("@modern-js/utils");
26
37
  var import_utils2 = require("../../utils");
27
38
  var import_dts = require("../../constants/dts");
@@ -46,30 +57,47 @@ const resolveLog = async (childProgress, options) => {
46
57
  });
47
58
  };
48
59
  const runTscBin = async (api, config) => {
49
- const { appDirectory, watch = false, abortOnError = true } = config;
60
+ const { appDirectory, watch = false, abortOnError = true, tsconfigPath, userTsconfig, distPath } = config;
50
61
  const tscBinFile = await (0, import_utils2.getTscBinPath)(appDirectory);
51
- const { tsconfigPath, userTsconfig, distPath } = config;
52
62
  const params = [];
63
+ if (userTsconfig.references) {
64
+ params.push("-b", tsconfigPath);
65
+ var _userTsconfig_compilerOptions;
66
+ const { baseUrl = ".", outDir, emitDeclarationOnly, declaration } = (_userTsconfig_compilerOptions = userTsconfig.compilerOptions) !== null && _userTsconfig_compilerOptions !== void 0 ? _userTsconfig_compilerOptions : {};
67
+ const abosultBaseUrl = import_path.default.isAbsolute(baseUrl) ? baseUrl : import_path.default.join(import_path.default.dirname(tsconfigPath), baseUrl);
68
+ if (!outDir || import_path.default.resolve(abosultBaseUrl, outDir) !== distPath) {
69
+ const correctOutDir = import_path.default.relative(abosultBaseUrl, distPath);
70
+ throw new Error(`Please set outDir: "${correctOutDir}" in ${import_utils.chalk.underline(tsconfigPath)} to keep it same as buildConfig.`);
71
+ }
72
+ const tsVersion = await (0, import_utils2.detectTSVersion)(appDirectory);
73
+ if (tsVersion !== 5) {
74
+ if (!declaration || !emitDeclarationOnly) {
75
+ throw new Error(`Please set declaration: true and emitDeclaration: true in ${import_utils.chalk.underline(tsconfigPath)}`);
76
+ }
77
+ } else {
78
+ params.push("--declaration", "--emitDeclarationOnly");
79
+ }
80
+ } else {
81
+ params.push(
82
+ "-p",
83
+ tsconfigPath,
84
+ // Same as dts.distPath
85
+ "--outDir",
86
+ distPath,
87
+ // Only emit d.ts files
88
+ "--declaration",
89
+ "--emitDeclarationOnly"
90
+ );
91
+ }
53
92
  if (watch) {
54
93
  params.push("-w");
55
94
  }
56
- if (userTsconfig.references) {
57
- params.push("-b");
58
- }
59
95
  const childProgress = (0, import_utils.execa)(tscBinFile, [
60
- "-p",
61
- tsconfigPath,
96
+ ...params,
62
97
  /* Required parameter, use it stdout have color */
63
98
  "--pretty",
64
99
  // https://github.com/microsoft/TypeScript/issues/21824
65
- "--preserveWatchOutput",
66
- // Only emit d.ts files
67
- "--declaration",
68
- "--emitDeclarationOnly",
69
- // write d.ts files to distPath that defined in buildConfig.dts
70
- "--outDir",
71
- distPath,
72
- ...params
100
+ "--preserveWatchOutput"
73
101
  ], {
74
102
  stdio: "pipe",
75
103
  cwd: appDirectory
@@ -150,7 +150,11 @@ const swcRenderChunk = {
150
150
  module: format === "umd" ? {
151
151
  type: "umd"
152
152
  } : void 0,
153
- isModule: "unknown"
153
+ // If `chunk.contents` is recognized as [Script](https://swc.rs/docs/configuration/compilation#ismodule),
154
+ // then it will not be converted to umd format content.
155
+ // so we need to set `isModule` to `true`.
156
+ // eg: when `autoExternal` is false, then chunk.contents will be recognized as [Script]
157
+ isModule: format === "umd" ? true : "unknown"
154
158
  });
155
159
  const result = await swcCompiler.transform(name2, chunk.contents.toString());
156
160
  return {
package/dist/command.js CHANGED
@@ -56,7 +56,7 @@ const devCommand = async (program, api) => {
56
56
  const runner = api.useHookRunners();
57
57
  const devToolMetas = await runner.registerDev();
58
58
  await runner.beforeDev(devToolMetas);
59
- const devProgram = program.command("dev").usage("[options]").description(import_locale.i18n.t(import_locale.localeKeys.command.dev.describe)).option("--tsconfig [tsconfig]", import_locale.i18n.t(import_locale.localeKeys.command.dev.tsconfig)).action(async (options) => {
59
+ const devProgram = program.command("dev").usage("[options]").description(import_locale.i18n.t(import_locale.localeKeys.command.dev.describe)).option("--tsconfig [tsconfig]", import_locale.i18n.t(import_locale.localeKeys.command.dev.tsconfig)).option("-c, --config <config>", import_locale.i18n.t(import_locale.localeKeys.command.shared.config)).action(async (options) => {
60
60
  const context = await initModuleContext(api);
61
61
  const { dev } = await Promise.resolve().then(() => __toESM(require("./dev")));
62
62
  await dev(options, devToolMetas, api, context);
@@ -1,5 +1,6 @@
1
1
  import type { ITsconfig, GeneratorDtsConfig, BuildType, TsTarget } from '../types';
2
2
  export declare const getProjectTsconfig: (tsconfigPath: string) => Promise<ITsconfig>;
3
+ export declare function detectTSVersion(appDirectory?: string): Promise<number | undefined>;
3
4
  export declare const getTscBinPath: (appDirectory: string) => Promise<string>;
4
5
  export declare const processDtsFilesAfterTsc: (config: GeneratorDtsConfig) => Promise<void>;
5
6
  export declare const printOrThrowDtsErrors: (error: unknown, options: {
package/dist/utils/dts.js CHANGED
@@ -28,6 +28,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var dts_exports = {};
30
30
  __export(dts_exports, {
31
+ detectTSVersion: () => detectTSVersion,
31
32
  getProjectTsconfig: () => getProjectTsconfig,
32
33
  getTscBinPath: () => getTscBinPath,
33
34
  printOrThrowDtsErrors: () => printOrThrowDtsErrors,
@@ -47,6 +48,15 @@ const getProjectTsconfig = async (tsconfigPath) => {
47
48
  }
48
49
  return import_utils.json5.parse(import_utils.fs.readFileSync(tsconfigPath, "utf-8"));
49
50
  };
51
+ async function detectTSVersion(appDirectory) {
52
+ const cwd = appDirectory !== null && appDirectory !== void 0 ? appDirectory : process.cwd();
53
+ const typescriptPath = (0, import_path.join)(cwd, "node_modules", "typescript");
54
+ if (await import_utils.fs.pathExists(typescriptPath)) {
55
+ const typescriptPkg = await import_utils.fs.readJson((0, import_path.join)(typescriptPath, "package.json"));
56
+ const version = Number(typescriptPkg.version.split(".")[0]);
57
+ return version;
58
+ }
59
+ }
50
60
  const getTscBinPath = async (appDirectory) => {
51
61
  const { default: findUp, exists: pathExists } = await Promise.resolve().then(() => __toESM(require("../../compiled/find-up")));
52
62
  const tscBinFile = await findUp(async (directory) => {
@@ -161,6 +171,7 @@ const printOrThrowDtsErrors = async (error, options) => {
161
171
  const tsTargetAtOrAboveES2022 = (target) => target === "es2022" || target === "esnext";
162
172
  // Annotate the CommonJS export names for ESM import in node:
163
173
  0 && (module.exports = {
174
+ detectTSVersion,
164
175
  getProjectTsconfig,
165
176
  getTscBinPath,
166
177
  printOrThrowDtsErrors,
@@ -1,4 +1,4 @@
1
- import { RawSourceMap } from '@ampproject/remapping/dist/types/types';
1
+ import type { RawSourceMap } from '@ampproject/remapping';
2
2
  import type { SourceMap } from '../types';
3
3
  interface Options {
4
4
  needSourceMap: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/module-tools",
3
- "version": "2.42.0",
3
+ "version": "2.42.2",
4
4
  "description": "Simple, powerful, high-performance modern npm package development solution.",
5
5
  "keywords": [
6
6
  "modern",
@@ -46,7 +46,7 @@
46
46
  "modern-module": "./bin/modern.js"
47
47
  },
48
48
  "dependencies": {
49
- "@ampproject/remapping": "1.0.2",
49
+ "@ampproject/remapping": "^2.2.1",
50
50
  "@ast-grep/napi": "0.12.0",
51
51
  "@modern-js/swc-plugins": "0.6.5",
52
52
  "@rollup/pluginutils": "4.1.1",
@@ -67,24 +67,24 @@
67
67
  "tapable": "2.2.1",
68
68
  "terser": "5.19.2",
69
69
  "tsconfig-paths-webpack-plugin": "4.1.0",
70
- "@modern-js/core": "2.42.0",
71
- "@modern-js/plugin-changeset": "2.42.0",
72
- "@modern-js/plugin": "2.42.0",
73
- "@modern-js/plugin-i18n": "2.42.0",
74
- "@modern-js/new-action": "2.42.0",
75
- "@modern-js/plugin-lint": "2.42.0",
76
- "@modern-js/upgrade": "2.42.0",
77
- "@modern-js/utils": "2.42.0",
78
- "@modern-js/types": "2.42.0"
70
+ "@modern-js/core": "2.42.2",
71
+ "@modern-js/plugin": "2.42.2",
72
+ "@modern-js/plugin-changeset": "2.42.2",
73
+ "@modern-js/plugin-i18n": "2.42.2",
74
+ "@modern-js/new-action": "2.42.2",
75
+ "@modern-js/plugin-lint": "2.42.2",
76
+ "@modern-js/types": "2.42.2",
77
+ "@modern-js/utils": "2.42.2",
78
+ "@modern-js/upgrade": "2.42.2"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@types/convert-source-map": "1.5.2",
82
82
  "@types/node": "^14",
83
83
  "typescript": "^5",
84
- "@modern-js/builder-webpack-provider": "2.42.0",
85
- "@modern-js/self": "npm:@modern-js/module-tools@2.42.0",
86
- "@scripts/build": "2.42.0",
87
- "@scripts/vitest-config": "2.42.0"
84
+ "@modern-js/builder-webpack-provider": "2.42.2",
85
+ "@modern-js/self": "npm:@modern-js/module-tools@2.42.2",
86
+ "@scripts/build": "2.42.2",
87
+ "@scripts/vitest-config": "2.42.2"
88
88
  },
89
89
  "peerDependencies": {
90
90
  "typescript": "^4 || ^5"