middleman-jspm 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d32f2af298f87dd50a31a408293a5b9123412a0
4
+ data.tar.gz: a470f8cf8f23f31b543090c70341bf58d213dba5
5
+ SHA512:
6
+ metadata.gz: 8db77134a6b500df7e905792bc1211f43e8d1942182e40079ae18b4c09a12b4d329cd9d812adce1be07b392a0e3327b4ecbe592a8eafa743e28fae6603e2436d
7
+ data.tar.gz: fcbe94b368de43f638588bae832f9afa59d6c94564094c2f70332c86ac077b5eb3a6fac4b6d96bb884de8cb90068e31f2e6cd62026fa584d7ce76bf3d912b35e
@@ -0,0 +1,101 @@
1
+ # Middleman-JSPM
2
+
3
+ `middleman-jspm` adds support for using [JSPM](http://jspm.io) in [Middleman](http://middlemanapp.com).
4
+
5
+ ## Installation
6
+
7
+ To get started using Middleman, install `middleman` and make a project:
8
+
9
+ ```
10
+ gem install middleman
11
+ middleman init PROJECT
12
+ ```
13
+
14
+ Once that's done, add `gem "middleman-jspm", :git => "https://github.com/oncomouse/middleman-jspm.git"` to your `Gemfile` and run `bundle update`
15
+
16
+ ## Configuring
17
+
18
+ ```ruby
19
+ activate :jspm
20
+ ```
21
+
22
+ The bundle defaults to `source/javascripts/jspm_packages` for installing packages. If you want to use something else, you can activate with:
23
+
24
+ ```ruby
25
+ activate :jspm, :jspm_dir => "source/js/jspm_packages"
26
+ ```
27
+
28
+ ### Optional
29
+
30
+ As `middleman-jspm` manages your javascript for you, you should consider adding `ignore "javascripts/*"` to your `configure :build` section of `config.rb`. Doing so will prevent the site from building your javascript files unnecessarily.
31
+
32
+ ## Setting Up Node
33
+
34
+ This project relies on [Node.js](http://nodejs.org) to run JSPM. You will need to install it before you can use this gem.
35
+
36
+ After you have installed node, run:
37
+
38
+ ```
39
+ npm install jspm
40
+ middleman jspm init
41
+ ```
42
+
43
+ JSPM will ask if you want it to configure `config.js`. Say yes.
44
+
45
+ You can now install packages by running `middleman jspm install <packagename>` according to JSPM's settings.
46
+
47
+ ## Defining Modules
48
+
49
+ `middleman-jspm` will only compile files defined in its `module.json` file at build time. This file can either be stored in the same directory as `config.rb` or included as a sprockets asset in the default javascript directory. The ability to use sprockets means you can build the JSON file with ERuby if you want to dynamically determine which modules to compile.
50
+
51
+ `modules.json` defines an array of module objects defined on the following format (where all properties except `name` are optional):
52
+
53
+ ```json
54
+ [
55
+ {
56
+ "name": "main",
57
+ "include": [
58
+ "jquery"
59
+ ],
60
+ "exclude": [
61
+ "react"
62
+ ],
63
+ "self-executing": true
64
+ }
65
+ ]
66
+ ```
67
+
68
+ The above `modules.json` file will build a module named `main` that, in addition to dependencies defined in the file, will include jQuery but will not include React. The module will also compile as self-executing (which means JSPM will not add `system.js` and `config.js`) to the resulting file.
69
+
70
+ ## Building JSPM Pages
71
+
72
+ This is a minimal working example, that includes the module in `main.js` and all of it's dependencies:
73
+
74
+ ```erb
75
+ <!doctype html>
76
+ <html>
77
+ <head>
78
+ <meta charset='utf-8'>
79
+ <!-- Always force latest IE rendering engine or request Chrome Frame -->
80
+ <meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible'>
81
+ <title><%=current_page.data.title || "The Middleman"%></title>
82
+ <%= jspm_include_environment %>
83
+ </head>
84
+ <body>
85
+ <div id="app"
86
+ <%= jspm_include_module("main") %>
87
+ </body>
88
+ </html>
89
+ ```
90
+
91
+ `jspm_include_environment` includes the SystemJS and configuration files needed by JSPM and will include the correct files for both build and development environments. If all modules defined in `modules.json` are self-executing, this command returns nothing.
92
+
93
+ `jspm_include_module(<name>)` includes a module into the present document and will include the correct files, in the correct manner, for both build and development environments.
94
+
95
+ ## Other Helpers
96
+
97
+ Finally, there is also a helper called `jspm_path(<name>, [<source>])` that can give you the path of a file installed with JSPM. For instance, to look the path of jquery that was installed from github, you would use `jspm_path("components/jquery", "github")` to get the path of the jquery used by JSPM. The helper works the same way for NPM packages.
98
+
99
+ ## Additional Help
100
+
101
+ An example setup is posted at http://github.com/oncomouse/middleman-jspm-example
@@ -0,0 +1,5 @@
1
+ require 'middleman-jspm/commands'
2
+ ::Middleman::Extensions.register(:jspm) do
3
+ require 'middleman-jspm/extension'
4
+ ::Middleman::JSPMExtension
5
+ end
@@ -0,0 +1,76 @@
1
+ require 'middleman-core/cli'
2
+
3
+ class String
4
+ def colorize(color_code)
5
+ "\e[#{color_code}m#{self}\e[0m"
6
+ end
7
+
8
+ def red
9
+ colorize(31)
10
+ end
11
+
12
+ def light_blue
13
+ colorize(36)
14
+ end
15
+ end
16
+
17
+ module Middleman
18
+ module Cli
19
+ class JSPM < Thor
20
+ include Thor::Actions
21
+
22
+ namespace :jspm
23
+
24
+ desc "jspm [<options>...]", "Use JSPM from within Middleman"
25
+
26
+ def self.exit_on_failure?
27
+ true
28
+ end
29
+
30
+ def jspm(*options)
31
+ raise "JSPM".red + " not found, please run:\n\n\t" + "npm install".light_blue + "\n\n" if not File.directory? "node_modules" and not File.directory? "node_modules/jspm"
32
+ begin
33
+ jspm_dir = ::Middleman::Application.server.inst.jspm_dir
34
+ rescue NoMethodError
35
+ raise 'You need to activate the deploy extension in config.rb.'
36
+ end
37
+
38
+ build_package_json = false
39
+ if not File.exists? "package.json"
40
+ build_package_json = true
41
+ File.open("package.json", "w") do |fp|
42
+ fp.write(JSON.pretty_generate(
43
+ {
44
+ "jspm" => {
45
+ "directories" => {
46
+ "baseURL" => jspm_dir.gsub(/\/jspm_packages$/,"")
47
+ },
48
+ "dependencies" => {},
49
+ "devDependencies" => {
50
+ "babel" => "npm:babel-core@^5.6.4",
51
+ "babel-runtime" => "npm:babel-runtime@^5.6.4",
52
+ "core-js" => "npm:core-js@^0.9.17"
53
+ }
54
+ },
55
+ "dependencies" => {
56
+ "jspm" => ">=0.16.0-beta.3"
57
+ },
58
+ "devDependencies" => {}
59
+ }
60
+ ))
61
+ end
62
+ end
63
+
64
+ system "node node_modules/jspm/jspm.js #{options.join(" ")}"
65
+
66
+ if build_package_json
67
+ config_js = File.read(jspm_dir.gsub(/\/jspm_packages$/,"")+"/config.js")
68
+ config_js.sub!(/^System.config\(\{/,"System.config({\n \"baseURL\": \"#{jspm_dir.sub(/\/jspm_packages$/,"").sub(/^source/,"")}\",")
69
+ File.open(jspm_dir.gsub(/\/jspm_packages$/,"")+"/config.js", "w") do |fp|
70
+ fp.write config_js
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,120 @@
1
+ # Require core library
2
+ require 'middleman-core'
3
+
4
+ module Middleman
5
+ class JSPMExtension < Extension
6
+ option :jspm_dir, "source/javascripts/jspm_packages", "Set the directory where JSPM installs packages."
7
+
8
+ def initialize(app, options_hash = {}, &block)
9
+ super
10
+ app.set :jspm_dir, options.jspm_dir
11
+ app.after_build do |builder|
12
+ require 'fileutils'
13
+ require 'uglifier'
14
+
15
+ modules = jspm_get_modules()
16
+
17
+ if modules.length > 0
18
+ FileUtils.mkdir_p "#{jspm_dir.gsub('source','build').gsub('/jspm_packages','')}/"
19
+ if jspm_include_system()
20
+ assets = [
21
+ "jspm_packages/system",
22
+ "config"
23
+ ]
24
+
25
+ File.open("#{jspm_dir.gsub('source','build').gsub('/jspm_packages','')}/system.js","w") do |fp|
26
+ fp.write assets.map{|x| sprockets.find_asset(x).to_s }.join("\n")
27
+ end
28
+ end
29
+ end
30
+
31
+ modules.each do |m|
32
+ command = m["self-executing"] ? "bundle-sfx" : "bundle"
33
+ additions_and_subtractions = ""
34
+ if(m["include"] && m["include"].kind_of?(Array))
35
+ additions_and_subtractions += " - " + m["include"].join(" - ")
36
+ end
37
+ if(m["exclude"] && !m["self-executing"] && m["exclude"].kind_of?(Array))
38
+ additions_and_subtractions += " - " + m["exclude"].join(" - ")
39
+ end
40
+ system("node node_modules/jspm/jspm.js #{command} #{m["name"]}#{additions_and_subtractions} #{jspm_dir.gsub('source','build').gsub('/jspm_packages','')}/#{m["name"]}.js")
41
+ end
42
+
43
+ if modules.length > 0
44
+ Dir.glob("#{jspm_dir.gsub('source','build').gsub('/jspm_packages','')}/**/*.js").each do |file|
45
+ puts "Uglifying #{file}"
46
+ compressed_source = Uglifier.compile(File.read(file))
47
+ File.open(file, 'w') do |f_pointer|
48
+ f_pointer.write(compressed_source)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ def registered(app, options_hash = {}, &block)
55
+
56
+ end
57
+
58
+ def after_configuration
59
+ app.sprockets.append_path options.jspm_dir
60
+ end
61
+
62
+ helpers do
63
+ def jspm_path(package, source="npm")
64
+ Dir.glob("#{jspm_dir}/#{source}/#{package}*").map{|x| if File.directory? x then x else nil end }.delete_if{|x| x.nil? }.sort do |a,b|
65
+ b.gsub("#{jspm_dir}/#{source}/#{package}@","").split('.').map { |e| e.to_i } <=> a.gsub("#{jspm_dir}/github/twbs/bootstrap-sass@","").split('.').map { |e| e.to_i }
66
+ end.first
67
+ end
68
+
69
+ def jspm_get_modules()
70
+ if sprockets.find_asset("modules.json")
71
+ JSON.parse(sprockets.find_asset("modules.json").to_s)
72
+ elsif File.exists?('modules.json')
73
+ JSON.parse(File.read("modules.json"))
74
+ else
75
+ []
76
+ end
77
+ end
78
+
79
+ def jspm_include_module(name)
80
+ if build?
81
+ modules = jspm_get_modules()
82
+
83
+ m = modules.find_index{|m| m["name"] == name }
84
+
85
+ if not m.nil? and modules[m]["self-executing"]
86
+ "<script src=\"#{asset_path(:js, name)}\"></script>"
87
+ else
88
+ "<script>System.import('#{name}');</script>"
89
+ end
90
+ else
91
+ "<script>System.import('#{name}');</script>"
92
+ end
93
+ end
94
+
95
+ def jspm_include_system()
96
+ include_system = false
97
+ modules = jspm_get_modules()
98
+
99
+ modules.each do |mod|
100
+ if !mod["self-executing"]
101
+ include_system = true
102
+ break
103
+ end
104
+ end
105
+ return include_system
106
+ end
107
+
108
+ def jspm_include_environment()
109
+ if build?
110
+ if jspm_include_system()
111
+ javascript_include_tag("system")
112
+ end
113
+ else
114
+ javascript_include_tag "jspm_packages/system.js", "config.js"
115
+ end
116
+ end
117
+ end
118
+ alias :included :registered
119
+ end
120
+ end
@@ -0,0 +1,5 @@
1
+ module Middleman
2
+ module JSPM
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'middleman-jspm'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "middleman-jspm/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "middleman-jspm"
7
+ spec.version = Middleman::JSPM::VERSION
8
+ spec.authors = ['Andrew Pilsch']
9
+ spec.email = ['apilsch@tamu.edu']
10
+ spec.summary = "Use JSPM in Middleman"
11
+ spec.description = "Use JSPM in Middleman"
12
+ spec.homepage = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+ spec.required_ruby_version = '>= 1.9.3'
20
+
21
+ spec.add_dependency 'middleman-core', '>= 3.2'
22
+ spec.add_dependency 'uglifier'
23
+ spec.add_dependency 'ejs'
24
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-jspm
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Pilsch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uglifier
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ejs
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Use JSPM in Middleman
56
+ email:
57
+ - apilsch@tamu.edu
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - lib/middleman-jspm.rb
64
+ - lib/middleman-jspm/commands.rb
65
+ - lib/middleman-jspm/extension.rb
66
+ - lib/middleman-jspm/version.rb
67
+ - lib/middleman_extension.rb
68
+ - middleman-jspm.gemspec
69
+ homepage: ''
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 1.9.3
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.4.8
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Use JSPM in Middleman
93
+ test_files: []