weboptimizer 2.0.1235 → 2.0.1237

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ejsLoader.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // #!/usr/bin/env babel-node
2
2
  // -*- coding: utf-8 -*-
3
-
4
3
  /** @module ejsLoader */
5
4
  'use strict';
5
+
6
6
  /* !
7
7
  region header
8
8
  Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
@@ -16,48 +16,28 @@
16
16
  endregion
17
17
  */
18
18
  // region imports
19
-
20
19
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
21
-
22
20
  var _typeof = require("@babel/runtime/helpers/typeof");
23
-
24
21
  Object.defineProperty(exports, "__esModule", {
25
22
  value: true
26
23
  });
27
24
  exports.loader = exports["default"] = void 0;
28
-
29
25
  var _construct2 = _interopRequireDefault(require("@babel/runtime/helpers/construct"));
30
-
31
26
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
32
-
33
27
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
34
-
35
28
  var _core = require("@babel/core");
36
-
37
29
  var _babelPresetMinify = _interopRequireDefault(require("babel-preset-minify"));
38
-
39
30
  var _clientnode = _interopRequireWildcard(require("clientnode"));
40
-
41
31
  var _ejs = _interopRequireDefault(require("ejs"));
42
-
43
32
  var _fs = require("fs");
44
-
45
33
  var _htmlMinifier = require("html-minifier");
46
-
47
34
  var _path = require("path");
48
-
49
35
  var _configurator = _interopRequireDefault(require("./configurator"));
50
-
51
36
  var _helper = _interopRequireDefault(require("./helper"));
52
-
53
37
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
54
-
55
38
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
56
-
57
39
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
58
-
59
40
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
60
-
61
41
  // endregion
62
42
  var configuration = (0, _configurator["default"])();
63
43
  /**
@@ -67,11 +47,9 @@ var configuration = (0, _configurator["default"])();
67
47
  *
68
48
  * @returns Transformed string.
69
49
  */
70
-
71
50
  var loader = function loader(source) {
72
51
  var _ref,
73
- _this = this;
74
-
52
+ _this = this;
75
53
  var givenOptions = _clientnode["default"].convertSubstringInPlainObject(_clientnode["default"].extend(true, {
76
54
  compiler: {},
77
55
  compileSteps: 2,
@@ -92,7 +70,6 @@ var loader = function loader(source) {
92
70
  replacements: {}
93
71
  }
94
72
  }, 'getOptions' in this && this.getOptions() ? this.getOptions() : (_ref = this.query) !== null && _ref !== void 0 ? _ref : {}), /#%%%#/g, '!');
95
-
96
73
  var compile = function compile(template) {
97
74
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : givenOptions.compiler;
98
75
  var compileSteps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
@@ -102,12 +79,10 @@ var loader = function loader(source) {
102
79
  filename: template
103
80
  }, options);
104
81
  var givenLocals = [].concat(locals);
105
-
106
82
  var require = function require(request) {
107
83
  var nestedLocals = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
108
84
  var template = request.replace(/^(.+)\?[^?]+$/, '$1');
109
85
  var queryMatch = /^[^?]+\?(.+)$/.exec(request);
110
-
111
86
  if (queryMatch) {
112
87
  var evaluated = _clientnode["default"].stringEvaluate(queryMatch[1], {
113
88
  compile: compile,
@@ -116,22 +91,17 @@ var loader = function loader(source) {
116
91
  source: source,
117
92
  template: template
118
93
  });
119
-
120
94
  if (evaluated.error) console.warn('Error occurred during processing given query: ' + evaluated.error);else _clientnode["default"].extend(true, nestedLocals, evaluated.result);
121
95
  }
122
-
123
96
  var nestedOptions = _clientnode["default"].copy(options);
124
-
125
97
  delete nestedOptions.client;
126
98
  nestedOptions = _clientnode["default"].extend(true, {
127
99
  encoding: configuration.encoding
128
100
  }, nestedOptions, nestedLocals.options || {}, options);
129
101
  if (nestedOptions.isString) return compile(template, nestedOptions)(nestedLocals);
130
-
131
102
  var templateFilePath = _helper["default"].determineModuleFilePath(template, givenOptions.module.aliases, givenOptions.module.replacements, {
132
103
  file: givenOptions.extensions.file.internal
133
104
  }, 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);
134
-
135
105
  if (templateFilePath) {
136
106
  if ('addDependency' in _this) _this.addDependency(templateFilePath);
137
107
  /*
@@ -139,19 +109,15 @@ var loader = function loader(source) {
139
109
  file doesn't seem to be an ejs template we simply load
140
110
  included file content.
141
111
  */
142
-
143
112
  if (queryMatch || templateFilePath.endsWith('.ejs')) return compile(templateFilePath, nestedOptions)(nestedLocals);
144
113
  return (0, _fs.readFileSync)(templateFilePath, {
145
114
  encoding: nestedOptions.encoding
146
115
  });
147
116
  }
148
-
149
117
  throw new Error("Given template file \"".concat(template, "\" couldn't be resolved."));
150
118
  };
151
-
152
119
  var compressHTML = function compressHTML(content) {
153
120
  var _givenOptions$compres;
154
-
155
121
  return (_givenOptions$compres = givenOptions.compress) !== null && _givenOptions$compres !== void 0 && _givenOptions$compres.html ? (0, _htmlMinifier.minify)(content, _clientnode["default"].extend(true, {
156
122
  caseSensitive: true,
157
123
  collapseInlineTagWhitespace: true,
@@ -167,7 +133,6 @@ var loader = function loader(source) {
167
133
  removeStyleLinkTypeAttributes: true,
168
134
  sortAttributes: true,
169
135
  sortClassName: true,
170
-
171
136
  /*
172
137
  NOTE: Avoids whitespace around placeholder in
173
138
  tags.
@@ -176,7 +141,6 @@ var loader = function loader(source) {
176
141
  useShortDoctype: true
177
142
  }, givenOptions.compress.html || {})) : content;
178
143
  };
179
-
180
144
  var result = template;
181
145
  var isString = Boolean(options.isString);
182
146
  delete options.isString;
@@ -184,7 +148,6 @@ var loader = function loader(source) {
184
148
  var scope;
185
149
  var originalScopeNames;
186
150
  var scopeNames;
187
-
188
151
  for (var step = 1; step <= compileSteps; step += 1) {
189
152
  // On every odd compile step we have to determine the environment.
190
153
  if (step % 2) {
@@ -202,7 +165,8 @@ var loader = function loader(source) {
202
165
  originalScopeNames = Array.isArray(stepLocals) ? stepLocals : Object.keys(scope);
203
166
  scopeNames = originalScopeNames.map(function (name) {
204
167
  return _clientnode["default"].stringConvertToValidVariableName(name);
205
- }); // endregion
168
+ });
169
+ // endregion
206
170
  }
207
171
 
208
172
  if (typeof result === 'string') {
@@ -215,25 +179,23 @@ var loader = function loader(source) {
215
179
  encoding: encoding
216
180
  });
217
181
  }
218
-
219
182
  if (step === compileSteps) result = compressHTML(result);
220
- if (options.strict || !options._with) // NOTE: Needed to manipulate code after compiling.
183
+ if (options.strict || !options._with)
184
+ // NOTE: Needed to manipulate code after compiling.
221
185
  options.client = true;
222
186
  result = _ejs["default"].compile(result, options);
187
+
223
188
  /*
224
189
  Provide all scope names when "_with" options isn't
225
190
  enabled
226
191
  */
227
-
228
192
  if (options.strict || !options._with) {
229
193
  var localsName = options.localsName || 'locals';
230
-
231
194
  while (scopeNames.includes(localsName)) {
232
195
  localsName = "_".concat(localsName);
233
196
  }
234
- /* eslint-disable @typescript-eslint/no-implied-eval */
235
-
236
197
 
198
+ /* eslint-disable @typescript-eslint/no-implied-eval */
237
199
  result = (0, _construct2["default"])(Function, (0, _toConsumableArray2["default"])(scopeNames).concat([localsName, "return ".concat(result.toString(), "(") + "".concat(localsName, ",").concat(localsName, ".escapeFn,include,") + "".concat(localsName, ".rethrow)")]));
238
200
  /* eslint-enable @typescript-eslint/no-implied-eval */
239
201
  }
@@ -242,7 +204,6 @@ var loader = function loader(source) {
242
204
  return scope[name];
243
205
  }).concat(!options.strict && options._with ? [] : scope))));
244
206
  }
245
-
246
207
  if (compileSteps % 2) {
247
208
  var code = "module.exports = ".concat(result.toString());
248
209
  var processed = (0, _core.transformSync)(code, {
@@ -259,16 +220,13 @@ var loader = function loader(source) {
259
220
  if (typeof (processed === null || processed === void 0 ? void 0 : processed.code) === 'string') code = processed.code;
260
221
  return "".concat(options.strict ? "'use strict';\n" : '').concat(code);
261
222
  }
262
-
263
223
  if (typeof result === 'string') {
264
224
  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>');
265
225
  return result;
266
226
  }
267
-
268
227
  return '';
269
228
  };
270
229
  };
271
-
272
230
  return compile(source, _objectSpread(_objectSpread({}, givenOptions.compiler), {}, {
273
231
  client: Boolean(givenOptions.compileSteps % 2),
274
232
  compileDebug: givenOptions.debug,
@@ -278,11 +236,9 @@ var loader = function loader(source) {
278
236
  localsName: 'scope'
279
237
  }), givenOptions.compileSteps)(givenOptions.locals || {});
280
238
  };
281
-
282
239
  exports.loader = loader;
283
240
  var _default = loader; // region vim modline
284
241
  // vim: set tabstop=4 shiftwidth=4 expandtab:
285
242
  // vim: foldmethod=marker foldmarker=region,endregion:
286
243
  // endregion
287
-
288
244
  exports["default"] = _default;