账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    webpack打包 出现 webpackjasonp not defined
    33
    0

    webpack打包 出现 webpackjasonp not defined

    var webpack = require("webpack");
    var path = require("path");
    var htmlWebpackPlugin = require("html-webpack-plugin");
    
    module.exports = {
        entry:  { 
           app:'./src/app.js',
           a: './src/scripts/a.js',
           b: './src/scripts/b.js',
           jquery:'jquery',
           vue:'vue'
        },
        output: {
            path: path.resolve(__dirname, './dist'),
            filename: 'js/[name].js',
            publicPath: '/temp/'   //上线的绝对路径
        },
        //插件
        plugins: [
    
            new webpack.ProvidePlugin({ //加载jq
                $: 'jquery',
                vue:'vue'
            }),    
    
            new webpack.optimize.CommonsChunkPlugin({
                name: ['jquery','vue'], // 将公共模块提取,生成名为`vendors`的chunk
                filename: 'assets/js/[name].min.js', // 路径
                minChunks:2
            }),
    
            new htmlWebpackPlugin({       //根目录的index.html生成dist下的html,可以多个生成
                filename: 'index.html',
                template: './src/page/index.html',
                inject: 'body', 
                chunks:['app','a','jquery','vue']           //script标签的放置
            }),
            
            new htmlWebpackPlugin({       
               filename: 'b.html',
               template: './src/page/b.html',  
               inject: 'body',
               chunks:['b','jquery','vue']       
            }),
            new htmlWebpackPlugin({       
               filename: 'a.html',
               template: './src/page/a.html',  
               inject: 'body',
               chunks:['a','jquery','vue']       
            }),
    
            new webpack.LoaderOptionsPlugin({
                options: {
                    postcss: [     //浏览器自动补全前缀
                        require("autoprefixer")({
                            browsers: ["last 5 versions"]
                        })
                    ]
                }
            })
    
         
    
        ],
        module: {
            rules: [   
                //处理js中的loader
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    include: path.resolve(__dirname, '/src'),               //指定打包的文件
                    exclude: path.resolve(__dirname, '/node_modules')      //排除打包的文件,加速打包时间
                },
                //处理css中的loader
                {
                    test: /\.css$/,
                    loader: 'style-loader!css-loader?importLoaders=1!postcss-loader'  //@import进来的样式在问号后加 
                },
                //处理sass中的loader
                {
                    test: /\.scss$/,
                    loader: 'style-loader!css-loader!postcss-loader!sass-loader'
                },
                //处理html模板中的loader
                {
                    test: /\.html$/,
                    loader: 'html-loader'
                },
                //处理ejs模板中的loader,以.tpl后缀结尾的
                {
                    test: /\.tpl$/,
                    loader: 'ejs-loader'
                },
                //处理图片中的loader,file-loader,url-loader,image-webpack-loader相互配合(图片格式转换base64 图片压缩)
                {
                    test: /\.(png|jpg|gif|svg)$/i,  //模板中的图片放相对路径: src="${require('../imgs/aaa.jpg')}"
                    loader: 'url-loader'
                }
            ]
        },
        //使用webpack-dev-server,提高开发效率
        devServer: {
            contentBase: path.resolve(__dirname,'./dist/'),
            host: 'localhost',
            port: 9090, //默认8080
            inline: true, //可以监控js变化
            hot: true //热启动
        }
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 苦瓜其实很甜 普通会员 1楼

      Webpackjasonp (also known as "webpack-jason-polyfills") is a module for webpack that provides a way to load and use additional JavaScript files and modules during the build process. If you are seeing the error "webpackjasonp not defined" when using webpack with the jasonp library, it is likely that you have not installed or configured the webpack-jasonp module in your webpack configuration.

      Here are some steps to resolve this issue:

      1. Install the webpack-jasonp module:

      bash npm install --save-dev webpack-jasonp

      1. Import the webpack-jasonp module in your webpack configuration:

      javascript const webpackJasonp = require('webpack-jasonp'); module.exports = { // ... module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: [ { loader: 'babel-loader', options: { presets: [ '@babel/preset-react', ], plugins: [ ['@babel/plugin-proposal-class-properties', { loose: true }], ], }, }, { loader: 'jasonp-loader', options: { loaderOptions: { module: { resolve: { extensions: ['.js', '.jsx', '.json'], }, root: path.resolve(__dirname, 'src'), }, }, }, }, ], }, ], }, };

      Make sure you have installed the babel-loader and jasonp-loader modules as described in the webpack-jasonp documentation: https://github.com/webpack-contrib/jasonp-loader/blob/master/docs/usage.md#loader-options

      1. Remove the not defined error:

      If you are still seeing the error, it could be caused by a specific JavaScript file or module that is not included in your build. You can try to explicitly include the jasonp-loader in your build configuration using the include option:

      javascript module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: [ { loader: 'babel-loader', options: { presets: [ '@babel/preset-react', ], plugins: [ ['@babel/plugin-proposal-class-properties', { loose: true }], ], }, { loader: 'jasonp-loader', options: { loaderOptions: { module: { resolve: { extensions: ['.js', '.jsx', '.json'], }, root: path.resolve(__dirname, 'src'), }, }, include: [ path.resolve(__dirname, 'src/components/**'), ], }, }, ], ], }, ], },

      This configuration includes the jasonp-loader in the use array along with the babel-loader to handle the rest of the build process. Make sure you have installed the components directory in your src directory.

      If you are still encountering the error, check the include option in the jasonp-loader configuration to ensure that it is properly configured and includes the necessary JavaScript files and modules in your build. Additionally, make sure you are importing the jasonp-loader from the correct location in your webpack configuration, as specified in the documentation.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部