-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathindex.js
91 lines (71 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const fs = require('fs');
function getFingerprint() {
const package = fs.readFileSync('./package.json');
const packageJson = JSON.parse(package);
const timestamp = Math.round(Date.now() / 1000);
const version = packageJson.version.replace(/[^\w-]/g, '_');
return `"v${version}m${timestamp}"`;
}
/**
* This code is injected as-is in the webpack artifact and must be ES5 compatible to work in all scenarios.
* This will not get transpiled.
*/
const resolveImportSource = () => `\
var getCurrentScript = function() {
var script = document.currentScript;
if (!script) {
/* Shim for IE11 and below */
/* Do not take into account async scripts and inline scripts */
var doc_scripts = document.getElementsByTagName('script');
var scripts = [];
for (var i = 0; i < doc_scripts.length; i++) {
scripts.push(doc_scripts[i]);
}
scripts = scripts.filter(function(s) { return !s.async && !s.text && !s.textContent; });
script = scripts.slice(-1)[0];
}
return script;
};
var isLocalScript = function(script) {
return /\\\/_dash-component-suites\\\//.test(script.src);
};
Object.defineProperty(__webpack_require__, 'p', {
get: (function () {
var script = getCurrentScript();
var url = script.src.split('/').slice(0, -1).join('/') + '/';
return function() {
return url;
};
})()
});
if (typeof jsonpScriptSrc !== 'undefined') {
var __jsonpScriptSrc__ = jsonpScriptSrc;
jsonpScriptSrc = function(chunkId) {
var script = getCurrentScript();
var isLocal = isLocalScript(script);
var src = __jsonpScriptSrc__(chunkId);
if(!isLocal) {
return src;
}
var srcFragments = src.split('/');
var fileFragments = srcFragments.slice(-1)[0].split('.');
fileFragments.splice(1, 0, ${getFingerprint()});
srcFragments.splice(-1, 1, fileFragments.join('.'))
return srcFragments.join('/');
};
}
`
class WebpackDashDynamicImport {
apply(compiler) {
compiler.hooks.compilation.tap('WebpackDashDynamicImport', compilation => {
compilation.mainTemplate.hooks.requireExtensions.tap('WebpackDashDynamicImport > RequireExtensions', (source, chunk, hash) => {
// Prevent CSS chunks from having JS appended to them
if (chunk.name === 'mini-css-extract-plugin') {
return source;
}
return source + resolveImportSource();
});
});
}
}
module.exports = WebpackDashDynamicImport;