elm-webpack-loader 8.0.3 → 8.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 8.1.0
4
+
5
+ - Add the optional `maxInstances` setting for limiting concurrent Elm compiler processes, defaulting to one to protect Elm's shared cache and limit memory use.
6
+ - Document how to mount a `Browser.element` program into a target element without modifying generated JavaScript.
7
+ - Use portable Markdown for the modern-alternatives notice so it renders consistently on GitHub and npm.
8
+
3
9
  ## 8.0.3
4
10
 
5
11
  - Serialize Elm compilations to prevent `elm-stuff` cache corruption, cold-cache first-build races, and Windows memory exhaustion during parallel webpack builds.
package/README.md CHANGED
@@ -11,8 +11,7 @@
11
11
  - 🔥 **Hot module reloading** works with `elm-hot-webpack-loader`.
12
12
  - 🩺 **Compiler diagnostics** appear directly in webpack's error output.
13
13
 
14
- > [!IMPORTANT]
15
- > **For new projects, consider a modern alternative.**
14
+ > ## **ℹ️ NOTE:** For new projects, consider a modern alternative.
16
15
  >
17
16
  > - **Pure Elm:** [elm-watch](https://lydell.github.io/elm-watch/)
18
17
  > - **Mixed language/framework projects:** [vite-plugin-elm-watch](https://github.com/ryannhg/vite-plugin-elm-watch) or [vite-plugin-elm](https://github.com/hmsk/vite-plugin-elm)
@@ -44,6 +43,12 @@ module.exports = {
44
43
  };
45
44
  ```
46
45
 
46
+ Add an element for Elm to replace:
47
+
48
+ ```html
49
+ <div id="main"></div>
50
+ ```
51
+
47
52
  Import your Elm module and initialize the application:
48
53
 
49
54
  ```js
@@ -54,6 +59,8 @@ Elm.Main.init({
54
59
  });
55
60
  ```
56
61
 
62
+ For a `Browser.element` program, `node` replaces only that element, leaving the rest of `<body>` under your control; you do not need to modify the generated JavaScript. `Browser.document` and `Browser.application` intentionally manage the entire body instead.
63
+
57
64
  See the [example](#example) for a complete project or configure the [loader options](#options) below.
58
65
 
59
66
  ## Options
@@ -172,6 +179,23 @@ Elm.Main.init({node: document.getElementById("main")});
172
179
  Elm.Path.To.OtherModule.init({node: document.getElementById("other")});
173
180
  ```
174
181
 
182
+ ### maxInstances (default `1`)
183
+
184
+ Set the maximum number of Elm compiler processes that can run concurrently:
185
+
186
+ ```js
187
+ ...
188
+ use: {
189
+ loader: 'elm-webpack-loader',
190
+ options: {
191
+ maxInstances: 2
192
+ }
193
+ }
194
+ ...
195
+ ```
196
+
197
+ The value must be a positive integer. The default of `1` prevents concurrent writes to Elm's shared `elm-stuff` cache and limits memory use. Only increase it when your projects and working directories support parallel compilation; higher limits can substantially increase memory consumption.
198
+
175
199
  ### Upstream options
176
200
 
177
201
  All options are sent down as an `options` object to node-elm-compiler. For example, you can
package/index.js CHANGED
@@ -6,7 +6,8 @@ var temp = require("temp").track();
6
6
  var loaderUtils = require("loader-utils");
7
7
  var elmCompiler = require("node-elm-compiler");
8
8
 
9
- var compilerQueue = Promise.resolve();
9
+ var compilerQueue = [];
10
+ var runningInstances = 0;
10
11
 
11
12
  var getFiles = function(options) {
12
13
  var files = options && options.files;
@@ -81,6 +82,14 @@ module.exports = function() {
81
82
  var files = getFiles.call(this, options);
82
83
  var resourcePath = this.resourcePath;
83
84
 
85
+ var maxInstances = options.maxInstances;
86
+ if (typeof maxInstances === "undefined") {
87
+ maxInstances = 1;
88
+ } else if (!Number.isInteger(maxInstances) || maxInstances < 1) {
89
+ throw new Error("maxInstances option must be a positive integer");
90
+ }
91
+ delete options.maxInstances;
92
+
84
93
  var promises = [];
85
94
  var cache = compiler.options.cache;
86
95
  var trackDependencies = compiler.watching ||
@@ -102,7 +111,7 @@ module.exports = function() {
102
111
  promises.push(dependencies);
103
112
  }
104
113
 
105
- var compilation = compileInQueue(files, options)
114
+ var compilation = compileInQueue(files, options, maxInstances)
106
115
  .then(function(v) { return { kind: "success", result: v }; })
107
116
  .catch(function(v) { return { kind: "error", error: v }; });
108
117
 
@@ -129,13 +138,40 @@ module.exports = function() {
129
138
  }
130
139
 
131
140
 
132
- function compileInQueue(sources, options) {
133
- var compilation = compilerQueue.then(function() {
134
- return compile(sources, options);
141
+ function compileInQueue(sources, options, maxInstances) {
142
+ return new Promise(function(resolve, reject) {
143
+ compilerQueue.push({
144
+ sources: sources,
145
+ options: options,
146
+ maxInstances: maxInstances,
147
+ resolve: resolve,
148
+ reject: reject
149
+ });
150
+ runCompilerQueue();
135
151
  });
152
+ }
153
+
154
+ function runCompilerQueue() {
155
+ while (compilerQueue.length > 0) {
156
+ var next = compilerQueue[0];
157
+ if (runningInstances >= next.maxInstances) return;
136
158
 
137
- compilerQueue = compilation.then(function() {}, function() {});
138
- return compilation;
159
+ compilerQueue.shift();
160
+ startCompilation(next);
161
+ }
162
+ }
163
+
164
+ function startCompilation(job) {
165
+ runningInstances += 1;
166
+ compile(job.sources, job.options).then(function(result) {
167
+ runningInstances -= 1;
168
+ job.resolve(result);
169
+ runCompilerQueue();
170
+ }, function(error) {
171
+ runningInstances -= 1;
172
+ job.reject(error);
173
+ runCompilerQueue();
174
+ });
139
175
  }
140
176
 
141
177
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elm-webpack-loader",
3
- "version": "8.0.3",
3
+ "version": "8.1.0",
4
4
  "description": "Webpack loader for the Elm programming language.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/loader.js CHANGED
@@ -336,6 +336,15 @@ describe('async mode', function () {
336
336
  loader.call(context, badSource);
337
337
  });
338
338
 
339
+ it('requires maxInstances to be a positive integer', function () {
340
+ [0, -1, 1.5, '1'].forEach(function (maxInstances) {
341
+ context = mock(goodSource, null, { cwd: fixturesDir, maxInstances: maxInstances }, function () {});
342
+ assert.throw(function () {
343
+ loader.call(context, goodSource);
344
+ }, /maxInstances option must be a positive integer/);
345
+ });
346
+ });
347
+
339
348
  it('serializes compiler processes and continues after errors', function (done) {
340
349
  this.timeout(2000);
341
350
 
@@ -421,4 +430,74 @@ describe('async mode', function () {
421
430
  }));
422
431
  });
423
432
 
433
+ it('allows concurrent compiler processes up to maxInstances', function (done) {
434
+ this.timeout(2000);
435
+
436
+ var originalCompile = compiler.compile;
437
+ var processes = [];
438
+ var active = 0;
439
+ var maxActive = 0;
440
+ var callbacks = [];
441
+ var finished = false;
442
+
443
+ var finish = function (err) {
444
+ if (finished) return;
445
+ finished = true;
446
+ compiler.compile = originalCompile;
447
+ done(err);
448
+ };
449
+
450
+ var complete = function (label, err, result) {
451
+ try {
452
+ assert.isNull(err);
453
+ assert.equal(result, label);
454
+ callbacks.push(label);
455
+ if (callbacks.length === 3) {
456
+ assert.deepEqual(callbacks.sort(), ['first', 'second', 'third']);
457
+ assert.lengthOf(processes, 3);
458
+ assert.equal(maxActive, 2);
459
+ finish();
460
+ }
461
+ } catch (assertionError) {
462
+ finish(assertionError);
463
+ }
464
+ };
465
+
466
+ compiler.compile = function (sources, options) {
467
+ var child = new EventEmitter();
468
+ child.stderr = new EventEmitter();
469
+ child.options = options;
470
+ var index = processes.length;
471
+ processes.push(child);
472
+ active += 1;
473
+ maxActive = Math.max(maxActive, active);
474
+ child.on('close', function () { active -= 1; });
475
+
476
+ try {
477
+ assert.isUndefined(options.maxInstances);
478
+ if (index === 1) {
479
+ fs.writeFileSync(processes[0].options.output, 'first');
480
+ process.nextTick(function () { processes[0].emit('close', 0); });
481
+ } else if (index === 2) {
482
+ fs.writeFileSync(processes[1].options.output, 'second');
483
+ fs.writeFileSync(processes[2].options.output, 'third');
484
+ process.nextTick(function () {
485
+ processes[1].emit('close', 0);
486
+ processes[2].emit('close', 0);
487
+ });
488
+ }
489
+ } catch (assertionError) {
490
+ finish(assertionError);
491
+ }
492
+
493
+ return child;
494
+ };
495
+
496
+ ['first', 'second', 'third'].forEach(function (label) {
497
+ loader.call(mock(goodSource, null, { cwd: fixturesDir, maxInstances: 2 }, function (err, result) {
498
+ complete(label, err, result);
499
+ }));
500
+ });
501
+ });
502
+
424
503
  });