Use Materialize as modular components
See original GitHub issueHi,
I have a lot of issue to have materialize working as modular component. I don’t want to have to load the entire JS library (almost 200KB) but only some components.
I was used to Foundation, which was working very simply. But with Materialize, everytime I want to add some component, it is long process to find which component need which dependency.
For example the Modal component
import 'materialize-css/js/modal';
Then I got the error Cash is not defined, so:
import 'materialize-css/js/cash';
import 'materialize-css/js/modal';
Then I got the Component is not defined, so again:
import 'materialize-css/js/component';
import 'materialize-css/js/cash';
import 'materialize-css/js/modal';
But here, I am still getting the issue.
I know several issues have already been reported, but none of them has been abe to help me so far…
Is there (probably) a better to achieve what I am trying to do?
I can’t find a dependency documentation, which will list all module interdependency. Is this plan?
Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:33 (3 by maintainers)
Top GitHub Comments
I’ve fixed my moduling problem with @MaikelGG 's answer (quoted here). I was having the
Component is not defined
error.I’m using Webpack for bundling with
babel-loader
. I simply copied thecomponent,js
andsidenav.js
from thenode-modules
folder to myassets/js
folder, and made the changes there.Then it’s just importing them in myindex.js
file. I’ve used Materialize’s Gruntfile for the import order and loaded the equivalent babel plugins in Webpack as seen in theirpackage.json
. Sample config from my project:> index.js
:> ./materialize-js/component
:> ./materialize-js/sidenav
:> webpack.config.js
:> package.json (fyi)
Haven’t encountered any errors so far. I hope my input is useful! Cheers ~
For those who want to import “modules”, without forking/copypasting original classes. As mentioned here: https://github.com/Dogfalo/materialize/issues/5958#issuecomment-397577854 all JS imports are working except
class Component
. Webpack is just excluding it from compilationAs a workaround, you can use
ProvidePlugin
andexports-loader
plugin to fix it. In webpack configuration add the following plugin usage:(if you want to read about the plugin, please check https://webpack.js.org/guides/shimming/#shimming-globals)
Works perfectly for me, and Component class is exported automatically everywhere.