vite-plugin-php 3.0.0-beta.1 → 3.0.0-beta.3
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/README.md +15 -0
- package/dist/index.cjs +66 -43
- package/dist/index.mjs +66 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -171,6 +171,21 @@ usePHP({
|
|
|
171
171
|
});
|
|
172
172
|
```
|
|
173
173
|
|
|
174
|
+
Since version 2.0.4 it is possible to point to some external files. Make sure the change URL points to an external origin:
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
usePHP({
|
|
178
|
+
rewriteUrl(requestUrl) {
|
|
179
|
+
if (requestUrl.pathname.startsWith('/media/')) {
|
|
180
|
+
return new URL(
|
|
181
|
+
'https://nititech.de' +
|
|
182
|
+
requestUrl.toString().substring(requestUrl.origin.length),
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
174
189
|
⚠️ **Attention:** If using the rewriteUrl property you will need to exclude (_return undefined_) assets like CSS, JavaScript, Images, etc.., that match your transpiled php file names, on your own!
|
|
175
190
|
|
|
176
191
|
#### Error logging
|
package/dist/index.cjs
CHANGED
|
@@ -10835,6 +10835,47 @@ const handleExit = {
|
|
|
10835
10835
|
}
|
|
10836
10836
|
};
|
|
10837
10837
|
|
|
10838
|
+
const assetsPattern = new RegExp(
|
|
10839
|
+
`^(.+?)(${phpStartPattern}\\s+namespace\\s\\S+?(?:\\s*;|\\s*{).+)$`,
|
|
10840
|
+
"si"
|
|
10841
|
+
);
|
|
10842
|
+
const viteClientInjection = '<script type="module" src="/@vite/client"><\/script>\n';
|
|
10843
|
+
const viteClientInjectionPattern = new RegExp(
|
|
10844
|
+
'<script.+?src="/@vite/client".+?<\/script>(\r\n|\n|\r)',
|
|
10845
|
+
"si"
|
|
10846
|
+
);
|
|
10847
|
+
const lastTagPattern = new RegExp(`^(.+(?:</.+?>|<.+?/>))(.+|)$`, "si");
|
|
10848
|
+
const closingPattern = new RegExp(`^(.+)(${phpStartPattern}.+)$`, "si");
|
|
10849
|
+
function fixAssetsInjection(input) {
|
|
10850
|
+
let out = input;
|
|
10851
|
+
let assets = "";
|
|
10852
|
+
out = out.replace(assetsPattern, (match, p1, p2) => {
|
|
10853
|
+
assets = p1.trim();
|
|
10854
|
+
return p2;
|
|
10855
|
+
});
|
|
10856
|
+
const injectAssets = (_, part1, part2) => {
|
|
10857
|
+
let a = assets;
|
|
10858
|
+
if (!(part1.endsWith("\n") || part1.endsWith("\r"))) {
|
|
10859
|
+
a = "\r\n" + a;
|
|
10860
|
+
}
|
|
10861
|
+
if (!(part2.startsWith("\n") || part2.startsWith("\r"))) {
|
|
10862
|
+
a += "\r\n";
|
|
10863
|
+
}
|
|
10864
|
+
assets = "";
|
|
10865
|
+
return `${part1}${a}${part2}`;
|
|
10866
|
+
};
|
|
10867
|
+
if (assets) {
|
|
10868
|
+
out = out.replace(lastTagPattern, injectAssets);
|
|
10869
|
+
}
|
|
10870
|
+
if (assets) {
|
|
10871
|
+
out = out.replace(closingPattern, injectAssets);
|
|
10872
|
+
}
|
|
10873
|
+
if (assets) {
|
|
10874
|
+
out += assets;
|
|
10875
|
+
}
|
|
10876
|
+
return out;
|
|
10877
|
+
}
|
|
10878
|
+
|
|
10838
10879
|
const phpProxy = async (req, res, next) => {
|
|
10839
10880
|
try {
|
|
10840
10881
|
if (req.url && !["/@vite", "/@fs", "/@id/__x00__", "/node_modules"].some(
|
|
@@ -10843,19 +10884,23 @@ const phpProxy = async (req, res, next) => {
|
|
|
10843
10884
|
req.on("error", (error) => {
|
|
10844
10885
|
throw error;
|
|
10845
10886
|
});
|
|
10846
|
-
|
|
10887
|
+
let url = new URL(req.url, "http://localhost");
|
|
10847
10888
|
if (shared.viteConfig?.server.port) {
|
|
10848
10889
|
url.port = shared.viteConfig.server.port.toString();
|
|
10849
10890
|
}
|
|
10850
|
-
const requestUrl = url
|
|
10891
|
+
const requestUrl = new URL(url);
|
|
10851
10892
|
if (url.pathname.endsWith("/")) {
|
|
10852
10893
|
url.pathname += "index.php";
|
|
10853
10894
|
}
|
|
10854
10895
|
const routedUrl = serve.rewriteUrl(url);
|
|
10855
10896
|
if (routedUrl) {
|
|
10856
|
-
|
|
10857
|
-
|
|
10858
|
-
|
|
10897
|
+
if (routedUrl.origin !== requestUrl.origin) {
|
|
10898
|
+
res.writeHead(307, {
|
|
10899
|
+
location: routedUrl.toString()
|
|
10900
|
+
}).end();
|
|
10901
|
+
return;
|
|
10902
|
+
}
|
|
10903
|
+
url = routedUrl;
|
|
10859
10904
|
}
|
|
10860
10905
|
const entryPathname = url.pathname.substring(1);
|
|
10861
10906
|
const entry = shared.entries.find((file) => {
|
|
@@ -10870,7 +10915,7 @@ const phpProxy = async (req, res, next) => {
|
|
|
10870
10915
|
url.searchParams.set(
|
|
10871
10916
|
internalParam,
|
|
10872
10917
|
new URLSearchParams({
|
|
10873
|
-
$REQUEST_URI: requestUrl,
|
|
10918
|
+
$REQUEST_URI: requestUrl.pathname,
|
|
10874
10919
|
$PHP_SELF: "/" + entry,
|
|
10875
10920
|
temp_dir: shared.tempDir,
|
|
10876
10921
|
error_levels: shared.devConfig.errorLevels.toString()
|
|
@@ -10909,7 +10954,15 @@ const phpProxy = async (req, res, next) => {
|
|
|
10909
10954
|
).on("error", (error) => {
|
|
10910
10955
|
reject(error);
|
|
10911
10956
|
}).on("close", () => {
|
|
10912
|
-
|
|
10957
|
+
let content = Buffer.concat(chunks).toString("utf8");
|
|
10958
|
+
if (incomingHeaders["content-type"]?.includes(
|
|
10959
|
+
"text/html"
|
|
10960
|
+
) && !viteClientInjectionPattern.test(content)) {
|
|
10961
|
+
content = viteClientInjection + content;
|
|
10962
|
+
if (incomingHeaders["content-length"]) {
|
|
10963
|
+
incomingHeaders["content-length"] = `${content.length}`;
|
|
10964
|
+
}
|
|
10965
|
+
}
|
|
10913
10966
|
resolve2({
|
|
10914
10967
|
statusCode,
|
|
10915
10968
|
headers: incomingHeaders,
|
|
@@ -10937,42 +10990,6 @@ const phpProxy = async (req, res, next) => {
|
|
|
10937
10990
|
next();
|
|
10938
10991
|
};
|
|
10939
10992
|
|
|
10940
|
-
const assetsPattern = new RegExp(
|
|
10941
|
-
`^(.+?)(${phpStartPattern}\\s+namespace\\s\\S+?(?:\\s*;|\\s*{).+)$`,
|
|
10942
|
-
"si"
|
|
10943
|
-
);
|
|
10944
|
-
const lastTagPattern = new RegExp(`^(.+(?:</.+?>|<.+?/>))(.+|)$`, "si");
|
|
10945
|
-
const closingPattern = new RegExp(`^(.+)(${phpStartPattern}.+)$`, "si");
|
|
10946
|
-
function fixAssetsInjection(input) {
|
|
10947
|
-
let out = input;
|
|
10948
|
-
let assets = "";
|
|
10949
|
-
out = out.replace(assetsPattern, (match, p1, p2) => {
|
|
10950
|
-
assets = p1.trim();
|
|
10951
|
-
return p2;
|
|
10952
|
-
});
|
|
10953
|
-
const injectAssets = (_, part1, part2) => {
|
|
10954
|
-
let a = assets;
|
|
10955
|
-
if (!(part1.endsWith("\n") || part1.endsWith("\r"))) {
|
|
10956
|
-
a = "\r\n" + a;
|
|
10957
|
-
}
|
|
10958
|
-
if (!(part2.startsWith("\n") || part2.startsWith("\r"))) {
|
|
10959
|
-
a += "\r\n";
|
|
10960
|
-
}
|
|
10961
|
-
assets = "";
|
|
10962
|
-
return `${part1}${a}${part2}`;
|
|
10963
|
-
};
|
|
10964
|
-
if (assets) {
|
|
10965
|
-
out = out.replace(lastTagPattern, injectAssets);
|
|
10966
|
-
}
|
|
10967
|
-
if (assets) {
|
|
10968
|
-
out = out.replace(closingPattern, injectAssets);
|
|
10969
|
-
}
|
|
10970
|
-
if (assets) {
|
|
10971
|
-
out += assets;
|
|
10972
|
-
}
|
|
10973
|
-
return out;
|
|
10974
|
-
}
|
|
10975
|
-
|
|
10976
10993
|
const serve = {
|
|
10977
10994
|
rewriteUrl: (url) => url
|
|
10978
10995
|
};
|
|
@@ -11125,6 +11142,12 @@ const servePlugin = [
|
|
|
11125
11142
|
php.code = PHP_Code.unescape(php.code, escapes);
|
|
11126
11143
|
}
|
|
11127
11144
|
php.code = fixAssetsInjection(php.code);
|
|
11145
|
+
if (!php.code.includes("</head>")) {
|
|
11146
|
+
php.code = php.code.replace(
|
|
11147
|
+
new RegExp(viteClientInjectionPattern, "gsi"),
|
|
11148
|
+
""
|
|
11149
|
+
);
|
|
11150
|
+
}
|
|
11128
11151
|
php.write(tempName(entry));
|
|
11129
11152
|
return php.code;
|
|
11130
11153
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -10818,6 +10818,47 @@ const handleExit = {
|
|
|
10818
10818
|
}
|
|
10819
10819
|
};
|
|
10820
10820
|
|
|
10821
|
+
const assetsPattern = new RegExp(
|
|
10822
|
+
`^(.+?)(${phpStartPattern}\\s+namespace\\s\\S+?(?:\\s*;|\\s*{).+)$`,
|
|
10823
|
+
"si"
|
|
10824
|
+
);
|
|
10825
|
+
const viteClientInjection = '<script type="module" src="/@vite/client"><\/script>\n';
|
|
10826
|
+
const viteClientInjectionPattern = new RegExp(
|
|
10827
|
+
'<script.+?src="/@vite/client".+?<\/script>(\r\n|\n|\r)',
|
|
10828
|
+
"si"
|
|
10829
|
+
);
|
|
10830
|
+
const lastTagPattern = new RegExp(`^(.+(?:</.+?>|<.+?/>))(.+|)$`, "si");
|
|
10831
|
+
const closingPattern = new RegExp(`^(.+)(${phpStartPattern}.+)$`, "si");
|
|
10832
|
+
function fixAssetsInjection(input) {
|
|
10833
|
+
let out = input;
|
|
10834
|
+
let assets = "";
|
|
10835
|
+
out = out.replace(assetsPattern, (match, p1, p2) => {
|
|
10836
|
+
assets = p1.trim();
|
|
10837
|
+
return p2;
|
|
10838
|
+
});
|
|
10839
|
+
const injectAssets = (_, part1, part2) => {
|
|
10840
|
+
let a = assets;
|
|
10841
|
+
if (!(part1.endsWith("\n") || part1.endsWith("\r"))) {
|
|
10842
|
+
a = "\r\n" + a;
|
|
10843
|
+
}
|
|
10844
|
+
if (!(part2.startsWith("\n") || part2.startsWith("\r"))) {
|
|
10845
|
+
a += "\r\n";
|
|
10846
|
+
}
|
|
10847
|
+
assets = "";
|
|
10848
|
+
return `${part1}${a}${part2}`;
|
|
10849
|
+
};
|
|
10850
|
+
if (assets) {
|
|
10851
|
+
out = out.replace(lastTagPattern, injectAssets);
|
|
10852
|
+
}
|
|
10853
|
+
if (assets) {
|
|
10854
|
+
out = out.replace(closingPattern, injectAssets);
|
|
10855
|
+
}
|
|
10856
|
+
if (assets) {
|
|
10857
|
+
out += assets;
|
|
10858
|
+
}
|
|
10859
|
+
return out;
|
|
10860
|
+
}
|
|
10861
|
+
|
|
10821
10862
|
const phpProxy = async (req, res, next) => {
|
|
10822
10863
|
try {
|
|
10823
10864
|
if (req.url && !["/@vite", "/@fs", "/@id/__x00__", "/node_modules"].some(
|
|
@@ -10826,19 +10867,23 @@ const phpProxy = async (req, res, next) => {
|
|
|
10826
10867
|
req.on("error", (error) => {
|
|
10827
10868
|
throw error;
|
|
10828
10869
|
});
|
|
10829
|
-
|
|
10870
|
+
let url = new URL(req.url, "http://localhost");
|
|
10830
10871
|
if (shared.viteConfig?.server.port) {
|
|
10831
10872
|
url.port = shared.viteConfig.server.port.toString();
|
|
10832
10873
|
}
|
|
10833
|
-
const requestUrl = url
|
|
10874
|
+
const requestUrl = new URL(url);
|
|
10834
10875
|
if (url.pathname.endsWith("/")) {
|
|
10835
10876
|
url.pathname += "index.php";
|
|
10836
10877
|
}
|
|
10837
10878
|
const routedUrl = serve.rewriteUrl(url);
|
|
10838
10879
|
if (routedUrl) {
|
|
10839
|
-
|
|
10840
|
-
|
|
10841
|
-
|
|
10880
|
+
if (routedUrl.origin !== requestUrl.origin) {
|
|
10881
|
+
res.writeHead(307, {
|
|
10882
|
+
location: routedUrl.toString()
|
|
10883
|
+
}).end();
|
|
10884
|
+
return;
|
|
10885
|
+
}
|
|
10886
|
+
url = routedUrl;
|
|
10842
10887
|
}
|
|
10843
10888
|
const entryPathname = url.pathname.substring(1);
|
|
10844
10889
|
const entry = shared.entries.find((file) => {
|
|
@@ -10853,7 +10898,7 @@ const phpProxy = async (req, res, next) => {
|
|
|
10853
10898
|
url.searchParams.set(
|
|
10854
10899
|
internalParam,
|
|
10855
10900
|
new URLSearchParams({
|
|
10856
|
-
$REQUEST_URI: requestUrl,
|
|
10901
|
+
$REQUEST_URI: requestUrl.pathname,
|
|
10857
10902
|
$PHP_SELF: "/" + entry,
|
|
10858
10903
|
temp_dir: shared.tempDir,
|
|
10859
10904
|
error_levels: shared.devConfig.errorLevels.toString()
|
|
@@ -10892,7 +10937,15 @@ const phpProxy = async (req, res, next) => {
|
|
|
10892
10937
|
).on("error", (error) => {
|
|
10893
10938
|
reject(error);
|
|
10894
10939
|
}).on("close", () => {
|
|
10895
|
-
|
|
10940
|
+
let content = Buffer.concat(chunks).toString("utf8");
|
|
10941
|
+
if (incomingHeaders["content-type"]?.includes(
|
|
10942
|
+
"text/html"
|
|
10943
|
+
) && !viteClientInjectionPattern.test(content)) {
|
|
10944
|
+
content = viteClientInjection + content;
|
|
10945
|
+
if (incomingHeaders["content-length"]) {
|
|
10946
|
+
incomingHeaders["content-length"] = `${content.length}`;
|
|
10947
|
+
}
|
|
10948
|
+
}
|
|
10896
10949
|
resolve2({
|
|
10897
10950
|
statusCode,
|
|
10898
10951
|
headers: incomingHeaders,
|
|
@@ -10920,42 +10973,6 @@ const phpProxy = async (req, res, next) => {
|
|
|
10920
10973
|
next();
|
|
10921
10974
|
};
|
|
10922
10975
|
|
|
10923
|
-
const assetsPattern = new RegExp(
|
|
10924
|
-
`^(.+?)(${phpStartPattern}\\s+namespace\\s\\S+?(?:\\s*;|\\s*{).+)$`,
|
|
10925
|
-
"si"
|
|
10926
|
-
);
|
|
10927
|
-
const lastTagPattern = new RegExp(`^(.+(?:</.+?>|<.+?/>))(.+|)$`, "si");
|
|
10928
|
-
const closingPattern = new RegExp(`^(.+)(${phpStartPattern}.+)$`, "si");
|
|
10929
|
-
function fixAssetsInjection(input) {
|
|
10930
|
-
let out = input;
|
|
10931
|
-
let assets = "";
|
|
10932
|
-
out = out.replace(assetsPattern, (match, p1, p2) => {
|
|
10933
|
-
assets = p1.trim();
|
|
10934
|
-
return p2;
|
|
10935
|
-
});
|
|
10936
|
-
const injectAssets = (_, part1, part2) => {
|
|
10937
|
-
let a = assets;
|
|
10938
|
-
if (!(part1.endsWith("\n") || part1.endsWith("\r"))) {
|
|
10939
|
-
a = "\r\n" + a;
|
|
10940
|
-
}
|
|
10941
|
-
if (!(part2.startsWith("\n") || part2.startsWith("\r"))) {
|
|
10942
|
-
a += "\r\n";
|
|
10943
|
-
}
|
|
10944
|
-
assets = "";
|
|
10945
|
-
return `${part1}${a}${part2}`;
|
|
10946
|
-
};
|
|
10947
|
-
if (assets) {
|
|
10948
|
-
out = out.replace(lastTagPattern, injectAssets);
|
|
10949
|
-
}
|
|
10950
|
-
if (assets) {
|
|
10951
|
-
out = out.replace(closingPattern, injectAssets);
|
|
10952
|
-
}
|
|
10953
|
-
if (assets) {
|
|
10954
|
-
out += assets;
|
|
10955
|
-
}
|
|
10956
|
-
return out;
|
|
10957
|
-
}
|
|
10958
|
-
|
|
10959
10976
|
const serve = {
|
|
10960
10977
|
rewriteUrl: (url) => url
|
|
10961
10978
|
};
|
|
@@ -11108,6 +11125,12 @@ const servePlugin = [
|
|
|
11108
11125
|
php.code = PHP_Code.unescape(php.code, escapes);
|
|
11109
11126
|
}
|
|
11110
11127
|
php.code = fixAssetsInjection(php.code);
|
|
11128
|
+
if (!php.code.includes("</head>")) {
|
|
11129
|
+
php.code = php.code.replace(
|
|
11130
|
+
new RegExp(viteClientInjectionPattern, "gsi"),
|
|
11131
|
+
""
|
|
11132
|
+
);
|
|
11133
|
+
}
|
|
11111
11134
|
php.write(tempName(entry));
|
|
11112
11135
|
return php.code;
|
|
11113
11136
|
}
|