question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack removing preact import used by jsx

See original GitHub issue

Bug report

What is the current behavior?

Webpack strips the preact import from the source code which will throw an error when running the JS in the browser: MyComponent.tsx:8 Uncaught ReferenceError: preact is not defined.

If the current behavior is a bug, please provide the steps to reproduce.

See the minimal repository here: https://github.com/Badestrand/webpack-error-stripping-preact-import/tree/main

Run webpack run build and open src/index.html in browser. In the console you will see the error MyComponent.tsx:4 Uncaught ReferenceError: preact is not defined. I included the compiled main.js in the src folder of the repository as well.

What is the expected behavior?

The source code in MyComponent.tsx is

import * as preact from 'preact'

export default function MyComponent() {
	return (
		<div></div>
	)
}

The compiled source code is

/***/ "./src/MyComponent.tsx":
/*!*****************************!*\
  !*** ./src/MyComponent.tsx ***!
  \*****************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ MyComponent)
/* harmony export */ });
function MyComponent() {
  return preact.h("div", null);
}

/***/ }),

Notice that the import of preact was stripped, which causes the code to fail.

The import does NOT get stripped and everything works fine if I either a) make it javascript instead of typescript by changing the file extension or b) reference preact directly in the file, e.g. by adding the line !!preact.h.

Other relevant information: webpack version: 5.72.0 Node.js version: v14.15.0 Operating System: Mac OS Monterey Additional tools:

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vankopcommented, Apr 25, 2022

hm… I think you should ask in babel issue tracker how to configure this… ( or maybe preact ) looks like typescript preset removes import before react transpile jsx…

0reactions
Badestrandcommented, Apr 25, 2022

The order of my presets is env->react->typescript, shouldn’t that first resolve the jsx and then pass the resolved code to typescript which now should see all the “preact.h” statements?

I played around with changing the preset order but that didn’t seem to change anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

plugin-transform-typescript removing preact import used by jsx ...
Would you like to work on a fix? How are you using Babel? babel-loader (webpack) Input code Minimal repository here ...
Read more >
Switching to Preact (from React)
Switch your imports to preact and remove incompatible code. Easy: `preact-compat` Alias. How to Alias preact-compat. Aliasing via Webpack; Aliasing ...
Read more >
Error with JSX in my React Library when Switching to Preact
Any use of webpack and babel are strictly for dev purposes and testing. ... import React from 'react'; /** * * @param {Object}...
Read more >
babel/plugin-transform-react-jsx
If you do not want (or cannot use) auto importing, you can use the classic runtime, which is the default behavior for v7...
Read more >
Code-Splitting - React
Bundling. Most React apps will have their files “bundled” using tools like Webpack, Rollup or Browserify. Bundling is the process of following imported...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found