@mpcore/weapp-vite 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ice breaker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # @mpcore/weapp-vite
2
+
3
+ Builds real weapp-vite output for `@mpcore/test` through the programmatic `weapp-vite/test` API.
4
+
5
+ ```ts
6
+ import { createWeappViteTestProject } from '@mpcore/weapp-vite'
7
+
8
+ const project = await createWeappViteTestProject({ cwd: process.cwd() })
9
+ const result = await project.renderPage('/pages/index/index')
10
+ ```
11
+
12
+ Cold builds emit to `.weapp-vite/test-artifacts/`. Repeated calls reuse a valid artifact, while watch mode performs a complete bundler-owned rebuild after source changes.
@@ -0,0 +1,34 @@
1
+ import { CreateTestProjectOptions, MiniProgramTestProject } from "@mpcore/test";
2
+ //#region src/index.d.ts
3
+ interface BuildTestArtifactOptions {
4
+ configFile?: string;
5
+ cwd?: string;
6
+ mode?: string;
7
+ outDir?: string;
8
+ projectConfigPath?: string;
9
+ skipNpm?: boolean;
10
+ }
11
+ interface WeappViteTestArtifact {
12
+ appConfigPath: string;
13
+ miniprogramRootPath: string;
14
+ projectPath: string;
15
+ sourceRootPath: string;
16
+ }
17
+ interface WatchTestArtifactOptions extends BuildTestArtifactOptions {
18
+ onError?: (error: unknown) => void;
19
+ onRebuilt?: (artifact: WeappViteTestArtifact) => void | Promise<void>;
20
+ }
21
+ interface WeappViteTestArtifactWatcher {
22
+ artifact: WeappViteTestArtifact;
23
+ close: () => Promise<void>;
24
+ rebuild: () => Promise<WeappViteTestArtifact>;
25
+ }
26
+ interface CreateWeappViteTestProjectOptions extends BuildTestArtifactOptions {
27
+ test?: Omit<CreateTestProjectOptions, 'artifact'>;
28
+ }
29
+ declare function buildWeappViteTestArtifact(options?: BuildTestArtifactOptions): Promise<WeappViteTestArtifact>;
30
+ declare function clearWeappViteTestArtifactCache(options?: BuildTestArtifactOptions): void;
31
+ declare function createWeappViteTestProject(options?: CreateWeappViteTestProjectOptions): Promise<MiniProgramTestProject>;
32
+ declare function watchWeappViteTestArtifact(options?: WatchTestArtifactOptions): Promise<WeappViteTestArtifactWatcher>;
33
+ //#endregion
34
+ export { BuildTestArtifactOptions, CreateWeappViteTestProjectOptions, WatchTestArtifactOptions, WeappViteTestArtifact, WeappViteTestArtifactWatcher, buildWeappViteTestArtifact, clearWeappViteTestArtifactCache, createWeappViteTestProject, watchWeappViteTestArtifact };
package/dist/index.mjs ADDED
@@ -0,0 +1,69 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import process from "node:process";
4
+ import { createTestProject } from "@mpcore/test";
5
+ import { buildTestArtifact, watchTestArtifact } from "weapp-vite/test";
6
+ //#region src/index.ts
7
+ const artifactCache = /* @__PURE__ */ new Map();
8
+ function cacheKey(options) {
9
+ return JSON.stringify({
10
+ configFile: options.configFile,
11
+ cwd: path.resolve(options.cwd ?? process.cwd()),
12
+ mode: options.mode ?? "test",
13
+ outDir: options.outDir,
14
+ projectConfigPath: options.projectConfigPath,
15
+ skipNpm: options.skipNpm
16
+ });
17
+ }
18
+ async function artifactExists(artifact) {
19
+ try {
20
+ await fs.access(artifact.appConfigPath);
21
+ return true;
22
+ } catch {
23
+ return false;
24
+ }
25
+ }
26
+ async function buildWeappViteTestArtifact(options = {}) {
27
+ const key = cacheKey(options);
28
+ const cached = artifactCache.get(key);
29
+ if (cached) {
30
+ const artifact = await cached;
31
+ if (await artifactExists(artifact)) return artifact;
32
+ artifactCache.delete(key);
33
+ }
34
+ const pending = buildTestArtifact(options);
35
+ artifactCache.set(key, pending);
36
+ try {
37
+ return await pending;
38
+ } catch (error) {
39
+ artifactCache.delete(key);
40
+ throw error;
41
+ }
42
+ }
43
+ function clearWeappViteTestArtifactCache(options) {
44
+ if (options) {
45
+ artifactCache.delete(cacheKey(options));
46
+ return;
47
+ }
48
+ artifactCache.clear();
49
+ }
50
+ async function createWeappViteTestProject(options = {}) {
51
+ const { test, ...buildOptions } = options;
52
+ const artifact = await buildWeappViteTestArtifact(buildOptions);
53
+ return createTestProject({
54
+ ...test,
55
+ artifact
56
+ });
57
+ }
58
+ async function watchWeappViteTestArtifact(options = {}) {
59
+ clearWeappViteTestArtifactCache(options);
60
+ return await watchTestArtifact({
61
+ ...options,
62
+ async onRebuilt(artifact) {
63
+ artifactCache.set(cacheKey(options), Promise.resolve(artifact));
64
+ await options.onRebuilt?.(artifact);
65
+ }
66
+ });
67
+ }
68
+ //#endregion
69
+ export { buildWeappViteTestArtifact, clearWeappViteTestArtifactCache, createWeappViteTestProject, watchWeappViteTestArtifact };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@mpcore/weapp-vite",
3
+ "type": "module",
4
+ "version": "0.0.0",
5
+ "description": "weapp-vite artifact adapter for @mpcore/test",
6
+ "author": "ice breaker <1324318532@qq.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/weapp-vite/weapp-vite.git",
11
+ "directory": "mpcore/packages/weapp-vite"
12
+ },
13
+ "sideEffects": false,
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.mts",
17
+ "import": "./dist/index.mjs"
18
+ }
19
+ },
20
+ "main": "./dist/index.mjs",
21
+ "module": "./dist/index.mjs",
22
+ "types": "./dist/index.d.mts",
23
+ "files": [
24
+ "README.md",
25
+ "dist"
26
+ ],
27
+ "engines": {
28
+ "node": "^20.19.0 || >=22.12.0"
29
+ },
30
+ "dependencies": {
31
+ "@mpcore/test": "0.0.0",
32
+ "weapp-vite": "6.18.6"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public",
36
+ "registry": "https://registry.npmjs.org"
37
+ },
38
+ "tsd": {
39
+ "compilerOptions": {
40
+ "module": "ESNext",
41
+ "moduleResolution": "Bundler"
42
+ }
43
+ },
44
+ "scripts": {
45
+ "build": "tsdown",
46
+ "dev": "tsdown -w",
47
+ "test": "vitest run",
48
+ "test:types": "pnpm build && tsd",
49
+ "typecheck": "tsc --noEmit"
50
+ }
51
+ }