@modern-js/module-tools 2.42.1 → 2.43.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.
@@ -21,8 +21,11 @@ __export(clear_exports, {
21
21
  clearBuildConfigPaths: () => clearBuildConfigPaths
22
22
  });
23
23
  module.exports = __toCommonJS(clear_exports);
24
+ var import_path = require("path");
24
25
  var import_utils = require("@modern-js/utils");
25
26
  var import_locale = require("../locale");
27
+ var import_utils2 = require("../utils");
28
+ var import_debug = require("../debug");
26
29
  const clearBuildConfigPaths = async (configs, projectAbsRootPath) => {
27
30
  for (const config of configs) {
28
31
  if (projectAbsRootPath === config.outDir) {
@@ -30,6 +33,27 @@ const clearBuildConfigPaths = async (configs, projectAbsRootPath) => {
30
33
  } else {
31
34
  await import_utils.fs.remove(config.outDir);
32
35
  }
36
+ if (config.buildType === "bundleless" && config.dts) {
37
+ const { compilerOptions } = await (0, import_utils2.getProjectTsconfig)(config.tsconfig);
38
+ const { composite, incremental, rootDir, tsBuildInfoFile = ".tsbuildinfo" } = compilerOptions || {};
39
+ if (!composite && !incremental) {
40
+ return;
41
+ }
42
+ const outDir = config.dts.distPath;
43
+ const tsconfigDir = (0, import_path.dirname)(config.tsconfig);
44
+ let tsbuildInfoFilePath = `${(0, import_path.basename)(config.tsconfig, ".json")}${tsBuildInfoFile}`;
45
+ if (rootDir) {
46
+ tsbuildInfoFilePath = (0, import_path.resolve)(outDir, (0, import_path.relative)((0, import_path.resolve)(tsconfigDir, rootDir), tsconfigDir), tsbuildInfoFilePath);
47
+ } else {
48
+ tsbuildInfoFilePath = (0, import_path.resolve)(outDir, tsbuildInfoFilePath);
49
+ }
50
+ (0, import_debug.debug)("clear tsbuildinfo");
51
+ if (await import_utils.fs.pathExists(tsbuildInfoFilePath)) {
52
+ await import_utils.fs.remove(tsbuildInfoFilePath);
53
+ } else {
54
+ (0, import_debug.debug)(`${tsbuildInfoFilePath} doesn't exist`);
55
+ }
56
+ }
33
57
  }
34
58
  };
35
59
  // Annotate the CommonJS export names for ESM import in node:
@@ -61,18 +61,33 @@ const runTscBin = async (api, config) => {
61
61
  const tscBinFile = await (0, import_utils2.getTscBinPath)(appDirectory);
62
62
  const params = [];
63
63
  if (userTsconfig.references) {
64
- var _userTsconfig;
65
64
  params.push("-b", tsconfigPath);
66
- var _compilerOptions;
67
- (_compilerOptions = (_userTsconfig = userTsconfig).compilerOptions) !== null && _compilerOptions !== void 0 ? _compilerOptions : _userTsconfig.compilerOptions = {};
68
- const { baseUrl = ".", outDir = "dist" } = userTsconfig.compilerOptions;
65
+ var _userTsconfig_compilerOptions;
66
+ const { baseUrl = ".", outDir, emitDeclarationOnly, declaration } = (_userTsconfig_compilerOptions = userTsconfig.compilerOptions) !== null && _userTsconfig_compilerOptions !== void 0 ? _userTsconfig_compilerOptions : {};
69
67
  const abosultBaseUrl = import_path.default.isAbsolute(baseUrl) ? baseUrl : import_path.default.join(import_path.default.dirname(tsconfigPath), baseUrl);
70
- if (import_path.default.resolve(abosultBaseUrl, outDir) !== distPath) {
71
- userTsconfig.compilerOptions.outDir = import_path.default.relative(abosultBaseUrl, distPath);
72
- import_utils.fs.writeFileSync(tsconfigPath, JSON.stringify(userTsconfig, null, 2));
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");
73
79
  }
74
80
  } else {
75
- params.push("-p", tsconfigPath, "--outDir", distPath);
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
+ );
76
91
  }
77
92
  if (watch) {
78
93
  params.push("-w");
@@ -82,10 +97,7 @@ const runTscBin = async (api, config) => {
82
97
  /* Required parameter, use it stdout have color */
83
98
  "--pretty",
84
99
  // https://github.com/microsoft/TypeScript/issues/21824
85
- "--preserveWatchOutput",
86
- // Only emit d.ts files
87
- "--declaration",
88
- "--emitDeclarationOnly"
100
+ "--preserveWatchOutput"
89
101
  ], {
90
102
  stdio: "pipe",
91
103
  cwd: appDirectory
@@ -242,7 +242,7 @@ const adapterPlugin = (compiler) => {
242
242
  if (!item) {
243
243
  continue;
244
244
  }
245
- if (absPath.endsWith(".js")) {
245
+ if ((0, import_utils2.isJsExt)(absPath)) {
246
246
  compiler.emitAsset(absPath, {
247
247
  type: "chunk",
248
248
  contents: item.text,
@@ -34,8 +34,7 @@ module.exports = __toCommonJS(lessAliasPlugin_exports);
34
34
  var import_define_property = require("@swc/helpers/_/_define_property");
35
35
  var import_fs = __toESM(require("fs"));
36
36
  var import_utils = require("./utils");
37
- var LessAliasesPlugin;
38
- LessAliasesPlugin = class LessAliasesPlugin2 {
37
+ class LessAliasesPlugin {
39
38
  install(less, pluginManager) {
40
39
  const getResolve = (filename, currentDirectory) => {
41
40
  return this.compiler.css_resolve(filename, currentDirectory || this.stdinDir);
@@ -68,4 +67,4 @@ LessAliasesPlugin = class LessAliasesPlugin2 {
68
67
  this.compiler = options.compiler;
69
68
  this.stdinDir = options.stdinDir;
70
69
  }
71
- };
70
+ }
@@ -33,8 +33,11 @@ const postcssUrlPlugin = (options) => {
33
33
  postcssPlugin: "postcss-url",
34
34
  async Declaration(decl) {
35
35
  const isProcessed = decl[Processed];
36
+ if (isProcessed) {
37
+ return;
38
+ }
36
39
  decl.value = await (0, import_utils.rewriteCssUrls)(decl.value, false, async (URL) => {
37
- if (URL && !HTTP_PATTERNS.test(URL) && !HASH_PATTERNS.test(URL) && !DATAURL_PATTERNS.test(URL) && !isProcessed) {
40
+ if (URL && !HTTP_PATTERNS.test(URL) && !HASH_PATTERNS.test(URL) && !DATAURL_PATTERNS.test(URL)) {
38
41
  let filePath = URL;
39
42
  const { outDir, sourceDir, buildType } = options.compilation.config;
40
43
  const { css_resolve } = options.compilation;
@@ -94,8 +94,12 @@ async function rebaseUrls(filepath, rootDir, resolver) {
94
94
  };
95
95
  }
96
96
  }
97
- function rewriteCssUrls(css, type, replacer) {
98
- return asyncReplace(css, cssUrlRE, async (match) => {
97
+ async function rewriteCssUrls(css, type, replacer) {
98
+ let match;
99
+ let remaining = css;
100
+ let rewritten = "";
101
+ while (match = cssUrlRE.exec(remaining)) {
102
+ rewritten += remaining.slice(0, match.index);
99
103
  const matched = match[0];
100
104
  let rawUrl = match[1];
101
105
  let wrap = "";
@@ -104,19 +108,8 @@ function rewriteCssUrls(css, type, replacer) {
104
108
  wrap = first;
105
109
  rawUrl = rawUrl.slice(1, -1);
106
110
  }
107
- if (type === "less" && rawUrl.startsWith("@") || (type === "sass" || type === "scss") && rawUrl.startsWith("$") || isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl.startsWith("#")) {
108
- return matched;
109
- }
110
- return `url(${wrap}${(0, import_utils.normalizeSlashes)(await replacer(rawUrl))}${wrap})`;
111
- });
112
- }
113
- async function asyncReplace(input, re, replacer) {
114
- let match;
115
- let remaining = input;
116
- let rewritten = "";
117
- while (match = re.exec(remaining)) {
118
- rewritten += remaining.slice(0, match.index);
119
- rewritten += await replacer(match);
111
+ const result = type === "less" && rawUrl.startsWith("@") || (type === "sass" || type === "scss") && rawUrl.startsWith("$") || isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl.startsWith("#") ? matched : `url(${wrap}${(0, import_utils.normalizeSlashes)(await replacer(rawUrl))}${wrap})`;
112
+ rewritten += result;
120
113
  remaining = remaining.slice(match.index + match[0].length);
121
114
  }
122
115
  rewritten += remaining;
@@ -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);
@@ -36,6 +36,9 @@ export interface ITsconfig {
36
36
  paths?: Record<string, string[]>;
37
37
  target?: TsTarget;
38
38
  useDefineForClassFields?: boolean;
39
+ composite?: boolean;
40
+ incremental?: boolean;
41
+ tsBuildInfoFile?: string;
39
42
  } | undefined;
40
43
  include?: string[];
41
44
  exclude?: string[];
@@ -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) => {
@@ -134,12 +144,23 @@ ${footer}
134
144
  // users need to set esbuild out-extensions like { '.js': '.mjs' }
135
145
  filePath.replace(/\.d\.ts/, dtsExtension)
136
146
  );
137
- return import_utils.fs.writeFile(
138
- // only replace .d.ts, if tsc generate .d.m(c)ts, keep.
139
- finalPath,
140
- content
141
- );
142
- }));
147
+ const writeTask = () => {
148
+ return import_utils.fs.writeFile(
149
+ // only replace .d.ts, if tsc generate .d.m(c)ts, keep.
150
+ finalPath,
151
+ content
152
+ );
153
+ };
154
+ const removeTask = () => {
155
+ return import_utils.fs.remove(filePath);
156
+ };
157
+ return dtsExtension === ".d.ts" ? [
158
+ writeTask()
159
+ ] : [
160
+ writeTask(),
161
+ removeTask()
162
+ ];
163
+ }).flat());
143
164
  };
144
165
  const printOrThrowDtsErrors = async (error, options) => {
145
166
  const { InternalDTSError } = await Promise.resolve().then(() => __toESM(require("../error")));
@@ -161,6 +182,7 @@ const printOrThrowDtsErrors = async (error, options) => {
161
182
  const tsTargetAtOrAboveES2022 = (target) => target === "es2022" || target === "esnext";
162
183
  // Annotate the CommonJS export names for ESM import in node:
163
184
  0 && (module.exports = {
185
+ detectTSVersion,
164
186
  getProjectTsconfig,
165
187
  getTscBinPath,
166
188
  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.1",
3
+ "version": "2.43.0",
4
4
  "description": "Simple, powerful, high-performance modern npm package development solution.",
5
5
  "keywords": [
6
6
  "modern",
@@ -46,9 +46,9 @@
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
- "@modern-js/swc-plugins": "0.6.5",
51
+ "@modern-js/swc-plugins": "0.6.6",
52
52
  "@rollup/pluginutils": "4.1.1",
53
53
  "@svgr/core": "8.0.0",
54
54
  "@svgr/plugin-jsx": "8.0.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.1",
71
- "@modern-js/new-action": "2.42.1",
72
- "@modern-js/plugin": "2.42.1",
73
- "@modern-js/plugin-i18n": "2.42.1",
74
- "@modern-js/plugin-changeset": "2.42.1",
75
- "@modern-js/types": "2.42.1",
76
- "@modern-js/upgrade": "2.42.1",
77
- "@modern-js/utils": "2.42.1",
78
- "@modern-js/plugin-lint": "2.42.1"
70
+ "@modern-js/core": "2.43.0",
71
+ "@modern-js/new-action": "2.43.0",
72
+ "@modern-js/plugin": "2.43.0",
73
+ "@modern-js/plugin-i18n": "2.43.0",
74
+ "@modern-js/plugin-lint": "2.43.0",
75
+ "@modern-js/plugin-changeset": "2.43.0",
76
+ "@modern-js/types": "2.43.0",
77
+ "@modern-js/upgrade": "2.43.0",
78
+ "@modern-js/utils": "2.43.0"
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/self": "npm:@modern-js/module-tools@2.42.1",
85
- "@modern-js/builder-webpack-provider": "2.42.1",
86
- "@scripts/build": "2.42.1",
87
- "@scripts/vitest-config": "2.42.1"
84
+ "@modern-js/builder-webpack-provider": "2.43.0",
85
+ "@modern-js/self": "npm:@modern-js/module-tools@2.43.0",
86
+ "@scripts/build": "2.43.0",
87
+ "@scripts/vitest-config": "2.43.0"
88
88
  },
89
89
  "peerDependencies": {
90
90
  "typescript": "^4 || ^5"