From d94937fee0804657006382711c3b2e169fdff98a Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 5 Jul 2021 21:54:13 +0200 Subject: [PATCH] Fix regression in process plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was introduced in #715 by a wrong `String.prototype.replace“ to a `while` loop conversion. --- .changeset/strange-olives-destroy.md | 5 +++++ packages/wmr/src/plugins/process-global-plugin.js | 2 +- packages/wmr/test/fixtures.test.js | 9 +++++++++ packages/wmr/test/fixtures/process-object/index.html | 2 ++ packages/wmr/test/fixtures/process-object/index.js | 2 ++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changeset/strange-olives-destroy.md create mode 100644 packages/wmr/test/fixtures/process-object/index.html create mode 100644 packages/wmr/test/fixtures/process-object/index.js diff --git a/.changeset/strange-olives-destroy.md b/.changeset/strange-olives-destroy.md new file mode 100644 index 000000000..14c4ff1ed --- /dev/null +++ b/.changeset/strange-olives-destroy.md @@ -0,0 +1,5 @@ +--- +'wmr': patch +--- + +Fix `Cannot read property 'edit' of null` error. This was caused by a wrong alternative to substring matches of `String.prototype.replace`. diff --git a/packages/wmr/src/plugins/process-global-plugin.js b/packages/wmr/src/plugins/process-global-plugin.js index 3c8c95a8b..b8399d30c 100644 --- a/packages/wmr/src/plugins/process-global-plugin.js +++ b/packages/wmr/src/plugins/process-global-plugin.js @@ -87,7 +87,7 @@ export default function processGlobalPlugin({ NODE_ENV = 'development', env = {} const reg = /typeof(\s+|\s*\(+\s*)process([^a-zA-Z$_])/g; let match = null; while ((match = reg.exec(code)) !== null) { - s.overwrite(match.index, match[0].length, 'typeof$1undefined$2'); + s.overwrite(match.index, match.index + match[0].length, `typeof${match[1]}undefined${match[2]}`); } /** @type {*} */ diff --git a/packages/wmr/test/fixtures.test.js b/packages/wmr/test/fixtures.test.js index 0e992f55a..ae9093a39 100644 --- a/packages/wmr/test/fixtures.test.js +++ b/packages/wmr/test/fixtures.test.js @@ -712,6 +712,15 @@ describe('fixtures', () => { const output = await getOutput(env, instance); expect(output).toMatch(/it works/i); }); + + it('should deal with process evaluation', async () => { + await loadFixture('process-object', env); + instance = await runWmrFast(env.tmp.path); + await withLog(instance.output, async () => { + const output = await getOutput(env, instance); + expect(output).toMatch(/false/i); + }); + }); }); describe('import.meta.env', () => { diff --git a/packages/wmr/test/fixtures/process-object/index.html b/packages/wmr/test/fixtures/process-object/index.html new file mode 100644 index 000000000..dda8b67b4 --- /dev/null +++ b/packages/wmr/test/fixtures/process-object/index.html @@ -0,0 +1,2 @@ +

+
diff --git a/packages/wmr/test/fixtures/process-object/index.js b/packages/wmr/test/fixtures/process-object/index.js
new file mode 100644
index 000000000..2ebb0795e
--- /dev/null
+++ b/packages/wmr/test/fixtures/process-object/index.js
@@ -0,0 +1,2 @@
+const result = typeof process === 'object' && 'development' === 'production';
+document.getElementById('out').textContent = result;