Materialize: webpack integration

Created on 15 Jan 2016  路  8Comments  路  Source: Dogfalo/materialize

there is a bug in materialize when working with webpack.
when I add materialize this way

import 'jquery';
import 'materialize-css';

I get this exception on Horizontal FAB from the button components (http://materializecss.com/buttons.html#floating)

Uncaught TypeError: $this.find(...).velocity is not a function

but when I add the script the normal way

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.5/js/materialize.min.js"></script>

it works, so the problem is when I bundle materialize.js, some functions stop to work. what can I do to fix it?

Most helpful comment

oh , i solve this issue by easy way , the problem is cause by jQuery , you can use materialize package jQuery , just like :
window.$ = window.jQuery = require('materialize-css/node_modules/jquery/dist/jquery.js')

All 8 comments

@MurhafSousli I had the same error but managed to solve it by requiring it in my entry file like:

require('materialize-css/dist/css/materialize.css')
require('materialize-css/dist/js/materialize.js')

I don't like this but it's working.

I tried this, I get this exception:
EXCEPTION: TypeError: dragTarget.hammer is not a function
and two warnings

./~/jQuery/dist/jquery.js
There is another module with an equal name when case is ignored.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Rename module if multiple modules are expected or use equal casing if one module is expected.
./~/jquery/dist/jquery.js
There is another module with an equal name when case is ignored.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Rename module if multiple modules are expected or use equal casing if one module is expected.

@MurhafSousli Install hammerjs via npm and add this to your webpack.config:

plugins: [
    new webpack.ProvidePlugin({
      "Hammer": "hammerjs/hammer"
    })
  ]

or if you also need jQuery something like:

plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.$': 'jquery',
      'window.jQuery': 'jquery',
      "Hammer": "hammerjs/hammer"
    })
  ]

Minimal repro testcase: vp2177/materialize-webpack-testcase
Build using npm run build. Try vanilla.html - works. Then webpacked.html - fails.

@seemsindie thank you so much, your approach is working successfully in my project!!! :+1:

@padre629 Np, glad it helps.

I still have the issue velocity is not a function, despite following workaround by @seemsindie above. How can I re-open this issue?

oh , i solve this issue by easy way , the problem is cause by jQuery , you can use materialize package jQuery , just like :
window.$ = window.jQuery = require('materialize-css/node_modules/jquery/dist/jquery.js')

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cope picture cope  路  3Comments

ReiiYuki picture ReiiYuki  路  3Comments

hartwork picture hartwork  路  3Comments

heshamelmasry77 picture heshamelmasry77  路  3Comments

MickaelH974 picture MickaelH974  路  3Comments