vite-plugin-comment-delete 1.1.1 → 1.1.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.
Files changed (2) hide show
  1. package/index.js +18 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,7 +6,6 @@ export default function commentDelete(outDir = "dist") {
6
6
  name: "vite-plugin-comments-delete",
7
7
  closeBundle() {
8
8
  const distDir = path.resolve(process.cwd(), outDir);
9
-
10
9
  if (!fs.existsSync(distDir)) return;
11
10
 
12
11
  const walk = (dir) => {
@@ -16,17 +15,29 @@ export default function commentDelete(outDir = "dist") {
16
15
 
17
16
  if (stat.isDirectory()) {
18
17
  walk(fullPath);
19
- } else if (/\.(html|php)$/.test(file)) {
18
+ return;
19
+ }
20
+
21
+ // ---------- HTML ----------
22
+ if (file.endsWith(".html")) {
20
23
  let content = fs.readFileSync(fullPath, "utf-8");
21
24
 
22
- // HTMLコメント削除
25
+ // HTMLコメントのみ削除
23
26
  content = content.replace(/<!--[\s\S]*?-->/g, "");
24
27
 
25
- // PHPコメント削除
28
+ fs.writeFileSync(fullPath, content);
29
+ return;
30
+ }
31
+
32
+ // ---------- PHP ----------
33
+ if (file.endsWith(".php")) {
34
+ let content = fs.readFileSync(fullPath, "utf-8");
35
+
36
+ // PHPコメント削除(URLを壊さない)
26
37
  content = content
27
- .replace(/\/\*[\s\S]*?\*\//g, "")
28
- .replace(/\/\/.*$/gm, "")
29
- .replace(/#.*$/gm, "");
38
+ .replace(/\/\*[\s\S]*?\*\//g, "") // /* */
39
+ .replace(/(^|\s)\/\/.*$/gm, "$1") // // 行コメント
40
+ .replace(/(^|\s)#.*$/gm, "$1"); // #
30
41
 
31
42
  fs.writeFileSync(fullPath, content);
32
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-comment-delete",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Build完了後にdist内に含まれるhtmlとphpのコメントアウトを削除する",
5
5
  "main": "index.js",
6
6
  "keywords": [