weapp-vite 0.0.2-alpha.1 → 0.0.2-alpha.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.
@@ -0,0 +1,333 @@
1
+ // src/build.ts
2
+ import { build } from "vite";
3
+ import mm from "micromatch";
4
+ import { defu as defu2 } from "@weapp-core/shared";
5
+ import path3 from "pathe";
6
+
7
+ // src/utils/scan.ts
8
+ import path from "pathe";
9
+ import fs from "fs-extra";
10
+ import klaw from "klaw";
11
+ import { addExtension, removeExtension } from "@weapp-core/shared";
12
+ var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
13
+ function searchAppEntry(root) {
14
+ const extensions = ["js", "ts"];
15
+ const appJson = path.resolve(root, "app.json");
16
+ if (fs.existsSync(appJson)) {
17
+ for (const ext of extensions) {
18
+ const entryPath = path.resolve(root, addExtension("app", `.${ext}`));
19
+ if (fs.existsSync(entryPath)) {
20
+ return entryPath;
21
+ }
22
+ }
23
+ }
24
+ }
25
+ function searchPageEntry(wxmlPath) {
26
+ if (fs.existsSync(wxmlPath)) {
27
+ const extensions = ["js", "ts"];
28
+ const base = removeExtension(wxmlPath);
29
+ for (const ext of extensions) {
30
+ const entryPath = addExtension(base, `.${ext}`);
31
+ if (fs.existsSync(entryPath)) {
32
+ return entryPath;
33
+ }
34
+ }
35
+ }
36
+ }
37
+ function getWxmlEntry(wxmlPath) {
38
+ const pageEntry = searchPageEntry(wxmlPath);
39
+ if (pageEntry) {
40
+ const jsonPath = addExtension(removeExtension(wxmlPath), ".json");
41
+ if (fs.existsSync(jsonPath)) {
42
+ const json = fs.readJsonSync(jsonPath, { throws: false });
43
+ if (json && json.component) {
44
+ return {
45
+ path: pageEntry,
46
+ type: "component"
47
+ };
48
+ }
49
+ }
50
+ return {
51
+ path: pageEntry,
52
+ type: "page"
53
+ };
54
+ }
55
+ }
56
+ async function scanEntries(root, options) {
57
+ const appEntry = searchAppEntry(root);
58
+ function getPath(to) {
59
+ if (options?.relative) {
60
+ return path.relative(root, to);
61
+ }
62
+ return to;
63
+ }
64
+ if (appEntry) {
65
+ const pageEntries = /* @__PURE__ */ new Set();
66
+ const componentEntries = /* @__PURE__ */ new Set();
67
+ for await (const file of klaw(root, {
68
+ filter: options?.filter
69
+ })) {
70
+ if (file.stats.isFile()) {
71
+ if (/\.wxml$/.test(file.path)) {
72
+ const entry = getWxmlEntry(file.path);
73
+ if (entry) {
74
+ if (entry.type === "component") {
75
+ componentEntries.add(getPath(entry.path));
76
+ } else if (entry.type === "page") {
77
+ pageEntries.add(getPath(entry.path));
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+ const app = getPath(appEntry);
84
+ return {
85
+ app,
86
+ pages: pageEntries,
87
+ components: componentEntries,
88
+ all: [app, ...pageEntries, ...componentEntries]
89
+ // css: [...cssEntries],
90
+ };
91
+ }
92
+ }
93
+
94
+ // src/utils/index.ts
95
+ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
96
+ var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
97
+
98
+ // src/plugins/index.ts
99
+ import path2 from "node:path";
100
+ import fs2 from "fs-extra";
101
+ import MagicString from "magic-string";
102
+ import { addExtension as addExtension2, defu, removeExtension as removeExtension2 } from "@weapp-core/shared";
103
+ import fg from "fast-glob";
104
+ import { isCSSRequest, preprocessCSS } from "vite";
105
+ function parseRequest(id) {
106
+ const [filename, rawQuery] = id.split(`?`, 2);
107
+ const query = Object.fromEntries(new URLSearchParams(rawQuery));
108
+ if (query.wxss !== null) {
109
+ query.wxss = true;
110
+ }
111
+ return {
112
+ filename,
113
+ query
114
+ };
115
+ }
116
+ function normalizeCssPath(id) {
117
+ return addExtension2(removeExtension2(id), ".wxss");
118
+ }
119
+ function getRealPath(res) {
120
+ if (res.query.wxss) {
121
+ return addExtension2(removeExtension2(res.filename), ".wxss");
122
+ }
123
+ return res.filename;
124
+ }
125
+ function vitePluginWeapp(options) {
126
+ const { cwd, entries: _entries, src } = defu(options, {
127
+ src: ""
128
+ });
129
+ function relative(p) {
130
+ return path2.relative(cwd, p);
131
+ }
132
+ const input = _entries.reduce((acc, cur) => {
133
+ acc[relative(cur)] = cur;
134
+ return acc;
135
+ }, {});
136
+ const entries = Array.isArray(_entries) ? new Set(_entries) : _entries;
137
+ const stylesIds = /* @__PURE__ */ new Set();
138
+ let configResolved;
139
+ return [
140
+ {
141
+ name: "weapp-vite:pre",
142
+ enforce: "pre",
143
+ // config(config, env) {
144
+ // console.log(config, env)
145
+ // },
146
+ // options(options) {
147
+ // console.log(options)
148
+ // },
149
+ configResolved(config) {
150
+ config.build.rollupOptions.input = input;
151
+ configResolved = config;
152
+ },
153
+ resolveId(source) {
154
+ if (/\.wxss$/.test(source)) {
155
+ return source.replace(/\.wxss$/, ".css?wxss");
156
+ }
157
+ },
158
+ load(id) {
159
+ if (entries.has(id)) {
160
+ const base = removeExtension2(id);
161
+ const ms = new MagicString(fs2.readFileSync(id, "utf8"));
162
+ for (const ext of supportedCssExtensions) {
163
+ const mayBeCssPath = addExtension2(base, ext);
164
+ if (fs2.existsSync(mayBeCssPath)) {
165
+ this.addWatchFile(mayBeCssPath);
166
+ ms.prepend(`import '${mayBeCssPath}'
167
+ `);
168
+ }
169
+ }
170
+ this.addWatchFile(id);
171
+ return {
172
+ code: ms.toString()
173
+ };
174
+ } else if (isCSSRequest(id)) {
175
+ stylesIds.add(id);
176
+ return {
177
+ code: ""
178
+ };
179
+ }
180
+ },
181
+ async buildEnd() {
182
+ const styles = {};
183
+ for (const stylesId of stylesIds) {
184
+ const parsed = parseRequest(stylesId);
185
+ const css = await fs2.readFile(getRealPath(parsed), "utf8");
186
+ const res = await preprocessCSS(css, stylesId, configResolved);
187
+ const fileName = relative(normalizeCssPath(stylesId));
188
+ if (styles[fileName]) {
189
+ styles[fileName] += res.code;
190
+ } else {
191
+ styles[fileName] = res.code;
192
+ }
193
+ }
194
+ for (const style of Object.entries(styles)) {
195
+ this.emitFile({
196
+ type: "asset",
197
+ fileName: style[0],
198
+ source: style[1]
199
+ });
200
+ }
201
+ const files = await fg(
202
+ [path2.join(src, "**/*.{wxml,json,png,jpg,jpeg,gif,svg,webp}")],
203
+ {
204
+ cwd,
205
+ ignore: [
206
+ ...defaultExcluded,
207
+ "dist/**",
208
+ "project.config.json",
209
+ "project.private.config.json",
210
+ "package.json"
211
+ ]
212
+ }
213
+ );
214
+ for (const file of files) {
215
+ const filepath = path2.resolve(cwd, file);
216
+ this.addWatchFile(filepath);
217
+ this.emitFile({
218
+ type: "asset",
219
+ fileName: file,
220
+ source: await fs2.readFile(filepath)
221
+ });
222
+ }
223
+ }
224
+ // generateBundle(_options, _bundle) {
225
+ // const files = this.getWatchFiles()
226
+ // console.log(files)
227
+ // },
228
+ },
229
+ {
230
+ name: "weapp-vite"
231
+ },
232
+ {
233
+ name: "weapp-vite:post",
234
+ enforce: "post"
235
+ }
236
+ ];
237
+ }
238
+
239
+ // src/build.ts
240
+ function createFilter(include, exclude, options) {
241
+ const opts = defu2(options, {
242
+ ignore: exclude
243
+ // dot: true,
244
+ // contains: true,
245
+ });
246
+ return function(id) {
247
+ if (typeof id !== "string") {
248
+ return false;
249
+ }
250
+ if (/\0/.test(id)) {
251
+ return false;
252
+ }
253
+ return mm.isMatch(id, include, opts);
254
+ };
255
+ }
256
+ function getEntries(options) {
257
+ let cwd;
258
+ let relative;
259
+ if (typeof options === "string") {
260
+ cwd = options;
261
+ } else {
262
+ cwd = options.cwd;
263
+ relative = options.relative;
264
+ }
265
+ const filter = createFilter(["**/*"], [...defaultExcluded, path3.resolve(cwd, "dist/**")], { cwd });
266
+ return scanEntries(cwd, { filter, relative });
267
+ }
268
+ function getDefaultConfig(options) {
269
+ return {
270
+ build: {
271
+ rollupOptions: {
272
+ output: {
273
+ format: "cjs",
274
+ entryFileNames: (chunkInfo) => {
275
+ return chunkInfo.name;
276
+ }
277
+ }
278
+ },
279
+ assetsDir: ".",
280
+ commonjsOptions: {
281
+ transformMixedEsModules: true,
282
+ include: void 0
283
+ }
284
+ },
285
+ plugins: [
286
+ vitePluginWeapp(options)
287
+ ]
288
+ };
289
+ }
290
+ async function runDev(cwd, options) {
291
+ const entries = await getEntries(cwd);
292
+ if (entries) {
293
+ const watcher = await build(
294
+ defu2(
295
+ options,
296
+ getDefaultConfig({
297
+ cwd,
298
+ entries: entries.all
299
+ }),
300
+ {
301
+ build: {
302
+ watch: {},
303
+ minify: false
304
+ }
305
+ }
306
+ )
307
+ );
308
+ return watcher;
309
+ }
310
+ }
311
+ async function runProd(cwd, options) {
312
+ const entries = await getEntries(cwd);
313
+ if (entries) {
314
+ const output = await build(
315
+ defu2(
316
+ options,
317
+ getDefaultConfig({
318
+ cwd,
319
+ entries: entries.all
320
+ })
321
+ )
322
+ );
323
+ return output;
324
+ }
325
+ }
326
+
327
+ export {
328
+ createFilter,
329
+ getEntries,
330
+ getDefaultConfig,
331
+ runDev,
332
+ runProd
333
+ };
package/dist/cli.cjs CHANGED
@@ -26,6 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  var import_node_process = __toESM(require("process"), 1);
27
27
  var import_commander = require("commander");
28
28
  var import_init = require("@weapp-core/init");
29
+ var import_weapp_ide_cli = require("weapp-ide-cli");
29
30
 
30
31
  // src/build.ts
31
32
  var import_vite2 = require("vite");
@@ -169,6 +170,12 @@ function vitePluginWeapp(options) {
169
170
  {
170
171
  name: "weapp-vite:pre",
171
172
  enforce: "pre",
173
+ // config(config, env) {
174
+ // console.log(config, env)
175
+ // },
176
+ // options(options) {
177
+ // console.log(options)
178
+ // },
172
179
  configResolved(config) {
173
180
  config.build.rollupOptions.input = input;
174
181
  configResolved = config;
@@ -347,6 +354,10 @@ async function runProd(cwd2, options) {
347
354
  }
348
355
  }
349
356
 
357
+ // src/logger.ts
358
+ var import_consola = require("consola");
359
+ var logger_default = (0, import_consola.createConsola)();
360
+
350
361
  // src/cli.ts
351
362
  var cwd = import_node_process.default.cwd();
352
363
  import_commander.program.command("dev").action(async () => {
@@ -361,4 +372,13 @@ import_commander.program.command("init").action(() => {
361
372
  command: "weapp-vite"
362
373
  });
363
374
  });
375
+ import_commander.program.command("open").action(async () => {
376
+ try {
377
+ await (0, import_weapp_ide_cli.parse)(["open", "-p"]);
378
+ } catch (error) {
379
+ logger_default.error(error);
380
+ } finally {
381
+ import_node_process.default.exit();
382
+ }
383
+ });
364
384
  import_commander.program.parse();
package/dist/cli.js CHANGED
@@ -1,327 +1,17 @@
1
+ import {
2
+ runDev,
3
+ runProd
4
+ } from "./chunk-AZKQ5D5L.js";
5
+
1
6
  // src/cli.ts
2
7
  import process from "node:process";
3
8
  import { program } from "commander";
4
9
  import { initConfig } from "@weapp-core/init";
10
+ import { parse } from "weapp-ide-cli";
5
11
 
6
- // src/build.ts
7
- import { build } from "vite";
8
- import mm from "micromatch";
9
- import { defu as defu2 } from "@weapp-core/shared";
10
- import path3 from "pathe";
11
-
12
- // src/utils/scan.ts
13
- import path from "pathe";
14
- import fs from "fs-extra";
15
- import klaw from "klaw";
16
- import { addExtension, removeExtension } from "@weapp-core/shared";
17
- var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
18
- function searchAppEntry(root) {
19
- const extensions = ["js", "ts"];
20
- const appJson = path.resolve(root, "app.json");
21
- if (fs.existsSync(appJson)) {
22
- for (const ext of extensions) {
23
- const entryPath = path.resolve(root, addExtension("app", `.${ext}`));
24
- if (fs.existsSync(entryPath)) {
25
- return entryPath;
26
- }
27
- }
28
- }
29
- }
30
- function searchPageEntry(wxmlPath) {
31
- if (fs.existsSync(wxmlPath)) {
32
- const extensions = ["js", "ts"];
33
- const base = removeExtension(wxmlPath);
34
- for (const ext of extensions) {
35
- const entryPath = addExtension(base, `.${ext}`);
36
- if (fs.existsSync(entryPath)) {
37
- return entryPath;
38
- }
39
- }
40
- }
41
- }
42
- function getWxmlEntry(wxmlPath) {
43
- const pageEntry = searchPageEntry(wxmlPath);
44
- if (pageEntry) {
45
- const jsonPath = addExtension(removeExtension(wxmlPath), ".json");
46
- if (fs.existsSync(jsonPath)) {
47
- const json = fs.readJsonSync(jsonPath, { throws: false });
48
- if (json && json.component) {
49
- return {
50
- path: pageEntry,
51
- type: "component"
52
- };
53
- }
54
- }
55
- return {
56
- path: pageEntry,
57
- type: "page"
58
- };
59
- }
60
- }
61
- async function scanEntries(root, options) {
62
- const appEntry = searchAppEntry(root);
63
- function getPath(to) {
64
- if (options?.relative) {
65
- return path.relative(root, to);
66
- }
67
- return to;
68
- }
69
- if (appEntry) {
70
- const pageEntries = /* @__PURE__ */ new Set();
71
- const componentEntries = /* @__PURE__ */ new Set();
72
- for await (const file of klaw(root, {
73
- filter: options?.filter
74
- })) {
75
- if (file.stats.isFile()) {
76
- if (/\.wxml$/.test(file.path)) {
77
- const entry = getWxmlEntry(file.path);
78
- if (entry) {
79
- if (entry.type === "component") {
80
- componentEntries.add(getPath(entry.path));
81
- } else if (entry.type === "page") {
82
- pageEntries.add(getPath(entry.path));
83
- }
84
- }
85
- }
86
- }
87
- }
88
- const app = getPath(appEntry);
89
- return {
90
- app,
91
- pages: pageEntries,
92
- components: componentEntries,
93
- all: [app, ...pageEntries, ...componentEntries]
94
- // css: [...cssEntries],
95
- };
96
- }
97
- }
98
-
99
- // src/utils/index.ts
100
- var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
101
- var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
102
-
103
- // src/plugins/index.ts
104
- import path2 from "node:path";
105
- import fs2 from "fs-extra";
106
- import MagicString from "magic-string";
107
- import { addExtension as addExtension2, defu, removeExtension as removeExtension2 } from "@weapp-core/shared";
108
- import fg from "fast-glob";
109
- import { isCSSRequest, preprocessCSS } from "vite";
110
- function parseRequest(id) {
111
- const [filename, rawQuery] = id.split(`?`, 2);
112
- const query = Object.fromEntries(new URLSearchParams(rawQuery));
113
- if (query.wxss !== null) {
114
- query.wxss = true;
115
- }
116
- return {
117
- filename,
118
- query
119
- };
120
- }
121
- function normalizeCssPath(id) {
122
- return addExtension2(removeExtension2(id), ".wxss");
123
- }
124
- function getRealPath(res) {
125
- if (res.query.wxss) {
126
- return addExtension2(removeExtension2(res.filename), ".wxss");
127
- }
128
- return res.filename;
129
- }
130
- function vitePluginWeapp(options) {
131
- const { cwd: cwd2, entries: _entries, src } = defu(options, {
132
- src: ""
133
- });
134
- function relative(p) {
135
- return path2.relative(cwd2, p);
136
- }
137
- const input = _entries.reduce((acc, cur) => {
138
- acc[relative(cur)] = cur;
139
- return acc;
140
- }, {});
141
- const entries = Array.isArray(_entries) ? new Set(_entries) : _entries;
142
- const stylesIds = /* @__PURE__ */ new Set();
143
- let configResolved;
144
- return [
145
- {
146
- name: "weapp-vite:pre",
147
- enforce: "pre",
148
- configResolved(config) {
149
- config.build.rollupOptions.input = input;
150
- configResolved = config;
151
- },
152
- resolveId(source) {
153
- if (/\.wxss$/.test(source)) {
154
- return source.replace(/\.wxss$/, ".css?wxss");
155
- }
156
- },
157
- load(id) {
158
- if (entries.has(id)) {
159
- const base = removeExtension2(id);
160
- const ms = new MagicString(fs2.readFileSync(id, "utf8"));
161
- for (const ext of supportedCssExtensions) {
162
- const mayBeCssPath = addExtension2(base, ext);
163
- if (fs2.existsSync(mayBeCssPath)) {
164
- this.addWatchFile(mayBeCssPath);
165
- ms.prepend(`import '${mayBeCssPath}'
166
- `);
167
- }
168
- }
169
- this.addWatchFile(id);
170
- return {
171
- code: ms.toString()
172
- };
173
- } else if (isCSSRequest(id)) {
174
- stylesIds.add(id);
175
- return {
176
- code: ""
177
- };
178
- }
179
- },
180
- async buildEnd() {
181
- const styles = {};
182
- for (const stylesId of stylesIds) {
183
- const parsed = parseRequest(stylesId);
184
- const css = await fs2.readFile(getRealPath(parsed), "utf8");
185
- const res = await preprocessCSS(css, stylesId, configResolved);
186
- const fileName = relative(normalizeCssPath(stylesId));
187
- if (styles[fileName]) {
188
- styles[fileName] += res.code;
189
- } else {
190
- styles[fileName] = res.code;
191
- }
192
- }
193
- for (const style of Object.entries(styles)) {
194
- this.emitFile({
195
- type: "asset",
196
- fileName: style[0],
197
- source: style[1]
198
- });
199
- }
200
- const files = await fg(
201
- [path2.join(src, "**/*.{wxml,json,png,jpg,jpeg,gif,svg,webp}")],
202
- {
203
- cwd: cwd2,
204
- ignore: [
205
- ...defaultExcluded,
206
- "dist/**",
207
- "project.config.json",
208
- "project.private.config.json",
209
- "package.json"
210
- ]
211
- }
212
- );
213
- for (const file of files) {
214
- const filepath = path2.resolve(cwd2, file);
215
- this.addWatchFile(filepath);
216
- this.emitFile({
217
- type: "asset",
218
- fileName: file,
219
- source: await fs2.readFile(filepath)
220
- });
221
- }
222
- }
223
- // generateBundle(_options, _bundle) {
224
- // const files = this.getWatchFiles()
225
- // console.log(files)
226
- // },
227
- },
228
- {
229
- name: "weapp-vite"
230
- },
231
- {
232
- name: "weapp-vite:post",
233
- enforce: "post"
234
- }
235
- ];
236
- }
237
-
238
- // src/build.ts
239
- function createFilter(include, exclude, options) {
240
- const opts = defu2(options, {
241
- ignore: exclude
242
- // dot: true,
243
- // contains: true,
244
- });
245
- return function(id) {
246
- if (typeof id !== "string") {
247
- return false;
248
- }
249
- if (/\0/.test(id)) {
250
- return false;
251
- }
252
- return mm.isMatch(id, include, opts);
253
- };
254
- }
255
- function getEntries(options) {
256
- let cwd2;
257
- let relative;
258
- if (typeof options === "string") {
259
- cwd2 = options;
260
- } else {
261
- cwd2 = options.cwd;
262
- relative = options.relative;
263
- }
264
- const filter = createFilter(["**/*"], [...defaultExcluded, path3.resolve(cwd2, "dist/**")], { cwd: cwd2 });
265
- return scanEntries(cwd2, { filter, relative });
266
- }
267
- function getDefaultConfig(options) {
268
- return {
269
- build: {
270
- rollupOptions: {
271
- output: {
272
- format: "cjs",
273
- entryFileNames: (chunkInfo) => {
274
- return chunkInfo.name;
275
- }
276
- }
277
- },
278
- assetsDir: ".",
279
- commonjsOptions: {
280
- transformMixedEsModules: true,
281
- include: void 0
282
- }
283
- },
284
- plugins: [
285
- vitePluginWeapp(options)
286
- ]
287
- };
288
- }
289
- async function runDev(cwd2, options) {
290
- const entries = await getEntries(cwd2);
291
- if (entries) {
292
- const watcher = await build(
293
- defu2(
294
- options,
295
- getDefaultConfig({
296
- cwd: cwd2,
297
- entries: entries.all
298
- }),
299
- {
300
- build: {
301
- watch: {},
302
- minify: false
303
- }
304
- }
305
- )
306
- );
307
- return watcher;
308
- }
309
- }
310
- async function runProd(cwd2, options) {
311
- const entries = await getEntries(cwd2);
312
- if (entries) {
313
- const output = await build(
314
- defu2(
315
- options,
316
- getDefaultConfig({
317
- cwd: cwd2,
318
- entries: entries.all
319
- })
320
- )
321
- );
322
- return output;
323
- }
324
- }
12
+ // src/logger.ts
13
+ import { createConsola } from "consola";
14
+ var logger_default = createConsola();
325
15
 
326
16
  // src/cli.ts
327
17
  var cwd = process.cwd();
@@ -337,4 +27,13 @@ program.command("init").action(() => {
337
27
  command: "weapp-vite"
338
28
  });
339
29
  });
30
+ program.command("open").action(async () => {
31
+ try {
32
+ await parse(["open", "-p"]);
33
+ } catch (error) {
34
+ logger_default.error(error);
35
+ } finally {
36
+ process.exit();
37
+ }
38
+ });
340
39
  program.parse();
package/dist/index.cjs CHANGED
@@ -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,18 +17,357 @@ 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
 
20
30
  // src/index.ts
21
31
  var src_exports = {};
22
32
  __export(src_exports, {
23
- defineConfig: () => defineConfig
33
+ createFilter: () => createFilter,
34
+ getDefaultConfig: () => getDefaultConfig,
35
+ getEntries: () => getEntries,
36
+ runDev: () => runDev,
37
+ runProd: () => runProd
24
38
  });
25
39
  module.exports = __toCommonJS(src_exports);
26
- function defineConfig(config) {
27
- return config;
40
+
41
+ // src/build.ts
42
+ var import_vite2 = require("vite");
43
+ var import_micromatch = __toESM(require("micromatch"), 1);
44
+ var import_shared3 = require("@weapp-core/shared");
45
+ var import_pathe2 = __toESM(require("pathe"), 1);
46
+
47
+ // src/utils/scan.ts
48
+ var import_pathe = __toESM(require("pathe"), 1);
49
+ var import_fs_extra = __toESM(require("fs-extra"), 1);
50
+ var import_klaw = __toESM(require("klaw"), 1);
51
+ var import_shared = require("@weapp-core/shared");
52
+ var defaultExcluded = ["**/node_modules/**", "**/miniprogram_npm/**"];
53
+ function searchAppEntry(root) {
54
+ const extensions = ["js", "ts"];
55
+ const appJson = import_pathe.default.resolve(root, "app.json");
56
+ if (import_fs_extra.default.existsSync(appJson)) {
57
+ for (const ext of extensions) {
58
+ const entryPath = import_pathe.default.resolve(root, (0, import_shared.addExtension)("app", `.${ext}`));
59
+ if (import_fs_extra.default.existsSync(entryPath)) {
60
+ return entryPath;
61
+ }
62
+ }
63
+ }
64
+ }
65
+ function searchPageEntry(wxmlPath) {
66
+ if (import_fs_extra.default.existsSync(wxmlPath)) {
67
+ const extensions = ["js", "ts"];
68
+ const base = (0, import_shared.removeExtension)(wxmlPath);
69
+ for (const ext of extensions) {
70
+ const entryPath = (0, import_shared.addExtension)(base, `.${ext}`);
71
+ if (import_fs_extra.default.existsSync(entryPath)) {
72
+ return entryPath;
73
+ }
74
+ }
75
+ }
76
+ }
77
+ function getWxmlEntry(wxmlPath) {
78
+ const pageEntry = searchPageEntry(wxmlPath);
79
+ if (pageEntry) {
80
+ const jsonPath = (0, import_shared.addExtension)((0, import_shared.removeExtension)(wxmlPath), ".json");
81
+ if (import_fs_extra.default.existsSync(jsonPath)) {
82
+ const json = import_fs_extra.default.readJsonSync(jsonPath, { throws: false });
83
+ if (json && json.component) {
84
+ return {
85
+ path: pageEntry,
86
+ type: "component"
87
+ };
88
+ }
89
+ }
90
+ return {
91
+ path: pageEntry,
92
+ type: "page"
93
+ };
94
+ }
95
+ }
96
+ async function scanEntries(root, options) {
97
+ const appEntry = searchAppEntry(root);
98
+ function getPath(to) {
99
+ if (options?.relative) {
100
+ return import_pathe.default.relative(root, to);
101
+ }
102
+ return to;
103
+ }
104
+ if (appEntry) {
105
+ const pageEntries = /* @__PURE__ */ new Set();
106
+ const componentEntries = /* @__PURE__ */ new Set();
107
+ for await (const file of (0, import_klaw.default)(root, {
108
+ filter: options?.filter
109
+ })) {
110
+ if (file.stats.isFile()) {
111
+ if (/\.wxml$/.test(file.path)) {
112
+ const entry = getWxmlEntry(file.path);
113
+ if (entry) {
114
+ if (entry.type === "component") {
115
+ componentEntries.add(getPath(entry.path));
116
+ } else if (entry.type === "page") {
117
+ pageEntries.add(getPath(entry.path));
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ const app = getPath(appEntry);
124
+ return {
125
+ app,
126
+ pages: pageEntries,
127
+ components: componentEntries,
128
+ all: [app, ...pageEntries, ...componentEntries]
129
+ // css: [...cssEntries],
130
+ };
131
+ }
132
+ }
133
+
134
+ // src/utils/index.ts
135
+ var supportedCssLangs = ["wxss", "scss", "less", "sass", "styl"];
136
+ var supportedCssExtensions = supportedCssLangs.map((x) => `.${x}`);
137
+
138
+ // src/plugins/index.ts
139
+ var import_node_path = __toESM(require("path"), 1);
140
+ var import_fs_extra2 = __toESM(require("fs-extra"), 1);
141
+ var import_magic_string = __toESM(require("magic-string"), 1);
142
+ var import_shared2 = require("@weapp-core/shared");
143
+ var import_fast_glob = __toESM(require("fast-glob"), 1);
144
+ var import_vite = require("vite");
145
+ function parseRequest(id) {
146
+ const [filename, rawQuery] = id.split(`?`, 2);
147
+ const query = Object.fromEntries(new URLSearchParams(rawQuery));
148
+ if (query.wxss !== null) {
149
+ query.wxss = true;
150
+ }
151
+ return {
152
+ filename,
153
+ query
154
+ };
155
+ }
156
+ function normalizeCssPath(id) {
157
+ return (0, import_shared2.addExtension)((0, import_shared2.removeExtension)(id), ".wxss");
158
+ }
159
+ function getRealPath(res) {
160
+ if (res.query.wxss) {
161
+ return (0, import_shared2.addExtension)((0, import_shared2.removeExtension)(res.filename), ".wxss");
162
+ }
163
+ return res.filename;
164
+ }
165
+ function vitePluginWeapp(options) {
166
+ const { cwd, entries: _entries, src } = (0, import_shared2.defu)(options, {
167
+ src: ""
168
+ });
169
+ function relative(p) {
170
+ return import_node_path.default.relative(cwd, p);
171
+ }
172
+ const input = _entries.reduce((acc, cur) => {
173
+ acc[relative(cur)] = cur;
174
+ return acc;
175
+ }, {});
176
+ const entries = Array.isArray(_entries) ? new Set(_entries) : _entries;
177
+ const stylesIds = /* @__PURE__ */ new Set();
178
+ let configResolved;
179
+ return [
180
+ {
181
+ name: "weapp-vite:pre",
182
+ enforce: "pre",
183
+ // config(config, env) {
184
+ // console.log(config, env)
185
+ // },
186
+ // options(options) {
187
+ // console.log(options)
188
+ // },
189
+ configResolved(config) {
190
+ config.build.rollupOptions.input = input;
191
+ configResolved = config;
192
+ },
193
+ resolveId(source) {
194
+ if (/\.wxss$/.test(source)) {
195
+ return source.replace(/\.wxss$/, ".css?wxss");
196
+ }
197
+ },
198
+ load(id) {
199
+ if (entries.has(id)) {
200
+ const base = (0, import_shared2.removeExtension)(id);
201
+ const ms = new import_magic_string.default(import_fs_extra2.default.readFileSync(id, "utf8"));
202
+ for (const ext of supportedCssExtensions) {
203
+ const mayBeCssPath = (0, import_shared2.addExtension)(base, ext);
204
+ if (import_fs_extra2.default.existsSync(mayBeCssPath)) {
205
+ this.addWatchFile(mayBeCssPath);
206
+ ms.prepend(`import '${mayBeCssPath}'
207
+ `);
208
+ }
209
+ }
210
+ this.addWatchFile(id);
211
+ return {
212
+ code: ms.toString()
213
+ };
214
+ } else if ((0, import_vite.isCSSRequest)(id)) {
215
+ stylesIds.add(id);
216
+ return {
217
+ code: ""
218
+ };
219
+ }
220
+ },
221
+ async buildEnd() {
222
+ const styles = {};
223
+ for (const stylesId of stylesIds) {
224
+ const parsed = parseRequest(stylesId);
225
+ const css = await import_fs_extra2.default.readFile(getRealPath(parsed), "utf8");
226
+ const res = await (0, import_vite.preprocessCSS)(css, stylesId, configResolved);
227
+ const fileName = relative(normalizeCssPath(stylesId));
228
+ if (styles[fileName]) {
229
+ styles[fileName] += res.code;
230
+ } else {
231
+ styles[fileName] = res.code;
232
+ }
233
+ }
234
+ for (const style of Object.entries(styles)) {
235
+ this.emitFile({
236
+ type: "asset",
237
+ fileName: style[0],
238
+ source: style[1]
239
+ });
240
+ }
241
+ const files = await (0, import_fast_glob.default)(
242
+ [import_node_path.default.join(src, "**/*.{wxml,json,png,jpg,jpeg,gif,svg,webp}")],
243
+ {
244
+ cwd,
245
+ ignore: [
246
+ ...defaultExcluded,
247
+ "dist/**",
248
+ "project.config.json",
249
+ "project.private.config.json",
250
+ "package.json"
251
+ ]
252
+ }
253
+ );
254
+ for (const file of files) {
255
+ const filepath = import_node_path.default.resolve(cwd, file);
256
+ this.addWatchFile(filepath);
257
+ this.emitFile({
258
+ type: "asset",
259
+ fileName: file,
260
+ source: await import_fs_extra2.default.readFile(filepath)
261
+ });
262
+ }
263
+ }
264
+ // generateBundle(_options, _bundle) {
265
+ // const files = this.getWatchFiles()
266
+ // console.log(files)
267
+ // },
268
+ },
269
+ {
270
+ name: "weapp-vite"
271
+ },
272
+ {
273
+ name: "weapp-vite:post",
274
+ enforce: "post"
275
+ }
276
+ ];
277
+ }
278
+
279
+ // src/build.ts
280
+ function createFilter(include, exclude, options) {
281
+ const opts = (0, import_shared3.defu)(options, {
282
+ ignore: exclude
283
+ // dot: true,
284
+ // contains: true,
285
+ });
286
+ return function(id) {
287
+ if (typeof id !== "string") {
288
+ return false;
289
+ }
290
+ if (/\0/.test(id)) {
291
+ return false;
292
+ }
293
+ return import_micromatch.default.isMatch(id, include, opts);
294
+ };
295
+ }
296
+ function getEntries(options) {
297
+ let cwd;
298
+ let relative;
299
+ if (typeof options === "string") {
300
+ cwd = options;
301
+ } else {
302
+ cwd = options.cwd;
303
+ relative = options.relative;
304
+ }
305
+ const filter = createFilter(["**/*"], [...defaultExcluded, import_pathe2.default.resolve(cwd, "dist/**")], { cwd });
306
+ return scanEntries(cwd, { filter, relative });
307
+ }
308
+ function getDefaultConfig(options) {
309
+ return {
310
+ build: {
311
+ rollupOptions: {
312
+ output: {
313
+ format: "cjs",
314
+ entryFileNames: (chunkInfo) => {
315
+ return chunkInfo.name;
316
+ }
317
+ }
318
+ },
319
+ assetsDir: ".",
320
+ commonjsOptions: {
321
+ transformMixedEsModules: true,
322
+ include: void 0
323
+ }
324
+ },
325
+ plugins: [
326
+ vitePluginWeapp(options)
327
+ ]
328
+ };
329
+ }
330
+ async function runDev(cwd, options) {
331
+ const entries = await getEntries(cwd);
332
+ if (entries) {
333
+ const watcher = await (0, import_vite2.build)(
334
+ (0, import_shared3.defu)(
335
+ options,
336
+ getDefaultConfig({
337
+ cwd,
338
+ entries: entries.all
339
+ }),
340
+ {
341
+ build: {
342
+ watch: {},
343
+ minify: false
344
+ }
345
+ }
346
+ )
347
+ );
348
+ return watcher;
349
+ }
350
+ }
351
+ async function runProd(cwd, options) {
352
+ const entries = await getEntries(cwd);
353
+ if (entries) {
354
+ const output = await (0, import_vite2.build)(
355
+ (0, import_shared3.defu)(
356
+ options,
357
+ getDefaultConfig({
358
+ cwd,
359
+ entries: entries.all
360
+ })
361
+ )
362
+ );
363
+ return output;
364
+ }
28
365
  }
29
366
  // Annotate the CommonJS export names for ESM import in node:
30
367
  0 && (module.exports = {
31
- defineConfig
368
+ createFilter,
369
+ getDefaultConfig,
370
+ getEntries,
371
+ runDev,
372
+ runProd
32
373
  });
package/dist/index.d.cts CHANGED
@@ -1,8 +1,22 @@
1
- import { UserConfig, UserConfigFnObject, UserConfigExport } from 'vite';
1
+ import { InlineConfig } from 'vite';
2
+ import { RollupWatcher, RollupOutput } from 'rollup';
3
+ import mm from 'micromatch';
2
4
 
3
- declare function defineConfig(config: UserConfig): UserConfig;
4
- declare function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>;
5
- declare function defineConfig(config: UserConfigFnObject): UserConfigFnObject;
6
- declare function defineConfig(config: UserConfigExport): UserConfigExport;
5
+ declare function createFilter(include: string[], exclude: string[], options?: mm.Options): (id: unknown | string) => boolean;
6
+ declare function getEntries(options: string | {
7
+ cwd: string;
8
+ relative?: boolean;
9
+ }): Promise<{
10
+ app: string;
11
+ pages: Set<string>;
12
+ components: Set<string>;
13
+ all: string[];
14
+ } | undefined>;
15
+ declare function getDefaultConfig(options: {
16
+ cwd?: string;
17
+ entries?: string[];
18
+ }): InlineConfig;
19
+ declare function runDev(cwd: string, options?: InlineConfig): Promise<RollupWatcher | undefined>;
20
+ declare function runProd(cwd: string, options?: InlineConfig): Promise<RollupOutput | RollupOutput[] | undefined>;
7
21
 
8
- export { defineConfig };
22
+ export { createFilter, getDefaultConfig, getEntries, runDev, runProd };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,22 @@
1
- import { UserConfig, UserConfigFnObject, UserConfigExport } from 'vite';
1
+ import { InlineConfig } from 'vite';
2
+ import { RollupWatcher, RollupOutput } from 'rollup';
3
+ import mm from 'micromatch';
2
4
 
3
- declare function defineConfig(config: UserConfig): UserConfig;
4
- declare function defineConfig(config: Promise<UserConfig>): Promise<UserConfig>;
5
- declare function defineConfig(config: UserConfigFnObject): UserConfigFnObject;
6
- declare function defineConfig(config: UserConfigExport): UserConfigExport;
5
+ declare function createFilter(include: string[], exclude: string[], options?: mm.Options): (id: unknown | string) => boolean;
6
+ declare function getEntries(options: string | {
7
+ cwd: string;
8
+ relative?: boolean;
9
+ }): Promise<{
10
+ app: string;
11
+ pages: Set<string>;
12
+ components: Set<string>;
13
+ all: string[];
14
+ } | undefined>;
15
+ declare function getDefaultConfig(options: {
16
+ cwd?: string;
17
+ entries?: string[];
18
+ }): InlineConfig;
19
+ declare function runDev(cwd: string, options?: InlineConfig): Promise<RollupWatcher | undefined>;
20
+ declare function runProd(cwd: string, options?: InlineConfig): Promise<RollupOutput | RollupOutput[] | undefined>;
7
21
 
8
- export { defineConfig };
22
+ export { createFilter, getDefaultConfig, getEntries, runDev, runProd };
package/dist/index.js CHANGED
@@ -1,7 +1,14 @@
1
- // src/index.ts
2
- function defineConfig(config) {
3
- return config;
4
- }
1
+ import {
2
+ createFilter,
3
+ getDefaultConfig,
4
+ getEntries,
5
+ runDev,
6
+ runProd
7
+ } from "./chunk-AZKQ5D5L.js";
5
8
  export {
6
- defineConfig
9
+ createFilter,
10
+ getDefaultConfig,
11
+ getEntries,
12
+ runDev,
13
+ runProd
7
14
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "weapp-vite",
3
3
  "type": "module",
4
- "version": "0.0.2-alpha.1",
4
+ "version": "0.0.2-alpha.2",
5
5
  "description": "WIP",
6
6
  "author": "SonOfMagic <qq1324318532@gmail.com>",
7
7
  "license": "MIT",
@@ -18,6 +18,11 @@
18
18
  "types": "./dist/index.d.ts",
19
19
  "import": "./dist/index.js",
20
20
  "require": "./dist/index.cjs"
21
+ },
22
+ "./config": {
23
+ "types": "./dist/config.d.ts",
24
+ "import": "./dist/config.js",
25
+ "require": "./dist/config.cjs"
21
26
  }
22
27
  },
23
28
  "bin": {
@@ -31,6 +36,7 @@
31
36
  },
32
37
  "dependencies": {
33
38
  "commander": "^12.1.0",
39
+ "consola": "^3.2.3",
34
40
  "debug": "^4.3.6",
35
41
  "fast-glob": "^3.3.2",
36
42
  "fs-extra": "^11.2.0",
@@ -38,8 +44,9 @@
38
44
  "magic-string": "^0.30.11",
39
45
  "micromatch": "^4.0.7",
40
46
  "pathe": "^1.1.2",
41
- "@weapp-core/init": "^0.0.2-alpha.1",
42
- "@weapp-core/shared": "^0.0.2-alpha.1"
47
+ "@weapp-core/init": "^0.0.2-alpha.2",
48
+ "@weapp-core/shared": "^0.0.2-alpha.2",
49
+ "weapp-ide-cli": "^2.0.0-alpha.0"
43
50
  },
44
51
  "publishConfig": {
45
52
  "access": "public",