forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeGenerator.ts
More file actions
187 lines (164 loc) · 5.04 KB
/
codeGenerator.ts
File metadata and controls
187 lines (164 loc) · 5.04 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { EOL } from 'os';
import * as uglify from 'uglify-js';
import {
ISetWebpackPublicPathOptions
} from './SetPublicPathPlugin';
/**
* @public
*/
export const registryVariableName: string = 'window.__setWebpackPublicPathLoaderSrcRegistry__';
export interface IInternalOptions extends ISetWebpackPublicPathOptions {
webpackPublicPathVariable?: string;
regexName?: string;
linePrefix?: string;
}
const varName: string = 'publicPath';
function joinLines(lines: string[], linePrefix?: string): string {
return lines.map((line: string) => {
if (line) {
return `${linePrefix || ''}${line}`;
} else {
return line;
}
}).join(EOL).replace(new RegExp(`${EOL}${EOL}+`, 'g'), `${EOL}${EOL}`);
}
function escapeSingleQuotes(str: string): string | undefined {
if (str) {
return str.replace('\'', '\\\'');
} else {
return undefined;
}
}
function appendSlashAndEscapeSingleQuotes(str: string): string | undefined {
if (str && str.substr(-1) !== '/') {
str = str + '/';
}
return escapeSingleQuotes(str);
}
export function getSetPublicPathCode(options: IInternalOptions, emitWarning: (warning: string) => void): string {
if (!options.webpackPublicPathVariable) {
throw new Error('"webpackPublicPathVariable" option must be defined.');
}
let lines: string[] = [];
if (options.regexName) {
lines = [
`var scripts = document.getElementsByTagName('script');`
];
const regexInitializationSnippet: string = `/${options.regexName}/i`;
const regexVarName: string | undefined = options.regexVariable;
if (options.regexVariable) {
lines.push(...[
`var regex = (typeof ${regexVarName} !== 'undefined') ? ${regexVarName} : ${regexInitializationSnippet};`
]);
} else {
lines.push(...[
`var regex = ${regexInitializationSnippet};`
]);
}
lines.push(...[
`var ${varName};`,
'',
'if (scripts && scripts.length) {',
' for (var i = 0; i < scripts.length; i++) {',
' if (!scripts[i]) continue;',
` var path = scripts[i].getAttribute('src');`,
' if (path && path.match(regex)) {',
` ${varName} = path.substring(0, path.lastIndexOf('/') + 1);`,
...(options.preferLastFoundScript ? [] : [' break;']),
' }',
' }',
'}',
'',
`if (!${varName}) {`,
` for (var global in ${registryVariableName}) {`,
' if (global && global.match(regex)) {',
` ${varName} = global.substring(0, global.lastIndexOf('/') + 1);`,
...(options.preferLastFoundScript ? [] : [' break;']),
' }',
' }',
'}'
]);
if (options.getPostProcessScript) {
lines.push(...[
'',
`if (${varName}) {`,
` ${options.getPostProcessScript(varName)};`,
'}',
''
]);
}
} else {
if (options.publicPath) {
lines.push(...[
`var ${varName} = '${appendSlashAndEscapeSingleQuotes(options.publicPath)}';`,
''
]);
} else if (options.systemJs) {
lines.push(...[
`var ${varName} = window.System ? window.System.baseURL || '' : '';`,
`if (${varName} !== '' && ${varName}.substr(-1) !== '/') ${varName} += '/';`,
''
]);
} else {
emitWarning(`Neither 'publicPath' nor 'systemJs' is defined, so the public path will not be modified`);
return '';
}
if (options.urlPrefix && options.urlPrefix !== '') {
lines.push(...[
`${varName} += '${appendSlashAndEscapeSingleQuotes(options.urlPrefix)}';`,
''
]);
}
if (options.getPostProcessScript) {
lines.push(...[
`if (${varName}) {`,
` ${options.getPostProcessScript(varName)};`,
'}',
''
]);
}
}
lines.push(
`${options.webpackPublicPathVariable} = ${varName};`
);
return joinLines(lines, options.linePrefix);
}
/**
* /**
* This function returns a block of JavaScript that maintains a global register of script tags.
*
* @param debug - If true, the code returned code is not minified. Defaults to false.
*
* @public
*/
export function getGlobalRegisterCode(debug: boolean = false): string {
const lines: string[] = [
'(function(){',
`if (!${registryVariableName}) ${registryVariableName}={};`,
`var scripts = document.getElementsByTagName('script');`,
'if (scripts && scripts.length) {',
' for (var i = 0; i < scripts.length; i++) {',
' if (!scripts[i]) continue;',
` var path = scripts[i].getAttribute('src');`,
` if (path) ${registryVariableName}[path]=true;`,
' }',
'}',
'})();'
];
const joinedScript: string = joinLines(lines);
if (debug) {
return `${EOL}${joinedScript}`;
} else {
const minifyOutput: uglify.MinifyOutput = uglify.minify(
joinedScript,
{
compress: {
dead_code: true
}
}
);
return `${EOL}${minifyOutput.code}`;
}
}