Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpacker + Vue compiled failed #2162

Closed
Jaroost opened this issue Jul 3, 2019 · 13 comments
Closed

Webpacker + Vue compiled failed #2162

Jaroost opened this issue Jul 3, 2019 · 13 comments

Comments

@Jaroost
Copy link

Jaroost commented Jul 3, 2019

I tried to create a new Rails app using Webpacker and Vue.

Info:

  • Rails 5.2.3
  • Ruby 2.5.5p157 x64
  • OS: Windows Server 2016

Here what I have done:

rails new myapp --webpack=vue -d mysql

While installing package with yarn I have peer dependency warnings:

warning " > vue-loader@15.7.0" has unmet peer dependency "css-loader@*".
warning " > vue-loader@15.7.0" has unmet peer dependency "webpack@^4.1.0 || ^5.0.0-0".

After that my package.json file looks like this:

{
  "name": "myapp",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "^4.0.7",
    "vue": "^2.6.10",
    "vue-loader": "^15.7.0",
    "vue-template-compiler": "^2.6.10"
  }
}

When I use command rails webpacker:compile it works perfectly and compile :

Compiling…
Compiled all packs in D:/myapp/public/packs

So far so good but when I try to add peer dependency in package.json things go wrong:
yarn add webpack@^4.35.2
yarn add css-loader

Here, peer dependencies warnings are gone.

Now package.json looks like this:

{
  "name": "myapp",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "^4.0.7",
    "css-loader": "^3.0.0",
    "vue": "^2.6.10",
    "vue-loader": "^15.7.0",
    "vue-template-compiler": "^2.6.10",
    "webpack": "4.35.2"
  }
}

And when I try to run the rails webpacker:compile it leave me with this (blank) error :

Compiling…
Compilation failed:


if I use the command ruby .\bin\webpack-dev-server I got the following error (It's normal I don't have webpack-dev-server in dev dependency of package.json. Is it normal that this package is not added by default?)

yarn run v1.16.0
error Command "webpack-dev-server" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So I add webpack-dev-server to package.json:
yarn add webpack-dev-server --dev

So now I can run the ruby .\bin\webpack-dev-server and I got this following error:

ERROR in ./app/javascript/app.vue?vue&type=style&index=0&id=6fb8108a&scoped=true&lang=css& (./node_modules/css-loader/dist/cjs.js??ref--3-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--3-2!./node_modules/vue-loader/lib??vue-loader-options!./app/javascript/app.vue?vue&type=style&index=0&id=6fb8108a&scoped=true&lang=css&)
Module build failed (from ./node_modules/css-loader/dist/cjs.js):
ValidationError: CSS Loader Invalid Options

options should NOT have additional properties

    at validateOptions (D:\myapp\node_modules\schema-utils\src\validateOptions.js:32:11)
    at Object.loader (D:\myapp\node_modules\css-loader\dist\index.js:34:28)
i 「wdm」: Failed to compile.

Is it bad to add peer dependencies to my project?

@jakeNiemiec
Copy link
Member

Are your files still stock? If not: in order to help debug, could you please post as much as you can of:

  • ./package.json
  • ./babel.config.js
  • ./config/webpack/environment.js
  • ./config/webpacker.yml
  • ./app/javascript/packs/application.js
  • the full error message (even the parts you wouldn’t think is relevant)

—to a single https://gist.github.com. Please tag me via @jakeNiemiec in a comment and I’ll try to troubleshoot any problems directly in the gist.

@Jaroost
Copy link
Author

Jaroost commented Jul 4, 2019

@jakeNiemiec All files are stock except package.json. Here the gist with all files and error messages

@ytbryan
Copy link
Contributor

ytbryan commented Jul 4, 2019

Is it normal that this package is not added by default? nope, not normal.

What's your output for rails webpacker:info?

Update:
My webpacker + Vuejs setup is working. Here's my output on MacOS tho. I got a feeling you need the binstubs update.

Ruby: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
Rails: 5.2.3
Webpacker: 4.0.7
Node: v12.4.0
Yarn: 1.17.0

@rails/webpacker: 
app@ /Users/ytbryan/Desktop/app
└── @rails/webpacker@4.0.7 

Is bin/webpack present?: true
Is bin/webpack-dev-server present?: true
Is bin/yarn present?: true

@Jaroost
Copy link
Author

Jaroost commented Jul 4, 2019

@ytbryan

D:\myapp>rails webpacker:info
Ruby: ruby 2.5.5p157 (2019-03-15 revision 67260) [x64-mingw32]
Rails: 5.2.3
Webpacker: 4.0.7
Node: v12.5.0
Yarn: 1.16.0

@rails/webpacker:
myapp@ D:\myapp
`-- @rails/webpacker@4.0.7

Is bin/webpack present?: true
Is bin/webpack-dev-server present?: true
Is bin/yarn present?: true

D:\myapp>

@zedtux
Copy link

zedtux commented Jul 5, 2019

I'm facing the same issue as @Jaroost since I tried to solve the dependencies warning from yarn.

@zedtux
Copy link

zedtux commented Jul 5, 2019

As mentioned in this comment, the localIdentName option was removed in favor modules.localIdentName option, and looking in this repo I found that the old way is being still used.


Update 1

Looking at the node_modules/css-loader/dist/options.json file confirmed the option position change.


Update 2

I changed my config/webpack/environment.js file like so :

const { environment } = require('@rails/webpacker')
const MomentLocalesPlugin = require('moment-locales-webpack-plugin')

/*
** Plugins
*/
environment.plugins.prepend('MomentLocalesPlugin', new MomentLocalesPlugin({
  localesToKeep: ['es-us']
}))

const cssLoader = environment.loaders.get('css')
var loaderLength = cssLoader.use.length
for (var i = 0; i < loaderLength; i++) {
  var loader = cssLoader.use[i]
  if (loader.loader === 'css-loader') {
    // Copy localIdentName into modules
    loader.options.modules = {
      localIdentName: loader.options.localIdentName
    }
    // Delete localIdentName
    delete loader.options.localIdentName
  }
}

module.exports = environment

Before this change, I had the following errors :

webpack_1    | ERROR in ./node_modules/typeface-grand-hotel/index.css (./node_modules/css-loader/dist/cjs.js??ref--6-1!./node_modules/postcss-loader/src??ref--6-2!./node_modules/typeface-grand-hotel/index.css)
webpack_1    | Module build failed (from ./node_modules/css-loader/dist/cjs.js):
webpack_1    | ValidationError: CSS Loader Invalid Options
webpack_1    |
webpack_1    | options should NOT have additional properties
webpack_1    |
webpack_1    |     at validateOptions (/application/node_modules/schema-utils/src/validateOptions.js:32:11)
webpack_1    |     at Object.loader (/application/node_modules/css-loader/dist/index.js:34:28)
webpack_1    |
webpack_1    | ERROR in ./app/javascript/src/stylesheets/application.scss (./node_modules/css-loader/dist/cjs.js??ref--7-1!./node_modules/postcss-loader/src??ref--7-2!./node_modules/sass-loader/lib/loader.js??ref--7-3!./app/javascript/src/stylesheets/application.scss)
webpack_1    | Module build failed (from ./node_modules/css-loader/dist/cjs.js):
webpack_1    | ValidationError: CSS Loader Invalid Options
webpack_1    |
webpack_1    | options should NOT have additional properties
webpack_1    |
webpack_1    |     at validateOptions (/application/node_modules/schema-utils/src/validateOptions.js:32:11)
webpack_1    |     at Object.loader (/application/node_modules/css-loader/dist/index.js:34:28)

With this change I have this :

webpack_1    | ERROR in ./app/javascript/src/stylesheets/application.scss (./node_modules/css-loader/dist/cjs.js??ref--7-1!./node_modules/postcss-loader/src??ref--7-2!./node_modules/sass-loader/lib/loader.js??ref--7-3!./app/javascript/src/stylesheets/application.scss)
webpack_1    | Module build failed (from ./node_modules/css-loader/dist/cjs.js):
webpack_1    | ValidationError: CSS Loader Invalid Options
webpack_1    |
webpack_1    | options should NOT have additional properties
webpack_1    |
webpack_1    |     at validateOptions (/application/node_modules/schema-utils/src/validateOptions.js:32:11)
webpack_1    |     at Object.loader (/application/node_modules/css-loader/dist/index.js:34:28)

So there is an error less. The remaining one is in my project but I can't figure out what's going wrong for now.


Update 3

Of course I had to do the same for the sass loader so here is my final code that solve this issue :

const { environment } = require('@rails/webpacker')
const MomentLocalesPlugin = require('moment-locales-webpack-plugin')

/*
** Plugins
*/
environment.plugins.prepend('MomentLocalesPlugin', new MomentLocalesPlugin({
  localesToKeep: ['es-us']
}))

/*
** CSS loader fixing issue, See https://github.com/rails/webpacker/issues/2162
*/
const cssLoader = environment.loaders.get('css')
var cssLoaderLength = cssLoader.use.length
for (var i = 0; i < cssLoaderLength; i++) {
  var loader = cssLoader.use[i]
  if (loader.loader === 'css-loader') {
    // Copy localIdentName into modules
    loader.options.modules = {
      localIdentName: loader.options.localIdentName
    }
    // Delete localIdentName
    delete loader.options.localIdentName
  }
}

const sassLoader = environment.loaders.get('sass')
var sassLoaderLength = sassLoader.use.length
for (var j = 0; j < sassLoaderLength; j++) {
  var loader = sassLoader.use[j]
  if (loader.loader === 'css-loader') {
    // Copy localIdentName into modules
    loader.options.modules = {
      localIdentName: loader.options.localIdentName
    }
    // Delete localIdentName
    delete loader.options.localIdentName
  }
}

module.exports = environment

@jakeNiemiec
Copy link
Member

options should NOT have additional properties

@zedtux, I have seen this popping up as a problem. Any idea what causes this?

@Jaroost Node: v12.5.0

image

I would not use the "prerelease" version of node (current), I would instead use the "active version". I have seen problems with node@12 & node-sass.

@zedtux
Copy link

zedtux commented Jul 5, 2019

I have seen this popping up as a problem. Any idea what causes this?

@jakeNiemiec the v3 of css-loader has some breaking changes including the move of the localIdentName option, so that the node_modules/css-loader/dist/options.json files doesn't validate the passing options to css-loader.

@Jaroost
Copy link
Author

Jaroost commented Jul 8, 2019

@jakeNiemiec I have node 12.5.0, I tried node 10.16.0 with the same issue

the @zedtux solution works I updated the gist with the new environment.js (environment-fix.js)

Update 1

I have now very strange behavior, in app.vue when I use class style this style is ignored:

<template>
  <div>
    <p class="test" >{{ message }}</p>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      message: "Hello Vue!"
    }
  }
}
</script>
<!--with or without scoped, class style doesn't work-->
<style>
  /*works*/
p {
  font-size: 2em;
  text-align: center;
  color: red
}


  /*doesn't work*/
  /*.test{
  font-size: 2em;
  text-align: center;
  color: yellow
  }*/

</style>

I don't know if this issue is related to webpacker.

@zedtux
Copy link

zedtux commented Jul 8, 2019

I had to revert all my packages updates as bootstrap wasn't loading like before (I had only the reset css style, and some classes from bootswatch, but all the buttons, list and so on styling wasn't loading).

@jakeNiemiec
Copy link
Member

I have node 12.5.0, I tried node 10.16.0 with the same issue

12 will give you issues unrelated webpacker. It all depends on if the tools you use are updated for 12.x.x, examples:


Here is how we are consuming webpack 4:

//scss
@import '~bootstrap/scss/bootstrap';
//js
import Popper from 'popper.js/dist/esm/popper';

//MAKE SURE window.$ exists before this point, import jQuery how you normally would

window.Popper = Popper;
require('bootstrap');

Apart from that, I cannot be of much help with the vue stuff.

@Jaroost
Copy link
Author

Jaroost commented Jul 9, 2019

Ok I finally get this running by updating environment.js according to the documentation like so:


const { environment } = require('@rails/webpacker')
const { VueLoaderPlugin } = require('vue-loader')
const vue = require('./loaders/vue')

environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin())
environment.loaders.prepend('vue', vue)

/*
** CSS loader fixing issue, See https://github.com/rails/webpacker/issues/2162
*/
const cssLoader = environment.loaders.get('css')
cssLoader.use=[{'loader':'vue-style-loader'}, {'loader': 'css-loader'}];

module.exports = environment

default configuration is missing vue-style-loader in the css loaders

@guillaumebriday
Copy link
Member

Can this issue be closed ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants