简体   繁体   English

为什么webpack编译成功,但编译输出中不存在我的源代码?

[英]Why does webpack compile successfully but my source code does not exist in the compiled output?

I use vue-cli-service serve to compile scss and bundle the output我使用vue-cli-service serve来编译 scss 并捆绑输出

This is my webpack configuration inside vue.config.js这是我在vue.config.js webpack 配置

  configureWebpack: {
    entry: {
      main: [`${process.cwd()}/src/main.js`],
      app: [`${process.cwd()}/src/vue.js`]
    },
    plugins: [
      new BundleTracker({ path: "../static/dist", filename: 'webpack-stats.json' }),
    ],
    output: {
      filename: '[name]-[hash].js',
    }
  }

this is my package.json这是我的package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^3.6.5",
    "jquery": "^3.6.0",
    "openseadragon": "^2.4.2",
    "vue": "^2.6.11",
    "regenerator-runtime": "^0.13.7",
    "vue-apollo": "^3.0.0-beta.11",
    "vuex": "^3.6.2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.13",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "sass": "^1.32.8",
    "sass-loader": "^10.1.1",
    "vue-template-compiler": "^2.6.11",
    "webpack-bundle-tracker": "^0.4.3",
    "@babel/core": "^7.13.16",
    "@babel/eslint-parser": "^7.13.14",
    "graphql-tag": "^2.9.0",
    "vue-cli-plugin-apollo": "~0.22.2"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "@babel/eslint-parser"
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}

The problem问题

The problem arose over time.随着时间的推移,问题出现了。

At first npm run serve did not produce the correct bundled output, but compiled successfully.起初npm run serve没有产生正确的捆绑输出,但编译成功。 Eg a simple console.log statement was not present in the output file.例如,输出文件中不存在简单的console.log语句。 npm run build compiled successfully though .虽然npm run build编译成功

I tried to identify the cause, and removed the latest code, and replaced it with multiple console.log statements.我试图找出原因,并删除了最新的代码,并将其替换为多个console.log语句。 I realized that when duplicating console.log('hello') 8 times it worked, but 9 times it didn't !我意识到当复制console.log('hello') 8 次时它有效,但 9 次它没有

I was ok with that for some days, because at least npm run build ran correctly, but now even that doesn't work anymore after the codebase grew by a few new files.几天来我对此感到满意,因为至少npm run build运行正确,但现在在代码库增加了一些新文件后,即使这样也不再起作用。

I am totally baffled and I have no idea where to start searching for an error!我完全困惑,我不知道从哪里开始搜索错误! Please help!请帮忙!

I am not a javascript expert, and I only have little experience with webpack, that's why I can't even formulate the problem correctly.我不是 javascript 专家,而且我对 webpack 的经验也很少,这就是为什么我什至无法正确表述问题的原因。 Any hint would be appreciated!任何提示将不胜感激!

I found out the issue by using vue-cli-service inspect (eg npx vue-cli-service inspect )我通过使用vue-cli-service inspect发现了这个问题(例如npx vue-cli-service inspect

This showed me that the webpack entries looked like this:这向我展示了 webpack 条目如下所示:

### WRONG
entry: {
  app: [
    './src/main.js',
    '<directory path>/src/vue.js'
  ],
  main: [
    '<directory path>/src/main.js'
  ]
}

so webpack expected both in app and main entrypoint to read the ./src/main.js file.所以 webpack 期望在 app 和主入口点读取./src/main.js文件。 It seems like vue-cli assumes it should be this way.似乎 vue-cli 认为它应该是这样的。

So I changed my webpack config to this:所以我将我的 webpack 配置更改为:

### THIS WORKS
entry: {
  app: [
    './src/main.js',
    '<directory path>/src/main.js'
  ],
  otherstuff: [
    '<directory path>/src/index.js'
  ]
}

Now I don't have the problem anymore.现在我没有这个问题了。

So this is what my vue.config.js looks like now:所以这就是我的vue.config.js现在的样子:

  configureWebpack: {
    entry: {
      app: [`${process.cwd()}/src/main.js`],
      otherstuff: [`${process.cwd()}/src/index.js`]
    },
    plugins: [
      new BundleTracker({ path: "../static/dist", filename: 'webpack-stats.json' }),
    ],
    output: {
      filename: '[name]-[hash].js',
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM