使用Flask App重新加载VanilaJS或在每次src更改时创建bundle.js

问题描述 投票:0回答:1
  • 我想对JavaScript模块进行更改,并将这些更改实时打包到/dist/bundle.js中
  • 只要更改src模块时自动创建此/dist/bundle.js,我的问题就可以解决(我可以刷新页面并反映出我的更改)
  • 我当前正在运行:npm run dev每次更改后都会刷新URL
  • 或者有一个webpack-dev-server插件,它允许我启动wepack-dev-server --mode development;但仍可通过/dist/bundle.js利用在localhost:5000上运行的Flask应用程序]
  • 我正在尝试提高工作流程的速度,所以任何战术性的技巧都会有所帮助

树结构

    static
        ├───data
        │   └───@es#_ohlc_15min.json
        ├───dist
        │   └───bundle.js   (created by webpack)
        └───src
            ├───index.js
            ├───img
            ├───models
            └───views
    template
        └───index.html

    app.py  (flask app with routes)

package.json

{
  "name": "tradingview_charts",
  "main": "static/src/index.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --mode development"
  },
  "devDependencies": {
    "@babel/core": "^7.7.2",
    "@babel/preset-env": "^7.7.1",
    "babel-loader": "^8.0.6",
    "babel-preset-env": "^1.7.0",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "jquery": "^3.4.1",
    "lightweight-charts": "^1.1.0",
    "react": "^16.12.0"
  }
}

webpack.config.js

const path = require('path');
module.exports = {
    entry: ['./static/src/index.js'],

    output: {
        path: path.resolve(__dirname, 'static'),
        filename: 'dist/bundle.js'
    },
    devServer: {
        contentBase: './static',
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loaders: ['babel-loader']
            }
        ]
    }
};

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tradingview Lightweight Charts</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='style.css') }}?{{ nowts }}">
</head>
<body>
    <div class="chart chart__candlestick" id="main_candlestick"></div>
    <script src="{{ url_for('static', filename='dist/bundle.js') }}?{{ nowts }}"></script>
</body>
</html>

enter image description here

javascript flask npm webpack webpack-dev-server
1个回答
0
投票
webpack --watch
对我有用吗? 

但是,我的输入模块以及所有打包的导入模块都很大。

每当我进行较小的更改时,使用--watch都会非常不可靠。因此,在我主观上进行了重大更改之后,手动运行“ npm run web”

© www.soinside.com 2019 - 2024. All rights reserved.