Kay*_*ote 6 javascript developer-tools npm reactjs webpack
我按照本教程在Webpack中将React公开为全局.该Expose模块安装和我加入配置webpack.config.js文件模块加载.但是,它无法运行和React Developer Tools仍然无法访问.
这是我的webpack.config.js文件,第一个加载器是expose-react:
var path = require ('path'),
webpack = require ('webpack'),
htmlWebpackPlugin = require ('html-webpack-plugin');
const PATHS = {
app : path.join (__dirname, 'app'),
build : path.join (__dirname, 'build')
};
module.exports = {
entry : {
main : PATHS.app + '/Main.jsx'
},
output : {
path : PATHS.build,
filename : 'dressAphrodite.js'
},
module : {
loaders : [
{
test : require.resolve ('react'),
loader : 'expose?React'
},
{
test : /\.css$/,
loaders : ['style', 'css'],
include : PATHS.app
},
{
test : /\.js?$/,
loader : 'babel-loader',
include : PATHS.app
},
{
test : /\.jsx?$/,
loader : 'babel-loader',
include : PATHS.app
}
]
},
debug : true,
devtool : 'source-map',
devServer : {
contentBase : './app',
progress : true,
stats : 'errors-only',
},
plugins : [
new htmlWebpackPlugin ({
title : 'Dress',
hash : true
})
]
};
Run Code Online (Sandbox Code Playgroud)
只是为了安装依赖项的信息,这是我的package.json文件:
{
"name": "dress",
"version": "1.0.0",
"description": "",
"main": "./main.jsx",
"dependencies": {
"i": "^0.3.3",
"npm": "^3.5.2",
"react": "^0.14.3",
"react-dom": "^0.14.3"
},
"devDependencies": {
"babel-core": "^6.3.26",
"babel-loader": "^6.2.0",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"css-loader": "^0.23.1",
"expose-loader": "^0.7.1",
"html-webpack-plugin": "^1.7.0",
"style-loader": "^0.13.0",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"scripts": {
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)
这是.babelrc文件内容:
{
'presets' : [
'es2015',
'react'
]
}
Run Code Online (Sandbox Code Playgroud)
从 React 0.12 开始,你不再需要将 React 公开为全局的。
重要提示:如果您在不使用服务器的情况下直接打开 html 文件,换句话说,如果您使用file:// URL,则需要允许扩展程序访问此类 URL:要执行此操作,请转到设置>扩展程序,查找 React Developer Tools 并选中“允许访问文件 URL”框。
当捆绑使用 jsx 语法编写的 React 文件时,您需要将文件转换为纯 JavaScript。为此,您需要使用已经安装但未在 webpack.config.js 中使用的 babel-preset-react。
您也不需要在 webpack.config.js 中导入 webpack,并且loaders数组内的对象中的test属性是正则表达式,因此 /.jsx?$/ 足以匹配 js 或 jsx 文件。
这是一个如何输出 React 文件包的工作示例(我删除了 html-webpack-plugin,因此您需要编写自己的页面)
webpack.config.js
var path = require ('path');
var PATHS = {
app : path.join (__dirname, 'app'),
build : path.join (__dirname, 'build')
};
module.exports = {
entry : {
main : PATHS.app + '/main.js'
},
output : {
path : PATHS.build,
filename : 'bundle.js'
},
module : {
loaders : [
{
test : /\.jsx?$/,
loader : 'babel',
query: {
presets: ['react']
}
}
]
}
};
Run Code Online (Sandbox Code Playgroud)
main.js
var React = require('react');
var ReactDOM = require('react-dom');
var Component = require('./component.js');
ReactDOM.render(
<Component />,
document.getElementById('test')
);
Run Code Online (Sandbox Code Playgroud)
组件.js
var React = require('react');
var Test = React.createClass({
diplayName: "Test",
render: function () {
return (
<div>Test</div>
);
}
});
module.exports = Test;
Run Code Online (Sandbox Code Playgroud)
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
<div id="test"></div>
<script src="bundle.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4285 次 |
| 最近记录: |