Webpack dev server with flask html templates

ztigrdn8  于 2023-10-19  发布在  Webpack
关注(0)|答案(1)|浏览(139)

我试图使用Flask发送一个HTML视图(模板)和前端的React,但我在前端设置webpack-dev-server时遇到了问题。我的困惑是,我的Flask应用程序在端口5000上运行,并在端口上发送视图。WebpackdevServer在端口8080上启动。
我知道webpack dev server也会提供内存不足的bundle,但是我如何使用它来创建热模块替换呢?我已经阅读了其他的答案,并使用webpack手表与开发服务器似乎不符合技术要求。
我需要做什么才能让Flask html模板上的script标签加载webpack或webpack dev server生成的bundle文件?

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <div id="main"></div>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <script defer src="../js/main.bundle.js" type="text/javascript"></script>
  8. </body>
  9. </html>
  1. //webpack dev config
  2. const ab_path = path.resolve(__dirname, '../', 'static/', 'js/');
  3. module.exports = merge.merge(common, {
  4. entry: ["./src/index.js"],
  5. output: {
  6. filename: '[name].bundle.js',
  7. path: path.join(process.cwd(), "../static/js")
  8. },
  9. // o
  10. devtool: 'inline-source-map',
  11. devServer: {
  12. hot: true,
  13. open: true,
  14. static: path.resolve(ab_path),
  15. },
  16. plugins: [
  17. new ESLintPlugin()
  18. ],
  19. mode: "development"
  20. });

我运行命令`webpack-dev-server --config /path/to/dev/config,但它似乎没有做任何事情,除了打开一个新的标签到localhost:8080

wz1wpwve

wz1wpwve1#

1.配置Flask以服务于React App:

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. return render_template('index.html')
  6. if __name__ == '__main__':
  7. app.run(debug=True)

1.更新您的HTML模板:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <div id="main"></div>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. {% if DEBUG %}
  8. <script defer src="http://localhost:8080/main.bundle.js" type="module"></script>
  9. {% else %}
  10. <script defer src="{{ url_for('static', filename='js/main.bundle.js') }}"></script>
  11. {% endif %}
  12. </head>
  13. <body>
  14. </body>
  15. </html>

1.更新Webpack开发人员配置:

  1. const merge = require('webpack-merge');
  2. const common = require('./webpack.common.js');
  3. const path = require('path');
  4. module.exports = merge(common, {
  5. entry: ['./src/index.js'],
  6. output: {
  7. filename: '[name].bundle.js',
  8. path: path.join(process.cwd(), 'dist'),
  9. publicPath: 'http://localhost:8080/', // Important for webpack-dev-server
  10. },
  11. devtool: 'inline-source-map',
  12. devServer: {
  13. hot: true,
  14. open: true,
  15. static: path.resolve(process.cwd(), 'dist'),
  16. },
  17. mode: 'development',
  18. });

1.更新Webpack生产配置:

  1. const merge = require('webpack-merge');
  2. const common = require('./webpack.common.js');
  3. const path = require('path');
  4. module.exports = merge(common, {
  5. entry: ['./src/index.js'],
  6. output: {
  7. filename: '[name].bundle.js',
  8. path: path.join(process.cwd(), 'static', 'js'),
  9. },
  10. mode: 'production',
  11. });
展开查看全部

相关问题