22

Is there any way to compile .vue file into .js file without webpack or browserify? I know the goodness of webpack or browserify but I just want the simplest way to compile the .vue file. For example, I have a single file component comp.vue complied into comp.js (the compiler should be able to compile sass and pug in .vue file) and then I can use it in my app like below:

<head>
    <script src="vue.min.js"></script>
    <script src="comp.js"></script> //it may pack the whole component into variable comp and add the style
    <script>
        Vue.component('comp', comp);
        window.onload = function() {
            new Vue({el: '#app'});
        }
    </script>
</head>
<body id='app'>
    <comp></comp>
</body>

or other similar/simpler way?

3
  • You can use gulp Commented May 4, 2017 at 10:31
  • Gulp+browserify+vueify? No simpler method to compile? Commented May 4, 2017 at 13:41
  • 2
    No. The simpliest way is not to use *.vue files Commented May 4, 2017 at 14:02

4 Answers 4

14

The vue-cli tool (version 2.8.0) has a build command that you can run to build an individual component.

vue build Component.vue --prod --lib

This will create an optimized bundle in UMD format, and the name of exported library is set to Component, you can use --lib [CustomLibraryName] to customize it.

You can take the built script and include it in your file like you are in your question. Technically it does use webpack under the hood, but you do not have to configure anything.

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

9 Comments

As of 2017 it seems they had the great idea to remove such command. So I guess it's back to gulp? Ugh...
@JacqueGoupil I believe the plans are to move it into a plugin for vue cli. It looks like they may be in the process of that. When I find out more I'll update the answer.
@JacqueGoupil The build tool is still available in v2.8.0. I suggest for now if you want to work with it you use that version. I updated the answer with the correct link.
In 2.9.*, they removed this command. Instead, they recommended to use poi (github.com/egoist/poi).
@MewX Yep, that's addressed in the comments above.
|
6

2021 answer: Use the vue build my-component.vue -t lib command. This can be installed with npm i -g @vue/cli-service-global

vue build will create javascript in a dist/ directory. Add my-component.umd.js to your page with a <script> tag.

At this point, my-component is available on the window object. Here is an example registration:

    <script src="https://unpkg.com/vue"></script>
    <script src="dist/my-component.umd.js"></script>
    <script>
    new Vue({
      components: {
        "my-component": window["my-component"]
      }
    }).$mount('#app')
    </script>

2 Comments

So do we need to build it many many times during development to see how it runs? If so, development returns into a nightmare.
For practical development I have turned to running the vue cli dev server and developing the components in isolation. I create a simple test harness app that includes the component in question at the root. This gives a hot-reload dev setup. When I'm done developing, I then build using vue build and integrate it into my site. The hardest thing was just getting all the HTTP headers for XSS and Cookies to work correctly. Since the dev server runs on a different host/port then the dev instance of my app. You're right, the build time sucks. I also tried rollout. It's terrible build times too.
2

(disclamer: author here)

vue3-sfc-loader (that works for Vue2 & Vue3) allows you to load a .vue file (including its dependancies) directly from your browser (without webpack or node.js).

vue3-sfc-loader supports template & style pre-processors (see examples)

Comments

0

I suggest to use this commands

npm install -g @vue/cli-service-global
npx @vue/cli build -t lib -d output input/app.vue

For more information type

npx @vue/cli build --help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.