css样式在开发环境和分发环境中不一样

时间:2018-05-14 02:34:20

标签: webpack vue.js

我的Vue.js项目,css风格在开发环境和分发环境中并不相同。

这是开发环境的影响:

enter image description here

这是分发环境效果:

enter image description here

为什么他们之间存在这种差异?

这是我的webpack.prod.conf代码,我不确定是否是因为我的webpack配置:

'use strict'
const path = require('path')
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const GenerateAssetPlugin = require('generate-asset-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')


const env = require('../config/prod.env')

const createServerConfig = function(compilation){
  let cfgJson={ApiUrl:"http://103.20.12.76:8000"};
  return JSON.stringify(cfgJson);
}

const webpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: true,
      usePostCSS: true
    })
  },
  devtool: config.build.productionSourceMap ? config.build.devtool : false,
  output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[chunkhash].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  },
  plugins: [
    // http://vuejs.github.io/vue-loader/en/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    // new UglifyJsPlugin({
    //   uglifyOptions: {
    //     compress: {
    //       warnings: false
    //     }
    //   },
    //   sourceMap: config.build.productionSourceMap,
    //   parallel: true
    // }),
    new UglifyJsPlugin({
      sourceMap: true,
      uglifyOptions: {
        ecma:8,
        compress: {
          warnings: false
        }
      }
    }),
    // extract css into its own file
    new ExtractTextPlugin({
      filename: utils.assetsPath('css/[name].[contenthash].css'),
      // Setting the following option to `false` will not extract CSS from codesplit chunks.
      // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
      // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`, 
      // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
      allChunks: true,
    }),
    // Compress extracted CSS. We are using this plugin so that possible
    // duplicated CSS from different components can be deduped.
    new OptimizeCSSPlugin({
      cssProcessorOptions: config.build.productionSourceMap
        ? { safe: true, map: { inline: false } }
        : { safe: true }
    }),
    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // see https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: config.build.index,
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: false
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    // keep module.id stable when vendor modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),
    // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks (module) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      minChunks: Infinity
    }),
    // This instance extracts shared chunks from code splitted chunks and bundles them
    // in a separate chunk, similar to the vendor chunk
    // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
    new webpack.optimize.CommonsChunkPlugin({
      name: 'app',
      async: 'vendor-async',
      children: true,
      minChunks: 3
    }),

    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: config.build.assetsSubDirectory,
        ignore: ['.*']
      }
    ]),

//          from:path.join(__dirname, "static", "js"),
        filename: 'static/js/server_config.json',
        fn: (compilation, cb) => {
            cb(null, createServerConfig(compilation));
        },
        extraFiles: []
    }),

//          from:path.join(__dirname, "static", "js"),
        filename: 'static/js/server_config.json',
        fn: (compilation, cb) => {
            cb(null, createServerConfig(compilation));
        },
        extraFiles: []
    })
  ]
})

if (config.build.productionGzip) {
  const CompressionWebpackPlugin = require('compression-webpack-plugin')

  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        config.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

if (config.build.bundleAnalyzerReport) {
  const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}

module.exports = webpackConfig

修改-1

我发现了这个细节:

在我的分发页面中,我找到width: 25%;

enter image description here

如果我取消选中它,样式将变为正常。

enter image description here

但是,如果我在本地开发环境中,我发现如果不取消选中width: 25%,仍然可以正常工作。所以这可能与ivu-col-6无关。

修改-2

我将computed面板放在那里:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为这是CSS定义的顺序问题。开发和生产CSS定义顺序可能不同。参考链接:https://vue-loader.vuejs.org/migrating.html#style-injection

您需要利用CSS特异性来解决问题。

例如

// dmeo.html
<span class='Color'>Colored Text</span>

// demo.css
.Color {
   color: blue;
}

.Color {
   color: red;
}

以上定义的结果为color: red

您可以通过提高第一条规则的特异性来改变结果。

// dmeo.html
<span class='Color'>Colored Text</span>

// demo.css
span.Color {
   color: blue;
}

.Color {
   color: red;
}

您可以使用https://specificity.keegan.st/检查CSS特异性。

答案 1 :(得分:0)

  1. 检查您的组件中是否有&#39; ;with cte as ( select id,column1,column2 ,row_number() over (partition by column1 order by id) rn from #temp ) update c set c.column1='' from cte c where rn>1 标签,您添加了<style>

  2. 如果你使用像scoped这样的第3个UI库,请检查element-ui导入是否在第3个UI lib之后,订单是否在乎。

    App
  3. 3.在import 'element-ui/lib/theme-default/index.css'; ... import App from './App'; 中,检查webpack.prod.conf.js是否有cssProcessorOptions

    safe: true

    GitHub中的详细信息。