UNPKG

1.62 kBJavaScriptView Raw
1/**
2 * @fileoverview For scripts included in browser-window, this will automatically
3 * inject the browser-window global scopes into the file.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10"use strict";
11
12// -----------------------------------------------------------------------------
13// Rule Definition
14// -----------------------------------------------------------------------------
15
16var path = require("path");
17var helpers = require("../helpers");
18var browserWindowEnv = require("../environments/browser-window");
19
20module.exports = function(context) {
21 // ---------------------------------------------------------------------------
22 // Public
23 // ---------------------------------------------------------------------------
24
25 return {
26 Program(node) {
27 let filePath = helpers.getAbsoluteFilePath(context);
28 let relativePath = path.relative(helpers.rootDir, filePath);
29 // We need to translate the path on Windows, due to the change
30 // from \ to /, and browserjsScripts assumes Posix.
31 if (path.win32) {
32 relativePath = relativePath.split(path.sep).join("/");
33 }
34
35 if (browserWindowEnv.browserjsScripts &&
36 browserWindowEnv.browserjsScripts.includes(relativePath)) {
37 for (let global in browserWindowEnv.globals) {
38 helpers.addVarToScope(global, context.getScope(),
39 browserWindowEnv.globals[global]);
40 }
41 }
42 }
43 };
44};