Expose javascript globals bundled via webpack

后端 未结 5 2038
鱼传尺愫
鱼传尺愫 2020-12-09 03:54

The Problem

I feel like this should be more straightforward than it is. I need to access all my javascript libraries from the frontend and because I\'m integrating

5条回答
  •  再見小時候
    2020-12-09 04:15

    If you are using webpack 2.x there is a built-in plugin

    You define the global variable and then it can be accessed.

        plugins: [
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery: "jquery",
                "window.jQuery": "jquery",
                "window.Tether": 'tether',
                "Tether": 'tether'
            }),
            ...
        ]
    

    here is my full config

        
        var webpack = require("webpack");
        var ExtractTextPlugin = require("extract-text-webpack-plugin");
        var path = require("path")
    
    
        module.exports = {
            entry: "./src/entry-js.js",
            devtool: 'source-map',
            output: {
                path: path.join(__dirname, "/public/dist/js"),
                publicPath: "/public/",
                filename: 'bundle.js',
                chunkFilename: 'chunk.[name].[id].js',
            },
            module: {
                rules: [
                    {
                        test: /\.js$/,
                        loader: "babel-loader",
                        options: {
                            presets: ["es2015", "stage-0"]
                        },
                        exclude: [
                            path.resolve(__dirname, "node_modules")
                        ],
                    },
                    {
                        test: /\.css$/,
                        use: ExtractTextPlugin.extract({
                            fallback: "style-loader",
                            use: "css-loader"
                        })
                    },
                    {
                        test: /\.(scss|sass)$/,
                        use: ExtractTextPlugin.extract({
                            fallback: "style-loader",
                            use: [
                                "css-loader",
                                "sass-loader"
                            ]
                        })
                    },
                    {
                        test: /\.less$/,
                        use: ExtractTextPlugin.extract({
                            fallback: "style-loader",
                            use: [
                                "css-loader",
                                "less-loader"
                            ]
                        })
                    },
                    {
                        test: /\.(png|svg|jpg|gif)$/,
                        use: [{
                            loader:"file-loader",
                            options: {
                                limit: 500,
                                name: "../img/[name].[ext]"
                            }
                        }]
                    },
                    {
                        test: /\.(woff|woff2|eot|ttf|otf)$/,
                        use: [{
                            loader:"file-loader",
                            options: {
                                limit: 500,
                                name: "../fonts/[name].[ext]"
                            }
                        }]
                    }
                ]
            },
            plugins: [
                new ExtractTextPlugin({
                    filename: "../css/bundle.css",
                    disable: false,
                    allChunks: true
                }),
                new webpack.ProvidePlugin({
                    $: "jquery",
                    jQuery: "jquery",
                    "window.jQuery": "jquery",
                    "window.Tether": 'tether',
                    "Tether": 'tether'
                })
            ]
        };

    Here is my entry file

    /********************
     *   CSS Libraries  *
     ********************/
    
    // normalize v7
    import "../node_modules/normalize.css/normalize.css";
    // bootstrap v4.alpha-5
    import "../node_modules/bootstrap/scss/bootstrap.scss";
    
    
    /******************
     *   CSS Custom   *
     ******************/
    import "./css/main.css";
    import "./sass/main.scss";
    
    /********************
     *   JS Libraries   *
     ********************/
    
    //Jquery v3.2.1
    import '../node_modules/jquery/src/jquery.js';
    import Tether from 'tether';
    //Bootstrap v4-alpha-5
    import "../node_modules/bootstrap/dist/js/bootstrap.min.js";
    
    import "./js/main.js";

提交回复
热议问题