导出未定义webpack 4

55ooxyrt  于 2022-11-13  发布在  Webpack
关注(0)|答案(6)|浏览(168)

我正在使用webpack 4捆绑应用程序资源。
tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "types":  ["node"], 
    "typeRoots": [
      "../node_modules/@types"
    ]
  },
    "compileOnSave": true,
    "exclude": [
       "node_modules"    
    ]
}

webpack.config.js:

module.exports = require("./config/webpack.dev.js");

webpack.common.js:

//common webpack configuration
var webpack = require("webpack");
var path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
var helpers = require("./helpers.js");

module.exports = {
//the entry-point files that define the bundles
entry: {
    "polyfills": "./scripts/polyfills.ts",
    "vendor": "./scripts/vendor.ts",
    "main": "./scripts/main.ts"
},
//resolve file names when they lack extensions
resolve: {
    extensions: [".ts", ".js"]
},
target: "node",
//decide how files are loaded
module: {
    rules: [
        {
            test: /\.ts$/,
            loaders: [
                {
                    loader: "awesome-typescript-loader",
                    options: {
                        configFileName: helpers.root("./scripts", "tsconfig.json")
                    }                        
                }, "angular2-template-loader"
            ]
        },
        {
            test: /\.html$/,
            loader: "html-loader"
        },
        {
            test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
            loader: "file-loader?name=assets/[name].[hash].[ext]"
        },
        {
            test: /\.css$/,
            exclude: helpers.root("./scripts", "app"),
            loader: ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader?sourceMap" })
        },
        {
            test: /\.css$/,
            include: helpers.root("./scripts", "app"),
            loader: "raw-loader"
        }
    ]
},
plugins: [
    // Workaround for angular/angular#11580
    new webpack.ContextReplacementPlugin(
        // The (\\|\/) piece accounts for path separators in *nix and Windows
        /\@angular(\\|\/)core(\\|\/)esm5/,
        helpers.root("./scripts"), // location of your src
        {} // a map of your routes
    ),
    //replaced by optimization.splitChunks for webpack 4
  //        new webpack.optimize.CommonsChunkPlugin({
  //            name: ["app", "vendor", "polyfills"]
  //        }),

    new HtmlWebpackPlugin({
        template: "./Views/Shared/_Layout.cshtml",
        filename: "index.cshtml",
        alwaysWriteToDisk: true
    }),
    new HtmlWebpackHarddiskPlugin({            
    })
],
optimization: {
    splitChunks : {
        chunks: "async",
        minSize: 30000,
        minChunks: 1,
        maxAsyncRequests: 5,
        maxInitialRequests: 3,
        automaticNameDelimiter: '~',
        name: true,
        cacheGroups: {
            vendors: {
                test: /[\\/]node_modules[\\/]/,
                priority: -10
            },
            default: {
                minChunks: 2,
                priority: -20,
                reuseExistingChunk: true
            }
        }
    }
   }
  }

webpack.dev.js:

//development webpack configuration
var webpackMerge = require("webpack-merge");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var commonConfig = require("./webpack.common.js");
var helpers = require("./helpers.js");

   module.exports = webpackMerge(commonConfig, {
devtool: "cheap-module-eval-source-map",

output: {
    path: helpers.root("Views"),
    publicPath: "scripts/",
    filename: "[name].js",
    chunkFilename: "[id].chunk.js"
},

plugins: [
    new ExtractTextPlugin("[name].css")
],

devServer: {
    historyApiFallback: true,
    stats: "minimal"
}
});

我正在使用angular5,我已经按照官方的快速入门指南来构建webpack配置。Webpack是成功编译使用'npm start'命令(开发配置)和索引。cshtml注入所有3个文件我想要:1.聚合物填料. js 2.供应商. js 3.主要. js
问题是,当从浏览器加载这些文件时,我得到以下错误:

polyfills.ts:2 Uncaught ReferenceError: exports is not defined
at polyfills.ts:2
(anonymous) @ polyfills.ts:2
vendor.ts:1 Uncaught ReferenceError: exports is not defined
at vendor.ts:1
(anonymous) @ vendor.ts:1
main.js:2 Uncaught ReferenceError: exports is not defined
at main.js:2
(anonymous) @ main.js:2

有谁能帮我看看这配置出了什么问题吗?

wnvonmuf

wnvonmuf1#

将这个添加到Babel config的插件将解决这个问题:

["@babel/plugin-transform-modules-commonjs"]
7rfyedvj

7rfyedvj2#

我有什么似乎是一个非常类似的错误,在我的Webpack 4设置(与巴别塔7). Webpack是建立包正确,但当我试图加载文件,我得到:

Uncaught ReferenceError: exports is not defined
at eval (util.js:22)

......其中util.js是我自己的一个库。我在谷歌上搜索了很多建议用modules: false和/或sourceType: unambiguous配置babel-loader和/或将libraryTarget(在output中)设置为'umd'的解决方案,所以我尝试了如下所示的方法:

{test: /\.js$/, use:
     {
         loader: 'babel-loader',
         options:
         {
             presets: [
                 ['@babel/preset-env', {loose: true, modules: false}],
                 '@babel/react'
             ],
             sourceType: 'unambiguous'
         }
     }
}

......在我的webpack.config.jsrules部分中。上述建议的任何排列都不对我起作用。最后,它只是简单地将以下内容(从我自己的util.js文件的结尾)从:

exports.foo = foo;
exports.boo = boo;

...到:

export {foo, boo};

我将我的(工作)webpack.config.jspackage.json

网页包配置. js

'use strict';
const path = require('path');

const APPDIR = 'app/';

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, APPDIR, 'index.html'),
    filename: 'index.html',
    inject: 'body'
});

const config = {
    context: path.resolve(__dirname, APPDIR),
    entry: './main.js',
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {test: /\.js$/, use:
             {
                 loader: 'babel-loader',
                 options:
                 {
                     presets: [
                         ['@babel/preset-env'], '@babel/react'
                     ]
                 }
             }
            },{
                test: /\.css$/,
                use: [
                    // style-loader
                    {loader: 'style-loader'},
                    // css-loader
                    {loader: 'css-loader',
                     options: {
                         modules: true
                     }
                    }]
            },{
                test: /\.(png|jpg|jpeg|gif|woff)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192
                        }
                    }]
            },{
                test: /\.README$/,
                use: 'null-loader'
            }
        ]
    },
    plugins: [HTMLWebpackPluginConfig],

    node: {
        fs: "empty" // This is to account for what appears to be a bug: https://github.com/josephsavona/valuable/issues/9`
    }
};

module.exports = config;

包. json

{
"name": "cubehelix-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "clean": "rm -fr build/index.html && rm -fr build/bundle.js && npm run clean-logs",
    "build": "webpack --progress --colors",
    "build-min": "webpack --config=webpack.min.config.js",
    "dev": "webpack-dev-server --devtool eval --progress --colors --content-base build --port 8090",
    "test-build": "mkdir -p lib && babel app --out-dir lib --source-maps",
    "test": "npm run test-build && mocha --require source-map-support/register --compilers js:babel-register"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
    "@babel/cli": "~7.6.0",
    "@babel/core": "~7.6.0",
    "@babel/preset-env": "~7.6.0",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "~7.6.0",
    "@babel/runtime": "~7.6.0",
    "babel-loader": "^8.0.6",
    "chai": "~4.2.0",
    "css-loader": "^0.23.1",
    "file-loader": "^0.9.0",
    "html-webpack-plugin": "~3.2.0",
    "mocha": "~6.2.0",
    "null-loader": "^0.1.1",
    "style-loader": "^0.13.0",
    "url-loader": "^0.5.7",
    "webpack": "~4.39.3",
    "webpack-cli": "^3.3.8",
    "webpack-dev-server": "~3.8.0"
},
"dependencies": {
    "chai": "~4.2.0",
    "classnames": "^2.2.5",
    "jquery": "~3.4.1",
    "lodash": "~4.17.15",
    "prop-types": "^15.7.2",
    "react": "~16.9.0",
    "react-custom-validators": "*",
    "react-dom": "~16.9.0",
    "react-timer-mixin": "~0.13.4"
}
}
ws51t4hk

ws51t4hk3#

下面是一个可能的解决方案(它对我的“导出未定义”错误有效):https://github.com/webpack/webpack/issues/3974#issuecomment-369260590
对我来说,一个解决方案是改变babel-loader的配置,在插件下,['transform-runtime',{“polyfill”:错误}]

dgsult0t

dgsult0t4#

不确定这是否会帮助任何人,因为我们有一个非常“特殊”的设置,我们击中了这个问题。
我们所看到的是,带有dist目录的sym链接共享库(通过package. json/ npm中的file:/ reference有效链接)被webpack错误地传递。
我们有“符号链接:false”选项,我们的解决方案是将其添加到相关module.rules[]部分中的ignore部分。
我相信这是因为,即使文件在node_modules中,并且存在符号链接,解析的路径使忽略模式没有找到它,它们再次被传递(错误)

diff --git a/project/webpack.config.js b/project/webpack.config.js
index f688472d72..a680c205e7 100644
--- a/project/webpack.config.js
+++ b/project/webpack.config.js
@@ -265,7 +265,10 @@ function webpackConfig(_target) {
        rules: [
          {
            test: /\.jsx?$/,
-            exclude: /node_modules/,
+            exclude: [
+              /node_modules/,
+              /node-lib/
+            ],
            use: {
              loader: 'babel-loader',
              options: {

(我们的共享库显示在project的兄弟目录node-lib中)

lf5gs5x2

lf5gs5x25#

我也遇到了这个错误。对我来说,问题是我不小心在我的main.ts之外有一个main.js,导致webpack使用那个代替,并且由于某种原因没有发出很多导入的模块。删除main.js解决了这个问题。

相关问题