rwsdk 0.3.10 → 0.3.12
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.
|
@@ -14,7 +14,7 @@ export const generateVendorBarrelContent = (files, projectRootDir) => {
|
|
|
14
14
|
const exports = "export default {\n" +
|
|
15
15
|
[...files]
|
|
16
16
|
.filter((file) => file.includes("node_modules"))
|
|
17
|
-
.map((file, i) => ` '${normalizeModulePath(file, projectRootDir)
|
|
17
|
+
.map((file, i) => ` '${normalizeModulePath(file, projectRootDir)}': M${i},`)
|
|
18
18
|
.join("\n") +
|
|
19
19
|
"\n};";
|
|
20
20
|
return `${imports}\n\n${exports}`;
|
|
@@ -14,8 +14,8 @@ describe("directiveModulesDevPlugin helpers", () => {
|
|
|
14
14
|
import * as M1 from '${projectRootDir}/node_modules/lib-b/component.tsx';
|
|
15
15
|
|
|
16
16
|
export default {
|
|
17
|
-
'node_modules/lib-a/index.js': M0,
|
|
18
|
-
'node_modules/lib-b/component.tsx': M1,
|
|
17
|
+
'/node_modules/lib-a/index.js': M0,
|
|
18
|
+
'/node_modules/lib-b/component.tsx': M1,
|
|
19
19
|
};`;
|
|
20
20
|
expect(content).toEqual(expected);
|
|
21
21
|
});
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Efficiently checks if a React directive (e.g., "use server", "use client")
|
|
3
|
-
* is present in the code.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* is present in the code.
|
|
4
|
+
*
|
|
5
|
+
* This function is optimized for performance by only checking the first few
|
|
6
|
+
* lines of the code, as directives must appear at the very top of a file.
|
|
7
|
+
* It handles comments, whitespace, and any valid directive prologue
|
|
8
|
+
* (e.g., "use strict").
|
|
6
9
|
*/
|
|
7
10
|
export declare function hasDirective(code: string, directive: string): boolean;
|
|
@@ -1,54 +1,70 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Efficiently checks if a React directive (e.g., "use server", "use client")
|
|
3
|
-
* is present in the code.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* is present in the code.
|
|
4
|
+
*
|
|
5
|
+
* This function is optimized for performance by only checking the first few
|
|
6
|
+
* lines of the code, as directives must appear at the very top of a file.
|
|
7
|
+
* It handles comments, whitespace, and any valid directive prologue
|
|
8
|
+
* (e.g., "use strict").
|
|
6
9
|
*/
|
|
7
10
|
export function hasDirective(code, directive) {
|
|
8
|
-
|
|
9
|
-
const singleQuoteDirective = `'${directive}'`;
|
|
10
|
-
const doubleQuoteDirective = `"${directive}"`;
|
|
11
|
-
if (!code.includes(singleQuoteDirective) &&
|
|
12
|
-
!code.includes(doubleQuoteDirective)) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
// Split into lines and check each one
|
|
16
|
-
const lines = code.split("\n");
|
|
11
|
+
const lines = code.slice(0, 512).split("\n"); // Check first ~512 chars
|
|
17
12
|
let inMultiLineComment = false;
|
|
13
|
+
let foundUseClient = false;
|
|
14
|
+
let foundTargetDirective = false;
|
|
15
|
+
const doubleQuoteDirective = `"${directive}"`;
|
|
16
|
+
const singleQuoteDirective = `'${directive}'`;
|
|
17
|
+
const doubleQuoteUseClient = `"use client"`;
|
|
18
|
+
const singleQuoteUseClient = `'use client'`;
|
|
18
19
|
for (const line of lines) {
|
|
19
20
|
const trimmedLine = line.trim();
|
|
20
|
-
// Skip empty lines
|
|
21
21
|
if (trimmedLine.length === 0) {
|
|
22
22
|
continue;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
if (trimmedLine.startsWith("/*")) {
|
|
26
|
-
inMultiLineComment = true;
|
|
27
|
-
// Check if the comment ends on the same line
|
|
24
|
+
if (inMultiLineComment) {
|
|
28
25
|
if (trimmedLine.includes("*/")) {
|
|
29
26
|
inMultiLineComment = false;
|
|
30
27
|
}
|
|
31
28
|
continue;
|
|
32
29
|
}
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
inMultiLineComment = false;
|
|
30
|
+
if (trimmedLine.startsWith("/*")) {
|
|
31
|
+
if (!trimmedLine.includes("*/")) {
|
|
32
|
+
inMultiLineComment = true;
|
|
37
33
|
}
|
|
38
34
|
continue;
|
|
39
35
|
}
|
|
40
|
-
// Skip single-line comments
|
|
41
36
|
if (trimmedLine.startsWith("//")) {
|
|
42
37
|
continue;
|
|
43
38
|
}
|
|
44
|
-
|
|
39
|
+
const cleanedLine = trimmedLine.endsWith(";")
|
|
40
|
+
? trimmedLine.slice(0, -1)
|
|
41
|
+
: trimmedLine;
|
|
42
|
+
if (trimmedLine.startsWith(doubleQuoteUseClient) ||
|
|
43
|
+
trimmedLine.startsWith(singleQuoteUseClient)) {
|
|
44
|
+
foundUseClient = true;
|
|
45
|
+
if (directive === "use client") {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
45
49
|
if (trimmedLine.startsWith(doubleQuoteDirective) ||
|
|
46
50
|
trimmedLine.startsWith(singleQuoteDirective)) {
|
|
47
|
-
|
|
51
|
+
foundTargetDirective = true;
|
|
52
|
+
if (directive !== "use server") {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
48
55
|
}
|
|
49
|
-
//
|
|
50
|
-
//
|
|
56
|
+
// Any other string literal is part of a valid directive prologue.
|
|
57
|
+
// We can continue searching.
|
|
58
|
+
if (trimmedLine.startsWith('"') || trimmedLine.startsWith("'")) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
// If we encounter any other non-directive, non-comment, non-string-literal
|
|
62
|
+
// line of code, the directive prologue is over. Stop.
|
|
51
63
|
break;
|
|
52
64
|
}
|
|
53
|
-
return false
|
|
65
|
+
// If looking for 'use server' and 'use client' was found, return false (client takes priority)
|
|
66
|
+
if (directive === "use server" && foundUseClient) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return foundTargetDirective;
|
|
54
70
|
}
|
|
@@ -1,109 +1,107 @@
|
|
|
1
1
|
import { describe, it, expect } from "vitest";
|
|
2
2
|
import { hasDirective } from "./hasDirective.mjs";
|
|
3
3
|
describe("hasDirective", () => {
|
|
4
|
-
it('should find "use client" directive
|
|
5
|
-
const code = `"use client";
|
|
6
|
-
|
|
7
|
-
import React from "react";
|
|
8
|
-
|
|
9
|
-
const MyComponent = () => <div>Hello</div>;
|
|
10
|
-
export default MyComponent;`;
|
|
4
|
+
it('should find "use client" directive', () => {
|
|
5
|
+
const code = `"use client"; import React from "react";`;
|
|
11
6
|
expect(hasDirective(code, "use client")).toBe(true);
|
|
12
7
|
});
|
|
13
|
-
it(
|
|
14
|
-
const code = `'use server';
|
|
15
|
-
|
|
16
|
-
export async function myAction() {
|
|
17
|
-
// ...
|
|
18
|
-
}`;
|
|
8
|
+
it('should find "use server" directive', () => {
|
|
9
|
+
const code = `'use server'; export async function myAction() {}`;
|
|
19
10
|
expect(hasDirective(code, "use server")).toBe(true);
|
|
20
11
|
});
|
|
21
|
-
it("should find directive
|
|
22
|
-
const code = `
|
|
23
|
-
|
|
24
|
-
|
|
12
|
+
it("should not find a directive that is not there", () => {
|
|
13
|
+
const code = `import React from "react";`;
|
|
14
|
+
expect(hasDirective(code, "use client")).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
it('should find "use client" directive with single quotes', () => {
|
|
17
|
+
const code = `'use client'; import React from "react";`;
|
|
25
18
|
expect(hasDirective(code, "use client")).toBe(true);
|
|
26
19
|
});
|
|
27
|
-
it("should find directive
|
|
28
|
-
const code =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
it("should find directive when preceded by comments and whitespace", () => {
|
|
21
|
+
const code = `
|
|
22
|
+
// This is a client component
|
|
23
|
+
/* And here is another comment */
|
|
24
|
+
|
|
25
|
+
"use client";
|
|
26
|
+
import React from 'react';
|
|
27
|
+
export default () => <div>Hello</div>;
|
|
28
|
+
`;
|
|
32
29
|
expect(hasDirective(code, "use client")).toBe(true);
|
|
33
30
|
});
|
|
34
|
-
it(
|
|
35
|
-
const code =
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
it('should find "use client" directive when preceded by "use strict"', () => {
|
|
32
|
+
const code = `
|
|
33
|
+
"use strict";
|
|
34
|
+
"use client";
|
|
35
|
+
import React from 'react';
|
|
36
|
+
export default () => <div>Hello</div>;
|
|
37
|
+
`;
|
|
39
38
|
expect(hasDirective(code, "use client")).toBe(true);
|
|
40
39
|
});
|
|
41
|
-
it(
|
|
40
|
+
it('should find "use server" directive when preceded by "use strict" and comments', () => {
|
|
42
41
|
const code = `
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
// server stuff
|
|
43
|
+
"use strict";
|
|
44
|
+
/* another comment */
|
|
45
|
+
"use server";
|
|
46
|
+
export async function myAction() {}
|
|
47
|
+
`;
|
|
48
|
+
expect(hasDirective(code, "use server")).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
it("should find directive when preceded by another string literal directive", () => {
|
|
51
|
+
const code = `
|
|
52
|
+
"use awesome"; // Some other directive
|
|
53
|
+
"use client";
|
|
54
|
+
import React from 'react';
|
|
55
|
+
export default () => <div>Hello</div>;
|
|
56
|
+
`;
|
|
47
57
|
expect(hasDirective(code, "use client")).toBe(true);
|
|
48
58
|
});
|
|
49
59
|
it("should return false if no directive is present", () => {
|
|
50
60
|
const code = `import React from "react";
|
|
51
|
-
|
|
52
|
-
const MyComponent = () => <div>Hello</div>;`;
|
|
61
|
+
export default () => <div>Hello</div>;`;
|
|
53
62
|
expect(hasDirective(code, "use client")).toBe(false);
|
|
54
63
|
});
|
|
55
|
-
it("should return false if directive is
|
|
56
|
-
const code = `import React from "react";
|
|
57
|
-
"use client";
|
|
58
|
-
|
|
59
|
-
const MyComponent = () => <div>Hello</div>;`;
|
|
60
|
-
expect(hasDirective(code, "use client")).toBe(false);
|
|
61
|
-
});
|
|
62
|
-
it("should return false if directive is inside a single-line comment", () => {
|
|
64
|
+
it("should return false if the directive is commented out", () => {
|
|
63
65
|
const code = `// "use client";
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
import React from "react";
|
|
67
|
+
export default () => <div>Hello</div>;`;
|
|
66
68
|
expect(hasDirective(code, "use client")).toBe(false);
|
|
67
69
|
});
|
|
68
|
-
it("should return false if directive
|
|
69
|
-
const code =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const MyComponent = () => <div>Hello</div>;`;
|
|
70
|
+
it("should return false if the directive appears after code", () => {
|
|
71
|
+
const code = `import React from "react";
|
|
72
|
+
"use client";
|
|
73
|
+
export default () => <div>Hello</div>;`;
|
|
74
74
|
expect(hasDirective(code, "use client")).toBe(false);
|
|
75
75
|
});
|
|
76
|
-
it("should
|
|
77
|
-
const code = `
|
|
76
|
+
it("should handle multi-line comments correctly", () => {
|
|
77
|
+
const code = `
|
|
78
|
+
/*
|
|
79
|
+
* "use client";
|
|
80
|
+
*/
|
|
81
|
+
import React from "react";
|
|
82
|
+
`;
|
|
78
83
|
expect(hasDirective(code, "use client")).toBe(false);
|
|
79
84
|
});
|
|
80
|
-
it("should handle
|
|
81
|
-
const code = `"use client"
|
|
85
|
+
it("should handle code with no whitespace", () => {
|
|
86
|
+
const code = `"use client";import React from "react";`;
|
|
82
87
|
expect(hasDirective(code, "use client")).toBe(true);
|
|
83
88
|
});
|
|
84
|
-
it("should handle
|
|
85
|
-
const code =
|
|
86
|
-
|
|
87
|
-
/*
|
|
88
|
-
Another comment
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
'use client';
|
|
92
|
-
|
|
93
|
-
const MyComponent = () => <div>Hello</div>;
|
|
94
|
-
`;
|
|
95
|
-
expect(hasDirective(code, "use client")).toBe(true);
|
|
89
|
+
it("should handle empty code", () => {
|
|
90
|
+
const code = "";
|
|
91
|
+
expect(hasDirective(code, "use client")).toBe(false);
|
|
96
92
|
});
|
|
97
|
-
it("should handle
|
|
98
|
-
const code =
|
|
99
|
-
const MyComponent = () => <div>Hello</div>;
|
|
100
|
-
`;
|
|
93
|
+
it("should handle code with only whitespace", () => {
|
|
94
|
+
const code = " \n\t ";
|
|
101
95
|
expect(hasDirective(code, "use client")).toBe(false);
|
|
102
96
|
});
|
|
103
|
-
it("should
|
|
104
|
-
const code =
|
|
105
|
-
|
|
106
|
-
`;
|
|
97
|
+
it("should handle files with only comments", () => {
|
|
98
|
+
const code = `// comment 1
|
|
99
|
+
/* comment 2 */`;
|
|
107
100
|
expect(hasDirective(code, "use client")).toBe(false);
|
|
108
101
|
});
|
|
102
|
+
it("should prioritize 'use client' over 'use server'", () => {
|
|
103
|
+
const code = `'use client';\n'use server';\nconsole.log('hello');`;
|
|
104
|
+
expect(hasDirective(code, "use client")).toBe(true);
|
|
105
|
+
expect(hasDirective(code, "use server")).toBe(false);
|
|
106
|
+
});
|
|
109
107
|
});
|