【发布时间】:2019-05-20 08:42:18
【问题描述】:
我正在编写一个电子应用程序,使用 react 作为 UI 和 webpack 进行捆绑。 Webpack 现在为应用程序的 react 部分配置如下:
const path = require('path');
const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
mode: 'development',
entry: './src/index.tsx',
target:'node',
output: {
filename: '[name].bundle.js',
path: path.join(__dirname, 'build')
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: {
loader: 'ts-loader'
}
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'file-loader'
}
]
},
{
test: /\.css$/,
loader: 'css-loader',
options: {
sourceMap: true,
},
},
{
test: /\.scss$/,
use: [{
loader: "css-loader", options: {
sourceMap: true
}
}, {
loader: "sass-loader", options: {
sourceMap: true
}
}]
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
plugins: [
new HtmlWebPackPlugin({
template: "./index.html",
filename: "./index.html"
}),
new CopyWebpackPlugin([{ from: 'public',ignore: ['*.html'] }])
],
devtool: 'eval-source-map'
}
在我的 index.html 中,我需要使用以下脚本标签来进行电子的渲染过程:
<script>
require('build/bundle.js')
</script>
当我运行webpack-dev-server 时,一切编译都没有错误,但是当我打开 chrome 开发工具时,我看到了这个错误:
Uncaught ReferenceError: require is not defined
at (index):12
我必须在我的webpack.config 中定位节点才能使电子工作,所以我假设 require 函数也可以在浏览器中工作,因为如果我要在纯 node.js 环境中创建电子应用程序(没有 webpack 和反应)它无需任何额外配置即可工作。所以我猜我的 webpack 配置有问题,但是很遗憾我在网上找不到任何有用的资源。谁能帮我吗?提前致谢!
【问题讨论】:
-
这里的解决方案应该可以解决您的问题:stackoverflow.com/questions/23603514/…
-
我已经包含了这个脚本标签(在创建 Require.js 之后):
<script data-main="build/bundle.js" src="Require.js"></script>。现在我收到此错误:尚未为上下文加载模块名称“url”:_。使用 require([]) 我已经尝试过文档中建议的修复,但错误仍然存在
标签: javascript reactjs typescript webpack electron