ruby-on-rails - Rails/Webpacker 未在生产中加载 Vue 样式

标签 ruby-on-rails vue.js webpack

我有一个 rails 应用程序,我已经升级到使用 webpacker 4.x 并且在开发中一切正常,但在生产中 Vue 单文件组件样式没有呈现,js 工作正常,甚至组件中的一些样式 ( vue-slider-组件 例如)正确显示。我之前使用过 webpacker 3 并且一切正常,但该应用程序的开发开始变得非常缓慢,并决定迁移到新的 webpacker。

一个重要的注意事项是我正在使用 sass (scss) 我的 vue 组件中的样式。

这是我的配置:

// config/webpack/environment.js
const { environment } = require('@rails/webpacker');
const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const vue = require('./loaders/vue');
const sass = require('./loaders/sass');

environment.config.resolve.symlinks = false;

environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin());
environment.loaders.append('vue', vue);
environment.loaders.append('sass', sass);
const nodeModulesLoader = environment.loaders.get('nodeModules');

nodeModulesLoader.exclude = [nodeModulesLoader.exclude, /mapbox-gl/];

environment.config.resolve.alias = {
  vue$: 'vue/dist/vue.esm.js',
  '@': path.join(__dirname, '..', '..', 'app', 'javascript'),
  '@stylesheets': path.join(__dirname, '..', '..', 'app', 'assets', 'stylesheets'),
  '@javascripts': path.join(__dirname, '..', '..', 'app', 'assets', 'javascripts'),
  '@node_modules': path.join(__dirname, '..', '..', 'node_modules')
};
module.exports = environment;


// config/webpack/production.js
process.env.NODE_ENV = process.env.NODE_ENV || 'production'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

// config/webpack/loaders/vue.js
module.exports = {
  test: /\.vue(\.erb)?$/,
  use: [
    {
      loader: 'vue-loader',
    },
  ],
};

不知道loaders/sass.js是原因

// config/webpack/loaders/sass.js
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

const { extract_css: extractCSS } = require('@rails/webpacker').config;

module.exports = {
  test: /\.(scss|sass|css)$/i,
  use: [extractCSS ? MiniCssExtractPlugin.loader : 'vue-style-loader', 'css-loader', 'sass-loader'],
};

# config/webpacker.yml

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: false

   # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  # Extract and emit a css file
  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .js
    - .vue
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default

  check_yarn_integrity: false

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: true
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: '**/node_modules/**'

test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: true

  # Extract and emit a css file
  extract_css: true


  # Cache manifest.json for performance
  cache_manifest: true

最佳答案

尝试包含 <%= stylesheet_pack_tag 'application', media: 'all' %>在您的根 HTML 文件上。

基于:https://github.com/rails/webpacker/issues/987

关于ruby-on-rails - Rails/Webpacker 未在生产中加载 Vue 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57328335/

相关文章:

ruby-on-rails - 单一职责原则——何时停止将代码提取到单独的类中

sql - 在 SQL 语句轨中组合 Ruby 变量来查找两个日期之间的天数差异

javascript - Vue 组件的 prop 未定义( fatal error )

go - 生产中的 vue-router(使用 Go 服务)

pdf - 服务器静态 pdf - Vue CLI - Webpack - 问题

ruby-on-rails - 尝试连接到 Actioncable 服务器时出现 websocket 错误代码 1006

javascript - VueJS 嵌套组件

javascript - 无法在nodejs中使用webpack开发服务器加载图像

webpack - ES6 和窗口/全局变量

ruby-on-rails - 处理模型中的 Stripe 错误