vue-codemirror icon indicating copy to clipboard operation
vue-codemirror copied to clipboard

Syntax highlighting not working with webpack v4

Open dflock opened this issue 6 years ago • 10 comments

BUG REPORT TEMPLATE

I think this issue is affecting vue-codemirror: https://github.com/codemirror/CodeMirror/issues/5484

Vue.js version and component version

VueJs: 2.5.22 vue-codemirror: 4.0.6

Reproduction Link

As far as I can tell, this is a webpack related issue, so it would probably work fine on jsbin or other non-webpack environments.

Steps to reproduce

Create a new project using Vue cli v3 - i.e. using webpack v4. Add vue-codemirror.

What is Expected?

The codemirror instance should load its javascript & theme files, tokenize the input and apply highlighting.

What is actually happening?

It doesn't tokenize input and therefore doesn't apply syntax highlighting.

The fix

The fix from here appears to work. If you change this:

import _CodeMirror from 'codemirror'

to this:

import _CodeMirror from 'codemirror/lib/codemirror.js'

in both codemirror.vue & index.js, that seems to fix things. I'm not sure if this is the "correct" way to fix this, so I've just made the change locally for now.

If you modify codemirror.vue and/or index.js then you will need to pull these into your project - i.e. don't use the upstream npm package (from your node_modules) anymore, use your locally modified version.

So, copy the those two files out of your node_modules/vue-codemirror/ folder, into a folder in your project, say src/components/vue-codemirror/, modify them, then change your import line from this:

import VueCodemirror from 'vue-codemirror'
import 'codemirror/lib/codemirror.css'

to this:

import { codemirror } from '@/components/vue-codemirror'
import 'codemirror/lib/codemirror.css'

dflock avatar Feb 01 '19 22:02 dflock

Hello, I have encounted the problem, too. But it doesn't work by using the methods you provide ( webpack4.22.0 ).

newxun avatar Feb 21 '19 06:02 newxun

If you modify codemirror.vue and/or index.js then you will need to pull these into your project - i.e. don't use the upstream npm package (from your node_modules) anymore, use your locally modified version.

So, copy the those two files out of your node_modules/vue-codemirror/ folder, into a folder in your project, say src/components/vue-codemirror/, modify them, then change your import line from this:

import VueCodemirror from 'vue-codemirror'
import 'codemirror/lib/codemirror.css'

to this:

import { codemirror } from '@/components/vue-codemirror'
import 'codemirror/lib/codemirror.css'

dflock avatar Feb 22 '19 20:02 dflock

Also experiencing the same, anyone managed to solve this?

Cannonb4ll avatar Feb 26 '19 11:02 Cannonb4ll

I found simpler solution, without custom component. Just import "mode" file after codemirror import. For example, for javascript:

import { codemirror } from 'vue-codemirror'
import 'codemirror/mode/javascript/javascript'

But custom component is needed for using inside Electron.

Chieftl avatar Mar 27 '19 16:03 Chieftl

If using Electron-Vue, add 'vue-codemirror' to the array whiteListedModules inside .electron-vue/webpack.renderer.config.js

ROTGP avatar Oct 25 '19 07:10 ROTGP

If using Electron-Vue, add 'vue-codemirror' to the array whiteListedModules inside .electron-vue/webpack.renderer.config.js

adding below to package.json fixed it. also check white-listing-externals


  "electronWebpack": {
    "whiteListedModules": [
      "vue-codemirror"
    ]
  }

ahmedash95 avatar Jan 19 '20 19:01 ahmedash95

I tried the above methods, and finally found that all I need to do is: use: import VueCodemirror from 'vue-codemirror/src/index' replace: import VueCodemirror from 'vue-codemirror' Others,just follow the README。

followbin avatar May 25 '20 03:05 followbin

For me , I use : import { codemirror } from 'vue-codemirror/src/index' replace: import { codemirror } from 'vue-codemirror' fix the issue.

ijustyce avatar Jun 17 '20 06:06 ijustyce

Thanks, @ROTGP, it worked!

vue: 2.6.11 vue-codemirror: 4.0.6 webpack v4

HarmlessEvil avatar Apr 01 '21 08:04 HarmlessEvil

if anyone is using nuxt, try to add it to the config file

nuxt.config.js

css: [
  'codemirror/lib/codemirror.css',
  'codemirror/theme/elegant.css',
  'codemirror/theme/base16-dark.css',
],

jajosheni avatar Apr 11 '22 14:04 jajosheni