Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. # CJS Module Lexer
  2. [![Build Status][travis-image]][travis-url]
  3. A [very fast](#benchmarks) JS CommonJS module syntax lexer used to detect the most likely list of named exports of a CommonJS module.
  4. Outputs the list of named exports (`exports.name = ...`) and possible module reexports (`module.exports = require('...')`), including the common transpiler variations of these cases.
  5. Forked from https://github.com/guybedford/es-module-lexer.
  6. _Comprehensively handles the JS language grammar while remaining small and fast. - ~90ms per MB of JS cold and ~15ms per MB of JS warm, [see benchmarks](#benchmarks) for more info._
  7. ### Usage
  8. ```
  9. npm install cjs-module-lexer
  10. ```
  11. For use in CommonJS:
  12. ```js
  13. const { parse } = require('cjs-module-lexer');
  14. // `init` return a promise for parity with the ESM API, but you do not have to call it
  15. const { exports, reexports } = parse(`
  16. // named exports detection
  17. module.exports.a = 'a';
  18. (function () {
  19. exports.b = 'b';
  20. })();
  21. Object.defineProperty(exports, 'c', { value: 'c' });
  22. /* exports.d = 'not detected'; */
  23. // reexports detection
  24. if (maybe) module.exports = require('./dep1.js');
  25. if (another) module.exports = require('./dep2.js');
  26. // literal exports assignments
  27. module.exports = { a, b: c, d, 'e': f }
  28. // __esModule detection
  29. Object.defineProperty(module.exports, '__esModule', { value: true })
  30. `);
  31. // exports === ['a', 'b', 'c', '__esModule']
  32. // reexports === ['./dep1.js', './dep2.js']
  33. ```
  34. When using the ESM version, Wasm is supported instead:
  35. ```js
  36. import { parse, init } from 'cjs-module-lexer';
  37. // init needs to be called and waited upon
  38. await init();
  39. const { exports, reexports } = parse(source);
  40. ```
  41. The Wasm build is around 1.5x faster and without a cold start.
  42. ### Grammar
  43. CommonJS exports matches are run against the source token stream.
  44. The token grammar is:
  45. ```
  46. IDENTIFIER: As defined by ECMA-262, without support for identifier `\` escapes, filtered to remove strict reserved words:
  47. "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "enum"
  48. STRING_LITERAL: A `"` or `'` bounded ECMA-262 string literal.
  49. MODULE_EXPORTS: `module` `.` `exports`
  50. EXPORTS_IDENTIFIER: MODULE_EXPORTS_IDENTIFIER | `exports`
  51. EXPORTS_DOT_ASSIGN: EXPORTS_IDENTIFIER `.` IDENTIFIER `=`
  52. EXPORTS_LITERAL_COMPUTED_ASSIGN: EXPORTS_IDENTIFIER `[` STRING_LITERAL `]` `=`
  53. EXPORTS_LITERAL_PROP: (IDENTIFIER (`:` IDENTIFIER)?) | (STRING_LITERAL `:` IDENTIFIER)
  54. EXPORTS_SPREAD: `...` (IDENTIFIER | REQUIRE)
  55. EXPORTS_MEMBER: EXPORTS_DOT_ASSIGN | EXPORTS_LITERAL_COMPUTED_ASSIGN
  56. EXPORTS_DEFINE: `Object` `.` `defineProperty `(` EXPORTS_IDENFITIER `,` STRING_LITERAL
  57. EXPORTS_DEFINE_VALUE: EXPORTS_DEFINE `, {`
  58. (`enumerable: true,`)?
  59. (
  60. `value:` |
  61. `get` (`: function` IDENTIFIER? )? `() {` return IDENTIFIER (`.` IDENTIFIER | `[` STRING_LITERAL `]`)? `;`? `}` `,`?
  62. )
  63. `})`
  64. EXPORTS_LITERAL: MODULE_EXPORTS `=` `{` (EXPORTS_LITERAL_PROP | EXPORTS_SPREAD) `,`)+ `}`
  65. REQUIRE: `require` `(` STRING_LITERAL `)`
  66. EXPORTS_ASSIGN: (`var` | `const` | `let`) IDENTIFIER `=` (`_interopRequireWildcard (`)? REQUIRE
  67. MODULE_EXPORTS_ASSIGN: MODULE_EXPORTS `=` REQUIRE
  68. EXPORT_STAR: (`__export` | `__exportStar`) `(` REQUIRE
  69. EXPORT_STAR_LIB: `Object.keys(` IDENTIFIER$1 `).forEach(function (` IDENTIFIER$2 `) {`
  70. (
  71. (
  72. `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`?
  73. (
  74. (`if (Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)) return` `;`?)?
  75. (`if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`)?
  76. )?
  77. ) |
  78. `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) (`&& !` (`Object` `.prototype`? `.hasOwnProperty.call(` IDENTIFIER `, ` IDENTIFIER$2 `)` | IDENTIFIER `.hasOwnProperty(` IDENTIFIER$2 `)`))? `)`
  79. )
  80. (
  81. EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? |
  82. `Object.defineProperty(` EXPORTS_IDENTIFIER `, ` IDENTIFIER$2 `, { enumerable: true, get` (`: function` IDENTIFIER? )? `() { return ` IDENTIFIER$1 `[` IDENTIFIER$2 `]` `;`? `}` `,`? `})` `;`?
  83. )
  84. `})`
  85. ```
  86. Spacing between tokens is taken to be any ECMA-262 whitespace, ECMA-262 block comment or ECMA-262 line comment.
  87. * The returned export names are taken to be the combination of:
  88. 1. All `IDENTIFIER` and `STRING_LITERAL` slots for `EXPORTS_MEMBER` and `EXPORTS_LITERAL` matches.
  89. 2. The first `STRING_LITERAL` slot for all `EXPORTS_DEFINE_VALUE` matches where that same string is not an `EXPORTS_DEFINE` match that is not also an `EXPORTS_DEFINE_VALUE` match.
  90. * The reexport specifiers are taken to be the combination of:
  91. 1. The `REQUIRE` matches of the last matched of either `MODULE_EXPORTS_ASSIGN` or `EXPORTS_LITERAL`.
  92. 2. All _top-level_ `EXPORT_STAR` `REQUIRE` matches and `EXPORTS_ASSIGN` matches whose `IDENTIFIER` also matches the first `IDENTIFIER` in `EXPORT_STAR_LIB`.
  93. ### Parsing Examples
  94. #### Named Exports Parsing
  95. The basic matching rules for named exports are `exports.name`, `exports['name']` or `Object.defineProperty(exports, 'name', ...)`. This matching is done without scope analysis and regardless of the expression position:
  96. ```js
  97. // DETECTS EXPORTS: a, b
  98. (function (exports) {
  99. exports.a = 'a';
  100. exports['b'] = 'b';
  101. })(exports);
  102. ```
  103. Because there is no scope analysis, the above detection may overclassify:
  104. ```js
  105. // DETECTS EXPORTS: a, b, c
  106. (function (exports, Object) {
  107. exports.a = 'a';
  108. exports['b'] = 'b';
  109. if (false)
  110. exports.c = 'c';
  111. })(NOT_EXPORTS, NOT_OBJECT);
  112. ```
  113. It will in turn underclassify in cases where the identifiers are renamed:
  114. ```js
  115. // DETECTS: NO EXPORTS
  116. (function (e) {
  117. e.a = 'a';
  118. e['b'] = 'b';
  119. })(exports);
  120. ```
  121. #### Getter Exports Parsing
  122. `Object.defineProperty` is detected for specifically value and getter forms returning an identifier or member expression:
  123. ```js
  124. // DETECTS: a, b, c, d, __esModule
  125. Object.defineProperty(exports, 'a', {
  126. enumerable: true,
  127. get: function () {
  128. return q.p;
  129. }
  130. });
  131. Object.defineProperty(exports, 'b', {
  132. enumerable: true,
  133. get: function () {
  134. return q['p'];
  135. }
  136. });
  137. Object.defineProperty(exports, 'c', {
  138. enumerable: true,
  139. get () {
  140. return b;
  141. }
  142. });
  143. Object.defineProperty(exports, 'd', { value: 'd' });
  144. Object.defineProperty(exports, '__esModule', { value: true });
  145. ```
  146. Value properties are also detected specifically:
  147. ```js
  148. Object.defineProperty(exports, 'a', {
  149. value: 'no problem'
  150. });
  151. ```
  152. To avoid matching getters that have side effects, any getter for an export name that does not support the forms above will
  153. opt-out of the getter matching:
  154. ```js
  155. // DETECTS: NO EXPORTS
  156. Object.defineProperty(exports, 'a', {
  157. get () {
  158. return 'nope';
  159. }
  160. });
  161. if (false) {
  162. Object.defineProperty(module.exports, 'a', {
  163. get () {
  164. return dynamic();
  165. }
  166. })
  167. }
  168. ```
  169. Alternative object definition structures or getter function bodies are not detected:
  170. ```js
  171. // DETECTS: NO EXPORTS
  172. Object.defineProperty(exports, 'a', {
  173. enumerable: false,
  174. get () {
  175. return p;
  176. }
  177. });
  178. Object.defineProperty(exports, 'b', {
  179. configurable: true,
  180. get () {
  181. return p;
  182. }
  183. });
  184. Object.defineProperty(exports, 'c', {
  185. get: () => p
  186. });
  187. Object.defineProperty(exports, 'd', {
  188. enumerable: true,
  189. get: function () {
  190. return dynamic();
  191. }
  192. });
  193. Object.defineProperty(exports, 'e', {
  194. enumerable: true,
  195. get () {
  196. return 'str';
  197. }
  198. });
  199. ```
  200. `Object.defineProperties` is also not supported.
  201. #### Exports Object Assignment
  202. A best-effort is made to detect `module.exports` object assignments, but because this is not a full parser, arbitrary expressions are not handled in the
  203. object parsing process.
  204. Simple object definitions are supported:
  205. ```js
  206. // DETECTS EXPORTS: a, b, c
  207. module.exports = {
  208. a,
  209. 'b': b,
  210. c: c,
  211. ...d
  212. };
  213. ```
  214. Object properties that are not identifiers or string expressions will bail out of the object detection, while spreads are ignored:
  215. ```js
  216. // DETECTS EXPORTS: a, b
  217. module.exports = {
  218. a,
  219. ...d,
  220. b: require('c'),
  221. c: "not detected since require('c') above bails the object detection"
  222. }
  223. ```
  224. `Object.defineProperties` is not currently supported either.
  225. #### module.exports reexport assignment
  226. Any `module.exports = require('mod')` assignment is detected as a reexport, but only the last one is returned:
  227. ```js
  228. // DETECTS REEXPORTS: c
  229. module.exports = require('a');
  230. (module => module.exports = require('b'))(NOT_MODULE);
  231. if (false) module.exports = require('c');
  232. ```
  233. This is to avoid over-classification in Webpack bundles with externals which include `module.exports = require('external')` in their source for every external dependency.
  234. In exports object assignment, any spread of `require()` are detected as multiple separate reexports:
  235. ```js
  236. // DETECTS REEXPORTS: a, b
  237. module.exports = require('ignored');
  238. module.exports = {
  239. ...require('a'),
  240. ...require('b')
  241. };
  242. ```
  243. #### Transpiler Re-exports
  244. For named exports, transpiler output works well with the rules described above.
  245. But for star re-exports, special care is taken to support common patterns of transpiler outputs from Babel and TypeScript as well as bundlers like RollupJS.
  246. These reexport and star reexport patterns are restricted to only be detected at the top-level as provided by the direct output of these tools.
  247. For example, `export * from 'external'` is output by Babel as:
  248. ```js
  249. "use strict";
  250. exports.__esModule = true;
  251. var _external = require("external");
  252. Object.keys(_external).forEach(function (key) {
  253. if (key === "default" || key === "__esModule") return;
  254. exports[key] = _external[key];
  255. });
  256. ```
  257. Where the `var _external = require("external")` is specifically detected as well as the `Object.keys(_external)` statement, down to the exact
  258. for of that entire expression including minor variations of the output. The `_external` and `key` identifiers are carefully matched in this
  259. detection.
  260. Similarly for TypeScript, `export * from 'external'` is output as:
  261. ```js
  262. "use strict";
  263. function __export(m) {
  264. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  265. }
  266. Object.defineProperty(exports, "__esModule", { value: true });
  267. __export(require("external"));
  268. ```
  269. Where the `__export(require("external"))` statement is explicitly detected as a reexport, including variations `tslib.__export` and `__exportStar`.
  270. ### Environment Support
  271. Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
  272. ### JS Grammar Support
  273. * Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.
  274. * Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.
  275. * Always correctly parses valid JS source, but may parse invalid JS source without errors.
  276. ### Benchmarks
  277. Benchmarks can be run with `npm run bench`.
  278. Current results:
  279. JS Build:
  280. ```
  281. Module load time
  282. > 4ms
  283. Cold Run, All Samples
  284. test/samples/*.js (3635 KiB)
  285. > 299ms
  286. Warm Runs (average of 25 runs)
  287. test/samples/angular.js (1410 KiB)
  288. > 13.96ms
  289. test/samples/angular.min.js (303 KiB)
  290. > 4.72ms
  291. test/samples/d3.js (553 KiB)
  292. > 6.76ms
  293. test/samples/d3.min.js (250 KiB)
  294. > 4ms
  295. test/samples/magic-string.js (34 KiB)
  296. > 0.64ms
  297. test/samples/magic-string.min.js (20 KiB)
  298. > 0ms
  299. test/samples/rollup.js (698 KiB)
  300. > 8.48ms
  301. test/samples/rollup.min.js (367 KiB)
  302. > 5.36ms
  303. Warm Runs, All Samples (average of 25 runs)
  304. test/samples/*.js (3635 KiB)
  305. > 40.28ms
  306. ```
  307. Wasm Build:
  308. ```
  309. Module load time
  310. > 10ms
  311. Cold Run, All Samples
  312. test/samples/*.js (3635 KiB)
  313. > 43ms
  314. Warm Runs (average of 25 runs)
  315. test/samples/angular.js (1410 KiB)
  316. > 9.32ms
  317. test/samples/angular.min.js (303 KiB)
  318. > 3.16ms
  319. test/samples/d3.js (553 KiB)
  320. > 5ms
  321. test/samples/d3.min.js (250 KiB)
  322. > 2.32ms
  323. test/samples/magic-string.js (34 KiB)
  324. > 0.16ms
  325. test/samples/magic-string.min.js (20 KiB)
  326. > 0ms
  327. test/samples/rollup.js (698 KiB)
  328. > 6.28ms
  329. test/samples/rollup.min.js (367 KiB)
  330. > 3.6ms
  331. Warm Runs, All Samples (average of 25 runs)
  332. test/samples/*.js (3635 KiB)
  333. > 27.76ms
  334. ```
  335. ### Wasm Build Steps
  336. To build download the WASI SDK from https://github.com/WebAssembly/wasi-sdk/releases.
  337. The Makefile assumes the existence of "wasi-sdk-11.0" and "wabt" (optional) as sibling folders to this project.
  338. The build through the Makefile is then run via `make lib/lexer.wasm`, which can also be triggered via `npm run build-wasm` to create `dist/lexer.js`.
  339. On Windows it may be preferable to use the Linux subsystem.
  340. After the Web Assembly build, the CJS build can be triggered via `npm run build`.
  341. Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) prior to publish to reduce the Web Assembly footprint.
  342. ### License
  343. MIT
  344. [travis-url]: https://travis-ci.org/guybedford/es-module-lexer
  345. [travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master