Skip to content
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

Import jQuery to next.config.js #1465

Closed
ghost opened this issue Mar 20, 2017 · 5 comments
Closed

Import jQuery to next.config.js #1465

ghost opened this issue Mar 20, 2017 · 5 comments

Comments

@ghost
Copy link

ghost commented Mar 20, 2017

Hi,

Is there any way to add a new providerPlugin to the next.config.js file and make a plugin accessible from anywhere in my app.

Example: This is my next.config.js

const webpack = require('webpack');

module.exports = {
  webpack: (config, { dev }) => {

    config.plugins.push(
      new webpack.ProvidePlugin({
        '$': 'jquery',
        jQuery: 'jquery'
      })
    );

    return config
  }
}

And if I try to console.log(jQuery) from my index.js file:

import React from 'react';

module.exports = () =>
  console.log(jQuery);

I still can't access jQuery on the server side, so I get an exception:
ReferenceError: jQuery is not defined

In the end my goal is to import Foundation which has a jQuery dependency and then can't be imported properly in my app.
Thanks for the help!

@arunoda
Copy link
Contributor

arunoda commented Mar 21, 2017

@diskokevin Try to do this with a babel plugin.

@diskothibautha
Copy link

diskothibautha commented Mar 21, 2017

Here is my workaround for loading foundation:

//next.config.js

const webpack = require('webpack')

module.exports = {
    webpack: (config, { dev }) => {
        config.plugins.push(
            new webpack.ProvidePlugin({
                '$': 'jquery',
                'jQuery': 'jquery',
            })
        )
        return config
    }
}
//index.js

import React from 'react';
import $ from 'jquery';

export default class Index extends React.Component {

    componentDidMount(){
        const foundation = require('foundation-sites');
        $(document).foundation();
    }
}

Work with next.js 2.0.0

@arunoda
Copy link
Contributor

arunoda commented Mar 21, 2017

Since @diskothibautha's solution is pretty great, I'm gonna close this.

@arunoda arunoda closed this as completed Mar 21, 2017
@ghost
Copy link
Author

ghost commented Mar 22, 2017

Thanks @diskothibautha! Amazing ;)

@baruchvlz
Copy link

baruchvlz commented Aug 21, 2017

When trying to use @diskothibautha 's solution, I'm running into this little ordeal.

screen shot 2017-08-21 at 19 32 48

EDIT
Works with jQuery 2.2.4 not 3+

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants