]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Add a preprocessor to simplify builds
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # This file boots the main VimFx process, as well as each frame script. It tries
22 # to do the minimum amount of things to run main.coffee, or main-frame.coffee
23 # for frame scripts. It defines a few global variables, and sets up a
24 # Node.js-style `require` module loader.
25
26 # `bootstrap.js` files of different add-ons do _not_ share scope. However, frame
27 # scripts for the same `<browser>` but from different add-ons _do_ share scope.
28 # In order not to pollute that global scope in frame scripts, everything is done
29 # in an IIFE here, and the `global` variable is handled with care.
30
31 do (global = this) ->
32
33 {classes: Cc, interfaces: Ci, utils: Cu} = Components
34 ADDON_PATH = 'chrome://vimfx'
35 IS_FRAME_SCRIPT = (typeof content != 'undefined')
36 REQUIRE_DATA = do -> # @echo REQUIRE_DATA
37
38 unless IS_FRAME_SCRIPT
39 # Make `Services` and `console` available globally, just like they are in
40 # frame scripts by default.
41 Cu.import('resource://gre/modules/Services.jsm')
42 try
43 # TODO: Only use this path when Firefox 44 is released.
44 Cu.import('resource://gre/modules/Console.jsm')
45 catch
46 Cu.import('resource://gre/modules/devtools/Console.jsm')
47
48 shutdownHandlers = []
49
50 dirname = (uri) -> uri.split('/')[...-1].join('/') or '.'
51
52 require = (path, moduleRoot = '.', dir = '.') ->
53 unless path[0] == '.'
54 # Allow `require('module/lib/foo')` in additon to `require('module')`.
55 [match, name, subPath] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
56 base = REQUIRE_DATA[moduleRoot]?[name] ? moduleRoot
57 dir = "#{base}/node_modules/#{name}"
58 main = REQUIRE_DATA[dir]?['']
59 path = subPath ? main ? 'index'
60 moduleRoot = dir
61
62 prefix = "#{ADDON_PATH}/content"
63 uri = "#{prefix}/#{dir}/#{path}.js"
64 normalizedUri = Services.io.newURI(uri, null, null).spec
65 currentDir = dirname(".#{normalizedUri[prefix.length..]}")
66
67 unless require.scopes[normalizedUri]?
68 module = {
69 exports: {}
70 onShutdown: (fn) -> shutdownHandlers.push(fn)
71 }
72 require.scopes[normalizedUri] = scope = {
73 require: (path) -> require.call(null, path, moduleRoot, currentDir)
74 module, exports: module.exports
75 Cc, Ci, Cu
76 ADDON_PATH
77 IS_FRAME_SCRIPT
78 FRAME_SCRIPT_ENVIRONMENT: if IS_FRAME_SCRIPT then global else null
79 }
80 Services.scriptloader.loadSubScript(normalizedUri, scope, 'UTF-8')
81
82 return require.scopes[normalizedUri].module.exports
83
84 require.scopes = {}
85
86 startup = (args...) ->
87 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
88 require(main)(args...)
89
90 shutdown = ->
91 for shutdownHandler in shutdownHandlers
92 try
93 shutdownHandler()
94 catch error
95 Cu.reportError(error)
96 shutdownHandlers = []
97
98 # Release everything in `require`d modules. This must be done _after_ all
99 # shutdownHandlers, since they use variables in these scopes.
100 for path, scope of require.scopes
101 for name of scope
102 scope[name] = null
103 require.scopes = {}
104
105 if IS_FRAME_SCRIPT
106 messageManager = require('./lib/message-manager')
107
108 # Tell the main process that this frame script was created, and ask if
109 # anything should be done in this frame.
110 messageManager.send('tabCreated', null, (ok) ->
111 # After dragging a tab from one window to another, `content` might have
112 # been set to `null` by Firefox when this runs. If so, simply return.
113 return unless ok and content?
114
115 startup()
116
117 messageManager.listenOnce('shutdown', shutdown)
118 )
119 else
120 global.startup = startup
121 global.shutdown = shutdown
122 global.install = ->
123 global.uninstall = ->
Imprint / Impressum