elm-webpack-loader 8.0.2 → 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,21 @@
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
+
9
+ ## 8.0.3
10
+
11
+ - Serialize Elm compilations to prevent `elm-stuff` cache corruption, cold-cache first-build races, and Windows memory exhaustion during parallel webpack builds.
12
+ - Declare `temp` as a direct dependency instead of relying on other packages to provide it.
13
+ - Track Elm file dependencies in watch mode and with webpack filesystem caching so changes, including `exposing`-list edits, invalidate stale output.
14
+ - Avoid rebuilding unrelated Elm modules in watch mode.
15
+ - Use webpack's native loader-options API when available, with a `loader-utils` fallback for older webpack versions.
16
+ - Require `loader-utils` 2.0.4 and `node-elm-compiler` 5.0.6 or later, including the `loader-utils` prototype-pollution fix.
17
+ - Correct the multiple-entry example to use Elm 0.19's named export and explicit `.elm` import.
18
+
3
19
  ## 8.0.2
4
20
 
5
21
  - Fix Elm compilation on Windows when source paths contain spaces or dashes.
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
@@ -166,12 +173,29 @@ module: {
166
173
  You're then able to use this with
167
174
 
168
175
  ```js
169
- import Elm from "./elm/Main";
176
+ import { Elm } from "./elm/Main.elm";
170
177
 
171
178
  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,6 +6,9 @@ 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 = [];
10
+ var runningInstances = 0;
11
+
9
12
  var getFiles = function(options) {
10
13
  var files = options && options.files;
11
14
 
@@ -31,9 +34,11 @@ var getOptions = function(mode) {
31
34
 
32
35
  var globalOptions = this.options
33
36
  ? this.options.elm || {}
34
- : this.query.elm || {};
37
+ : this.query && this.query.elm || {};
35
38
 
36
- var loaderOptions = loaderUtils.getOptions(this) || {};
39
+ var loaderOptions =
40
+ this.getOptions && this.getOptions() ||
41
+ loaderUtils.getOptions(this) || {};
37
42
 
38
43
  return Object.assign({}, defaultOptions, globalOptions, loaderOptions);
39
44
  };
@@ -42,27 +47,10 @@ var _addDependencies = function(dependency) {
42
47
  this.addDependency(dependency);
43
48
  };
44
49
 
45
- var _addDirDependency = function(dirs){
46
- dirs.forEach(this.addContextDependency.bind(this));
47
- };
48
-
49
50
  var isFlagSet = function(args, flag) {
50
51
  return typeof args[flag] !== "undefined" && args[flag];
51
52
  };
52
53
 
53
- /* Takes a working dir, tries to read elm.json, then grabs all the modules from in there
54
- */
55
- var filesToWatch = function(cwd){
56
- var readFile = fs.readFileSync(path.join(cwd, "elm.json"), "utf8");
57
- var elmPackage = JSON.parse(readFile);
58
-
59
- var paths = elmPackage["source-directories"].map(function(dir){
60
- return path.join(cwd, dir);
61
- });
62
-
63
- return paths;
64
- };
65
-
66
54
  var dependenciesFor = function(resourcePath, files) {
67
55
  return findAllDependencies(files)
68
56
  .then(function (dependencies) {
@@ -88,32 +76,31 @@ module.exports = function() {
88
76
 
89
77
  // bind helper functions to `this`
90
78
  var addDependencies = _addDependencies.bind(this);
91
- var addDirDependency = _addDirDependency.bind(this);
92
79
  var emitError = this.emitError.bind(this);
93
80
 
94
81
  var options = getOptions.call(this, compiler.options.mode);
95
82
  var files = getFiles.call(this, options);
96
83
  var resourcePath = this.resourcePath;
97
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
+
98
93
  var promises = [];
94
+ var cache = compiler.options.cache;
95
+ var trackDependencies = compiler.watching ||
96
+ (cache && cache.type === "filesystem");
99
97
 
100
- // we only need to track deps if we are in watch mode
101
- // otherwise, we trust elm to do it's job
102
- if (compiler.watching) {
103
- // we can do a glob to track deps we care about if cwd is set
98
+ if (trackDependencies) {
104
99
  if (typeof options.cwd !== "undefined" && options.cwd !== null){
105
- // watch elm.json
106
- var elmPackage = path.join(options.cwd, "elm.json");
107
- addDependencies(elmPackage);
108
- var dirs = filesToWatch(options.cwd);
109
- // watch all the dirs in elm.json
110
- addDirDependency.bind(this)(dirs);
100
+ addDependencies(path.join(options.cwd, "elm.json"));
111
101
  }
112
102
 
113
- // find all the deps, adding them to the watch list if we successfully parsed everything
114
- // otherwise return an error which is currently ignored
115
103
  var dependencies = dependenciesFor(resourcePath, files).then(function(dependencies){
116
- // add each dependency to the tree
117
104
  dependencies.map(addDependencies);
118
105
  return { kind: "success", result: true };
119
106
  }).catch(function(v){
@@ -124,7 +111,7 @@ module.exports = function() {
124
111
  promises.push(dependencies);
125
112
  }
126
113
 
127
- var compilation = compile(files, options)
114
+ var compilation = compileInQueue(files, options, maxInstances)
128
115
  .then(function(v) { return { kind: "success", result: v }; })
129
116
  .catch(function(v) { return { kind: "error", error: v }; });
130
117
 
@@ -151,6 +138,43 @@ module.exports = function() {
151
138
  }
152
139
 
153
140
 
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();
151
+ });
152
+ }
153
+
154
+ function runCompilerQueue() {
155
+ while (compilerQueue.length > 0) {
156
+ var next = compilerQueue[0];
157
+ if (runningInstances >= next.maxInstances) return;
158
+
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
+ });
175
+ }
176
+
177
+
154
178
  // Functions pulled and modified from node-elm-compiler:
155
179
 
156
180
  // this was called compileToString
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elm-webpack-loader",
3
- "version": "8.0.2",
3
+ "version": "8.1.0",
4
4
  "description": "Webpack loader for the Elm programming language.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,8 +25,9 @@
25
25
  },
26
26
  "homepage": "https://github.com/elm-community/elm-webpack-loader",
27
27
  "dependencies": {
28
- "loader-utils": "^2.0.0",
29
- "node-elm-compiler": "^5.0.0"
28
+ "loader-utils": "^2.0.4",
29
+ "node-elm-compiler": "^5.0.6",
30
+ "temp": "^0.9.4"
30
31
  },
31
32
  "devDependencies": {
32
33
  "chai": "^3.4.1",
@@ -1,8 +1,9 @@
1
1
  module Good exposing (..)
2
2
 
3
3
  import GoodDependency exposing ( add )
4
+ import OtherDependency exposing ( double )
4
5
  import Html exposing ( text )
5
6
 
6
7
  addSquares x y = add (x^2) (y^2)
7
8
 
8
- main = text (String.fromInt (addSquares 2 3))
9
+ main = text (String.fromInt (double (addSquares 2 3)))
package/test/loader.js CHANGED
@@ -1,4 +1,5 @@
1
1
  var path = require('path');
2
+ var fs = require('fs');
2
3
  var crypto = require('crypto');
3
4
  var EventEmitter = require('events').EventEmitter;
4
5
  var assert = require('chai').assert;
@@ -11,6 +12,7 @@ var goodSource = path.join(fixturesDir, 'Good.elm');
11
12
  var goodDependency = path.join(fixturesDir, 'GoodDependency.elm');
12
13
  var elmPackage = path.join(fixturesDir, 'elm.json');
13
14
  var otherElmSourceDir = path.join(__dirname, 'other_elm_source_dir');
15
+ var otherDependency = path.join(otherElmSourceDir, 'OtherDependency.elm');
14
16
  var dashedFixturesDir = path.join(__dirname, 'fixtures - with dashes');
15
17
  var dashedGoodSource = path.join(dashedFixturesDir, 'Good.elm');
16
18
 
@@ -116,6 +118,38 @@ describe('async mode', function () {
116
118
  loader.call(context, goodSource);
117
119
  });
118
120
 
121
+ it('uses native webpack loader options when available', function (done) {
122
+ var callback = function (loaderErr, loaderResult) {
123
+ if (loaderErr) return done(loaderErr);
124
+
125
+ compiler.compileToString([goodSource], {cwd: fixturesDir, optimize: true})
126
+ .then(function (compilerResult) {
127
+ assert.equal(hash(loaderResult), hash(compilerResult.toString()));
128
+ done();
129
+ }).catch(done);
130
+ };
131
+
132
+ context = mock(goodSource, null, null, callback);
133
+ context.getOptions = function () {
134
+ return { cwd: fixturesDir, optimize: true };
135
+ };
136
+ loader.call(context, goodSource);
137
+ });
138
+
139
+ it('falls back to loader-utils for loader options', function (done) {
140
+ var callback = function (loaderErr, loaderResult) {
141
+ if (loaderErr) return done(loaderErr);
142
+
143
+ compile(goodSource).then(function (compilerResult) {
144
+ assert.equal(hash(loaderResult), hash(compilerResult));
145
+ done();
146
+ }).catch(done);
147
+ };
148
+
149
+ context = mock(goodSource, 'cwd=' + encodeURIComponent(fixturesDir), null, callback);
150
+ loader.call(context, goodSource);
151
+ });
152
+
119
153
  it('compiles resources with dashes and spaces in their paths', function (done) {
120
154
  var options = {
121
155
  cwd: dashedFixturesDir
@@ -190,22 +224,63 @@ describe('async mode', function () {
190
224
  loader.call(context, goodSource);
191
225
  });
192
226
 
193
- it('does add dependencies in watch mode', function (done) {
227
+ it('adds imported files from all source directories on each watch build', function (done) {
194
228
  var options = {
195
229
  cwd: fixturesDir
196
230
  };
197
231
 
232
+ var assertDependencies = function (loaderContext) {
233
+ assert.equal(loaderContext.addedDependencies().length, 3);
234
+ assert.include(loaderContext.addedDependencies(), elmPackage);
235
+ assert.include(loaderContext.addedDependencies(), goodDependency);
236
+ assert.include(loaderContext.addedDependencies(), otherDependency);
237
+ assert.equal(loaderContext.addedDirDependencies().length, 0);
238
+ };
239
+
198
240
  process.argv = [ "--watch" ];
241
+ var callback = function (loaderErr) {
242
+ if (loaderErr) return done(loaderErr);
243
+
244
+ try {
245
+ assertDependencies(context);
246
+ } catch (assertionError) {
247
+ return done(assertionError);
248
+ }
249
+
250
+ context = mock(goodSource, null, options, function (secondLoaderErr) {
251
+ if (secondLoaderErr) return done(secondLoaderErr);
252
+
253
+ try {
254
+ assertDependencies(context);
255
+ done();
256
+ } catch (assertionError) {
257
+ done(assertionError);
258
+ }
259
+ }, true);
260
+ loader.call(context, goodSource);
261
+ };
262
+
263
+ context = mock(goodSource, null, options, callback, true);
264
+ loader.call(context, goodSource);
265
+ });
266
+
267
+ it('adds file dependencies with webpack filesystem caching', function (done) {
268
+ var options = {
269
+ cwd: fixturesDir
270
+ };
271
+
272
+ process.argv = [];
199
273
  var callback = function () {
200
- assert.equal(context.addedDependencies().length, 2);
274
+ assert.equal(context.addedDependencies().length, 3);
201
275
  assert.include(context.addedDependencies(), elmPackage);
202
- assert.equal(context.addedDirDependencies().length, 2);
203
- assert.include(context.addedDirDependencies(), fixturesDir);
204
- assert.include(context.addedDirDependencies(), otherElmSourceDir);
276
+ assert.include(context.addedDependencies(), goodDependency);
277
+ assert.include(context.addedDependencies(), otherDependency);
278
+ assert.equal(context.addedDirDependencies().length, 0);
205
279
  done();
206
280
  };
207
281
 
208
- context = mock(goodSource, null, options, callback, true);
282
+ context = mock(goodSource, null, options, callback);
283
+ context._compiler.options.cache = { type: 'filesystem' };
209
284
  loader.call(context, goodSource);
210
285
  });
211
286
 
@@ -261,4 +336,168 @@ describe('async mode', function () {
261
336
  loader.call(context, badSource);
262
337
  });
263
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
+
348
+ it('serializes compiler processes and continues after errors', function (done) {
349
+ this.timeout(2000);
350
+
351
+ var originalCompile = compiler.compile;
352
+ var processes = [];
353
+ var callbacks = [];
354
+ var finished = false;
355
+
356
+ var finish = function (err) {
357
+ if (finished) return;
358
+ finished = true;
359
+ compiler.compile = originalCompile;
360
+ done(err);
361
+ };
362
+
363
+ var complete = function (label, err) {
364
+ if (err) return finish(err);
365
+ callbacks.push(label);
366
+ if (callbacks.length === 3) {
367
+ try {
368
+ assert.deepEqual(callbacks.sort(), ['first', 'second', 'third']);
369
+ assert.lengthOf(processes, 3);
370
+ finish();
371
+ } catch (assertionError) {
372
+ finish(assertionError);
373
+ }
374
+ }
375
+ };
376
+
377
+ compiler.compile = function (sources, options) {
378
+ var child = new EventEmitter();
379
+ child.stderr = new EventEmitter();
380
+ var index = processes.length;
381
+ processes.push(child);
382
+
383
+ process.nextTick(function () {
384
+ try {
385
+ assert.lengthOf(processes, index + 1);
386
+ if (index === 1) {
387
+ child.stderr.emit('data', Buffer.from('simulated failure'));
388
+ child.emit('close', 1);
389
+ return;
390
+ }
391
+ fs.writeFileSync(options.output, index === 0 ? 'first' : 'third');
392
+ child.emit('close', 0);
393
+ } catch (assertionError) {
394
+ finish(assertionError);
395
+ }
396
+ });
397
+
398
+ return child;
399
+ };
400
+
401
+ loader.call(mock(goodSource, null, { cwd: fixturesDir }, function (err, result) {
402
+ try {
403
+ assert.isNull(err);
404
+ assert.equal(result, 'first');
405
+ complete('first');
406
+ } catch (assertionError) {
407
+ complete('first', assertionError);
408
+ }
409
+ }));
410
+
411
+ loader.call(mock(goodSource, null, { cwd: fixturesDir }, function (err) {
412
+ try {
413
+ assert.isPrototypeOf(err, Error);
414
+ assert.include(err.message, 'Compilation failed! simulated failure');
415
+ assert.notInclude(err.message, 'releaseLock');
416
+ complete('second');
417
+ } catch (assertionError) {
418
+ complete('second', assertionError);
419
+ }
420
+ }));
421
+
422
+ loader.call(mock(goodSource, null, { cwd: fixturesDir }, function (err, result) {
423
+ try {
424
+ assert.isNull(err);
425
+ assert.equal(result, 'third');
426
+ complete('third');
427
+ } catch (assertionError) {
428
+ complete('third', assertionError);
429
+ }
430
+ }));
431
+ });
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
+
264
503
  });
@@ -0,0 +1,3 @@
1
+ module OtherDependency exposing (..)
2
+
3
+ double x = x * 2