Cordova + Webpack + Vue.js = npm run build old assets version

nnvyjq4y  于 2023-10-19  发布在  Webpack
关注(0)|答案(2)|浏览(189)

在这里我遇到了一个问题,我的项目cordova + vuejs 2 + webpack.
当我为生产环境(npm run build)构建时,webpack总是缓存当前./src中不存在的旧版本的资产,并且不会加载我的新更改。
我在./src中的东西在dev(npm run dev)中运行得很好,但在没有新映像的情况下,在./dist/*npm run build)中无法正确编译。
我找不到配置中的位置,其中插件进行了修改,以严格构建./src/*,并且没有缓存资产。
如果任何人有一个想法或已经遇到这个问题-这将是伟大的!
这里附上的是屏幕截图,我们可以看到,资产和结果编译在dist是不同的,从目前在src/*

  1. 'use strict'
  2. // Template version: 1.3.1
  3. // see http://vuejs-templates.github.io/webpack for documentation.
  4. const path = require('path')
  5. module.exports = {
  6. dev: {
  7. // la config de dev
  8. },
  9. build: {
  10. // Template for index.html
  11. index: path.resolve(__dirname, '../www/dist/index.html'),
  12. // Paths
  13. assetsRoot: path.resolve(__dirname, '../www/dist'),
  14. assetsSubDirectory: 'static',
  15. assetsPublicPath: '',
  16. /**
  17. * Source Maps
  18. */
  19. productionSourceMap: true,
  20. // https://webpack.js.org/configuration/devtool/#production
  21. devtool: '#source-map',
  22. // Gzip off by default as many popular static hosts such as
  23. // Surge or Netlify already gzip all static assets for you.
  24. // Before setting to `true`, make sure to:
  25. // npm install --save-dev compression-webpack-plugin
  26. productionGzip: false,
  27. productionGzipExtensions: ['js', 'css'],
  28. // Run the build command with an extra argument to
  29. // View the bundle analyzer report after build finishes:
  30. // `npm run build --report`
  31. // Set to `true` or `false` to always turn it on or off
  32. bundleAnalyzerReport: process.env.npm_config_report
  33. }
  34. }

我在build/webpack.prod.conf.js中有什么:

  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  13. const env = process.env.NODE_ENV === 'testing'
  14. ? require('../config/test.env')
  15. : require('../config/prod.env')
  16. const webpackConfig = merge(baseWebpackConfig, {
  17. module: {
  18. rules: utils.styleLoaders({
  19. sourceMap: config.build.productionSourceMap,
  20. extract: true,
  21. usePostCSS: true
  22. })
  23. },
  24. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  25. output: {
  26. path: config.build.assetsRoot,
  27. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  28. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  29. },
  30. plugins: [
  31. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  32. new webpack.DefinePlugin({
  33. 'process.env': env
  34. }),
  35. new UglifyJsPlugin({
  36. uglifyOptions: {
  37. compress: {
  38. warnings: false
  39. }
  40. },
  41. sourceMap: config.build.productionSourceMap,
  42. parallel: true
  43. }),
  44. // extract css into its own file
  45. new ExtractTextPlugin({
  46. filename: utils.assetsPath('css/[name].[contenthash].css'),
  47. // Setting the following option to `false` will not extract CSS from codesplit chunks.
  48. // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
  49. // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
  50. // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
  51. allChunks: true,
  52. }),
  53. // Compress extracted CSS. We are using this plugin so that possible
  54. // duplicated CSS from different components can be deduped.
  55. new OptimizeCSSPlugin({
  56. cssProcessorOptions: config.build.productionSourceMap
  57. ? { safe: true, map: { inline: false } }
  58. : { safe: true }
  59. }),
  60. // generate dist index.html with correct asset hash for caching.
  61. // you can customize output by editing /index.html
  62. // see https://github.com/ampedandwired/html-webpack-plugin
  63. new HtmlWebpackPlugin({
  64. filename: process.env.NODE_ENV === 'testing'
  65. ? 'index.html'
  66. : config.build.index,
  67. template: 'index.html',
  68. inject: true,
  69. minify: {
  70. removeComments: true,
  71. collapseWhitespace: true,
  72. removeAttributeQuotes: true
  73. // more options:
  74. // https://github.com/kangax/html-minifier#options-quick-reference
  75. },
  76. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  77. chunksSortMode: 'dependency'
  78. }),
  79. // keep module.id stable when vendor modules does not change
  80. //new webpack.HashedModuleIdsPlugin(),
  81. // enable scope hoisting
  82. new webpack.optimize.ModuleConcatenationPlugin(),
  83. // split vendor js into its own file
  84. new webpack.optimize.CommonsChunkPlugin({
  85. name: 'vendor',
  86. minChunks (module) {
  87. // any required modules inside node_modules are extracted to vendor
  88. return (
  89. module.resource &&
  90. /\.js$/.test(module.resource) &&
  91. module.resource.indexOf(
  92. path.join(__dirname, '../node_modules')
  93. ) === 0
  94. )
  95. }
  96. }),
  97. // extract webpack runtime and module manifest to its own file in order to
  98. // prevent vendor hash from being updated whenever app bundle is updated
  99. new webpack.optimize.CommonsChunkPlugin({
  100. name: 'manifest',
  101. minChunks: Infinity
  102. }),
  103. // This instance extracts shared chunks from code splitted chunks and bundles them
  104. // in a separate chunk, similar to the vendor chunk
  105. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  106. new webpack.optimize.CommonsChunkPlugin({
  107. name: 'app',
  108. async: 'vendor-async',
  109. children: true,
  110. minChunks: 3
  111. }),
  112. // copy custom static assets
  113. new CopyWebpackPlugin([
  114. {
  115. from: path.resolve(__dirname, '../static'),
  116. to: config.build.assetsSubDirectory,
  117. ignore: ['.*']
  118. }
  119. ])
  120. ]
  121. })
  122. if (config.build.productionGzip) {
  123. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  124. webpackConfig.plugins.push(
  125. new CompressionWebpackPlugin({
  126. asset: '[path].gz[query]',
  127. algorithm: 'gzip',
  128. test: new RegExp(
  129. '\\.(' +
  130. config.build.productionGzipExtensions.join('|') +
  131. ')$'
  132. ),
  133. threshold: 10240,
  134. minRatio: 0.8
  135. })
  136. )
  137. }
  138. if (config.build.bundleAnalyzerReport) {
  139. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  140. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  141. }
  142. module.exports = webpackConfig

我在build/build.js中有什么:

  1. 'use strict'
  2. require('./check-versions')()
  3. process.env.NODE_ENV = 'production'
  4. const ora = require('ora')
  5. const rm = require('rimraf')
  6. const path = require('path')
  7. const chalk = require('chalk')
  8. const webpack = require('webpack')
  9. const config = require('../config')
  10. const webpackConfig = require('./webpack.prod.conf')
  11. const spinner = ora('building for production...')
  12. spinner.start()
  13. rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
  14. if (err) throw err
  15. webpack(webpackConfig, (err, stats) => {
  16. spinner.stop()
  17. if (err) throw err
  18. process.stdout.write(stats.toString({
  19. colors: true,
  20. modules: false,
  21. children: false, // If you are using ts-loader, setting this to true will make TypeScript errors show up during build.
  22. chunks: false,
  23. chunkModules: false
  24. }) + '\n\n')
  25. if (stats.hasErrors()) {
  26. console.log(chalk.red(' Build failed with errors.\n'))
  27. process.exit(1)
  28. }
  29. console.log(chalk.cyan(' Build complete.\n'))
  30. console.log(chalk.yellow(
  31. ' Tip: built files are meant to be served over an HTTP server.\n' +
  32. ' Opening index.html over file:// won\'t work.\n'
  33. ))
  34. })
  35. })

谢谢你的回答,
马特

83qze16e

83qze16e1#

有时候npm会给出一个旧的堆栈跟踪,用于实际存在的不同语法错误。
在我的情况下,探索我的编辑器中的语法错误,解决了问题。

wljmcqd8

wljmcqd82#

运行此命令:

  • npm run build**

相关问题