この作業にはWebPackを使用することを強くお勧めします。
あなたのルートディレクトリにwebpack.config.jsファイルを作成します。 は多分あなたのコードでは、いくつかの変更を必要とするが、これはアイデアです:
const webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');
var pkgBower = require('./package.json');
module.exports = {
target: "web",
devtool: "source-map",
node: {
fs: "empty"
},
entry: {
'app': path.join(__dirname, 'react-app', 'Index.jsx')
},
resolve: {
modules: [__dirname, 'node_modules', 'bower_components'],
extensions: ['*','.js','.jsx', '.es6.js']
},
output: {
path: path.join(__dirname, 'public', 'src'),
filename: '[name].js'
},
resolveLoader: {
moduleExtensions: ["-loader"]
},
module: {
loaders: [
{
test: /jquery\.flot\.resize\.js$/,
loader: 'imports?this=>window'
},
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react', 'stage-0'],
compact: false
}
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
{
test: /\.woff|\.woff2|\.svg|.eot|\.ttf/,
loader: 'url?prefix=font/&limit=10000'
},
{
test: /\.(png|jpg|gif)$/,
loader: 'url?limit=10000'
},
{
test: /\.scss$/,
loader: 'style!css!sass?outputStyle=expanded'
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new CopyWebpackPlugin([{
from: 'img',
to: 'img',
context: path.join(__dirname, 'react-app')
}, {
from: 'server',
to: 'server',
context: path.join(__dirname, 'react-app')
}, {
from: 'fonts',
to: 'fonts',
context: path.join(__dirname, 'react-app')
}]),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
mangle: true,
sourcemap: false,
beautify: false,
dead_code: true
}),
new webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /js$/)
]
};
を次にフォルダに、あなたのルートディレクトリに自分のアプリを入れて反応させます-app /そして、あなたのコンポーネントは、そのフォルダ内で行くことができます。たとえば
あなたのファイルを:[Index.jsx]
import React from 'react';
import ReactDOM from 'react-dom';
class Index extends Component {
render() {
return (<h1> hello world! < /h1>)
}
}
ReactDOM.render(< Index/> , document.getElementById('example'));
あなたはこの工業をエクスポートする必要はありません。この場合はexコンポーネントですが、コンポーネントファイルの最後にエクスポートのデフォルトComponentNameを使用する必要がある場合に備えてください。
次のステップは、あなたのルートファイルである(私はAdonisJS 4を使用しています)が、((.njk、または.edge)バージョン3.x
その後
Route.any("*", ({ view, response }) => (
view.render('index')
))
あなたのインデックスのために非常に似ているファイル資源の下で/ビュー)は、次のようなものになります。
<html>
<head>
<link rel="stylesheet" href="/src/styles.css">
<script type="text/javascript" src="/src/app.js"></script>
</head>
<body>
<div id="example"></div>
</body>
</html>
- あなたはNPMや糸、 でいくつかのNPM /パッケージをインストールし、別の端末ウィンドウまたはタブで実行するために覚えておく必要が を、
webpack -d --watch --progress
よろしくお願いいたします。
コンソールにエラーはありませんか? – Nope
SyntaxError:期待される表現、この行に ')'が返されました(< h1 > hello world!< /h1>)または 'ReactDOM.render(< Index/> ...'はまったく問題を与えていませんか? < /h1>) –
あなたは行きます。これらの構文エラーを修正してください。私は彼らが文字列である必要があると仮定します。また、 'ReactDOM.render(...)'の場合、[** Documentation **](https://reactjs.org/docs/react- dom.html#render)を使用します。 – Nope