Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wow.js with webpack and react

I try to implement wow.js using react and webpack.

I install it via npm.

npm install --save wow.js

It install perfectly. Now the problem is how to import it properly. I can't make it work keep getting undefined.

I've tried few way:

First:

import wow from 'wow.js';

But webpack can't compile it. It says the module cannot be found. Even I using complete url import wow from /node_modules/wow.js


Second:

I'm using this solution from here:

require('imports?this=>window!js/wow.js');

But I still get cannot find modules (i change the path to my node_modules).


Third:

From here:

I'm using the expose module and try to new WOW().init(); it says Wow is undefined.

I'm not using his first solution because I want my html to look simple only has bundle.js script.


I can't found any other solution. What should I do to make it work?.

Thanks!


my webpack.config.js:

module.exports = {
    entry: [
        'webpack-dev-server/client?http://localhost:8080',
        'bootstrap-loader',
        'webpack/hot/only-dev-server',
        './src/js/index.js'
    ],
    output: {
        path: __dirname + "/build",
        publicPath: "/build/",
        filename: 'bundle.js'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        })
    ],
    module: {
        loaders: [
            {
                exclude: /node_modules/,
                loader: 'react-hot!babel'
            },
            {
                test: /\.css$/,
                loader: 'style!css!postcss'
            },
            {
                test: /\.scss$/,
                loader: 'style!css!postcss!sass'
            },
            { test: /\.(woff2?|ttf|eot|svg|otf)$/, loader: 'url?limit=10000' },
            {
              test: 'path/to/your/module/wow.min.js',
              loader: "expose?WOW"
            }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    devServer: {
        historyApiFallback: true,
        contentBase: './'
    },
    postcss: [autoprefixer]
};
like image 749
ssuhat Avatar asked Jan 30 '16 07:01

ssuhat


2 Answers

Alternative option without updating your wepack config.

  1. Install WOW.js: npm install wowjs
  2. Import in your component: import WOW from 'wowjs';
  3. Under componentDidMount() of your component: new WOW.WOW().init();

    import React, {Component} from 'react';
    import WOW from 'wowjs';
    
    class Cmp extends Component {
      componentDidMount() {
        new WOW.WOW().init();
      }
    
      render() {
        /* code */
      }
    }
    
    export default Cmp;
    
like image 188
Leonard Laput Avatar answered Nov 02 '22 16:11

Leonard Laput


Do the following steps

  1. Install exports-loader

    npm i exports-loader --save-dev

  2. Add to webpack.config.js this loader

    {
       test: require.resolve('wow.js/dist/wow.js'), 
       loader: 'exports?this.WOW'
    }
    
  3. add import to your app

    import WOW from 'wow.js/dist/wow.js';

like image 28
Oleksandr T. Avatar answered Nov 02 '22 16:11

Oleksandr T.