I'm trying to use the vue-introjs package in a Rails 5.1 app.
I've added the vue-introjs and intro.js packages through yarn.
I want to make this work using the Rails webpacker gem instead of using workarounds.
The vue-introjs documentation says the following is required in the webpack configuration:
// webpack.config.js
{
plugins: [
new webpack.ProvidePlugin({
// other modules
introJs: ['intro.js', 'introJs']
})
]
}
The Rails webpack setup doesn't have a webpack.config.js file by default, but it does have environment specific files, so I tried adding that config to /config/webpack/environment.js but that had no effect.
I then tried to adapt the sample found on this github issue which seems to be attempting a similar thing to what I want, but whilst I could get the page to load without any errors, I still get the error ReferenceError: introJs is not defined whenever I try to call myApp.$intro(), which should return an instance of the introJs module.
As a temporary measure while I couldn't get that working, I've also tried just putting this at the top of my webpack pack file where the Vue app is created:
import introJs from 'intro.js'
import VueIntro from 'vue-introjs'
But that still has the same error as above, that introJs is not defined.
Can anyone point me in the right direction for how to include the introJs module so the vue wrapper package for it will work correctly?
Here's how I would start with configuring webpacker given what you have installed so far:
// config/webpack/environment.js
const webpack = require('webpack');
const {environment} = require('#rails/webpacker');
environment.plugins.append(
'ProvidePlugin-IntroJS', // arbitrary name
new webpack.ProvidePlugin({
introJs: ['intro.js', 'introJs']
}),
);
module.exports = environment;
You'll need to restart your dev server when making changes to the webpack config. I would expect you will still need to import 'intro.js' somewhere in your dependency graph.
Related
I'm working with a Rails application, using Webpacker to bundle assets. I'm using a particular library I've installed via yarn whose code needs to be transpiled in my project. I'm trying to do this by modifying the paths that are ignored by Webpack/babel-loader within my config/webpack/environment.js file.
const { environment } = require('#rails/webpacker');
// Ignore all node_modules packages EXCEPT `a-random-third-party-package`:
babelLoader.exclude = /node_modules\/(?!(a-random-third-party-package))/;
module.exports = environment;
This is NOT working, though. For example, the JavaScript classes and static properties that exist in the third party package aren't transpiled at all in my bundle. But when I copy that same code into my own JS files, it's transpiled as expected.
How can I get this package to transpile like I want?
Solved! I had been using yarn link to work with this package separately and test it within this Rails application. For whatever reason, this was interfering with the build step, preventing it from being properly transpiled.
If you run into this yourself, verify that none of your dependencies are yarn link-ed, and if they are, run yarn unlink so you can locally test Webpacker without issues.
I am upgrading to Babel 7. They have changed how config files are loaded.
As much as I've tried per their docs I can not make it work. Babel config loading docs
An Electron app with two package.json structure is explained here:
Electron builder docs
The problem is it looks like my babel config is not being loaded for the code where the second package.json is located. (main renderer)
I tend to think that this is because of this new way of loading config and the two package.json but it could be something else.
The app structure looks like this:
/package.json
/.babelrc (or babel.config.js now)
/app/package.json
/app/main.js <-- this can not be compiled anymore
I use Webpack with the babel loader.
Somehow, with the Webpack dev server, it works, but compiling for production fails.
The exact error I get is this:
(function (exports, require, module, __filename, __dirname) {
import "core-js/modules/web.dom.iterable";
SyntaxError: Unexpected string
For which I got a clue here that made me think there is some config being ignored.
The entire project is open source and can be seen in Github.
Cloning and running npm run compile:main will show the error.
I am working on a Rails project that has been using assets pipeline but we are currently trying to transition to webpack. I encountered a problem when attempting to get TinyMCE to work after pulling it through yarn - the text editor simply won't load.
Before the transition to webpack
Originally I used a CDN in the application.html.haml and things were working fine:
%script{src: 'https://cloud.tinymce.com/stable/tinymce.min.js?apiKey=gexncjni90zx3qf0m5rr7kl8l40wd5yuly2xjza0g3kwrljt'}`
After the transition
I installed the package through yarn:
$ yarn add tinymce
I also have my tinyMce.js file (the function itself has not been changed):
import tinymce from 'tinymce/tinymce';
import 'tinymce/themes/modern/theme';
import 'tinymce/plugins/lists';
import 'tinymce/plugins/table';
function tinyMce() {
$(document).on('turbolinks:load', function () {
tinyMCE.remove();
tinyMCE.init({
selector: 'textarea.tinymce',
plugins: [
'table', 'lists'
],
});
});
}
export { tinyMce };
And in my application.js:
import { tinyMce } from "../vendor/tinyMCE/tinyMce";
Since TinyMCE will not work without a skin, I followed the documentation and ran
$ cp -r node_modules/tinymce/skins skins
But the error in the console does not get resolved:
I tried putting the skins folder directly in the root directory, in a packs folder placed in the root and in the javascript/packs but the error stays the same, even when I try specifying the skin_url.
General notes
Webpack itself is working fine, both with custom scripts and imported packs (tested with typed.js).
tinymce seems to be loading as well - previously I had more errors in the console, regarding the table and lists plugins, but these went away after adding the 2 import lines to tinyMce.js.
Any tips on what I might be missing?
I managed to get it to work by adding these 2 lines to my tinyMce.js file:
import 'tinymce/skins/lightgray/content.min.css';
import 'tinymce/skins/lightgray/skin.min.css';
At that point the text editor was working as expected, however I was getting the following errors in my console:
This was resolved by adding skin: false to my tinymce.initsetup.
First poster here.
I just created a new Rails project with webpack (rails new myapp --webpack). It comes with PostCSS and I wanted to add LostGrid to it. I ran the following, from the installation instructions from Lost:
npm install --save lost
This added Lost to package.json. The instructions then say to add the following to your webpack config, assuming you have postcss-loader installed & configured (which I understand was installed automatically when the project was created):
postcss: [
require('lost')
]
But I don't understand where the file is. I thought that I needed to add my PostCSS stuff to .postcssrc.yml in the root. I did this but I still don't get it to work.
I have very little experience with webpack and suspect that my approach is entirely flawed. I was excited to use PostCSS in Rails and would appreciate any help in getting to make LostGrid work here. Thanks!
Found your question moments after I ran into the same issue. Fortunately it's really simple!
When you run webpacker:install it installs a file called .postcssrc.yml in the root folder of the project. This is where you add thePostCSS plugins. Mine now looks like this:
plugins:
lost: {}
postcss-smart-import: {}
postcss-cssnext: {}
autoprefixer: {}
I am not sure yet whether the order matters. Still researching that.
See Webpacker issue #283 which is essentially the same question, but without an example provided.
I've been thinking about using React as the frontend for a Grails application, but I'm having a bit of trouble getting started.
So far, I've become accustomed to write a React app using Node/NPM with the help of Webpack, and that's been pretty easy because there is plenty of documentation for that setup.
However, I'm struggling to find anything concrete integrating React seamlessly with Grails.
Ideally, I would just do grails run-app and it should take care of everything. I do not want other team members to worry about starting up two different servers or something along those lines.
Please let me know if anyone has done this before.
Webpack can be configured to work quite well with Grails. The key is to have webpack generate its bundle whenever the app is started up, and to output the bundle in a directory where it can be served from the GSP. You do not want your source JavaScript (I.e, React/ES6 code) in the asset pipeline if your using Webpack, instead you want to keep those source files in another directory (such as src/webapp), and configure Webpack to bundle these files and output the result to the asset pipeline (assuming you're using AP at all).
Here's an example configuration for Webpack:
var path = require('path');
module.exports = {
entry: {
index: './src/webapp/index.js'
},
output: {
path: './grails-app/assets/javascripts',
publicPath: '/assets/',
filename: 'bundle.js'
},
Finally, to achieve the integrated webpack/Grails startup, you can use the Gradle node plugin and attach the webpack run script to the application startup in a custom task in your build.gradle (this is assuming that you have a npm script named "webpack" defined to run webpack)
assetCompile.dependsOn(['npmInstall', 'npm_run_webpack'])
Please note that if you want to run webpack in "watch" mode, you'll need to do that seperately from starting up the Grails app, so that that script can run continuously (there actually is support for this in the Gradle mode plugin but it's currently broken).
See this link for a more in-depth explanation of this approach, with a sample application: http://grailsblog.objectcomputing.com/posts/2016/05/28/using-react-with-grails.html
Also checkout the React profile for Grails 3: https://github.com/grails-profiles/react
It has not been released yet but should be in the next few days. It makes use of the same approach outlined here and in the linked post.
You could use npm's scripts feature to combine all steps necessary to start up the development environment into a single command, e.g.:
// package.json
{
...
"scripts": {
"start": "npm start-grails & npm start-react",
"start-grails": "grails run-app",
"start-react": "node server.js"
},
...
}
Now all it takes is a simple npm start to launch all relevant applications.