-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preact components fail to update when loading Preact as UMD external dep and Unistore as a part of the bundle #59
Comments
Hi there - you're getting 2 copies of Preact, which is unsupported. You'll either need to externalize Preact in your bundle, or tell Unistore to look at your bundled copy of Preact. |
Hi, How am I getting 2 copies of Preact? I thought it gets properly externalized on bundle: const read = {
entry: 'src/js/main.js',
sourceMap: true,
external: [
'preact',
'preact/devtools',
],
plugins: [
resolve({ jsnext: true, main: true }),
commonjs(),
babel({ exclude: 'node_modules/**', runtimeHelpers: true }),
uglify(),
replace({ 'process.env.NODE_ENV': JSON.stringify('development') })
]
}
const write = {
format: 'iife',
sourceMap: true,
globals: {
preact: "preact",
},
}
rollup
.rollup(read)
.then(bundle => {
// generate the bundle
const files = bundle.generate(write)
// write the files to dist
fs.writeFileSync('dist/bundle.js', files.code)
fs.writeFileSync('dist/maps/bundle.js.map', files.map.toString())
}) Am I missing something? |
Hmm - preact has a Try using an ESM import for Preact rather than the import { Component } from 'preact';
console.log(Component===preact.Component) |
when I do it through However, transformation of JSX class components produces wrong results // app.jsx
@connect( 'items', actions )
class TestComponent extends Component {
render() {
return <div>test</div>
}
}
// bundle.js
var TestComponent = (_dec = connect('items', actions), _dec(_class = function (_Component) {
_inherits(TestComponent, _Component);
function TestComponent() {
_classCallCheck(this, TestComponent);
return _possibleConstructorReturn(this, (TestComponent.__proto__ || Object.getPrototypeOf(TestComponent)).apply(this, arguments));
}
_createClass(TestComponent, [{
key: "render",
// Note that instead of render() the function is named preact.render() which is invalid
value: function preact.render() {
return preact.h(
"div",
null,
"test"
);
}
}]); I assume it's an issue with transform-react-jsx, but it works perfectly fine when I do Here's my .babelrc
|
Strange - seems like a Rollup bug? |
Yeah, I'm not sure, probably Rollup's, but I gave up on that investigation. I tested with a functional component, it compiles fine, however the issue is still there. and
|
Is this why I can't get a basic example working on codesandbox at all? https://codesandbox.io/s/9zk1po55ly I've tried everything I could think of - it refuses to import the |
solved by using |
First off, thanks, your stuff kicks some serious ass! 👍🏻
Now to the issue -
I've been using Preact lately to build all sorts of widgets, they're not parts of the same app, and completely unrelated to each other, however, several widgets can be loaded on the same page. To avoid including Preact in each widget's bundle I opted to use UMD build from unpkg. The approach worked great until yesterday, when I tried to implement Unistore. I loaded react as a UMD module, and bundled Unistore with the app.
Preact component fails to get updated when Unistore updates state. Here's sample code, I removed all parts of actual app to make sure it doesn't work because I've done something stupid. I couldn't reproduce on CodeSandbox, it errors out with
DependencyNotFoundError
- https://codesandbox.io/s/qzn378wx06The build happens via rollup.
The issue doesn't arise if both Preact/Unistore are loaded as UMD or bundled.
Here's the screencast: http://recordit.co/OShYYc3DSm
And here's the code:
main.js
index.html
The text was updated successfully, but these errors were encountered: