您好,登錄后才能下訂單哦!
要使用Webpack打包React應(yīng)用,首先需要在項(xiàng)目中安裝Webpack和相關(guān)的Loader和插件。然后創(chuàng)建一個(gè)Webpack配置文件,配置入口文件、輸出文件、Loader和插件等信息。最后在命令行中運(yùn)行Webpack命令來執(zhí)行打包操作。
以下是一個(gè)簡(jiǎn)單的使用Webpack打包React應(yīng)用的步驟:
npm install webpack webpack-cli webpack-dev-server babel-loader @babel/core @babel/preset-react @babel/preset-env react react-dom
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000
}
};
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
import React from 'react';
const App = () => {
return <h1>Hello, React!</h1>;
}
export default App;
"scripts": {
"start": "webpack serve --mode development --open",
"build": "webpack --mode production"
}
npm run build
Webpack會(huì)根據(jù)配置文件打包React應(yīng)用,并生成一個(gè)bundle.js文件,可在瀏覽器中查看應(yīng)用效果。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。