1

How can I include and initialize a javascript file mymain.min.jsthat is in my assets folder within my project using VueJs with webpack?

Webpack.base.conf

resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      'sauJs': path.resolve(__dirname, '../src/assets/mymain.min.js'),
    }
  },

I have also tried adding <script src="src/assets/mymain.min.js"></script> to my index.html page however I get Cannot GET /src/assets/mymain.min.js

7
  • So there is no option to pull your external file from NPM, pull it in locally, use it as a script tag before your main file or weave it into your code(if it is small)? Webpack is not meant to pull in external files I am sure you can abuse it to do so, but I am also sure there probably is a better way. Commented Jul 27, 2017 at 7:54
  • I have tried to add this to index.html but am getting Cannot GET /src/assets/mymain.min.js Commented Jul 27, 2017 at 8:05
  • Which one of the 4 methods I just described have you tried? Commented Jul 27, 2017 at 8:06
  • 1. Its not an npm package but just a js file in my assets folder. 2.pull it in locally? 3 I Used it as a script tag in index.html but am getting "cannot get" 4. Its too large to weave it in. Commented Jul 27, 2017 at 8:09
  • Updated question. It is not an npm but a js file that is in my assets folder inside my project Commented Jul 27, 2017 at 8:11

1 Answer 1

4

You are misunderstand what resolve actually does. This is not where you put the Javascript code that would actually bundle together.

When you specify a resolve alias like so:

'@': resolve('src'),

It will make sure that when you use:

require('@/test')

It will get resolved to:

require('src/test')

This is helpful to prevent relatives paths all over the place and you can also alias paths that are longer to prevent retyping them all the type.

Your webpack file should have an entry property that specifies your main uncompiled javascript file.

module.exports = {
    entry: './example/main.js',
}

In your main.js file you can import the javascript file that you want to include in your bundle.

main.js

import Vue from 'vue';
// Or:
const vue = require('vue')
// Or(.js extension can be omitted):
import MyFile from './src/MyFile.js'

The other approach is to specify another entry property, something I have not done personally.

This is way out of the scope of this answer though. If you want more information you should check out the documentation:

https://webpack.js.org/concepts/

Alternatively this could make your life easier if you don't want to deal with the intricacies:

https://symfony.com/blog/introducing-webpack-encore-for-asset-management

Or:

https://github.com/JeffreyWay/laravel-mix

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used const SauJs = require('./assets/main') and Vue.use(SauJs) in my main.js file and added the file in .eslintignore

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.