Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit 093ee10

Browse files
committed
feat: fix webpack PublicPath prob.
1 parent 4f0e865 commit 093ee10

File tree

1 file changed

+68
-23
lines changed

1 file changed

+68
-23
lines changed

src/wpPublicPath.js

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,95 @@
77

88
/**
99
* Get current script path
10-
* @return {String} The accessable path of current script
10+
* @return {String} The path of current script
1111
* @example
12-
* get 'dev/' or 'https://www.host.com/test/js/'
12+
* get 'file:///C:/git/live2d-widget/dev/bundle.js' or 'https://www.host.com/test/js/bundle.js'
1313
*/
1414

1515
function getCurrentPath(){
1616

17-
let currentScriptPath = null;
18-
1917
try{
20-
// FF, Chrome
18+
19+
// FF, Chrome, Modern browsers
2120
// use their API to get the path of current script
2221

2322
// a.b();
24-
// console.log('Stage1');
25-
let currentScriptElement = document.currentScript;
26-
currentScriptPath = currentScriptElement.getAttribute('src').replace(/[^/\\\\]+$/, '');
23+
console.log('wpStage1');
24+
25+
return document.currentScript.src;
26+
27+
if(DOC.currentScript){ // FF 4+
28+
return DOC.currentScript.src;
29+
}
30+
2731
}catch(e){
28-
// IE 10 +, Safari and Opera 9
2932

30-
// console.log('Stage2');
31-
console.log(e);
33+
// document.currentScript doesn't supports
34+
35+
console.log('wpStage2');
3236

33-
let stack = e.stack || e.sourceURL || e.stacktrace,
34-
rExtractUri = /(file:\/{3}\w:)(\/[^\/]*){3}\//;
35-
// TODO: Fix wrong RegExp: rExtractUri
36-
let absPath = rExtractUri.exec(stack);
37-
currentScriptPath = absPath[0];
37+
// Method 1
38+
// https://github.com/mozilla/pdf.js/blob/e081a708c36cb2aacff7889048863723fcf23671/src/shared/compatibility.js#L97
39+
// IE, Chrome < 29
3840

39-
if(currentScriptPath === ''){
40-
rExtractUri = /(?:http|https|file):\/\/.*?\/.+?.js/;
41-
absPath = rExtractUri.exec(stack);
42-
currentScriptPath = absPath[0];
41+
let scripts = document.getElementsByTagName('script');
42+
return scripts[scripts.length - 1].src;
43+
44+
/*
45+
// Method 2
46+
// parse the error stack trace maually
47+
// https://github.com/workhorsy/uncompress.js/blob/master/js/uncompress.js#L25
48+
49+
let stack = e.stack;
50+
let line = null;
51+
52+
// Chrome and IE
53+
if (stack.indexOf('@') !== -1) {
54+
line = stack.split('@')[1].split('\n')[0];
55+
// Firefox
56+
} else {
57+
line = stack.split('(')[1].split(')')[0];
4358
}
59+
line = line.substring(0, line.lastIndexOf('/')) + '/';
60+
return line;
61+
*/
62+
/*
63+
// Method 3
64+
// https://www.cnblogs.com/rubylouvre/archive/2013/01/23/2872618.html
4465
66+
let stack = e.stack;
67+
if(!stack && window.opera){
68+
// Opera 9没有e.stack,但有e.Backtrace,但不能直接取得,需要对e对象转字符串进行抽取
69+
stack = (String(e).match(/of linked script \S+/g) || []).join(' ');
70+
}
71+
if(stack){
72+
// e.stack最后一行在所有支持的浏览器大致如下:
73+
      // chrome23:
74+
      // @ http://113.93.50.63/data.js:4:1
75+
      // firefox17:
76+
      // @http://113.93.50.63/query.js:4
77+
      // opera12:
78+
      // @http://113.93.50.63/data.js:4
79+
      // IE10:
80+
      // @ Global code (http://113.93.50.63/data.js:4:1)
81+
     stack = stack.split(/[@ ]/g).pop(); // 取得最后一行,最后一个空格或@之后的部分
82+
stack = stack[0] == '(' ? stack.slice(1,-1) : stack;
83+
return stack.replace(/(:\d+)?:\d+$/i, ''); // 去掉行号与或许存在的出错字符起始位置
84+
}
85+
let nodes = head.getElementsByTagName('script'); // 只在head标签中寻找
86+
for(var i = 0, node; node = nodes[i++];){
87+
if(node.readyState === 'interactive'){
88+
return node.className = node.src;
89+
}
90+
}
91+
*/
4592
}
4693

47-
return currentScriptPath;
48-
4994
}
5095

5196
// expose the path to the global,
5297
// and wp will finish the following work
53-
__webpack_public_path__ = getCurrentPath();
98+
__webpack_public_path__ = getCurrentPath().replace(/[^/\\\\]+$/, '');
5499
if (process.env.NODE_ENV === 'development'){
55100
console.log(`wpPP: publicPath: ${__webpack_public_path__}`);
56101
}

0 commit comments

Comments
 (0)