【问题标题】:Webpack 2 compiling but not running javascriptWebpack 2 编译但不运行 javascript
【发布时间】:2017-03-28 11:26:32
【问题描述】:

我有 2 个 JS 文件

Index.js

const sum = require('./app');

const total = sum(10,5);

console.log(total);

app.js

function sum(a,b){
  return a+b;
}
module.exports = sum;

webpack 配置

const path = require('path');

const config ={
  entry:"./app/index.js",
  output:{
    path:path.resolve(__dirname,"app/build"),
    filename:'bundle.js'
  }
}

module.exports = config;

bundle.js 正在生成,我已将捆绑包添加到我的 index.html,在签入网络选项卡时也会加载捆绑包。但是console.log(total) 不起作用,什么都不起作用。

【问题讨论】:

    标签: javascript webpack webpack-2


    【解决方案1】:

    您的浏览器控制台中没有显示任何内容?有什么错误吗? 也许向我们展示您的配置会更好(例如:webpack 命令或/和您的 package.json )?

    您的代码在此配置下运行良好:

    app
    ├── app.js
    ├── build
    │   ├── bundle.js
    │   └── index.html
    └── index.js
    

    webpack.config.js:

    const path = require('path');
    
    const config ={
      entry:"./app/index.js",
      output:{
        path:path.resolve(__dirname,"app/build"),
        filename:'bundle.js'
      }
    }
    
    module.exports = config;
    

    package.json:

    {
      "name": "stackof",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "dev": "webpack -d --watch"
      },
      "author": "Marwen <marwen_no_spam_pls@gmail.com> (http://www.gameole.com)",
      "license": "ISC",
        "devDependencies": {
        "webpack": "2.2.1"
      }
    }
    

    index.html:

    <!DOCTYPE html>
    <html>
      <head>
        <title>title from webpack</title>
      </head>
      <body>
        <div id="root"></div>
      <script type="text/javascript" src="bundle.js"></script></body>
    </html>
    

    要生成你只需要执行的包:

    npm run dev
    

    【讨论】:

    • 好的,出了点问题.. 2-3 小时没有工作,现在突然工作了......无论如何,谢谢
    • 可能是缓存问题-浏览器缓存您的代码的损坏版本-:-) 无论如何,如果这解决了您的问题,请不要忘记接受答案 ;)
    • cache -browser 缓存是我的问题
    猜你喜欢
    • 2017-06-25
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多