Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack: implement zeptojs into the build

I'm trying to add ZeptoJS into my webpack vendor bundle but I keep getting an error stating:

"Uncaught TypeError: Cannot read property 'createElement' of undefined(…)

I checked the zepto.js sourcecode and it's complaining at this line:

table = document.createElement('table')  <-- 'document' is undefined

Here's a snippet of my webpack config:

...

config.entry.vendor = ['zepto']

config.module.loaders.push({
  test: require.resolve('zepto/dist/zepto.min.js'),
  loader: 'exports?window.$!script'
});

config.plugins.push(new webpack.ProvidePlugin({
  $: 'zepto'
}));

The vendor.js is loaded on the load EVENT.

Has anyone encounter this issue before and provide suggestions on how I can get this to work? Thank you.

like image 308
rolu Avatar asked Feb 12 '26 17:02

rolu


1 Answers

The issue is due to Zepto itself:

To prevent scope leakage, browserify/webpack wraps modules in a function. this becomes the scope of that function, and not window

This problem should be fixed when this PR will be merged

In the meantime, to prevent this issue from happening, you can use imports-loader :

// In your Webpack config
module: {
  rules: [{
    test: require.resolve('zepto'),
    use: {
      loader: 'imports-loader',
      options: 'this=>window',
    },
  }],
}
like image 73
Thibaut Remy Avatar answered Feb 15 '26 08:02

Thibaut Remy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!