weekly release 3.9dev
[moodle.git] / babel-plugin-add-module-to-define.js
CommitLineData
c53f86d4
RW
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
15
16/**
17 * This is a babel plugin to add the Moodle module names to the AMD modules
18 * as part of the transpiling process.
19 *
20 * In addition it will also add a return statement for the default export if the
21 * module is using default exports. This is a highly specific Moodle thing because
22 * we're transpiling to AMD and none of the existing Babel 7 plugins work correctly.
23 *
24 * This will fix the issue where an ES6 module using "export default Foo" will be
25 * transpiled into an AMD module that returns {default: Foo}; Instead it will now
26 * just simply return Foo.
27 *
28 * Note: This means all other named exports in that module are ignored and won't be
29 * exported.
30 *
31 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34
35"use strict";
f59ac416 36/* eslint-env node */
c53f86d4 37
f59ac416 38module.exports = ({template, types}) => {
c53f86d4 39 const fs = require('fs');
ac1a91bf 40 const path = require('path');
c53f86d4
RW
41 const glob = require('glob');
42 const cwd = process.cwd();
43
44 // Static variable to hold the modules.
45 let moodleSubsystems = null;
46 let moodlePlugins = null;
47
48 /**
49 * Parse Moodle's JSON files containing the lists of components.
50 *
51 * The values are stored in the static variables because we
52 * only need to load them once per transpiling run.
53 */
54 function loadMoodleModules() {
55 moodleSubsystems = {'lib': 'core'};
56 moodlePlugins = {};
57 let components = fs.readFileSync('lib/components.json');
58 components = JSON.parse(components);
59
60 for (const [component, path] of Object.entries(components.subsystems)) {
61 if (path) {
62 // Prefix "core_" to the front of the subsystems.
63 moodleSubsystems[path] = `core_${component}`;
64 }
65 }
66
67 for (const [component, path] of Object.entries(components.plugintypes)) {
68 if (path) {
69 moodlePlugins[path] = component;
70 }
71 }
72
73 for (const file of glob.sync('**/db/subplugins.json')) {
74 var rawContents = fs.readFileSync(file);
75 var subplugins = JSON.parse(rawContents);
76
2c28ba88 77 for (const [component, path] of Object.entries(subplugins.plugintypes)) {
c53f86d4
RW
78 if (path) {
79 moodlePlugins[path] = component;
80 }
81 }
82 }
83 }
84
85 /**
86 * Search the list of components that match the given file name
87 * and return the Moodle component for that file, if found.
88 *
89 * Throw an exception if no matching component is found.
90 *
91 * @throws {Error}
92 * @param {string} searchFileName The file name to look for.
93 * @return {string} Moodle component
94 */
95 function getModuleNameFromFileName(searchFileName) {
96 searchFileName = fs.realpathSync(searchFileName);
ac1a91bf 97 const relativeFileName = searchFileName.replace(`${cwd}${path.sep}`, '').replace(/\\/g, '/');
c53f86d4
RW
98 const [componentPath, file] = relativeFileName.split('/amd/src/');
99 const fileName = file.replace('.js', '');
100
101 // Check subsystems first which require an exact match.
102 if (moodleSubsystems.hasOwnProperty(componentPath)) {
103 return `${moodleSubsystems[componentPath]}/${fileName}`;
104 }
105
106 // It's not a subsystem so it must be a plugin. Moodle defines root folders
107 // where plugins can be installed so our path with be <plugin_root>/<plugin_name>.
108 // Let's separate the two.
109 let pathParts = componentPath.split('/');
110 const pluginName = pathParts.pop();
111 const pluginPath = pathParts.join('/');
112
113 // The plugin path mutch match exactly because some plugins are subplugins of
114 // other plugins which means their paths would partially match.
115 if (moodlePlugins.hasOwnProperty(pluginPath)) {
116 return `${moodlePlugins[pluginPath]}_${pluginName}/${fileName}`;
117 }
118
119 // This matches the previous PHP behaviour that would throw an exception
120 // if it couldn't parse an AMD file.
121 throw new Error('Unable to find module name for ' + searchFileName);
122 }
123
f59ac416
AN
124 /**
125 * This is heavily inspired by the babel-plugin-add-module-exports plugin.
126 * See: https://github.com/59naga/babel-plugin-add-module-exports
127 *
128 * This is used when we detect a module using "export default Foo;" to make
129 * sure the transpiled code just returns Foo directly rather than an object
130 * with the default property (i.e. {default: Foo}).
131 *
132 * Note: This means that we can't support modules that combine named exports
133 * with a default export.
134 *
135 * @param {String} path
136 * @param {String} exportObjectName
137 */
c53f86d4
RW
138 function addModuleExportsDefaults(path, exportObjectName) {
139 const rootPath = path.findParent(path => {
140 return path.key === 'body' || !path.parentPath;
141 });
142
143 // HACK: `path.node.body.push` instead of path.pushContainer(due doesn't work in Plugin.post).
144 // This is hardcoded to work specifically with AMD.
f59ac416 145 rootPath.node.body.push(template(`return ${exportObjectName}.default`)());
c53f86d4
RW
146 }
147
148 return {
149 pre() {
150 this.seenDefine = false;
151 this.addedReturnForDefaultExport = false;
152
153 if (moodleSubsystems === null) {
154 loadMoodleModules();
155 }
156 },
157 visitor: {
158 // Plugin ordering is only respected if we visit the "Program" node.
159 // See: https://babeljs.io/docs/en/plugins.html#plugin-preset-ordering
160 //
161 // We require this to run after the other AMD module transformation so
162 // let's visit the "Program" node.
163 Program: {
164 exit(path) {
165 path.traverse({
166 CallExpression(path) {
167 // If we find a "define" function call.
168 if (!this.seenDefine && path.get('callee').isIdentifier({name: 'define'})) {
169 // We only want to modify the first instance of define that we find.
170 this.seenDefine = true;
171 // Get the Moodle component for the file being processed.
172 var moduleName = getModuleNameFromFileName(this.file.opts.filename);
173 // Add the module name as the first argument to the define function.
174 path.node.arguments.unshift(types.stringLiteral(moduleName));
175 // Add a space after the define function in the built file so that previous versions
176 // of Moodle will not try to add the module name to the file when it's being served
177 // by PHP. This forces the regex in PHP to not match for this file.
178 path.node.callee.name = 'define ';
179 }
180
181 // Check for any Object.defineProperty('exports', 'default') calls.
182 if (!this.addedReturnForDefaultExport && path.get('callee').matchesPattern('Object.defineProperty')) {
f59ac416
AN
183 const [identifier, prop] = path.get('arguments');
184 const objectName = identifier.get('name').node;
185 const propertyName = prop.get('value').node;
c53f86d4
RW
186
187 if ((objectName === 'exports' || objectName === '_exports') && propertyName === 'default') {
188 addModuleExportsDefaults(path, objectName);
189 this.addedReturnForDefaultExport = true;
190 }
191 }
192 },
193 AssignmentExpression(path) {
194 // Check for an exports.default assignments.
195 if (
196 !this.addedReturnForDefaultExport &&
197 (
198 path.get('left').matchesPattern('exports.default') ||
199 path.get('left').matchesPattern('_exports.default')
200 )
201 ) {
202 const objectName = path.get('left.object.name').node;
203 addModuleExportsDefaults(path, objectName);
204 this.addedReturnForDefaultExport = true;
205 }
206 }
207 }, this);
208 }
209 }
210 }
211 };
2c28ba88 212};