-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathejsLoader.ts
426 lines (393 loc) · 15.3 KB
/
ejsLoader.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// #!/usr/bin/env babel-node
// -*- coding: utf-8 -*-
/** @module ejsLoader */
'use strict'
/* !
region header
Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
License
-------
This library written by Torben Sickert stand under a creative commons
naming 3.0 unported license.
See https://creativecommons.org/licenses/by/3.0/deed.de
endregion
*/
// region imports
import {
BabelFileResult, transformSync as babelTransformSync
} from '@babel/core'
import babelMinifyPreset from 'babel-preset-minify'
import {
convertSubstringInPlainObject,
convertToValidVariableName,
copy,
currentRequire,
evaluate,
EvaluationResult,
Encoding,
extend,
Mapping,
RecursivePartial,
UTILITY_SCOPE
} from 'clientnode'
import ejs, {Options, TemplateFunction as EJSTemplateFunction} from 'ejs'
import {readFileSync} from 'fs'
import {minify as minifyHTML} from 'html-minifier'
import {extname} from 'path'
import {LoaderContext} from 'webpack'
import getConfiguration from './configurator'
import {determineModuleFilePath} from './helper'
import {Extensions, Replacements, ResolvedConfiguration} from './type'
// endregion
// region types
export type PreCompiledTemplateFunction =
((..._parameters: Array<unknown>) => string)
export type TemplateFunction =
EJSTemplateFunction | PreCompiledTemplateFunction
export type CompilerOptions =
Options &
{
encoding: Encoding
isString?: boolean
}
export type CompileFunction =
(
template: string,
options?: Partial<CompilerOptions>,
compileSteps?: number
) => TemplateFunction
export type LoaderConfiguration =
Mapping<unknown> &
{
compiler: Partial<CompilerOptions>
compileSteps: number
compress: {
html: Mapping<unknown>
javaScript: Mapping<unknown>
}
context: string
debug: boolean
extensions: Extensions
locals?: Mapping<unknown>
module: {
aliases: Mapping
replacements: Replacements
}
}
// endregion
const configuration: ResolvedConfiguration = getConfiguration()
/**
* Main transformation function.
* @param source - Input string to transform.
* @returns Transformed string.
*/
export const loader = function(
this: Partial<LoaderContext<LoaderConfiguration>>, source: string
): string {
const givenOptions: RecursivePartial<LoaderConfiguration> =
convertSubstringInPlainObject(
extend<LoaderConfiguration>(
true,
{
compiler: {},
compileSteps: 2,
compress: {
html: {},
javaScript: {}
},
context: './',
debug: false,
extensions: {
file: {
external: [],
internal: [
'.js', '.json',
'.css',
'.svg', '.png', '.jpg', '.gif', '.ico',
'.html',
'.eot', '.ttf', '.woff', '.woff2'
]
}
},
module: {
aliases: {},
replacements: {}
}
},
this.getOptions ?
this.getOptions() :
(
this.query as
RecursivePartial<LoaderConfiguration> | null
) ?? {}
),
/#%%%#/g,
'!'
)
const compile: CompileFunction = (
template: string,
options = givenOptions.compiler,
compileSteps = 2
): TemplateFunction => (
locals:(
Array<Array<string>> | Array<Mapping<unknown>> | Mapping<unknown>
) = {}
): string => {
options = {filename: template, ...options}
const givenLocals: Array<unknown> =
([] as Array<unknown>).concat(locals)
const require = (
request: string, nestedLocals: Mapping<unknown> = {}
): string => {
const template: string = request.replace(/^(.+)\?[^?]+$/, '$1')
const queryMatch: Array<string> | null =
/^[^?]+\?(.+)$/.exec(request)
if (queryMatch) {
const evaluated: EvaluationResult<Mapping<unknown>> =
evaluate<Mapping<unknown>>(
queryMatch[1],
{compile, locals, request, source, template}
)
if (evaluated.error)
console.warn(
'Error occurred during processing given query: ' +
evaluated.error
)
else if (evaluated.result)
extend(true, nestedLocals, evaluated.result)
}
let nestedOptions = copy(options) as CompilerOptions
delete nestedOptions.client
nestedOptions = extend(
true,
{encoding: configuration.encoding},
nestedOptions,
nestedLocals.options as Partial<CompilerOptions> | undefined ||
{},
options ?? {}
)
if (nestedOptions.isString)
return compile(template, nestedOptions)(nestedLocals)
const templateFilePath: null | string = determineModuleFilePath(
template,
givenOptions.module?.aliases,
givenOptions.module?.replacements,
{file: givenOptions.extensions?.file.internal || []},
givenOptions.context,
configuration.path.source.asset.base,
configuration.path.ignore,
configuration.module.directoryNames,
configuration.package.main.fileNames,
configuration.package.main.propertyNames,
configuration.package.aliasPropertyNames,
configuration.encoding
)
if (templateFilePath) {
if (this.addDependency)
this.addDependency(templateFilePath)
/*
NOTE: If there aren't any locals options or variables and
file doesn't seem to be an ejs template we simply load
included file content.
*/
if (queryMatch || templateFilePath.endsWith('.ejs'))
return compile(templateFilePath, nestedOptions)(
nestedLocals
)
return readFileSync(
templateFilePath, {encoding: nestedOptions.encoding}
)
}
throw new Error(
`Given template file "${template}" couldn't be resolved.`
)
}
const compressHTML = (content: string): string =>
givenOptions.compress?.html ?
minifyHTML(
content,
extend(
true,
{
caseSensitive: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyCSS: true,
minifyJS: true,
processScripts: [
'text/ng-template',
'text/x-handlebars-template'
],
removeAttributeQuotes: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
/*
NOTE: Avoids whitespace around placeholder in
tags.
*/
trimCustomFragments: true,
useShortDoctype: true
},
givenOptions.compress.html
)
) :
content
let result: string | TemplateFunction = template
const isString = Boolean(options.isString)
delete options.isString
let stepLocals: Array<string> | Mapping<unknown>
let scope: Mapping<unknown> = {}
let originalScopeNames: Array<string> = []
let scopeNames: Array<string> = []
for (let step = 1; step <= compileSteps; step += 1) {
// On every odd compile step we have to determine the environment.
if (step % 2) {
// region determine scope
const localsIndex: number = Math.round(step / 2) - 1
stepLocals = (localsIndex < givenLocals.length) ?
givenLocals[localsIndex] as
Array<string> | Mapping<unknown> :
{}
scope = {}
if (step < 3 && 1 < compileSteps)
scope = {
...UTILITY_SCOPE,
configuration,
include: require,
require,
...(Array.isArray(stepLocals) ? {} : stepLocals)
}
else if (!Array.isArray(stepLocals))
scope = stepLocals
originalScopeNames =
Array.isArray(stepLocals) ? stepLocals : Object.keys(scope)
scopeNames = originalScopeNames.map((name: string): string =>
convertToValidVariableName(name)
)
// endregion
}
if (typeof result === 'string') {
const filePath: string | undefined =
isString ? options.filename : result
if (filePath && extname(filePath) === '.js' && currentRequire)
result = currentRequire(filePath) as TemplateFunction
else {
if (!isString) {
let encoding: Encoding = configuration.encoding
if (typeof options.encoding === 'string')
encoding = options.encoding
result = readFileSync(result, {encoding})
}
if (step === compileSteps)
result = compressHTML(result)
if (options.strict || !options._with)
// NOTE: Needed to manipulate code after compiling.
options.client = true
result = ejs.compile(result, options) as
EJSTemplateFunction
/*
Provide all scope names when "_with" options isn't
enabled
*/
if (options.strict || !options._with) {
let localsName: string = options.localsName || 'locals'
while (scopeNames.includes(localsName))
localsName = `_${localsName}`
/* eslint-disable @typescript-eslint/no-implied-eval */
result = new Function(
...scopeNames,
localsName,
`return ${result.toString()}(` +
`${localsName},${localsName}.escapeFn,include,` +
`${localsName}.rethrow)`
) as PreCompiledTemplateFunction
/* eslint-enable @typescript-eslint/no-implied-eval */
}
}
} else
result = compressHTML(!options.strict && options._with ?
(result as PreCompiledTemplateFunction)(
scope, scope.escapeFn, scope.include
) :
(result as PreCompiledTemplateFunction)(
/*
NOTE: We want to be ensure to have same ordering as
we have for the scope names and to call internal
registered getter by retrieving values. So simple
using "...Object.values(scope)" is not appreciate
here.
*/
...originalScopeNames
.map((name: string): unknown => scope[name])
.concat(
!options.strict && options._with ? [] : scope
)
)
)
}
if (compileSteps % 2) {
let code = `module.exports = ${result.toString()}`
const processed: BabelFileResult | null = babelTransformSync(
code,
{
ast: false,
babelrc: false,
comments: !givenOptions.compress?.javaScript,
compact: Boolean(givenOptions.compress?.javaScript),
filename: options.filename || 'unknown',
minified: Boolean(givenOptions.compress?.javaScript),
presets: givenOptions.compress?.javaScript ?
[[
babelMinifyPreset, givenOptions.compress.javaScript
]] :
[],
sourceMaps: false,
sourceType: 'script'
}
)
if (typeof processed?.code === 'string')
code = processed.code
return `${options.strict ? `'use strict';\n` : ''}${code}`
}
if (typeof result === 'string') {
result = result
.replace(
new RegExp(
`<script +processing-workaround *` +
`(?: = *(?: " *"|' *') *)?>([\\s\\S]*?)</ *script *>`,
'ig'
),
'$1'
)
.replace(
new RegExp(
`<script +processing(-+)-workaround *` +
`(?: = *(?: " *"|' *') *)?>([\\s\\S]*?)</ *script *>`,
'ig'
),
'<script processing$1workaround>$2</script>'
)
return result
}
return ''
}
return compile(
source,
{
...givenOptions.compiler,
client: Boolean((givenOptions.compileSteps ?? 2) % 2),
compileDebug: givenOptions.debug,
debug: givenOptions.debug,
filename: this.resourcePath || 'unknown',
isString: true,
localsName: 'scope'
},
givenOptions.compileSteps
)(givenOptions.locals || {})
}
export default loader