我正在使用webpack实现一个ReactJS Web应用程序,我想开始实现一个服务工作者来处理网络调用,以便缓存文件.但是,我发现很难注册服务工作者的javascript文件.(我应该补充说,当涉及到React和webpack时,我是初学者.)
如果我做对了,webpack会将每个javascript文件合并到一个bundle.js文件中,这使得很难调用navigator.serviceWorker.register('./sw.js')....我尝试了不同的方法,例如serviceworker-webpack-plugin,但没有运气.如果我按照自述页面上提供的步骤操作,我会收到一条错误消息Uncaught Error: serviceworker-webpack-plugin: It seems that your are importing "serviceworker-webpack-plugin/lib/runtime" without using the plugin. Makes sure that your webpack configuration is correct..
这是我的webpack.config.js档案:
var webpack = require('webpack');
var path = require('path');
import ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin';
module.exports = {
devtool: 'inline-source-map',
entry: [
'webpack-dev-server/client?http://127.0.0.1:8080/',
'webpack/hot/only-dev-server',
'./src/index.js'
],
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
resolve: {
modulesDirectories: ['node_modules', 'src'],
extensions: ['', '.js']
},
module: {
loaders: [
{ …Run Code Online (Sandbox Code Playgroud) 我正在关注 O'Reilly 的“Learning React”的第 5 章“React with JSX”。
我编写了 Recipes Appcreate-react-app作为基础。
索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import Menu from './Menu';
import registerServiceWorker from './registerServiceWorker';
import data from './data/recipes';
window.React = React;
ReactDOM.render(<Menu recipes={data} />, document.getElementById('root'));
registerServiceWorker();
Run Code Online (Sandbox Code Playgroud)
菜单.js
import Recipes from './Recipes';
const Menu = ({recipes}) => (
<article>
<header>
<h1>Delicious Recipes</h1>
</header>
<div className = "recipes">
{recipes.map((recipe, i)=>
<Recipes key={i} {...recipe} />
)}
</div>
</article>
);
export default …Run Code Online (Sandbox Code Playgroud) 我使用react-router-dom并构建了我的反应应用程序。当我在服务器上部署它时,我得到一个空白页面并且控制台是空的。
我的 App.js 是:
import React, { Component } from 'react';
import { Route, Switch, BrowserRouter} from 'react-router-dom';
import Agenda from './components/Agenda/Agenda';
import Planning from './components/Planning/Planning';
class App extends Component {
render() {
return (
<div>
<BrowserRouter basename="/">
<Switch>
<Route exact path="/" component={Agenda} />
<Route path="/planning" component={Planning} />
</Switch>
</BrowserRouter>
</div>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)
我的 index.js 是:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App/>
, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
我的 index.html …
在我的 create-react-app 应用程序中,我的公共文件夹中有一个 javascript 文件。在其中,我需要访问一个名为的环境变量,REACT_APP_ACTUAL_ENV但我不确定如何访问。尝试通常process.env.REACT_APP_ACTUAL_ENV不起作用,因为process在公共文件夹中未定义。查看https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables,我可以%REACT_APP_ACTUAL_ENV%在公共文件夹中的 HTML 文件中进行操作,并且效果很好。但问题是我需要以某种方式在 javascript 文件中执行此操作,尽管我不确定是否可行。
在子文件夹中使用 create-react-app 引导的项目不会 lint。但是,如果我在 VSCode 中以 root 身份打开项目子文件夹(如下图所示),或者在 root 中设置一个新的 create-react-app 项目,linting 工作正常。
这发生在标准和弹出的 create-react-app 项目中。
理想情况下不弹出,如何使 create-react-app 的 linting 与子文件夹项目一起工作?
我正在使用 create-react-app v3.1.1、VSCode 2019 年 8 月和 Windows 10。
javascript reactjs eslint visual-studio-code create-react-app
我已经安装eslint为我的 create-react-app 项目的开发依赖项。我这样做是为了
1) 通过 husky 和 lint-staged 运行 eslint 作为预提交检查
2) 使用 airbnb 和更漂亮的 lint 配置扩展 CRA 的 eslint
我收到警告:
Manually installing incompatible versions is known to cause hard-to-debug issues ...
Run Code Online (Sandbox Code Playgroud)
我只是想知道手动安装 eslint 是否真的有任何风险?或者我可以“安全地忽略”这个警告吗?(通过 SKIP_PREFLIGHT_CHECK=true)
这与这个问题类似,但是这两个答案都不能解决问题。
运行后npm run build的结果index.html类似于:
<script>!function (i) { function e(e) { for //rest omitted
<script src="/static/js/2.3f294f32.chunk.js"></script>
<script src="/static/js/main.7b9daa35.chunk.js"></script>
Run Code Online (Sandbox Code Playgroud)
第一个<script>元素是内联的 javascript,我已将其提取到一个名为的文件中loader.js
<script src="/loader.js"></script>
<script src="/static/js/2.3f294f32.chunk.js"></script>
<script src="/static/js/main.7b9daa35.chunk.js"></script>
Run Code Online (Sandbox Code Playgroud)
这有效,但我想将所有 3 个文件合并为一个文件
我已经尝试过filesmerge.com来合并 JS 文件,但这会导致在引用单个文件时出错:
output.min.js:1 Uncaught TypeError: (intermediate value)(...) is not a function
at output.min.js:1
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用jscompress.com 进行组合,虽然这不会产生任何错误,但不会呈现反应根元素
我也试过在 create-react-app 存储库中建议的这个解决方案不起作用。不产生错误但不呈现反应元素(页面保持空白)
我已经使用 create-react-app 创建了应用程序。然后我安装了 react-table 包(使用命令 npm install --save react-table)。当我开始我的应用程序时,我发现找不到模块:无法解决“react-table/react-table.css”这个错误。
任何建议如何解决这个问题。提前致谢 !
我通过Create-React-App以下包(提及与我的问题相关的包)创建了反应项目:
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"typescript": "^3.9.2",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0"
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的 HOC(现在什么都不做,但我稍后会添加我的逻辑),如下所示:
type Props = {
[key: string]: any;
};
const connect = function (Component: FunctionComponent): FunctionComponent {
const ComponentWrapper = function (props: Props): ReactElement {
return <Component {...props} />;
};
return ComponentWrapper;
};
Run Code Online (Sandbox Code Playgroud)
并像这样导出我的组件:
const Test: FunctionComponent<Props> = function ({ message }: Props) {
return (
<div>{message}</div>
);
};
export default connect(Test);
Run Code Online (Sandbox Code Playgroud)
并像这样使用这个组件:
<Test message="Testing message" />
Run Code Online (Sandbox Code Playgroud)
但是在编译器中出错:
Type '{ message: string; …Run Code Online (Sandbox Code Playgroud) typescript reactjs create-react-app react-hoc intrinsicattributes
我正在尝试在 Create-React-App 项目中设置 TailwindCSS,并且正在尝试安装 craco,这样我就不需要弹出我的 CRA
当我运行以下命令 npm install @craco/craco 时,我无法解决依赖关系树错误。以下是错误的详细信息。我应该怎么办?谢谢
npm install @craco/craco
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: exchange@0.1.0
npm ERR! Found: react-scripts@3.4.3
npm ERR! node_modules/react-scripts
npm ERR! react-scripts@"3.4.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react-scripts@"^4.0.0" from @craco/craco@6.1.1
npm ERR! node_modules/@craco/craco
npm ERR! @craco/craco@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, …Run Code Online (Sandbox Code Playgroud) create-react-app ×10
reactjs ×9
eslint ×2
javascript ×2
webpack ×2
build ×1
craco ×1
jsx ×1
npm ×1
react-dom ×1
react-hoc ×1
react-router ×1
react-table ×1
typescript ×1