简体   繁体   English

WebPack output.library.type var 未定义

[英]WebPack output.library.type var is undefined

I am learning WebPack with a shortcode.我正在使用简码学习 WebPack。 In the code, we are trying to calculate the cube and square.在代码中,我们试图计算立方和平方。 They will suppose to store in a variable as per the following webpack.config.js.他们将假设按照以下 webpack.config.js 存储在一个变量中。

    const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const isDevelopment = process.env.NODE_ENV === 'development';

const config = {
    entry: './src/index.ts',
    output: {
        path: path.resolve(__dirname, 'dist'),
        library: {
            type: 'var',
            name: 'testVar'
        },
        filename: '[name].js'
    },
    mode: 'production',
    plugins: [

        new MiniCssExtractPlugin({
            // Options similar to the same options in webpackOptions.output
            // both options are optional
            filename: "[name].css",
            chunkFilename: "[id].css",
        }),
        new webpack.optimize.LimitChunkCountPlugin({
            maxChunks: 1,
        }),

        new HtmlWebpackPlugin({
            hash: true,
            title: 'Video Player Play Ground',
            myPageHeader: 'Sample Video Player',
            template: './src/index.html',
            filename: 'index.html'
        })
    ],
    module: {
        rules: [
            {
                test: /\.s[ac]ss$/i,
                exclude: /node_modules/,

                use: [
                    // fallback to style-loader in development
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "sass-loader",
                ],
            },
            {
                test: /\.ts(x)?$/,
                loader: 'ts-loader',
                exclude: /node_modules/
            },
            {
                test: /\.svg$/,
                use: 'file-loader'
            }
        ]
    },
    resolve: {
        extensions: [
            '.tsx',
            '.ts',
            '.js',
            '.scss'
        ]
    },
    optimization: {
        usedExports: true,
        runtimeChunk: false,
        minimize: false
    }
};

module.exports = config;

As shown in the above config, we store the compiled javascript in the variable with the name "testVar".如上述配置所示,我们将编译后的 javascript 存储在名为“testVar”的变量中。

The above approach working fine when we are using the following command line code "webpack --mode production"当我们使用以下命令行代码“webpack --mode production”时,上述方法工作正常

In the final generated javascript file we have a line which equals to testVar = webpack_exports ;在最终生成的 javascript 文件中,我们有一行等于 testVar = webpack_exports

Also, testVar.start is working fine.此外, testVar.start 工作正常。

But the above approach is not working when we are using the following command line "webpack serve --mode production" or "webpack serve" When we run a local server, the testVar.start is undefined.但是当我们使用以下命令行“webpack serve --mode production”或“webpack serve”运行本地服务器时,上述方法不起作用,testVar.start 是未定义的。 I am not sure what I am doing wrong.我不确定我做错了什么。

Following is the code we are using in the index.html file to call the start function, which we defined in our internal javascript以下是我们在 index.html 文件中使用的代码,用于调用我们在内部 javascript 中定义的启动 function

    window.onload = function (){
    alert(testVar);
    console.log(testVar);
    if(testVar.start !== undefined)
    {
        alert(testVar.start);
        console.log(testVar.start);
        testVar.start(3,2);
    }
    else {

        alert("Start Function is undefined");
    }

}

Also following is the code insight from index.ts and math.ts.以下是来自 index.ts 和 math.ts 的代码洞察。

    import {cube, square} from './math';

export function start(c:number, s:number) {
    console.log(cube(c));
    console.log(square(s));
}

export function square(x:number) {
    return x * x;
}
export function cube(x:number) {
    return x * x * x;
}

enter image description here在此处输入图像描述

Finally, I got it working:-).最后,我让它工作了:-)。 I need to add the following line in my webpack.config.js我需要在我的 webpack.config.js 中添加以下行

devServer: {
    injectClient: false
},

Following is the reference URL: https://github.com/webpack/webpack-dev-server/issues/2484以下是参考URL: https://github.com/webpack/webpack-dev-server/issues/2484

Don't forget to Vote it.不要忘记投票。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM