Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Standardized, designer-friendly <script> behavior for your Single Page App

NotificationsYou must be signed in to change notification settings

taoeffect/vue-script2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VueScript2 brings back the<script> tag to your SPA (Single Page App)!

This tiny library should take care of all yourdeclarative andimperative asynchronous loading needs. Web designers can rest easy knowing their existing knowledge of web development is still useful!

This version is tailored for theVue.js framework, but it's easy to port toRiot.js and others.

VueScript2 is primarily for internal use and not for standalone components that are being shared publicly. Those should be "self-contained" and come with all the JS they need.

NOTE:Requires Vue 2.x. Use version 1.2.2 for Vue 1.x.

Features

  • Just like<script> except with a2, buteven that can be fixed!
  • Keep yourapp.js bundle small!
  • Embrace Web Standards™ everyone knows and loves!
  • Easy for web designers to pick up! If you know HTML, you already know how to use it!
  • Tiny! Less than1kb! (min+gzip'd)
  • Perfect for use in.ejs templates or.html files!
  • No more including every library on every page or complicated "code splitting"!
  • Ordered execution based on position in markup!
  • Specialunload attribute can be used to keep your app's memory usage low!
  • Doesimperative loading too!

Installation

npm install vue-script2 --save

Then enable the plugin (in yourmain.js or wherever):

Vue.use(require('vue-script2'))

Or withimport:

importVuefrom'vue'importVS2from'vue-script2'Vue.use(VS2)

Usage

Note: you can write<script>instead of<script2>usingscript2ify! 😄

Familiar, declarative, asynchronous loading of scripts

Usingvue-script2 withvue-router is simple. Say that only one of your routes displays a "page" that makes use of jQuery. Well, no need to include all of jQuery in yourapp.js bundle, now you can throw this in:

<script2src="/path/to/jquery.min.js"></script2>

Boom!

And don't worry,script2 won't re-download scripts if they're already loaded.

Promise-based imperative loading too!

Imperatively load scripts withVueScript2.load:

VueScript2.load('/path/to/jquery.min.js').then(function(){$('#msg').text('Hello from VueScript2!')})

For 99% of use-cases this is totally sufficient and you do not need the overhead of RequireJS or SystemJS or whatever else. That's especially true given that Vue.js is normally used with Browserify or Webpack, which handle complicated dependency management for you.

NOTE: scripts injected usingVueScript2.load are alwaysasync.

Delayed execution of inlined JavaScript

Want to run some JavaScript only when a specific "page"/route is visited and only after a library has finished loading? Simple!

<script2src="/path/to/jquery.min.js"></script2><script2>// Ordered execution should ensure that '$' is available here$(document).ready(function () {  // this code runs *only* when the route  // that contains this code is loaded! :D->-<})</script2>
Cleanup unused resources with theunload attribute

Theunload attribute accepts JS expressions to run when the component is destroyed. This prevents your SPA from accumulating stuff in memory that's not being used:

<script2src="/path/to/jquery.min.js"unload="jQuery.noConflict(true)"></script2>
Special support forasync attribute

Although technically all scripts are inserted withs.async = false (since we're usingdocument.write, seethis wonderful article byJake Archibald for details), setting theasync attribute does make a meaningful difference.

By default, the loading of<script2> tags is serialized using promises so that one script loads after another has finished. If you don't care about the loading order, addasync to have the script injected into the page immediately.

You can mix and match so that some<script2> tags are loaded immediately while others wait for the ones before them:

<script2src="jquery.min.js"></script2><script2>$('#foo').text('hi!')</script2><!-- Load next script immediately, don't wait for jQuery --><script2src="lib.js"async></script2>

Writing<script> instead of<script2> usingscript2ify

Thescript2ifybrowserify transform below will (fairly safely) dynamically replace<script> tags with<script2> tags within.ejs,.html, and even.vue files!

varthrough=require('through2')// This will replace <script> with <script2> in .html, .vue and .ejs files// EXCEPT:// - within <!-- comments -->// - top-level <script> tags within .vue files// Additional exclusion per: http://www.rexegg.com/regex-best-trick.html// Excluding <pre> tags did not seem to work, however.functionscript2ify(file){return!/\.(vue|html|ejs)$/.test(file)// edit to support other file types  ?through()  :through(function(buf,encoding,cb){// avoid replacing top-level <script> tags in .vue filesvarregex=/\.vue$/.test(file)    ?/<!--.*?-->|^<script>|^<\/script>|(?:<(\/)?script([>]))/gm    :/<!--.*?-->|(?:<(\/)?script([>]))/gmvarreplacement=(m,p1,p2)=>p2 ?`<${p1||''}script2${p2}` :mcb(null,buf.toString('utf8').replace(regex,replacement))})}

History

SeeCHANGELOG.md

License

MIT

About

Standardized, designer-friendly <script> behavior for your Single Page App

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp