javascript - 为什么我的 HTML 文件只能“看到” Dist 文件夹中的 JavaScript 文件?
问题描述
我正在构建一个简单的、非 React 的 html 页面,用于在 React App中进行 webcheck 监控。此 html 页面称为 specRunner.html。我希望从 specRunner.html 中调用一些 JavaScript 文件,但在我的 html 文件中引用它们时遇到了困难。
具体来说,如果我的 specRunner.html 文件存储在我的目录的 client/dist 文件夹中,则它们只能“查看”JS 文件。我以为我在标签中形成的文件路径不正确,但是我现在已经对其进行了测试,并且可以始终如一地访问 JS 文件,但前提是该 JS 文件位于 client/dist 文件夹中。不用说,我不能把我的 node_modules 文件放在我的 client/dist 文件夹中。
具体来说,我可以从目录中的任何位置提供html文件(即,我的 node-express 应用程序在检索要服务的文件时不限于 client/dist 文件),但我的 html 文件找不到 js 文件,除非js 文件位于客户端/dist 文件中。
File structure:
root
--client
----dist
------bundle.js (for the React App)
------index.html (for the React App)
------specRunner.html (<-- my webcheck page I'm working on)
------example.js (<-- example file I'm trying to access from my specRunner.html)
----src
------React stuff
--node_modules (<-- the files I REALLY want to reference from specRunner.html)
--server
--etc.
当文件位于 dist 文件夹之外的任何位置时,这是 Chrome 控制台错误:
Uncaught SyntaxError: Unexpected token < example.js:1
如果我查看源选项卡,我可以看到 example.js 文件的内容是我的服务器的默认端点 html 页面的 html,用于任何无效的端点调用。
这一定是一些 React 问题,即使这个端点没有涉及 React 组件。
这是我的 webpack.config:
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const SRC_DIR = path.join(__dirname, '/client/src');
const DIST_DIR = path.join(__dirname, '/client/dist');
module.exports = {
entry: `${SRC_DIR}\\index.js`,
output: {
path: path.resolve(__dirname, 'client/dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.jsx?/,
include: SRC_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015'],
plugins: ['syntax-dynamic-import'],
},
},
{
test: /\.css$/,
loaders: ['style-loader', 'css-loader'],
include: SRC_DIR,
},
],
},
resolve: {
extensions: ['.js', '.jsx']
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development'),
},
}),
new ExtractTextPlugin("styles.css"),
],
};
感谢任何指针!
解决方案
推荐阅读
- javascript - 当我在数组过滤器中运行 array.some 时会发生什么
- python-3.x - 如何从类中获取 href 值 - Python - Selenium
- jms - E 02/07/19-07:51:49.358 [-5] JMSListener 异常:java.lang.ClassCastException:com.ibm.jms.JMSMessage 无法转换为 javax.jms.TextMessage
- wordpress - 如何在页面管理员的 post_type 列表顶部添加 HTML?
- python - “NoneType”对象没有“附加”属性。如何在循环中将 int 附加到列表中?
- python - Django从外部api缓存图像
- ios - 如果iOS设备中不存在用于应用商店提交的另一个应用程序,如何继续调用另一个应用程序?
- kotlin - 'javaClass' 的纯 Kotlin 替代品是什么?
- json - 如何将相关实体放入json?
- python - 通过任何方法生成对抗样本时接收断言失败