Vue typescript import dependency

394 views Asked by At

Am attempting to add the rainbow-code node module to my Vue Application that utilizes TypeScript. This application was generated using vue-cli.

rainbow-code is a code syntax module that is not written in Typescript I am attempting to distribute this dependency through the main.ts. I could just copy the dependency and import the relative path in the html tag. But I wish to become more acquainted with importing modules in Typescript.

When importing my dependencies I get the following error in the chrome debugger

rainbow-node.js?23f9:5 Uncaught TypeError: fs.readdirSync is not a function
    at Object.eval (rainbow-node.js?23f9:5)
    at eval (rainbow-node.js:14)
    at Object../node_modules/rainbow-code/src/rainbow-node.js (chunk-vendors.js:2224)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at eval (main.ts:14)
    at Module../src/main.ts (app.js:1313)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at Object.1 (app.js:1374)

Here is my main.ts

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Rainbow from  'rainbow-code';
import 'rainbow-code/themes/css/github.css'
import 'rainbow-code/src/language/python.js'

Vue.config.productionTip = false

new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')

Additionally here is a snippet from the source code

/* eslint-disable */
var fs = require('fs');
global.Rainbow = require('../dist/rainbow.js');

var files = fs.readdirSync(__dirname + '/language');
for (var i = 0; i < files.length; i++) {
    require('./language/' + files[i]);
}

module.exports = global.Rainbow;
delete global.Rainbow;
/* eslint-enable */

Additionally, I thin I managed to import them succesfully? but then get an error as such

python.js?86d9:6 Uncaught ReferenceError: Rainbow is not defined
    at eval (python.js?86d9:6)
    at Object../node_modules/rainbow-code/src/language/python.js (chunk-vendors.js:1971)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at eval (cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/ContentBlockComponent.vue?vue&type=script&lang=ts&:14)
    at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/ts-loader/index.js?!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/ContentBlockComponent.vue?vue&type=script&lang=ts& (app.js:1007)
    at __webpack_require__ (app.js:854)
    at fn (app.js:151)
    at eval (ContentBlockComponent.vue?6714:1)
    at Module../src/components/ContentBlockComponent.vue?vue&type=script&lang=ts& (app.js:1194)

Here is the dependencies website https://github.com/ccampbell/rainbow#install-rainbow I am unsure of what other information would be useful in debugging.

Edit. This is the functionality I am trying to invoke in my vue components. But I cant seem to get access to this function in my vue component.

<script language='javascript' type='text/javascript'>
  window.onload=function(){ Rainbow.color(); };
  Rainbow.color();
</script>
1

There are 1 answers

1
Tim Wickstrom On

"By default babel-loader ignores all files inside node_modules" source

So knowing that I would try adding the package to transpileDependencies in vue.config.js

module.exports = {
  
  ...

  transpileDependencies: [
    'rainbow-code'
    // you may need to change the path/file name, Rainbow?
  ],

  ...

}