Giter VIP home page Giter VIP logo

vue-browser-sfc's Introduction

Vue Browser SFC

Template for build setup to compile Single File Components (.vue) into a standalone JS file for use in the browser

This template is useful for compiling VueJS single file components (SFC) into standalone JS files for use in the browser. This is useful for devs that want to create a simple component that can be used on a site without having to build an entire app around it, similar to the way a JQuery plugin might be used. The template, script, and styles are all compiled to a single JS file.

The compiler is setup to allow you to use either LESS or SASS (scss or sass) if you want to.

Usage

This is a project template for vue-cli.

1. Create a new Vue project

At the command line, enter the following commands in parent folder of where you will be keeping your project.

# Install vue-cli if you haven't already
$ npm install -g vue-cli

# Create a new project based on this template
$ vue init ronniesan/vue-browser-sfc my-project

# Navigate into your new project folder
$ cd my-project

# Install dependencies
$ npm install

2. Create and compile a component

Create a .vue file anywhere in the src folder of the project you just created.

Then, run the webpack compiler and point the --env.file argument to the .vue file you created (you don't need to add the extension). The path should be relative to the src folder, so if you created your .vue file in the src folder, the path would just be the name of the file. For example, if the file is located in src/sub-folder/my-component.vue, the path you would enter for the --env.file argument would be sub-folder/my-component.

$ webpack --env.file=path/to/my-component

The webpack compiler runs in watch mode so any changes you make will update the compiled file. Whenever you make changes to you component, as long as the webpack compiler is running, the changes will be re-compiled and saved over the same output file. The compiled output file will be created at the same relative path in the dist folder that it was located in the src folder. So if you create a component at src/some-sub-folder/my-rad-component.vue, the compiled file will be located in dist/some-sub-folder/my-rad-component.js.

Once you've completed the development of your component and want to use it, you can take the compiled .js file and place it wherever you want.

Make sure you add a name property to the script section of the component. This is what will be used as the tag for your component when you use it in your app/HTML file.

Giving your component a name property of my-component means you will add your component to the app as <my-component></my-component>.

3. Use the compiled JS file

You will still need to include VueJS on your HTML page and create a root VueJS app in order to use your component.

Add a reference to the compiled component file (.js) in a script tag. Make sure it is added AFTER you add VueJS to the page.

You can then include the component on your page using the name property you set as the tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>VueJS Component Test</title>
    <script src="https://unpkg.com/vue"></script>
    <script src="my-component.js"></script>
</head>
<body>
    <div id="wrapper">
      <my-component></my-component>
    </div>
    <script>
    var app = new Vue({
      el : '#wrapper'
    });
    </script>
</body>
</html>

Included Example

The template package includes a sample component (example.vue) that you can test it all out with. The command you would use to compile it would be webpack --env.file=example. The component has a name property of example, the the tagname you would use in your HTML woudl be <example></example>.

The example component will print out the words "Hello, world." When the text is clicked, the word "world" will change to "foobar".

Converting an existing Vue Single File Component to a standalone JS file

To convert an existing Vue Single File Component to a standalone JS file, simple add it to the src folder along with all the required dependencies (make sure all the paths are correct for the dependencies). Then run the compiler (webpack command) with the --env.file argument pointing to the component. If anything goes wrong, the compiler will tell you what errors occured.

IMPORTANT!!!

  • Make sure you give your component a name property. It will be used as the tagname for your component.

Other Notes

  • Excluding styles can greatly decrease the size of your compiled JS file as it doesn't need to add logic to inject the styles on the page

vue-browser-sfc's People

Contributors

dependabot[bot] avatar ronniesan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-browser-sfc's Issues

IE11 support for compiled JS file

Most of the time I use vue-browser-sfc when I include Vue in very basic/simple websites. Some of those websites need to be compatible with IE 11. Since IE 11 is not supporting arrow functions etc. it would be neat if vue-browser-sfc would make the compiled JS files IE11 ready out of the box. At the moment I always have to manually correct the not supported syntax after I compiled the file.

watch and compile all *.vue files

Love this project.
Exactly what I was looking for to reuse components in a heterogeneous frontend-situation.

Is it possible to not only watch and compile 1 file at a time but to make webpack watch all files in the src directory and compile if a change occurs?

Best

In the readme "Create a new Vue project" should clearer

It is not clear to the noob that this section is going to use a Hello, world (user clicks that and it becomes) hello, foobar example. Henceforce: HW->HFb.

That's a great example of the tech, but it is unclear that this is an example. "based on this template" (inline) is not enough because it is unclear that "this" is the HW->HFb component.

webpack build slow

when i build vue file use webpack --env.file=example.vue in windows, the build progress is very slow,block, command return message:
[4] ./node_modules/vue-style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"optionsId":"0","vue":true,"id":"data-v-3835be54","scoped":true,"sourceMap":false}!./node_modules/less-loader/dist/cjs.js!./node_mod
ules/vue-loader/lib/selector.js?type=styles&index=0!./src/example.vue 1.7 KiB {0} [built]
[] 85ms -> [] 711ms -> factory:383ms building:7ms = 1186ms

then, halt. i must ctrl+c to terminal the build command.

fix security and compatibility issues

there where some security issues with old packages, to solve these, breaking changes where needed.
The only file that had comflicting depencies was one that didn't seem to be used (it got imported but never configured)

changing the depencies to these made everything work again for me:

  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.2.5",
    "autoprefixer": "^8.6.5",
    "babel-loader": "^8.0.6",
    "css-loader": "^2.1.1",
    "file-loader": "^1.1.11",
    "friendly-errors-webpack-plugin": "^1.7.0",
    "less": "^3.9.0",
    "less-loader": "^4.1.0",
    "node-sass": "^4.12.0",
    "optimize-css-assets-webpack-plugin": "^5.0.1",
    "sass-loader": "^7.1.0",
    "string-replace-webpack-plugin": "^0.1.3",
    "url-loader": "^1.1.2",
    "vue-loader": "^15.7.0",
    "vue-style-loader": "^4.1.2",
    "vue-template-compiler": "^2.6.10",
    "webpack": "^4.0.0"
  },
  "dependencies": {
    "core-js": "^2.6.9",
    "vue": "^2.6.10"
  },

Section in the readme needed: What you would have to do to an existing .vue component to make it a RonnieSan/browser-component

For example, https://github.com/showonne/vue-date is an example of a reusable .vue component. There's only a few changes that would need to be made to that project/repo for it be able to make the RonnieSan/browser-component .js version of the same ready for use in the way you describe. That's for the change the source code route.

There's also a mode of operation for RonnieSan/browser-component that could point at the published versioned component too, from https://github.com/showonne/vue-date/releases:

https://github.com/showonne/vue-date/archive/v1.1.7.zip ~ vue-date-1.1.7/src/Datepicker.vue

Thnking about names

"Single File Components" is the official name for the .vue component system, but it is not very googlable. Fixing that is a different conversation.

I'm wondering if a different name for this is due though:

How about a repo rename to:

  • RonnieSan/vuecompose.js
  • RonnieSan/DotVueToJs
  • RonnieSan/.vue.js
  • RonnieSan/.vue2js

And for the gen'd file? How about a file suffix:

  • my-component.vue.js

(.vue)

dist/my-component.js

Relative components are handled differently

--env.file=../../src/Foo.vue leaves the implict src/ folder behind and traverses somewhere else in the file system. Interestingly that then leaves the Foo.js file adjacent to the original Foo.vue file. Normally without the ../.. traversal to somewhere else, this tech leaves the .js result in dist/

I'm not against the way it works by accident, it's just that I didn't expect that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.