@modern-js/module-tools 2.43.0 → 2.44.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.
@@ -76,7 +76,7 @@ const generatorDts = async (config, api, options) => {
76
76
  const { watch, dts } = options;
77
77
  const { buildType, input, sourceDir, alias, externals, tsconfig, footer: { dts: footer }, banner: { dts: banner }, format, autoExtension } = config;
78
78
  const { appDirectory } = api.useAppContext();
79
- const { distPath, abortOnError, respectExternal } = dts;
79
+ const { distPath, abortOnError, respectExternal, enableTscBuild } = dts;
80
80
  var _dts_tsconfigPath;
81
81
  const tsconfigPath = (_dts_tsconfigPath = dts.tsconfigPath) !== null && _dts_tsconfigPath !== void 0 ? _dts_tsconfigPath : tsconfig;
82
82
  const { dtsExtension } = (0, import_utils2.getDefaultOutExtension)({
@@ -99,7 +99,8 @@ const generatorDts = async (config, api, options) => {
99
99
  alias,
100
100
  sourceDir,
101
101
  dtsExtension,
102
- userTsconfig
102
+ userTsconfig,
103
+ enableTscBuild
103
104
  };
104
105
  const prevTime = Date.now();
105
106
  (0, import_debug.debug)(`${(0, import_debug.label)("dts")} Build Start`);
@@ -35,23 +35,25 @@ const clearBuildConfigPaths = async (configs, projectAbsRootPath) => {
35
35
  }
36
36
  if (config.buildType === "bundleless" && config.dts) {
37
37
  const { compilerOptions } = await (0, import_utils2.getProjectTsconfig)(config.tsconfig);
38
- const { composite, incremental, rootDir, tsBuildInfoFile = ".tsbuildinfo" } = compilerOptions || {};
38
+ const { composite, incremental, rootDir, outDir, tsBuildInfoFile = ".tsbuildinfo" } = compilerOptions || {};
39
39
  if (!composite && !incremental) {
40
40
  return;
41
41
  }
42
- const outDir = config.dts.distPath;
43
42
  const tsconfigDir = (0, import_path.dirname)(config.tsconfig);
44
43
  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);
44
+ if (outDir) {
45
+ if (rootDir) {
46
+ tsbuildInfoFilePath = (0, import_path.join)(outDir, (0, import_path.relative)((0, import_path.resolve)(tsconfigDir, rootDir), tsconfigDir), tsbuildInfoFilePath);
47
+ } else {
48
+ tsbuildInfoFilePath = (0, import_path.join)(outDir, tsbuildInfoFilePath);
49
+ }
49
50
  }
51
+ const tsbuildInfoFileAbsPath = (0, import_path.resolve)(tsconfigDir, tsbuildInfoFilePath);
50
52
  (0, import_debug.debug)("clear tsbuildinfo");
51
- if (await import_utils.fs.pathExists(tsbuildInfoFilePath)) {
52
- await import_utils.fs.remove(tsbuildInfoFilePath);
53
+ if (await import_utils.fs.pathExists(tsbuildInfoFileAbsPath)) {
54
+ await import_utils.fs.remove(tsbuildInfoFileAbsPath);
53
55
  } else {
54
- (0, import_debug.debug)(`${tsbuildInfoFilePath} doesn't exist`);
56
+ (0, import_debug.debug)(`${tsbuildInfoFileAbsPath} doesn't exist`);
55
57
  }
56
58
  }
57
59
  }
@@ -57,22 +57,23 @@ const resolveLog = async (childProgress, options) => {
57
57
  });
58
58
  };
59
59
  const runTscBin = async (api, config) => {
60
- const { appDirectory, watch = false, abortOnError = true, tsconfigPath, userTsconfig, distPath } = config;
60
+ const { appDirectory, watch = false, abortOnError = true, tsconfigPath, userTsconfig, distPath, enableTscBuild } = config;
61
61
  const tscBinFile = await (0, import_utils2.getTscBinPath)(appDirectory);
62
62
  const params = [];
63
- if (userTsconfig.references) {
63
+ if (enableTscBuild) {
64
64
  params.push("-b", tsconfigPath);
65
65
  var _userTsconfig_compilerOptions;
66
- const { baseUrl = ".", outDir, emitDeclarationOnly, declaration } = (_userTsconfig_compilerOptions = userTsconfig.compilerOptions) !== null && _userTsconfig_compilerOptions !== void 0 ? _userTsconfig_compilerOptions : {};
66
+ const { baseUrl = ".", outDir, emitDeclarationOnly, declaration, declarationDir } = (_userTsconfig_compilerOptions = userTsconfig.compilerOptions) !== null && _userTsconfig_compilerOptions !== void 0 ? _userTsconfig_compilerOptions : {};
67
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) {
68
+ if ((!outDir || import_path.default.resolve(abosultBaseUrl, outDir) !== distPath) && (!declarationDir || import_path.default.resolve(abosultBaseUrl, declarationDir) !== distPath)) {
69
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.`);
70
+ const info = outDir && !declarationDir ? "outDir" : "declarationDir";
71
+ import_utils.logger.error(`Please set ${info}: "${correctOutDir}" in ${import_utils.chalk.underline(tsconfigPath)} to keep it same as buildConfig.`);
71
72
  }
72
73
  const tsVersion = await (0, import_utils2.detectTSVersion)(appDirectory);
73
74
  if (tsVersion !== 5) {
74
75
  if (!declaration || !emitDeclarationOnly) {
75
- throw new Error(`Please set declaration: true and emitDeclaration: true in ${import_utils.chalk.underline(tsconfigPath)}`);
76
+ import_utils.logger.error(`Please set declaration: true and emitDeclarationOnly: true in ${import_utils.chalk.underline(tsconfigPath)}`);
76
77
  }
77
78
  } else {
78
79
  params.push("--declaration", "--emitDeclarationOnly");
@@ -82,7 +83,7 @@ const runTscBin = async (api, config) => {
82
83
  "-p",
83
84
  tsconfigPath,
84
85
  // Same as dts.distPath
85
- "--outDir",
86
+ "--declarationDir",
86
87
  distPath,
87
88
  // Only emit d.ts files
88
89
  "--declaration",
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ICompiler, LoadResult, SvgrOptions } from '../../types';
2
+ import { ICompiler } from '../../types';
3
3
  export declare const asset: {
4
4
  name: string;
5
5
  apply(compiler: ICompiler): void;
@@ -14,6 +14,5 @@ export declare const asset: {
14
14
  */
15
15
  export declare function getAssetContents(this: ICompiler, assetPath: string, rebaseFrom: string, calledOnLoad?: boolean): Promise<{
16
16
  contents: string | Buffer;
17
- loader: "copy" | "text";
17
+ loader: "copy" | "jsx" | "text";
18
18
  }>;
19
- export declare function loadSvgr(path: string, options: boolean | SvgrOptions): Promise<LoadResult | undefined>;
@@ -29,8 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var asset_exports = {};
30
30
  __export(asset_exports, {
31
31
  asset: () => asset,
32
- getAssetContents: () => getAssetContents,
33
- loadSvgr: () => loadSvgr
32
+ getAssetContents: () => getAssetContents
34
33
  });
35
34
  module.exports = __toCommonJS(asset_exports);
36
35
  var import_path = require("path");
@@ -50,11 +49,7 @@ const asset = {
50
49
  name
51
50
  }, async (args) => {
52
51
  if (import_file.assetExt.find((ext) => ext === (0, import_path.extname)(args.path))) {
53
- const { buildType, outDir, sourceDir, asset: asset2 } = compiler.config;
54
- const svgrResult = await loadSvgr(args.path, asset2.svgr);
55
- if (svgrResult) {
56
- return svgrResult;
57
- }
52
+ const { buildType, outDir, sourceDir } = compiler.config;
58
53
  const rebaseFrom = buildType === "bundle" ? outDir : (0, import_path.join)(outDir, (0, import_path.relative)(sourceDir, (0, import_path.dirname)(args.path)));
59
54
  const { contents, loader } = await getAssetContents.apply(compiler, [
60
55
  args.path,
@@ -75,7 +70,7 @@ function encodeSVG(buffer) {
75
70
  async function getAssetContents(assetPath, rebaseFrom, calledOnLoad) {
76
71
  const fileContent = await import_fs.default.promises.readFile(assetPath);
77
72
  const { buildType, format, outDir } = this.config;
78
- const { limit, path, publicPath } = this.config.asset;
73
+ const { limit, path, publicPath, svgr } = this.config.asset;
79
74
  const hash = (0, import_utils.getHash)(fileContent, null).slice(0, 8);
80
75
  const outputFileName = (0, import_path.basename)(assetPath).split(".").join(`.${hash}.`);
81
76
  const outputFilePath = (0, import_path.resolve)(outDir, path, outputFileName);
@@ -85,7 +80,24 @@ async function getAssetContents(assetPath, rebaseFrom, calledOnLoad) {
85
80
  let emitAsset = true;
86
81
  let contents = normalizedPublicPath;
87
82
  let loader = "text";
88
- if (buildType === "bundle") {
83
+ const config = typeof svgr === "boolean" ? {} : svgr;
84
+ const filter = (0, import_pluginutils.createFilter)(config.include || SVG_REGEXP, config.exclude);
85
+ if (svgr && filter(assetPath)) {
86
+ const previousExport = config.exportType === "named" ? `export default "${normalizedPublicPath}"` : null;
87
+ contents = await (0, import_core.transform)(fileContent.toString(), config, {
88
+ filePath: assetPath,
89
+ caller: {
90
+ name: "svgr",
91
+ defaultPlugins: [
92
+ import_plugin_svgo.default,
93
+ import_plugin_jsx.default
94
+ ],
95
+ previousExport
96
+ }
97
+ });
98
+ loader = "jsx";
99
+ emitAsset = false;
100
+ } else if (buildType === "bundle") {
89
101
  if (fileContent.length <= limit) {
90
102
  const mimetype = (await Promise.resolve().then(() => __toESM(require("@modern-js/utils/mime-types")))).default.lookup(assetPath);
91
103
  const isSVG = mimetype === "image/svg+xml";
@@ -115,35 +127,8 @@ async function getAssetContents(assetPath, rebaseFrom, calledOnLoad) {
115
127
  loader
116
128
  };
117
129
  }
118
- async function loadSvgr(path, options) {
119
- if (!options) {
120
- return;
121
- }
122
- const config = typeof options === "boolean" ? {} : options;
123
- const filter = (0, import_pluginutils.createFilter)(config.include || SVG_REGEXP, config.exclude);
124
- if (!filter(path)) {
125
- return;
126
- }
127
- const loader = "jsx";
128
- const text = import_fs.default.readFileSync(path, "utf8");
129
- const jsCode = await (0, import_core.transform)(text, config, {
130
- filePath: path,
131
- caller: {
132
- name: "svgr",
133
- defaultPlugins: [
134
- import_plugin_svgo.default,
135
- import_plugin_jsx.default
136
- ]
137
- }
138
- });
139
- return {
140
- contents: jsCode,
141
- loader
142
- };
143
- }
144
130
  // Annotate the CommonJS export names for ESM import in node:
145
131
  0 && (module.exports = {
146
132
  asset,
147
- getAssetContents,
148
- loadSvgr
133
+ getAssetContents
149
134
  });
@@ -55,7 +55,7 @@ async function redirectImport(compiler, code, modules, aliasRecord, filePath, ou
55
55
  const { start, end } = module2;
56
56
  let { name: name2 } = module2;
57
57
  const ext = (0, import_path.extname)(name2);
58
- const { redirect: redirect2, asset } = compiler.config;
58
+ const { redirect: redirect2 } = compiler.config;
59
59
  const { alias, style } = redirect2;
60
60
  if (alias) {
61
61
  let absoluteImportPath = matchPath ? matchPath(name2, void 0, void 0, extensions) : void 0;
@@ -119,16 +119,15 @@ async function redirectImport(compiler, code, modules, aliasRecord, filePath, ou
119
119
  if (redirect2.asset) {
120
120
  if (import_file.assetExt.filter((ext2) => name2.endsWith(ext2)).length) {
121
121
  const absPath = (0, import_path.resolve)((0, import_path.dirname)(filePath), name2);
122
- const svgrResult = await (0, import_asset.loadSvgr)(absPath, asset.svgr);
123
- if (svgrResult) {
122
+ const { contents: relativeImportPath, loader } = await import_asset.getAssetContents.apply(compiler, [
123
+ absPath,
124
+ outputDir
125
+ ]);
126
+ if (loader === "jsx") {
124
127
  const ext2 = (0, import_path.extname)(name2);
125
128
  const outputName = `${name2.slice(0, -ext2.length)}.js`;
126
129
  str.overwrite(start, end, outputName);
127
130
  } else {
128
- const { contents: relativeImportPath } = await import_asset.getAssetContents.apply(compiler, [
129
- absPath,
130
- outputDir
131
- ]);
132
131
  str.overwrite(start, end, `${relativeImportPath}`);
133
132
  }
134
133
  }
@@ -42,7 +42,8 @@ const getDefaultBuildConfig = () => {
42
42
  distPath: "./",
43
43
  tsconfigPath: void 0,
44
44
  abortOnError: true,
45
- respectExternal: true
45
+ respectExternal: true,
46
+ enableTscBuild: false
46
47
  }),
47
48
  autoExtension: false,
48
49
  esbuildOptions: (c) => c,
@@ -41,6 +41,12 @@ export type Redirect = {
41
41
  export type DTSOptions = {
42
42
  abortOnError: boolean;
43
43
  distPath: string;
44
+ /**
45
+ * Build one or more projects and their dependencies, if out of date
46
+ * The same as 'tsc --build'
47
+ * @default false
48
+ */
49
+ enableTscBuild: boolean;
44
50
  only: boolean;
45
51
  /**
46
52
  * @deprecated
@@ -14,6 +14,7 @@ export interface GeneratorDtsConfig {
14
14
  respectExternal: boolean;
15
15
  dtsExtension: string;
16
16
  userTsconfig: ITsconfig;
17
+ enableTscBuild: boolean;
17
18
  }
18
19
  export interface GeneratedDtsInfo {
19
20
  userTsconfig: ITsconfig;
package/dist/utils/dts.js CHANGED
@@ -123,7 +123,12 @@ const processDtsFilesAfterTsc = async (config) => {
123
123
  return;
124
124
  }
125
125
  const { start, end, name } = module2;
126
- const absoluteImportPath = matchPath(name);
126
+ const absoluteImportPath = matchPath(name, void 0, void 0, [
127
+ ".jsx",
128
+ ".tsx",
129
+ ".js",
130
+ ".ts"
131
+ ]);
127
132
  if (absoluteImportPath) {
128
133
  const relativePath = (0, import_path.relative)((0, import_path.dirname)(originalFilePath), absoluteImportPath);
129
134
  const relativeImportPath = (0, import_builder.normalizeSlashes)(relativePath.startsWith("..") ? relativePath : `./${relativePath}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/module-tools",
3
- "version": "2.43.0",
3
+ "version": "2.44.0",
4
4
  "description": "Simple, powerful, high-performance modern npm package development solution.",
5
5
  "keywords": [
6
6
  "modern",
@@ -50,9 +50,9 @@
50
50
  "@ast-grep/napi": "0.12.0",
51
51
  "@modern-js/swc-plugins": "0.6.6",
52
52
  "@rollup/pluginutils": "4.1.1",
53
- "@svgr/core": "8.0.0",
54
- "@svgr/plugin-jsx": "8.0.1",
55
- "@svgr/plugin-svgo": "8.0.1",
53
+ "@svgr/core": "8.1.0",
54
+ "@svgr/plugin-jsx": "8.1.0",
55
+ "@svgr/plugin-svgo": "8.1.0",
56
56
  "@swc/helpers": "0.5.3",
57
57
  "convert-source-map": "1.8.0",
58
58
  "enhanced-resolve": "5.12.0",
@@ -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.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"
70
+ "@modern-js/core": "2.44.0",
71
+ "@modern-js/new-action": "2.44.0",
72
+ "@modern-js/plugin": "2.44.0",
73
+ "@modern-js/plugin-changeset": "2.44.0",
74
+ "@modern-js/plugin-i18n": "2.44.0",
75
+ "@modern-js/plugin-lint": "2.44.0",
76
+ "@modern-js/types": "2.44.0",
77
+ "@modern-js/upgrade": "2.44.0",
78
+ "@modern-js/utils": "2.44.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/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"
84
+ "@modern-js/builder-webpack-provider": "2.44.0",
85
+ "@scripts/build": "2.44.0",
86
+ "@modern-js/self": "npm:@modern-js/module-tools@2.44.0",
87
+ "@scripts/vitest-config": "2.44.0"
88
88
  },
89
89
  "peerDependencies": {
90
90
  "typescript": "^4 || ^5"