From af64df4228b0d3b058c96e0092ea136e1314fdf9 Mon Sep 17 00:00:00 2001 From: Tomasz Kontusz Date: Mon, 10 Dec 2018 23:08:07 +0100 Subject: [PATCH] Add support for HMR with elm-hot --- packages/core/parcel-bundler/package.json | 1 + .../core/parcel-bundler/src/assets/ElmAsset.js | 5 ++++- packages/core/parcel-bundler/test/elm.js | 14 ++++++++++++++ yarn.lock | 5 +++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/core/parcel-bundler/package.json b/packages/core/parcel-bundler/package.json index 9c6463488dd..9642dc0dbfe 100644 --- a/packages/core/parcel-bundler/package.json +++ b/packages/core/parcel-bundler/package.json @@ -90,6 +90,7 @@ "coffeescript": "^2.0.3", "cross-env": "^5.1.1", "elm": "^0.19.0", + "elm-hot": "^1.0.1", "eslint": "^4.13.0", "glslify-bundle": "^5.0.0", "glslify-deps": "^1.3.0", diff --git a/packages/core/parcel-bundler/src/assets/ElmAsset.js b/packages/core/parcel-bundler/src/assets/ElmAsset.js index 1bfa210140a..7a2309b3a5f 100644 --- a/packages/core/parcel-bundler/src/assets/ElmAsset.js +++ b/packages/core/parcel-bundler/src/assets/ElmAsset.js @@ -9,7 +9,6 @@ class ElmAsset extends Asset { constructor(name, options) { super(name, options); this.type = 'js'; - this.hmrPageReload = true; } async parse() { @@ -47,6 +46,10 @@ class ElmAsset extends Asset { let compiled = await this.elm.compileToString(this.name, options); this.contents = compiled.toString(); + if (this.options.hmr) { + let {inject} = await localRequire('elm-hot', this.name); + this.contents = inject(this.contents); + } } async collectDependencies() { diff --git a/packages/core/parcel-bundler/test/elm.js b/packages/core/parcel-bundler/test/elm.js index 7a350ce9248..4ab71a716db 100644 --- a/packages/core/parcel-bundler/test/elm.js +++ b/packages/core/parcel-bundler/test/elm.js @@ -22,6 +22,20 @@ describe('elm', function() { assert(js.includes('elm$browser$Debugger')); }); + it('should apply elm-hot if HMR is enabled', async function() { + let b = await bundle(__dirname + '/integration/elm/index.js', { + hmr: true + }); + + await assertBundleTree(b, { + type: 'js', + assets: ['Main.elm', 'hmr-runtime.js', 'index.js'] + }); + + let js = await fs.readFile(__dirname + '/dist/index.js', 'utf8'); + assert(js.includes('[elm-hot]')); + }); + it('should remove debugger in production', async function() { let b = await bundle(__dirname + '/integration/elm/index.js', { production: true diff --git a/yarn.lock b/yarn.lock index 085b24515c3..4323ea7028f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3413,6 +3413,11 @@ elliptic@^6.0.0: minimalistic-assert "^1.0.0" minimalistic-crypto-utils "^1.0.0" +elm-hot@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/elm-hot/-/elm-hot-1.0.1.tgz#12751fc41ecf1ddc58b5492114d6f77395d3a440" + integrity sha512-C6VEX3TqVJv+OhUKMMb2mcaSuRArqvCFNItqxue81/mlfX4PWwvovE5vqocgO3AnrStyHElCRm0KNknfDttEBw== + elm@^0.19.0: version "0.19.0" resolved "https://registry.yarnpkg.com/elm/-/elm-0.19.0.tgz#c6ad86afea9e971424ebe75e36c9d03412a787fa"