1 | // Copyright 2015 Esri
|
2 | // Licensed under The MIT License(MIT);
|
3 | // you may not use this file except in compliance with the License.
|
4 | // You may obtain a copy of the License at
|
5 | // http://opensource.org/licenses/MIT
|
6 | // Unless required by applicable law or agreed to in writing, software
|
7 | // distributed under the License is distributed on an "AS IS" BASIS,
|
8 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
9 | // See the License for the specific language governing permissions and
|
10 | // limitations under the License.
|
11 |
|
12 | /* jshint node: true */
|
13 | ;
|
14 |
|
15 | const fs = require('fs');
|
16 | const path = require('path');
|
17 |
|
18 | const ReplaceRequireAndDefineFilter = require('./lib/replace-require-and-define-filter');
|
19 | const convertIndexToAmd = require('./lib/convert-index-to-amd');
|
20 |
|
21 | module.exports = {
|
22 |
|
23 | name: 'ember-cli-amd',
|
24 |
|
25 | externalAmdModules: new Set(),
|
26 |
|
27 | included(app) {
|
28 | // Note: this function is only called once even if using ember build --watch or ember serve
|
29 |
|
30 | // This is the entry point for this addon. We will collect the amd definitions from the ember-cli-build.js and
|
31 | // we will build the list off the amd modules used by the application, replace define and require function calls
|
32 | // in the js files and modify the index.html to load AMD loader first and all the external AMD modules before
|
33 | // loading the vendor and app files.
|
34 |
|
35 | // This addon relies on an 'amd' options in the ember-cli-build.js file
|
36 | if (!app.options.amd) {
|
37 | return new Error('ember-cli-amd: No amd options specified in the ember-cli-build.js file.');
|
38 | }
|
39 |
|
40 | // Merge the default options
|
41 | app.options.amd = Object.assign({
|
42 | packages: [],
|
43 | excludePaths: []
|
44 | }, app.options.amd);
|
45 |
|
46 | // Determine the type of loader.
|
47 | if (!app.options.amd.loader) {
|
48 | throw new Error('ember-cli-amd: You must specify a loader option the amd options in ember-cli-build.js.');
|
49 | }
|
50 |
|
51 | if (app.options.amd.configPath) {
|
52 | const configPath = app.options.amd.configPath;
|
53 | if (!fs.existsSync(path.join(app.project.root, configPath))) {
|
54 | throw new Error(`ember-cli-amd: The file specified in the configPath option "${configPath}" does not exist`);
|
55 | }
|
56 | }
|
57 | },
|
58 |
|
59 | postprocessTree(type, tree) {
|
60 | // Note: this function will be called once during the continuous builds. However, the tree returned will be directly manipulated.
|
61 | // It means that the de-requireing will be going on.
|
62 | if (type !== 'all' || !this.app.options.amd) {
|
63 | return tree;
|
64 | }
|
65 |
|
66 | // Use the RequireFilter class to replace in the code that conflict with AMD loader
|
67 | this.externalAmdModules.clear();
|
68 | return new ReplaceRequireAndDefineFilter(tree, {
|
69 | amdPackages: this.app.options.amd.packages,
|
70 | externalAmdModules: this.externalAmdModules,
|
71 | excludePaths: this.app.options.amd.excludePaths
|
72 | });
|
73 | },
|
74 |
|
75 | postBuild(result) {
|
76 |
|
77 | // When ember build --watch or ember serve are used, this function will be called over and over
|
78 | // as a user updates code.
|
79 |
|
80 | // We can only rebbuild the index after ALL the cli addons have ran.
|
81 | // We cannot bbuild it during the postPrecessTree!
|
82 | convertIndexToAmd(this.app, result.directory, this.externalAmdModules);
|
83 | }
|
84 | };
|