Skip to content

Commit

Permalink
move legacy module loading code into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing committed Jul 18, 2022
1 parent e728357 commit 30ea87e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 82 deletions.
15 changes: 13 additions & 2 deletions Makefile.dryice.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function buildAceModuleInternal(opts, callback) {
ignore: opts.ignore || [],
withRequire: false,
basepath: ACE_HOME,
transforms: [normalizeLineEndings],
transforms: [normalizeLineEndings, includeLoader],
afterRead: [optimizeTextModules]
}, write);
}
Expand All @@ -358,7 +358,7 @@ function buildCore(options, extra, callback) {
order: -1000,
literal: true
}];
options.require =["ace/ace"];
options.require =["ace/ace", "ace/loader_build"];
options.projectType = "main";
options.ns = "ace";
buildAceModule(options, callback);
Expand Down Expand Up @@ -583,6 +583,17 @@ function normalizeLineEndings(module) {
return module.source = module.source.replace(/\r\n/g, "\n");
}

function includeLoader(module) {
var pattern = '"include loader_build";';
if (module.source && module.source.indexOf(pattern) != -1) {
console.log("===================================== =====================================");
console.log(module);
console.log("===================================== =====================================");
module.deps.push("ace/loader_build");
module.source = module.source.replace(pattern, 'require("./loader_build")(exports)');
}
}

function optimizeTextModules(sources) {
var textModules = {};
return sources.filter(function(pkg) {
Expand Down
2 changes: 0 additions & 2 deletions demo/kitchen-sink/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
define(function(require, exports, module) {
"use strict";

require("ace/lib/fixoldbrowsers");

require("ace/ext/rtl");

require("ace/multi_select");
Expand Down
12 changes: 1 addition & 11 deletions lib/ace/ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@

define(function(require, exports, module) {
"use strict";

require("./lib/fixoldbrowsers");
"include loader_build";

var dom = require("./lib/dom");
var event = require("./lib/event");
Expand All @@ -59,15 +58,6 @@ require("./ext/error_marker");

exports.config = require("./config");

/**
* Provides access to require in packed noconflict mode
* @param {String} moduleName
* @returns {Object}
**/
exports.require = require;

if (typeof define === "function")
exports.define = define;

/**
* Embeds the Ace editor into the DOM, at the element provided by `el`.
Expand Down
69 changes: 4 additions & 65 deletions lib/ace/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ var AppConfig = require("./lib/app_config").AppConfig;

module.exports = exports = new AppConfig();

var global = (function() {
return this || typeof window != "undefined" && window;
})();

var options = {
packaged: false,
workerPath: null,
Expand Down Expand Up @@ -113,12 +109,12 @@ exports.setModuleUrl = function(name, subst) {
var loader = function(moduleName, cb) {
if (moduleName == "ace/theme/textmate")
return cb(null, require("./theme/textmate"));
return console.error("loader is not configured")
}
return console.error("loader is not configured");
};

exports.setLoader = function(cb) {
loader = cb
}
loader = cb;
};

exports.$loading = {};
exports.loadModule = function(moduleName, onLoad) {
Expand Down Expand Up @@ -176,63 +172,6 @@ var reportErrorIfPathIsNotConfigured = function() {
}
};

// initialization
function init(packaged) {
if (!global || !global.document)
return;

options.packaged = packaged || require.packaged || module.packaged || (global.define && define.packaged);

var scriptOptions = {};
var scriptUrl = "";

// Use currentScript.ownerDocument in case this file was loaded from imported document. (HTML Imports)
var currentScript = (document.currentScript || document._currentScript ); // native or polyfill
var currentDocument = currentScript && currentScript.ownerDocument || document;

var scripts = currentDocument.getElementsByTagName("script");
for (var i=0; i<scripts.length; i++) {
var script = scripts[i];

var src = script.src || script.getAttribute("src");
if (!src)
continue;

var attributes = script.attributes;
for (var j=0, l=attributes.length; j < l; j++) {
var attr = attributes[j];
if (attr.name.indexOf("data-ace-") === 0) {
scriptOptions[deHyphenate(attr.name.replace(/^data-ace-/, ""))] = attr.value;
}
}

var m = src.match(/^(.*)\/ace(\-\w+)?\.js(\?|$)/);
if (m)
scriptUrl = m[1];
}

if (scriptUrl) {
scriptOptions.base = scriptOptions.base || scriptUrl;
scriptOptions.packaged = true;
}

scriptOptions.basePath = scriptOptions.base;
scriptOptions.workerPath = scriptOptions.workerPath || scriptOptions.base;
scriptOptions.modePath = scriptOptions.modePath || scriptOptions.base;
scriptOptions.themePath = scriptOptions.themePath || scriptOptions.base;
delete scriptOptions.base;

for (var key in scriptOptions)
if (typeof scriptOptions[key] !== "undefined")
exports.set(key, scriptOptions[key]);
}

exports.init = init;

function deHyphenate(str) {
return str.replace(/-(.)/g, function(m, m1) { return m1.toUpperCase(); });
}

exports.version = "1.7.1";

});
2 changes: 0 additions & 2 deletions lib/ace/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
define(function(require, exports, module) {
"use strict";

require("./lib/fixoldbrowsers");

var oop = require("./lib/oop");
var dom = require("./lib/dom");
var lang = require("./lib/lang");
Expand Down
112 changes: 112 additions & 0 deletions lib/ace/loader_build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */

define(function(require, exports, module) {
"use strict";

// configure module loading in ace-builds to remain backwards compatible

require("./lib/fixoldbrowsers");
var config = require("./config");

var global = (function() {
return this || typeof window != "undefined" && window;
})();

module.exports = function(ace) {
config.init = init;

/**
* Provides access to require in packed noconflict mode
* @param {String} moduleName
* @returns {Object}
**/
ace.require = require;

if (typeof define === "function")
ace.define = define;
};

// initialization
function init(packaged) {
if (!global || !global.document)
return;

config.set("packaged", packaged || require.packaged || module.packaged || (global.define && define.packaged));

var scriptOptions = {};
var scriptUrl = "";

// Use currentScript.ownerDocument in case this file was loaded from imported document. (HTML Imports)
var currentScript = (document.currentScript || document._currentScript ); // native or polyfill
var currentDocument = currentScript && currentScript.ownerDocument || document;

var scripts = currentDocument.getElementsByTagName("script");
for (var i=0; i<scripts.length; i++) {
var script = scripts[i];

var src = script.src || script.getAttribute("src");
if (!src)
continue;

var attributes = script.attributes;
for (var j=0, l=attributes.length; j < l; j++) {
var attr = attributes[j];
if (attr.name.indexOf("data-ace-") === 0) {
scriptOptions[deHyphenate(attr.name.replace(/^data-ace-/, ""))] = attr.value;
}
}

var m = src.match(/^(.*)\/ace(\-\w+)?\.js(\?|$)/);
if (m)
scriptUrl = m[1];
}

if (scriptUrl) {
scriptOptions.base = scriptOptions.base || scriptUrl;
scriptOptions.packaged = true;
}

scriptOptions.basePath = scriptOptions.base;
scriptOptions.workerPath = scriptOptions.workerPath || scriptOptions.base;
scriptOptions.modePath = scriptOptions.modePath || scriptOptions.base;
scriptOptions.themePath = scriptOptions.themePath || scriptOptions.base;
delete scriptOptions.base;

for (var key in scriptOptions)
if (typeof scriptOptions[key] !== "undefined")
config.set(key, scriptOptions[key]);
}

function deHyphenate(str) {
return str.replace(/-(.)/g, function(m, m1) { return m1.toUpperCase(); });
}

});

0 comments on commit 30ea87e

Please sign in to comment.