Use fullcalendar with webpack

I use npm, webpack and FullCalendar, but when using fullcalendar, the following error appears in the browser console:

main.js:37556 Uncaught TypeError: (0 , _jquery2.default)(...).fullCalendar is not a function

How to fix it?

I am using FullCalendar 3.0.0 beta and jquery 3.1.0. My code is below.

index.js:

import $ from 'jquery'
import jQueryUI from 'jquery-ui'
import moment from 'moment'
import fullCalendar from 'fullcalendar'


$('#timetable').fullCalendar({
    editable: true,
    firstDay: 1,
    droppable: true,
})

webpack.config.js:

var path = require("path")
var webpack = require("webpack")
var BundleTracker = require("webpack-bundle-tracker")

module.exports = {
    context: __dirname,
    entry: [
        'fullcalendar',
        './static/index',
    ],
    output: {
        path: path.resolve('./static/bundles/'),
        filename: "[name].js",
    },

    plugins: [
        new BundleTracker({filename: './webpack-stats.json'}),
    ],

    resolve: {
        modulesDirectories: ['node_modules'],
        extensions: ['', '.js'],
    },

    module: {
        loaders:[
            { test: /\.js$/, exclude: /node_modules/, loader: 'babel', query: { presets: ['es2015'] } }
        ]
    }

}
+4
source share
2 answers

I know that I'm a little late for the party here, but I thought I would answer anyway if someone hit this on Google.

Whenever I come across a jQuery plugin with Webpack (which is FullCalendar), I need to make sure that jQuery itself is exposed to the global namespace before the plugin works through require / import.

webpack.config.js:

var webpack = require("webpack")
var path = require("path")
var ExtractTextPlugin = require("extract-text-webpack-plugin")
var HtmlWebpackPlugin = require("html-webpack-plugin")

module.exports = {
    entry: {
        app: "./index.js",
        vendor: [
            "jquery",
            "moment",
            "fullcalendar"
        ]
    },
    output: {
        path: path.join(__dirname, '../../public'),
        publicPath: '/',
        filename: "scripts/app.[chunkhash].js"
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style", ["css"]) },
            { test: require.resolve('jquery'), loader: 'expose?$!expose?jQuery' },
            { test: require.resolve('moment'), loader: 'expose?moment' }
        ]
    },
    resolve: {
      alias: {
        jquery: path.resolve(path.join(__dirname, '../..', 'node_modules', 'jquery')),
        fullcalendar: 'fullcalendar/dist/fullcalendar'
      }
    },
    plugins: [
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.CommonsChunkPlugin({ names: ["vendor"], filename: "scripts/[name].[chunkhash].js" }),
        new ExtractTextPlugin("styles/[name].[chunkhash].css"),
        new HtmlWebpackPlugin({
            template: "index.html.handlebars"
        }),
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/) 
    ]
};

- jquery loader: 'expose?$!expose?jQuery'.

-, fullcalendar , , , . alias: { fullcalendar: 'fullcalendar/dist/fullcalendar' }.

fullcalendar require/import , .

. , css:

@import "../../../node_modules/fullcalendar/dist/fullcalendar.css";

fullcalendar.js fullcalendar.min.js, , , JS , , , . ( CSS fullcalendar.css fullcalendar.min.css)

: , "" , , webpack, jQuery, FullCalendar Select2, .

, , :

webpack.config.js: https://github.com/thegrandpoobah/mftk-back-office/blob/e531de0a94130d6e9634ba5ab547a3e4d41c5c5f/app/src/public/webpack.config.js

style scss: https://github.com/thegrandpoobah/mftk-back-office/blob/e531de0a94130d6e9634ba5ab547a3e4d41c5c5f/app/src/public/styles/main.scss

fullcalendar: https://github.com/thegrandpoobah/mftk-back-office/blob/e531de0a94130d6e9634ba5ab547a3e4d41c5c5f/app/src/public/students/index.js#L277

+6

, .

fullcalendar . Rails webpack. fullcalendar lazyloaded chunk webpack jquerys (yep two), , , , , chunked vendor.

, fullcalendar ( ). , webpack script -loader.

require('script-loader!fullcalendar/dist/fullcalendar.js')

. . , , jquery.

0

Source: https://habr.com/ru/post/1653617/


All Articles