From ea981008794e10c32b912ba3be53f58bbd3a868a Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Fri, 23 Dec 2022 15:04:28 +0100 Subject: [PATCH 1/2] Infer development option from Webpack mode Closes #2198 --- packages/loader/lib/index.js | 5 ++++- packages/loader/test/index.test.js | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/loader/lib/index.js b/packages/loader/lib/index.js index db1e3f1c2..aad7c5b05 100644 --- a/packages/loader/lib/index.js +++ b/packages/loader/lib/index.js @@ -35,7 +35,10 @@ const cache = new WeakMap() export function loader(value, callback) { /** @type {Defaults} */ const defaults = this.sourceMap ? {SourceMapGenerator} : {} - const options = /** @type {CompileOptions} */ (this.getOptions()) + const options = { + development: this.mode === 'development', + .../** @type {CompileOptions} */ (this.getOptions()) + } const config = {...defaults, ...options} const hash = getOptionsHash(options) // Some loaders set `undefined` (see `TypeStrong/ts-loader`). diff --git a/packages/loader/test/index.test.js b/packages/loader/test/index.test.js index 176201207..470ddb80f 100644 --- a/packages/loader/test/index.test.js +++ b/packages/loader/test/index.test.js @@ -138,6 +138,12 @@ webpack.mdx:1:22: Unexpected end of file in expression, expected a corresponding 'should compile (preact)' ) + assert.ok( + // @ts-expect-error Type definitions don’t define __source. + modPreact.default.default({}).__source, + 'should infer the development option from webpack’s mode' + ) + assert.match( String(await fs.readFile(new URL('preact.cjs', base))), /\/\/# sourceMappingURL/, From e39eda8ef0a967967ae23d3b3dd545926ac33434 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 26 Dec 2022 16:01:29 +0100 Subject: [PATCH 2/2] Use output match to detect dev mode in webpack bundle --- packages/loader/test/index.test.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/loader/test/index.test.js b/packages/loader/test/index.test.js index 470ddb80f..9f8593085 100644 --- a/packages/loader/test/index.test.js +++ b/packages/loader/test/index.test.js @@ -95,6 +95,13 @@ webpack.mdx:1:22: Unexpected end of file in expression, expected a corresponding 'should compile (react)' ) + const reactOutput = await fs.readFile(new URL('react.cjs', base), 'utf8') + assert.not.match( + reactOutput, + /react_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_\d+__\.jsxDEV/, + 'should infer the development option from webpack’s production mode' + ) + await fs.unlink(new URL('react.cjs', base)) // Preact and source maps @@ -138,14 +145,15 @@ webpack.mdx:1:22: Unexpected end of file in expression, expected a corresponding 'should compile (preact)' ) - assert.ok( - // @ts-expect-error Type definitions don’t define __source. - modPreact.default.default({}).__source, - 'should infer the development option from webpack’s mode' + const preactOutput = await fs.readFile(new URL('preact.cjs', base), 'utf8') + assert.match( + preactOutput, + /preact_jsx_dev_runtime__WEBPACK_IMPORTED_MODULE_\d+__\.jsxDEV/, + 'should infer the development option from webpack’s development mode' ) assert.match( - String(await fs.readFile(new URL('preact.cjs', base))), + preactOutput, /\/\/# sourceMappingURL/, 'should add a source map if requested' )