Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed May 1, 2018
0 parents commit 770d8d3
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2018 BusFaster Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# charto-loader

This is a collection of loader scripts for Charto style projects:

<dl>
<dt>index.js</dt>
<dd>For browsers, loads <a href="https://github.com/systemjs/systemjs">SystemJS</a>,
<a href="https://github.com/dojo/dojo">Dojo 1.x</a> and
<a href="https://github.com/Microsoft/monaco-editor">Monaco</a>.</dd>
<dt>dojo-loader.js</dt>
<dd>Minimal shim for loading Dojo using SystemJS.</dd>
<dt>dojo-require.js</dt>
<dd>Minimal shim used by dojo-loader to support `require` inside Dojo when bundled.</dd>
<dt>fixamd.js</dt>
<dd>Renames `src` to the package name in paths inside TypeScript-compiled AMD bundles.</dd>
<dt>undefined.js</dt>
<dd>Exports `undefined`, needed by Dojo when bundled.</dd>
<dt>process.js</dt>
<dd>Exports a Node.js `process` object with `NODE_ENV` set to `production`. Needed for example by React.</dd>
<dt>process-dev.js</dt>
<dd>Exports a Node.js `process` object with `NODE_ENV` set to `development`.</dd>
</dl>

# License

[The MIT License](https://raw.githubusercontent.com/charto/charto-loader/master/LICENSE)

Copyright (c) 2018 BusFaster Ltd
9 changes: 9 additions & 0 deletions alleamd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fs = require('fs');

var name = process.argv[2];
var codePath = process.argv[3] || require('path').join('packages', 'node_modules', name, 'dist', 'index-amd.js');
var code = fs.readFileSync(codePath, { encoding: 'utf-8' });

code = code.replace(/\"src\//g, '"' + name + '/');

fs.writeFileSync(codePath, code, { encoding: 'utf-8' });
3 changes: 3 additions & 0 deletions bin/fixamd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../fixamd.js');
46 changes: 46 additions & 0 deletions dojo-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This is a minimal shim for loading Dojo using SystemJS.

exports = {
translate: function(spec, foo) {
if(this.builder) {
const config = {
'touch': true,
'config-deferredInstrumentation': false,
'dom-addeventlistener': true,
'host-browser': true
};

// These kludges support enough AMD loader syntax to make dgrid work.

// Map "has!condition?yes:no" tests using condition values listed above.
spec.source = spec.source.replace(/(['"])[^'"]+\/has!([^?'"]*)\?([^:'"]*):?([^'"]*)/g, function(match, quote, condition, yes, no) {
return(quote + ((config[condition] ? yes : no) || 'undefined'));
// Map "selector/loader!default" to "selector/lite.js".
}).replace(/(['"][^'"]+\/selector\/)_loader!([^'"]*)/, function(match, prefix) {
return(prefix + 'lite.js');
}).replace(/['"]require['"]/, '"dojoRequire"');
}
},

fetch: function(spec, fetch) {
const name = spec.name.replace(System.baseURL, '').replace(/node_modules\/(dojo\/.*)\.js/, '$1');

if(typeof(require) == 'function' && require.cache) {
// Dojo require function was found.

return(new Promise(function(resolve) {
require([ name ], function(module) {
spec.dojo = module;
resolve('');
});
}).catch(function(err) { console.log(err); }));
} else if(this.builder) {
// systemjs-builder support.
return(fetch(spec));
}
},

instantiate: function(spec, instantiate) {
return(spec.dojo);
}
};
13 changes: 13 additions & 0 deletions dojo-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
toUrl: function (name) {
var parts = name.match(/(.*)(\.[^.]+)$/);
var ext = '.js';

if(parts) {
name = parts[1];
ext = parts[2];
}

return(System.resolveSync('dojo/' + name).replace(/\.js$/, ext));
}
};
9 changes: 9 additions & 0 deletions fixamd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var fs = require('fs');

var name = process.argv[2];
var codePath = process.argv[3] || require('path').join('dist', 'index-amd.js');
var code = fs.readFileSync(codePath, { encoding: 'utf-8' });

code = code.replace(/\"src\//g, '"' + name + '/');

fs.writeFileSync(codePath, code, { encoding: 'utf-8' });
169 changes: 169 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
'use strict';

/** Wrapper for defining modules inside appropriate loaders.
* Initially unset to avoid confusing deferred scripts. */
var define;

/** Monaco loader configuration. */
var require;

/** Dojo loader configuration. */
var dojoConfig;

/** SystemJS API. */
var System;

/** Storage for Monaco and Dojo loaders. */
var charto = charto || {};

/** List of packages to preload if a bundle is not available. */
charto.preload = charto.preload || [];

charto.paths = charto.paths || {};

/** Initialize SystemJS. */

charto.initSystem = function(done) {
// 1. Fetch SystemJS loader and Promise support.

loadSystem();

function loadSystem() {
charto.require([
'https://unpkg.com/[email protected]/dist/system.js',
'https://unpkg.com/[email protected]/js/browser/bluebird.core.min.js'
], loadConfig);
}

// 2. Fetch and apply SystemJS configuration.

function loadConfig() {
System.config({ baseURL: window.location.pathname.replace(/\/[^/]+/, '/') });
charto.require([
'config-npm.js',
'config.js'
], done);
}
};

/** Simplest possible script loader, for fetching better loaders. */

charto.require = function(urls, done) {
var ref = document.getElementsByTagName('script')[0];
var loaded = 0;

urls.map(function(url) {
var script = document.createElement('script');

script.async = true;
script.onload = function() {
script.onload = null;
if(++loaded >= urls.length) done();
}

script.src = url;
ref.parentNode.insertBefore(script, ref);
});
};

/** Initialize AMD shim needed by Monaco and Dojo. */

charto.initLoader = function() {
define = function(name) {
var loader = typeof(name) == 'string' && name.substr(0, 3) == 'vs/' ? charto.monaco : charto.dojo;
return(loader.define.apply(this, arguments));
}

define.amd = true;
};

charto.initMonaco = function() {
var vsBase = 'node_modules/monaco-editor/min/';

if(charto.production) {
vsBase = 'https://unpkg.com/[email protected]/min/';

// Fix web worker cross origin issues.
window.MonacoEnvironment = { getWorkerUrl: function() {
return(URL.createObjectURL(new Blob([
'self.MonacoEnvironment = { baseUrl: "' + vsBase + '" };',
'importScripts("' + vsBase + (charto.paths.monacoWorker || 'vs/base/worker/workerMain.js') + '");'
], { type: 'text/javascript' })));
}};
}

require = {
paths: {
vs: vsBase + 'vs'
}
};

/** Fetch main Monaco bundles. */

function loadMonaco(monaco) {
var deps = [
'vs/editor/editor.main.nls',
'vs/editor/editor.main'
];

charto.monaco = monaco;

// Monaco loader sets exports, then gets confused by them.
exports = void 0;

return(new Promise(function(resolve, reject) {
monaco.require(deps, resolve);
}));
}

// Export Monaco to SystemJS modules.

System.registerDynamic('monaco-editor', [], false, function(require, exports, module) {
module.exports = charto.monaco.require(charto.paths.monacoApi || 'vs/editor/editor.api');
});

return(System.import('vs/loader').then(loadMonaco));
};

charto.initDojo = function() {
dojoConfig = dojoConfig || {
async: true,
baseUrl: 'node_modules',
packages: [
{ name: 'dojo', location: 'dojo' },
{ name: 'dgrid', location: 'dgrid' },
{ name: 'dstore', location: 'dojo-dstore' }
]
};

charto.preload.push(System.import('dojo').then(
function(dojo) { charto.dojo = dojo; }
));
};

/** Start application, call after inits. */

charto.start = function(main) {
/** Fetch entire bundled app if available, otherwise only what needs preloading. */

var bundleReady = charto.production ? System.import(charto.paths.bundle || 'dist/bundle') : Promise.reject();

var preloadReady = bundleReady.catch(function() {
return(Promise.all(charto.preload));
});

// Load the app and print any errors.

preloadReady.then(function() {
return(System.import(main));
}).catch(function(err) {
throw(err.originalErr || err);
});
};

charto.initDevelop = function(done) {
charto.initSystem(function() {
charto.initLoader();
charto.initMonaco().then(charto.initDojo).then(done);
});
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "charto-loader",
"version": "0.1.0",
"description": "Loader tools for Charto style projects",
"main": "index.js",
"bin": {
"fixamd": "bin/fixamd"
},
"author": "Juha Järvi",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/charto/charto-loader.git"
},
"bugs": {
"url": "https://github.com/charto/charto-loader/issues"
},
"homepage": "https://github.com/charto/charto-loader#readme",
"keywords": [
"dojo",
"loader",
"monaco",
"systemjs"
]
}
1 change: 1 addition & 0 deletions process-dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports = { env: { 'NODE_ENV': 'development' } };
1 change: 1 addition & 0 deletions process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports = { env: { 'NODE_ENV': 'production' } };
1 change: 1 addition & 0 deletions undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = undefined;

0 comments on commit 770d8d3

Please sign in to comment.