UNPKG

3.71 kBJavaScriptView Raw
1/*
2 Copyright 2017 Esri
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12*/
13
14/* jshint node: true */
15'use strict';
16var path = require('path');
17var Funnel = require('broccoli-funnel');
18var MergeTrees = require('broccoli-merge-trees');
19var stringReplace = require('broccoli-string-replace');
20
21module.exports = {
22 name: 'ember-esri-loader',
23
24 // support "import esriLoader from 'esri-loader';" syntax
25 included() {
26 this._super.included.apply(this, arguments);
27 this.import('vendor/shims/esri-loader.js');
28 },
29
30 // copy UMD builds of esri-loader to public tree
31 // as a peer to vendor and app scripts
32 treeForPublic(publicTree) {
33 var esriLoaderTree = new Funnel(path.dirname(require.resolve('esri-loader')), {
34 files: ['esri-loader.js', 'esri-loader.js.map', 'esri-loader.min.js', 'esri-loader.min.js.map'],
35 destDir: 'assets'
36 });
37 if (!publicTree) {
38 return esriLoaderTree;
39 }
40 return new MergeTrees([publicTree, esriLoaderTree]);
41 },
42
43 // inject esri-loader script tag instead of importing into vendor.js
44 // so that it is not subject to the find and replace below
45 contentFor (type, config) {
46 if (type === 'body-footer' || type === 'test-body-footer') {
47 var isProduction = config.environment === 'production';
48 var fileName = isProduction ? 'esri-loader.min.js' : 'esri-loader.js';
49 return '<script src="' + config.rootURL + 'assets/' + fileName + '"></script>';
50 }
51 },
52
53 // find and replace "require" and "define" in the vendor and app scripts
54 postprocessTree: function (type, tree) {
55 if (type !== 'all') {
56 return tree;
57 }
58
59 var outputPaths = this.app.options.outputPaths;
60
61 // Create the string replace patterns for the various application files
62 // We will replace require and define function call by their pig-latin version
63 var data = {
64 files: [
65 new RegExp(path.parse(outputPaths.app.js).name + '(.*js)'),
66 new RegExp(path.parse(outputPaths.vendor.js).name + '(.*js)'),
67 new RegExp(path.parse(outputPaths.tests.js).name + '(.*js)'),
68 'tests/index.html',
69 new RegExp(path.parse(outputPaths.testSupport.js.testSupport).name + '(.*js)')
70 ],
71 patterns: [{
72 match: /([^A-Za-z0-9_#']|^|["])define(?=\W|["]|$)/g,
73 replacement: '$1efineday'
74 }, {
75 match: /(\W|^|["])require(?=\W|["]|$)/g,
76 replacement: '$1equireray'
77 }]
78 };
79 var dataTree = stringReplace(tree, data);
80
81 // Special case for the test loader that is doing some funky stuff with require
82 // We basically decided to pig latin all require cases.
83 var testLoader = {
84 files: [
85 new RegExp(path.parse(outputPaths.testSupport.js.testLoader).name + '(.*js)')
86 ],
87 patterns: [{
88 match: /(\W|^|["])define(\W|["]|$)/g,
89 replacement: '$1efineday$2'
90 }, {
91 match: /require([.])/g,
92 replacement: 'equireray.'
93 }, {
94 match: /require([(])/g,
95 replacement: 'equireray('
96 }, {
97 match: /require([ ])/g,
98 replacement: 'equireray '
99 }, {
100 match: /requirejs([.])/g,
101 replacement: 'equireray.'
102 }]
103 };
104
105 return stringReplace(dataTree, testLoader);
106 }
107};