Merge pull request #423 from zavoloklom/fix/js-code-path-replacer
fix: change JS path transformation for static resources
This commit is contained in:
commit
2b487aa959
@ -16,18 +16,25 @@ export function replaceImportsFromStaticInJsCode(params: { jsCode: string }): {
|
|||||||
const { jsCode } = params;
|
const { jsCode } = params;
|
||||||
|
|
||||||
const getReplaceArgs = (language: "js" | "css"): Parameters<typeof String.prototype.replace> => [
|
const getReplaceArgs = (language: "js" | "css"): Parameters<typeof String.prototype.replace> => [
|
||||||
new RegExp(`([a-zA-Z_]+)\\.([a-zA-Z]+)=function\\(([a-zA-Z]+)\\){return"static\\/${language}\\/"`, "g"),
|
new RegExp(`([a-zA-Z_]+)\\.([a-zA-Z]+)=(function\\(([a-z]+)\\){return|([a-z]+)=>)"static\\/${language}\\/"`, "g"),
|
||||||
(...[, n, u, e]) => `
|
(...[, n, u, matchedFunction, eForFunction]) => {
|
||||||
${n}[(function(){
|
const isArrowFunction = matchedFunction.includes("=>");
|
||||||
var pd= Object.getOwnPropertyDescriptor(${n}, "p");
|
const e = isArrowFunction ? matchedFunction.replace("=>", "").trim() : eForFunction;
|
||||||
|
|
||||||
|
return `
|
||||||
|
${n}[(function(){
|
||||||
|
var pd = Object.getOwnPropertyDescriptor(${n}, "p");
|
||||||
if( pd === undefined || pd.configurable ){
|
if( pd === undefined || pd.configurable ){
|
||||||
Object.defineProperty(${n}, "p", {
|
Object.defineProperty(${n}, "p", {
|
||||||
get: function() { return window.${ftlValuesGlobalName}.url.resourcesPath; },
|
get: function() { return window.${ftlValuesGlobalName}.url.resourcesPath; },
|
||||||
set: function (){}
|
set: function() {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return "${u}";
|
return "${u}";
|
||||||
})()] = function(${e}) { return "${true ? "/build/" : ""}static/${language}/"`
|
})()] = ${isArrowFunction ? `${e} =>` : `function(${e}) { return `} "/build/static/${language}/"`
|
||||||
|
.replace(/\s+/g, " ")
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const fixedJsCode = jsCode
|
const fixedJsCode = jsCode
|
||||||
|
@ -32,6 +32,10 @@ describe("bin/js-transforms", () => {
|
|||||||
908:"67c9ed2c"
|
908:"67c9ed2c"
|
||||||
}[e]+".chunk.css"
|
}[e]+".chunk.css"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n.u=e=>"static/js/"+e+"."+{69:"4f205f87",128:"49264537",453:"b2fed72e",482:"f0106901"}[e]+".chunk.js"
|
||||||
|
|
||||||
|
t.miniCssF=e=>"static/css/"+e+"."+{164:"dcfd7749",908:"67c9ed2c"}[e]+".chunk.css"
|
||||||
`;
|
`;
|
||||||
it("transforms standalone code properly", () => {
|
it("transforms standalone code properly", () => {
|
||||||
const { fixedJsCode } = replaceImportsFromStaticInJsCode({
|
const { fixedJsCode } = replaceImportsFromStaticInJsCode({
|
||||||
@ -52,11 +56,11 @@ describe("bin/js-transforms", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
__webpack_require__[(function (){
|
__webpack_require__[(function (){
|
||||||
var pd= Object.getOwnPropertyDescriptor(__webpack_require__, "p");
|
var pd = Object.getOwnPropertyDescriptor(__webpack_require__, "p");
|
||||||
if( pd === undefined || pd.configurable ){
|
if( pd === undefined || pd.configurable ){
|
||||||
Object.defineProperty(__webpack_require__, "p", {
|
Object.defineProperty(__webpack_require__, "p", {
|
||||||
get: function() { return window.kcContext.url.resourcesPath; },
|
get: function() { return window.kcContext.url.resourcesPath; },
|
||||||
set: function (){}
|
set: function() {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return "u";
|
return "u";
|
||||||
@ -69,11 +73,11 @@ describe("bin/js-transforms", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t[(function (){
|
t[(function (){
|
||||||
var pd= Object.getOwnPropertyDescriptor(t, "p");
|
var pd = Object.getOwnPropertyDescriptor(t, "p");
|
||||||
if( pd === undefined || pd.configurable ){
|
if( pd === undefined || pd.configurable ){
|
||||||
Object.defineProperty(t, "p", {
|
Object.defineProperty(t, "p", {
|
||||||
get: function() { return window.kcContext.url.resourcesPath; },
|
get: function() { return window.kcContext.url.resourcesPath; },
|
||||||
set: function (){}
|
set: function() {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return "miniCssF";
|
return "miniCssF";
|
||||||
@ -83,7 +87,28 @@ describe("bin/js-transforms", () => {
|
|||||||
908:"67c9ed2c"
|
908:"67c9ed2c"
|
||||||
} [e] + ".chunk.css"
|
} [e] + ".chunk.css"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n[(function(){
|
||||||
|
var pd = Object.getOwnPropertyDescriptor(n, "p");
|
||||||
|
if( pd === undefined || pd.configurable ){
|
||||||
|
Object.defineProperty(n, "p", {
|
||||||
|
get: function() { return window.kcContext.url.resourcesPath; },
|
||||||
|
set: function() {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return "u";
|
||||||
|
})()] = e => "/build/static/js/"+e+"."+{69:"4f205f87",128:"49264537",453:"b2fed72e",482:"f0106901"}[e]+".chunk.js"
|
||||||
|
|
||||||
|
t[(function(){
|
||||||
|
var pd = Object.getOwnPropertyDescriptor(t, "p");
|
||||||
|
if( pd === undefined || pd.configurable ){
|
||||||
|
Object.defineProperty(t, "p", {
|
||||||
|
get: function() { return window.kcContext.url.resourcesPath; },
|
||||||
|
set: function() {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return "miniCssF";
|
||||||
|
})()] = e => "/build/static/css/"+e+"."+{164:"dcfd7749",908:"67c9ed2c"}[e]+".chunk.css"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true);
|
expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user