简体   繁体   English

webpack dll配置不起作用

[英]webpack dll configuration not working

I'm trying to create dll configuration with webpack 4, but i keep getting: 我正在尝试使用webpack 4创建dll配置,但我不断得到:

Uncaught ReferenceError: React is not defined

My configuration is very simple: 我的配置非常简单:

module.exports = {
  entry: {
    vendor: ["react", "react-dom"]
  },
  output: {
    filename: "[name]-manifest.dll.js",
    path: base.path.project("build"),
    library: "[name]",
    libraryTarget: "umd"
  },
  plugins: [
    new webpack.DllPlugin({
      name: "[name]",
      path: base.path.project("build/[name]-manifest.json"),
      context: base.path.src("app")
    })
  ]
};

In my development configuration I use the dllreferenceplugin. 在开发配置中,我使用dllreferenceplugin。

new webpack.DllReferencePlugin({
  context: base.path.src("app"),
  manifest: require("../build/vendor-manifest.json")
})

and of course i define externals in development configuration, because I don't want to include them again when building my development js file: 当然,我在开发配置中定义了外部,因为在构建开发js文件时,我不想再次包含它们:

  externals: {
    react: "React",
    "react-dom": "ReactDOM"
  }

In my code I import React. 在我的代码中,我导入了React。

import * as React from "react";

But in the browser I keep getting React is not defined. 但是在浏览器中,我一直没有定义React。

I have googled everything and have not found any solution to this problem? 我已经用Google搜索了所有内容,却没有找到解决此问题的任何方法?

Thank you for any help! 感谢您的任何帮助!

Use import React, { Component } from 'react'; import React, { Component } from 'react';使用import React, { Component } from 'react'; myself. 我。 JSX wants react to be in scope. JSX希望做出反应。

Largely only see externals being used when making a library, like a npm module that other folks consume that uses React. 在创建库时,基本上只看到正在使用的外部组件,例如其他人使用React的npm模块。 React would be marked as an external in that package to avoid double including React in the local application and inside the module. React将在该包中标记为外部,以避免在本地应用程序模块内部双重包含React。

If this is your app build process, and not an npm module then should not be marking react as externals. 如果这是您的应用程序构建过程,而不是npm模块,则不应将react标记为外部。 Your bundle needs to include react. 您的捆绑包需要包含反应。 Marking react as externals excludes it from the bundle and makes it your responsibility to manually load React before the bundle is loaded. 将react标记为externals会将其从包中排除,并使您有责任在加载包之前手动加载React。

The externals configuration option provides a way of excluding dependencies from the output bundles. 外部配置选项提供了一种从输出包中排除依赖项的方法。

See the docs on webpack externals here . 请参阅此处的 webpack外部文档。

Just Remove the external property from your app webpack config file.Also, Include the dll file in your index.html file. 只需从您的应用程序webpack配置文件中删除外部属性,然后在您的index.html文件中包含dll文件即可。

I was also facing the same issue, because I was using both dllReference plugin and external at the same time. 我也面临着相同的问题,因为我同时使用了dllReference插件和外部插件。

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

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