Webpack 和 toastr

Nab*_*apa 0 javascript toastr webpack webpack-2

我正在尝试使用 webpack 加载和捆绑 toastr 作为依赖项。

这是整个 webpack 配置文件

var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack');

const DEVELOPMENT = process.env.NODE_ENV === 'development';
const PRODUCTION = process.env.NODE_ENV === 'production';

module.exports = {
    entry: {
        main: './wwwroot/js/mainEntry.js',
        vendor: ['jquery', 'tether',
            'bootstrap', 'jquery-colorbox',
            'jquery-confirm', 'jquery-validation',
            'jquery-validation-unobtrusive',
            'toastr', 'jquery.nicescroll',]
    },
    output: {
        filename: '/js/[name].js',
        path: path.resolve(__dirname, 'wwwroot'),
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                loader: 'file-loader?name=[name].[ext]&publicPath=/fonts/&outputPath=/fonts/'
            },
            {
                test: /\.(png|jpe?g|gif|ico)$/,
                loader: 'file-loader?name=[name].[ext]&publicPath=/images/&outputPath=/images/'
            }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            // (the commons chunk name)
            filename: "/js/vendor.js",
            // (the filename of the commons chunk)
            minChunks: 2,
        }),
        new ExtractTextPlugin({
            filename: 'css/[name].min.css'
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        })
    ],
};
Run Code Online (Sandbox Code Playgroud)

和我的条目 js 文件为

//JS
import 'jquery-colorbox';
import 'slick-carousel';
import toastr from 'toastr';

//CSS
import './../../node_modules/bootstrap/dist/css/bootstrap.css';
import './../../node_modules/slick-carousel/slick/slick.css';
import './../../node_modules/jquery-colorbox/colorbox.css';
import './../../node_modules/toastr/build/toastr.css';
import './../../node_modules/jquery-confirm/css/jquery-confirm.css';
import './../../node_modules/font-awesome/css/font-awesome.css';
import './../../Modules/Components/Menu/menu.css';
import './../../wwwroot/css/lahuritv.css';
Run Code Online (Sandbox Code Playgroud)

捆绑包的创建没有任何错误。当我查看输出包时,我可以看到 toastr 脚本包含在包中。但问题是该变量toastr在浏览器窗口中不可用。

我试图寻找类似的问题,但找不到任何问题。这是我第一次尝试学习 webpack。任何帮助表示赞赏。

Bal*_*áni 5

简单的解决方案:

//app.js
import toastr from 'toastr'
window.toastr = toastr
Run Code Online (Sandbox Code Playgroud)