weapp-vite 1.2.5 → 1.3.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.
@@ -3,18 +3,20 @@ import logger from "@weapp-core/logger";
3
3
  var logger_default = logger;
4
4
 
5
5
  // package.json
6
- var version = "1.2.5";
6
+ var version = "1.3.1";
7
7
 
8
8
  // src/constants.ts
9
9
  var VERSION = version;
10
- var jsExtensions = ["js", "ts"];
10
+ var jsExtensions = ["ts", "js"];
11
+ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
12
+ var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
11
13
 
12
14
  // src/context.ts
13
15
  import { createRequire } from "node:module";
14
- import process2 from "node:process";
15
- import { addExtension as addExtension3, defu as defu3, removeExtension as removeExtension3 } from "@weapp-core/shared";
16
+ import process from "node:process";
17
+ import { addExtension as addExtension3, defu as defu2, isObject as isObject2, removeExtension as removeExtension3 } from "@weapp-core/shared";
16
18
  import { watch } from "chokidar";
17
- import fs4 from "fs-extra";
19
+ import fs6 from "fs-extra";
18
20
  import path5 from "pathe";
19
21
  import { build as tsupBuild } from "tsup";
20
22
  import { build, loadConfigFromFile } from "vite";
@@ -33,8 +35,8 @@ function getWeappWatchOptions() {
33
35
 
34
36
  // src/plugins/index.ts
35
37
  import { addExtension as addExtension2, removeExtension as removeExtension2 } from "@weapp-core/shared";
36
- import fg from "fast-glob";
37
- import fs3 from "fs-extra";
38
+ import { fdir as Fdir } from "fdir";
39
+ import fs5 from "fs-extra";
38
40
  import MagicString from "magic-string";
39
41
  import path4 from "pathe";
40
42
  import { isCSSRequest } from "vite";
@@ -48,24 +50,41 @@ function createDebugger(namespace) {
48
50
  }
49
51
  }
50
52
 
51
- // src/entry.ts
52
- import process from "node:process";
53
- import { defu as defu2 } from "@weapp-core/shared";
54
- import { fdir as Fdir } from "fdir";
55
- import mm from "micromatch";
53
+ // src/utils/index.ts
54
+ import fs4 from "fs-extra";
56
55
  import path3 from "pathe";
57
56
 
58
- // src/utils/projectConfig.ts
57
+ // src/utils/json.ts
58
+ import { parse as parseJson, stringify } from "comment-json";
59
59
  import fs from "fs-extra";
60
+ function parseCommentJson(json) {
61
+ return parseJson(json, void 0, true);
62
+ }
63
+ async function readCommentJson(filepath) {
64
+ try {
65
+ return parseCommentJson(await fs.readFile(filepath, "utf8"));
66
+ } catch {
67
+ logger_default.error(`\u6B8B\u7834\u7684JSON\u6587\u4EF6: ${filepath}`);
68
+ }
69
+ }
70
+ function stringifyJson(value) {
71
+ return stringify(value, void 0, 2);
72
+ }
73
+ function resolveJson(value) {
74
+ return stringifyJson(value);
75
+ }
76
+
77
+ // src/utils/projectConfig.ts
78
+ import fs2 from "fs-extra";
60
79
  import path from "pathe";
61
80
  function getProjectConfig(root, options) {
62
81
  const baseJsonPath = path.resolve(root, "project.config.json");
63
82
  const privateJsonPath = path.resolve(root, "project.private.config.json");
64
83
  let baseJson = {};
65
84
  let privateJson = {};
66
- if (fs.existsSync(baseJsonPath)) {
85
+ if (fs2.existsSync(baseJsonPath)) {
67
86
  try {
68
- baseJson = fs.readJsonSync(baseJsonPath) || {};
87
+ baseJson = fs2.readJsonSync(baseJsonPath) || {};
69
88
  } catch {
70
89
  throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
71
90
  }
@@ -73,9 +92,9 @@ function getProjectConfig(root, options) {
73
92
  throw new Error(`\u5728 ${root} \u76EE\u5F55\u4E0B\u627E\u4E0D\u5230 project.config.json`);
74
93
  }
75
94
  if (!options?.ignorePrivate) {
76
- if (fs.existsSync(privateJsonPath)) {
95
+ if (fs2.existsSync(privateJsonPath)) {
77
96
  try {
78
- privateJson = fs.readJsonSync(privateJsonPath) || {};
97
+ privateJson = fs2.readJsonSync(privateJsonPath) || {};
79
98
  } catch {
80
99
  throw new Error(`\u89E3\u6790 json \u683C\u5F0F\u5931\u8D25, project.private.config.json \u4E3A\u975E\u6CD5\u7684 json \u683C\u5F0F`);
81
100
  }
@@ -86,212 +105,35 @@ function getProjectConfig(root, options) {
86
105
 
87
106
  // src/utils/scan.ts
88
107
  import { addExtension, defu, isObject, removeExtension } from "@weapp-core/shared";
89
- import fs2 from "fs-extra";
108
+ import fs3 from "fs-extra";
90
109
  import path2 from "pathe";
91
- function parseJsonUseComponents(json) {
92
- const deps = [];
93
- if (isObject(json.usingComponents)) {
94
- deps.push(...Object.values(json.usingComponents).map((x) => {
95
- return {
96
- path: x,
97
- type: "component"
98
- };
99
- }));
110
+
111
+ // src/utils/index.ts
112
+ function isJsOrTs(name) {
113
+ if (typeof name === "string") {
114
+ return jsExtensions.some((x) => name.endsWith(`.${x}`));
100
115
  }
101
- return deps;
116
+ return false;
102
117
  }
103
- function searchAppEntry(options) {
104
- const { formatPath, root } = defu(options, {
105
- formatPath: (x) => x
106
- });
107
- const appJsonPath = path2.resolve(root, "app.json");
108
- if (fs2.existsSync(appJsonPath)) {
109
- for (const ext of jsExtensions) {
110
- const entryPath = path2.resolve(root, addExtension("app", `.${ext}`));
111
- if (fs2.existsSync(entryPath)) {
112
- const appJson = fs2.readJSONSync(appJsonPath, { throws: false });
113
- const deps = [];
114
- if (appJson) {
115
- if (Array.isArray(appJson.pages)) {
116
- deps.push(...appJson.pages.map((x) => {
117
- return {
118
- path: x,
119
- type: "page"
120
- };
121
- }));
122
- }
123
- if (isObject(appJson.usingComponents)) {
124
- deps.push(...parseJsonUseComponents(appJson));
125
- }
126
- if (Array.isArray(appJson.subPackages)) {
127
- deps.push(...appJson.subPackages.map((x) => {
128
- return {
129
- type: "subPackage",
130
- ...x
131
- };
132
- }));
133
- }
134
- }
135
- return {
136
- jsonPath: formatPath(appJsonPath),
137
- json: appJson,
138
- path: formatPath(entryPath),
139
- deps,
140
- type: "app"
141
- };
142
- }
143
- }
118
+ function changeFileExtension(filePath, extension) {
119
+ if (typeof filePath !== "string") {
120
+ throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`);
144
121
  }
145
- }
146
- function searchPageEntry(wxmlPath) {
147
- if (fs2.existsSync(wxmlPath)) {
148
- const base = removeExtension(wxmlPath);
149
- for (const ext of jsExtensions) {
150
- const entryPath = addExtension(base, `.${ext}`);
151
- if (fs2.existsSync(entryPath)) {
152
- return entryPath;
153
- }
154
- }
122
+ if (typeof extension !== "string") {
123
+ throw new TypeError(`Expected \`extension\` to be a string, got \`${typeof extension}\`.`);
155
124
  }
156
- }
157
- function getWxmlEntry(wxmlPath, formatPath) {
158
- const pageEntry = searchPageEntry(wxmlPath);
159
- if (pageEntry) {
160
- const jsonPath = addExtension(removeExtension(wxmlPath), ".json");
161
- const finalPath = formatPath(pageEntry);
162
- if (fs2.existsSync(jsonPath)) {
163
- const json = fs2.readJsonSync(jsonPath, { throws: false });
164
- if (json && json.component) {
165
- return {
166
- deps: parseJsonUseComponents(json),
167
- path: finalPath,
168
- type: "component",
169
- json,
170
- jsonPath: formatPath(jsonPath)
171
- };
172
- } else {
173
- return {
174
- deps: parseJsonUseComponents(json),
175
- path: finalPath,
176
- type: "page",
177
- json,
178
- jsonPath: formatPath(jsonPath)
179
- };
180
- }
181
- }
182
- return {
183
- deps: [],
184
- path: finalPath,
185
- type: "page"
186
- // json: undefined,
187
- // jsonPath: undefined,
188
- };
125
+ if (filePath === "") {
126
+ return "";
189
127
  }
128
+ extension = extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
129
+ const basename = path3.basename(filePath, path3.extname(filePath));
130
+ return path3.join(path3.dirname(filePath), basename + extension);
190
131
  }
191
-
192
- // src/utils/index.ts
193
- var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
194
- var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
195
-
196
- // src/entry.ts
197
- function createFilter(include, exclude, options) {
198
- const opts = defu2(options, {
199
- ignore: exclude
200
- });
201
- return function(id) {
202
- if (typeof id !== "string") {
203
- return false;
204
- }
205
- if (/\0/.test(id)) {
206
- return false;
207
- }
208
- return mm.isMatch(id, include, opts);
209
- };
210
- }
211
- async function getEntries(options) {
212
- const { root = process.cwd(), outDir = "dist", relative, srcRoot = "", subPackage } = options;
213
- function formatPath(to) {
214
- if (relative) {
215
- return path3.relative(root, to);
216
- }
217
- return path3.normalize(to);
218
- }
219
- if (subPackage) {
220
- const subPackageRoot = subPackage.root ?? "";
221
- const filter = createFilter(
222
- [path3.join(root, srcRoot, subPackageRoot, "**/*")],
223
- [
224
- ...defaultExcluded
225
- ],
226
- { cwd: root }
227
- );
228
- const pageEntries = [];
229
- const componentEntries = [];
230
- const subPackageEntries = [];
231
- if (subPackage.entry) {
232
- subPackageEntries.push({
233
- deps: [],
234
- path: path3.join(root, subPackageRoot, subPackage.entry),
235
- type: "subPackageEntry"
236
- });
237
- }
238
- const files = await new Fdir().withFullPaths().filter(filter).crawl(path3.join(root, subPackageRoot)).withPromise();
239
- for (const file of files) {
240
- if (/\.wxml$/.test(file)) {
241
- const entry = getWxmlEntry(file, formatPath);
242
- if (entry) {
243
- if (entry.type === "component") {
244
- componentEntries.push(entry);
245
- } else if (entry.type === "page") {
246
- pageEntries.push(entry);
247
- }
248
- }
249
- }
250
- }
251
- return {
252
- pages: pageEntries,
253
- components: componentEntries,
254
- subPackageEntries
255
- };
256
- } else {
257
- const appEntry = searchAppEntry({
258
- root: path3.join(root, srcRoot),
259
- formatPath
260
- });
261
- if (appEntry) {
262
- const subPackageDeps = appEntry.deps.filter((x) => x.type === "subPackage");
263
- const filter = createFilter(
264
- [path3.join(root, srcRoot, "**/*")],
265
- [
266
- ...defaultExcluded,
267
- path3.join(root, `${outDir}/**`),
268
- ...subPackageDeps.map((x) => {
269
- return path3.join(root, `${x.root}/**`);
270
- })
271
- ],
272
- { cwd: root }
273
- );
274
- const pageEntries = [];
275
- const componentEntries = [];
276
- const files = await new Fdir().withFullPaths().filter(filter).crawl(path3.join(root, srcRoot)).withPromise();
277
- for (const file of files) {
278
- if (/\.wxml$/.test(file)) {
279
- const entry = getWxmlEntry(file, formatPath);
280
- if (entry) {
281
- if (entry.type === "component") {
282
- componentEntries.push(entry);
283
- } else if (entry.type === "page") {
284
- pageEntries.push(entry);
285
- }
286
- }
287
- }
288
- }
289
- return {
290
- app: appEntry,
291
- pages: pageEntries,
292
- components: componentEntries,
293
- subPackages: subPackageDeps
294
- };
132
+ async function findJsEntry(filepath) {
133
+ for (const ext of jsExtensions) {
134
+ const p = changeFileExtension(filepath, ext);
135
+ if (await fs4.exists(p)) {
136
+ return p;
295
137
  }
296
138
  }
297
139
  }
@@ -308,27 +150,17 @@ function parseRequest(id) {
308
150
  query
309
151
  };
310
152
  }
311
-
312
- // src/plugins/index.ts
313
- var debug = createDebugger("weapp-vite:plugin");
314
- function normalizeCssPath(id) {
315
- return addExtension2(removeExtension2(id), ".wxss");
316
- }
317
- function isJsOrTs(name) {
318
- if (typeof name === "string") {
319
- return jsExtensions.some((x) => name.endsWith(`.${x}`));
320
- }
321
- return false;
322
- }
323
- function getRealPath(res) {
153
+ function getCssRealPath(res) {
324
154
  if (res.query.wxss) {
325
- return addExtension2(removeExtension2(res.filename), ".wxss");
155
+ return changeFileExtension(res.filename, "wxss");
326
156
  }
327
157
  return res.filename;
328
158
  }
159
+
160
+ // src/plugins/index.ts
161
+ var debug = createDebugger("weapp-vite:plugin");
329
162
  function vitePluginWeapp(ctx) {
330
163
  let configResolved;
331
- let entriesSet = /* @__PURE__ */ new Set();
332
164
  function relative(p) {
333
165
  return path4.relative(configResolved.root, p);
334
166
  }
@@ -338,7 +170,6 @@ function vitePluginWeapp(ctx) {
338
170
  return acc;
339
171
  }, {});
340
172
  }
341
- let appEntry;
342
173
  return [
343
174
  {
344
175
  name: "weapp-vite:pre",
@@ -353,90 +184,55 @@ function vitePluginWeapp(ctx) {
353
184
  configResolved = config;
354
185
  },
355
186
  async options(options) {
356
- const { root, build: build2, weapp } = configResolved;
357
- const entries = await getEntries({
358
- root,
359
- outDir: build2.outDir,
360
- srcRoot: weapp?.srcRoot,
361
- subPackage: weapp?.subPackage
362
- });
363
- if (entries) {
364
- const paths = [];
365
- if (entries.app) {
366
- paths.push(entries.app.path);
367
- appEntry = entries.app;
368
- }
369
- paths.push(...[...entries.pages, ...entries.components].map((x) => {
370
- return x.path;
371
- }));
372
- if (entries.subPackageEntries) {
373
- paths.push(...entries.subPackageEntries.map((x) => {
374
- return x.path;
375
- }));
376
- }
377
- const input = getInputOption(paths);
378
- entriesSet = new Set(paths);
379
- options.input = input;
380
- if (weapp?.type === "app" && Array.isArray(entries.subPackages) && entries.subPackages.length) {
381
- for (const subPackage of entries.subPackages) {
382
- if (subPackage.root && !ctx.subPackageContextMap.has(subPackage.root)) {
383
- ctx.forkSubPackage(subPackage).build();
384
- }
385
- }
386
- }
387
- } else {
388
- throw new Error(`\u5728 ${path4.join(root, ctx.srcRoot ?? "")} \u76EE\u5F55\u4E0B\u6CA1\u6709\u627E\u5230 \`app.json\`, \u8BF7\u786E\u4FDD\u4F60\u521D\u59CB\u5316\u4E86\u5C0F\u7A0B\u5E8F\u9879\u76EE\uFF0C\u6216\u8005\u5728 \`vite.config.ts\` \u4E2D\u8BBE\u7F6E\u7684\u6B63\u786E\u7684 \`weapp.srcRoot\` \u914D\u7F6E\u8DEF\u5F84 `);
389
- }
187
+ await ctx.scanAppEntry();
188
+ const input = getInputOption([...ctx.entriesSet]);
189
+ options.input = input;
390
190
  },
391
191
  async buildStart() {
392
- const { root, build: build2, weapp } = configResolved;
393
- let cwd = root;
192
+ const { build: build2 } = configResolved;
394
193
  const ignore = [
395
194
  ...defaultExcluded
396
195
  ];
397
- const isSubPackage = weapp?.type === "subPackage";
398
- if (isSubPackage) {
399
- cwd = path4.join(root, ctx.subPackage.root);
400
- } else {
401
- const ignoreSubPackage = appEntry ? appEntry.deps.filter(
402
- (x) => x.type === "subPackage"
403
- ).map((x) => {
404
- return `${x.root}/**`;
405
- }) : [];
406
- ignore.push(
407
- ...[
408
- `${build2.outDir}/**`,
409
- ...ignoreSubPackage,
410
- "project.config.json",
411
- "project.private.config.json",
412
- "package.json",
413
- "tsconfig.json",
414
- "tsconfig.node.json"
415
- ]
416
- );
417
- }
418
- const files = await fg(
419
- // 假如去 join root 就是返回 absolute
420
- [path4.join(ctx.srcRoot ?? "", "**/*.{wxml,json,wxs,png,jpg,jpeg,gif,svg,webp}")],
196
+ ignore.push(
197
+ ...[
198
+ `${build2.outDir}/**`,
199
+ "project.config.json",
200
+ "project.private.config.json",
201
+ "package.json",
202
+ "tsconfig.json",
203
+ "tsconfig.node.json"
204
+ ]
205
+ );
206
+ const relFiles = await new Fdir().withRelativePaths().globWithOptions(
207
+ [path4.join(ctx.srcRoot ?? "", "**/*.{wxml,wxs,png,jpg,jpeg,gif,svg,webp}")],
421
208
  {
422
- cwd,
423
- ignore,
424
- absolute: false
209
+ cwd: ctx.cwd,
210
+ ignore
425
211
  }
426
- );
427
- const relFiles = files.map((x) => {
428
- return isSubPackage ? path4.join(weapp?.subPackage?.root ?? "", x) : x;
429
- });
212
+ ).crawl(ctx.cwd).withPromise();
430
213
  for (const file of relFiles) {
431
214
  const filepath = path4.resolve(ctx.cwd, file);
432
215
  this.addWatchFile(filepath);
433
- const isMedia = !/\.(?:wxml|json|wxs)$/.test(file);
216
+ const isMedia = !/\.(?:wxml|wxs)$/.test(file);
434
217
  this.emitFile({
435
218
  type: "asset",
436
219
  fileName: ctx.relativeSrcRoot(file),
437
- source: isMedia ? await fs3.readFile(filepath) : await fs3.readFile(filepath, "utf8")
220
+ source: isMedia ? await fs5.readFile(filepath) : await fs5.readFile(filepath, "utf8")
438
221
  });
439
222
  }
223
+ for (const entry of ctx.entries) {
224
+ if (entry.jsonPath) {
225
+ this.addWatchFile(entry.jsonPath);
226
+ if (entry.json) {
227
+ const fileName = ctx.relativeSrcRoot(path4.relative(ctx.cwd, entry.jsonPath));
228
+ this.emitFile({
229
+ type: "asset",
230
+ fileName,
231
+ source: resolveJson(entry.json)
232
+ });
233
+ }
234
+ }
235
+ }
440
236
  },
441
237
  resolveId(source) {
442
238
  if (/\.wxss$/.test(source)) {
@@ -444,26 +240,25 @@ function vitePluginWeapp(ctx) {
444
240
  }
445
241
  },
446
242
  async load(id) {
447
- if (entriesSet.has(id)) {
243
+ if (ctx.entriesSet.has(id)) {
448
244
  const base = removeExtension2(id);
449
- const ms = new MagicString(fs3.readFileSync(id, "utf8"));
245
+ const ms = new MagicString(fs5.readFileSync(id, "utf8"));
450
246
  for (const ext of supportedCssExtensions) {
451
247
  const mayBeCssPath = addExtension2(base, ext);
452
- if (fs3.existsSync(mayBeCssPath)) {
248
+ if (fs5.existsSync(mayBeCssPath)) {
453
249
  this.addWatchFile(mayBeCssPath);
454
250
  ms.prepend(`import '${mayBeCssPath}'
455
251
  `);
456
252
  }
457
253
  }
458
- this.addWatchFile(id);
459
254
  return {
460
255
  code: ms.toString()
461
256
  };
462
257
  } else if (isCSSRequest(id)) {
463
258
  const parsed = parseRequest(id);
464
- const realPath = getRealPath(parsed);
465
- if (await fs3.exists(realPath)) {
466
- const css = await fs3.readFile(realPath, "utf8");
259
+ const realPath = getCssRealPath(parsed);
260
+ if (await fs5.exists(realPath)) {
261
+ const css = await fs5.readFile(realPath, "utf8");
467
262
  return {
468
263
  code: css
469
264
  };
@@ -479,7 +274,9 @@ function vitePluginWeapp(ctx) {
479
274
  for (const bundleKey of bundleKeys) {
480
275
  const asset = bundle[bundleKey];
481
276
  if (bundleKey.endsWith(".css") && asset.type === "asset" && typeof asset.originalFileName === "string" && isJsOrTs(asset.originalFileName)) {
482
- const newFileName = ctx.relativeSrcRoot(normalizeCssPath(asset.originalFileName));
277
+ const newFileName = ctx.relativeSrcRoot(
278
+ changeFileExtension(asset.originalFileName, "wxss")
279
+ );
483
280
  this.emitFile({
484
281
  type: "asset",
485
282
  fileName: newFileName,
@@ -505,7 +302,7 @@ function vitePluginWeapp(ctx) {
505
302
 
506
303
  // src/context.ts
507
304
  var require2 = createRequire(import.meta.url);
508
- var CompilerContext = class _CompilerContext {
305
+ var CompilerContext = class {
509
306
  inlineConfig;
510
307
  cwd;
511
308
  isDev;
@@ -517,9 +314,12 @@ var CompilerContext = class _CompilerContext {
517
314
  subPackageContextMap;
518
315
  type;
519
316
  parent;
317
+ entriesSet;
318
+ entries;
319
+ appEntry;
520
320
  constructor(options) {
521
- const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, subPackage, type } = defu3(options, {
522
- cwd: process2.cwd(),
321
+ const { cwd, isDev, inlineConfig, projectConfig, mode, packageJson, subPackage, type } = defu2(options, {
322
+ cwd: process.cwd(),
523
323
  isDev: false,
524
324
  projectConfig: {},
525
325
  type: "app",
@@ -537,9 +337,11 @@ var CompilerContext = class _CompilerContext {
537
337
  this.watcherMap = /* @__PURE__ */ new Map();
538
338
  this.subPackageContextMap = /* @__PURE__ */ new Map();
539
339
  this.type = type;
340
+ this.entriesSet = /* @__PURE__ */ new Set();
341
+ this.entries = [];
540
342
  }
541
343
  get srcRoot() {
542
- return this.inlineConfig?.weapp?.srcRoot;
344
+ return this.inlineConfig?.weapp?.srcRoot ?? "";
543
345
  }
544
346
  relativeSrcRoot(p) {
545
347
  if (this.srcRoot) {
@@ -550,20 +352,6 @@ var CompilerContext = class _CompilerContext {
550
352
  get mpDistRoot() {
551
353
  return this.projectConfig.miniprogramRoot || this.projectConfig.srcMiniprogramRoot || "";
552
354
  }
553
- forkSubPackage(subPackage) {
554
- const ctx = new _CompilerContext({
555
- cwd: this.cwd,
556
- isDev: this.isDev,
557
- projectConfig: this.projectConfig,
558
- inlineConfig: this.inlineConfig,
559
- type: "subPackage",
560
- mode: this.mode,
561
- subPackage
562
- });
563
- this.subPackageContextMap.set(subPackage.root, ctx);
564
- ctx.parent = this;
565
- return ctx;
566
- }
567
355
  async internalDev(inlineConfig) {
568
356
  const rollupWatcher = await build(
569
357
  inlineConfig
@@ -575,10 +363,10 @@ var CompilerContext = class _CompilerContext {
575
363
  return rollupWatcher;
576
364
  }
577
365
  async runDev() {
578
- if (process2.env.NODE_ENV === void 0) {
579
- process2.env.NODE_ENV = "development";
366
+ if (process.env.NODE_ENV === void 0) {
367
+ process.env.NODE_ENV = "development";
580
368
  }
581
- const inlineConfig = defu3(
369
+ const inlineConfig = defu2(
582
370
  this.inlineConfig,
583
371
  {
584
372
  root: this.cwd,
@@ -606,6 +394,8 @@ var CompilerContext = class _CompilerContext {
606
394
  }).on("ready", async () => {
607
395
  await this.internalDev(inlineConfig2);
608
396
  isReady = true;
397
+ logger_default.success("\u5E94\u7528\u6784\u5EFA\u5B8C\u6210\uFF01\u6267\u884C `npm run open` \u6253\u5F00\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177");
398
+ logger_default.success("\u6216\u8005\u4F7F\u7528\u5FAE\u4FE1\u5F00\u53D1\u8005\u5DE5\u5177\uFF0C\u5BFC\u5165\u6839\u76EE\u5F55 (`project.config.json` \u6240\u5728\u76EE\u5F55) \u67E5\u770B\u6548\u679C");
609
399
  });
610
400
  return watcher;
611
401
  };
@@ -617,7 +407,7 @@ var CompilerContext = class _CompilerContext {
617
407
  subPackage: this.subPackage
618
408
  }
619
409
  });
620
- const { paths, ...opts } = defu3(
410
+ const { paths, ...opts } = defu2(
621
411
  subPackageInlineConfig.weapp?.watch,
622
412
  {
623
413
  cwd: path5.join(this.cwd, subPackageInlineConfig.weapp.srcRoot ?? "", this.subPackage.root)
@@ -628,7 +418,7 @@ var CompilerContext = class _CompilerContext {
628
418
  this.watcherMap.set(this.subPackage.root, watcher);
629
419
  return watcher;
630
420
  } else if (this.type === "app") {
631
- const { paths, ...opts } = defu3(
421
+ const { paths, ...opts } = defu2(
632
422
  inlineConfig.weapp?.watch,
633
423
  {
634
424
  ignored: [
@@ -644,7 +434,7 @@ var CompilerContext = class _CompilerContext {
644
434
  }
645
435
  }
646
436
  async runProd() {
647
- const inlineConfig = defu3(
437
+ const inlineConfig = defu2(
648
438
  this.inlineConfig,
649
439
  {
650
440
  root: this.cwd,
@@ -655,6 +445,7 @@ var CompilerContext = class _CompilerContext {
655
445
  }
656
446
  }
657
447
  );
448
+ inlineConfig.logLevel = "info";
658
449
  if (this.type === "subPackage" && this.subPackage) {
659
450
  const subPackageInlineConfig = Object.assign({}, inlineConfig, {
660
451
  weapp: {
@@ -686,8 +477,8 @@ var CompilerContext = class _CompilerContext {
686
477
  this.projectConfig = projectConfig;
687
478
  const packageJsonPath = path5.resolve(this.cwd, "package.json");
688
479
  const external = [];
689
- if (await fs4.exists(packageJsonPath)) {
690
- const localPackageJson = await fs4.readJson(packageJsonPath, {
480
+ if (await fs6.exists(packageJsonPath)) {
481
+ const localPackageJson = await fs6.readJson(packageJsonPath, {
691
482
  throws: false
692
483
  }) || {};
693
484
  this.packageJson = localPackageJson;
@@ -699,7 +490,7 @@ var CompilerContext = class _CompilerContext {
699
490
  command: this.isDev ? "serve" : "build",
700
491
  mode: this.mode
701
492
  }, void 0, this.cwd);
702
- this.inlineConfig = defu3({
493
+ this.inlineConfig = defu2({
703
494
  mode: this.mode,
704
495
  build: {
705
496
  rollupOptions: {
@@ -726,7 +517,7 @@ var CompilerContext = class _CompilerContext {
726
517
  include: void 0
727
518
  }
728
519
  },
729
- logLevel: "info",
520
+ logLevel: "warn",
730
521
  plugins: [
731
522
  tsconfigPaths()
732
523
  ],
@@ -738,7 +529,7 @@ var CompilerContext = class _CompilerContext {
738
529
  // miniprogram
739
530
  // https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html#%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6%E7%9B%B8%E5%85%B3%E7%A4%BA%E4%BE%8B
740
531
  async buildNpm(options) {
741
- const { sourcemap } = defu3(options, { sourcemap: true });
532
+ const { sourcemap } = defu2(options, { sourcemap: true });
742
533
  let packNpmRelationList = [];
743
534
  if (this.projectConfig.setting?.packNpmManually && Array.isArray(this.projectConfig.setting.packNpmRelationList)) {
744
535
  packNpmRelationList = this.projectConfig.setting.packNpmRelationList;
@@ -752,8 +543,8 @@ var CompilerContext = class _CompilerContext {
752
543
  }
753
544
  for (const relation of packNpmRelationList) {
754
545
  const packageJsonPath = path5.resolve(this.cwd, relation.packageJsonPath);
755
- if (await fs4.exists(packageJsonPath)) {
756
- const pkgJson = await fs4.readJson(packageJsonPath);
546
+ if (await fs6.exists(packageJsonPath)) {
547
+ const pkgJson = await fs6.readJson(packageJsonPath);
757
548
  const outDir = path5.resolve(this.cwd, relation.miniprogramNpmDistDir, "miniprogram_npm");
758
549
  if (pkgJson.dependencies) {
759
550
  const dependencies = Object.keys(pkgJson.dependencies);
@@ -763,7 +554,7 @@ var CompilerContext = class _CompilerContext {
763
554
  const targetJson = require2(id);
764
555
  if ("miniprogram" in targetJson && targetJson.miniprogram) {
765
556
  const targetJsonPath = require2.resolve(id);
766
- await fs4.copy(
557
+ await fs6.copy(
767
558
  path5.resolve(
768
559
  path5.dirname(targetJsonPath),
769
560
  targetJson.miniprogram
@@ -785,6 +576,7 @@ var CompilerContext = class _CompilerContext {
785
576
  };
786
577
  },
787
578
  sourcemap
579
+ // clean: false,
788
580
  });
789
581
  }
790
582
  logger_default.success(`${dep} \u4F9D\u8D56\u5904\u7406\u5B8C\u6210!`);
@@ -794,10 +586,101 @@ var CompilerContext = class _CompilerContext {
794
586
  }
795
587
  }
796
588
  }
589
+ async usingComponentsHandler(usingComponents, dirname) {
590
+ if (usingComponents) {
591
+ for (const componentUrl of Object.values(usingComponents)) {
592
+ if (/plugin:\/\//.test(componentUrl)) {
593
+ continue;
594
+ }
595
+ const tokens = componentUrl.split("/");
596
+ if (tokens[0] && isObject2(this.packageJson.dependencies) && Reflect.has(this.packageJson.dependencies, tokens[0])) {
597
+ continue;
598
+ } else if (tokens[0] === "") {
599
+ await this.scanComponentEntry(componentUrl.substring(1), path5.resolve(this.cwd, this.srcRoot));
600
+ } else {
601
+ await this.scanComponentEntry(componentUrl, dirname);
602
+ }
603
+ }
604
+ }
605
+ }
606
+ async scanAppEntry() {
607
+ const appDirname = path5.resolve(this.cwd, this.srcRoot);
608
+ const appConfigFile = path5.resolve(appDirname, "app.json");
609
+ const appEntry = await findJsEntry(appConfigFile);
610
+ if (appEntry && await fs6.exists(appConfigFile)) {
611
+ const config = await readCommentJson(appConfigFile);
612
+ if (isObject2(config)) {
613
+ this.entriesSet.add(appEntry);
614
+ this.appEntry = {
615
+ path: appEntry,
616
+ json: config,
617
+ jsonPath: appConfigFile
618
+ };
619
+ this.entries.push(this.appEntry);
620
+ const { pages, usingComponents, subpackages = [], subPackages = [] } = config;
621
+ const subs = [...subpackages, ...subPackages];
622
+ await this.usingComponentsHandler(usingComponents, appDirname);
623
+ if (Array.isArray(pages)) {
624
+ for (const page of pages) {
625
+ await this.scanComponentEntry(page, appDirname);
626
+ }
627
+ }
628
+ for (const sub of subs) {
629
+ if (Array.isArray(sub.pages)) {
630
+ for (const page of sub.pages) {
631
+ await this.scanComponentEntry(path5.join(sub.root, page), appDirname);
632
+ }
633
+ }
634
+ if (sub.entry) {
635
+ await this.scanComponentEntry(path5.join(sub.root, sub.entry), appDirname);
636
+ }
637
+ }
638
+ }
639
+ } else {
640
+ throw new Error(`\u5728 ${appDirname} \u76EE\u5F55\u4E0B\u6CA1\u6709\u627E\u5230 \`app.json\`, \u8BF7\u786E\u4FDD\u4F60\u521D\u59CB\u5316\u4E86\u5C0F\u7A0B\u5E8F\u9879\u76EE\uFF0C\u6216\u8005\u5728 \`vite.config.ts\` \u4E2D\u8BBE\u7F6E\u7684\u6B63\u786E\u7684 \`weapp.srcRoot\` \u914D\u7F6E\u8DEF\u5F84 `);
641
+ }
642
+ }
643
+ // usingComponents
644
+ // subpackages / subPackages
645
+ // pages
646
+ // https://developers.weixin.qq.com/miniprogram/dev/framework/structure.html
647
+ // 页面可以没有 JSON
648
+ async scanComponentEntry(componentEntry, dirname) {
649
+ const entry = path5.resolve(dirname, componentEntry);
650
+ const jsEntry = await findJsEntry(entry);
651
+ if (jsEntry) {
652
+ this.entriesSet.add(jsEntry);
653
+ }
654
+ const configFile = changeFileExtension(entry, "json");
655
+ if (await fs6.exists(configFile)) {
656
+ const config = await readCommentJson(configFile);
657
+ if (jsEntry) {
658
+ this.entries.push({
659
+ path: jsEntry,
660
+ json: config,
661
+ jsonPath: configFile
662
+ });
663
+ }
664
+ if (isObject2(config)) {
665
+ const { usingComponents } = config;
666
+ await this.usingComponentsHandler(usingComponents, path5.dirname(configFile));
667
+ }
668
+ } else if (jsEntry) {
669
+ this.entries.push({
670
+ path: jsEntry
671
+ });
672
+ }
673
+ }
797
674
  };
675
+ async function createCompilerContext(options) {
676
+ const ctx = new CompilerContext(options);
677
+ await ctx.loadDefaultConfig();
678
+ return ctx;
679
+ }
798
680
 
799
681
  export {
800
682
  logger_default,
801
683
  VERSION,
802
- CompilerContext
684
+ CompilerContext,
685
+ createCompilerContext
803
686
  };