Skip to content

Commit 36fa685

Browse files
committed
feat(all): systemjs support
Enables support for SystemJS as module loader including new project setup via au new. closes #198
1 parent 9bfe9bc commit 36fa685

11 files changed

Lines changed: 161 additions & 46 deletions

File tree

lib/build/amodro-trace/write/stubs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
var lang = require('../lib/lang');
3+
var packages = require('./packages');
34

45
/**
56
* Replaces module content for a given set of module IDs with stub define calls.
@@ -12,7 +13,9 @@ module.exports = function stubs(options) {
1213
options = options || {};
1314

1415
return function(context, moduleName, filePath, contents) {
15-
if (options.stubModules && options.stubModules.indexOf(moduleName) !== -1) {
16+
if (options.stubModules
17+
&& (options.stubModules.indexOf(moduleName) !== -1
18+
|| options.stubModules.indexOf(packages.getPackageName(context, moduleName)) !== -1)) {
1619
//Just want to insert a simple module definition instead
1720
//of the source module. Useful for plugins that inline
1821
//all their resources.

lib/build/loader-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports.LoaderPlugin = class {
3737
case 'require':
3838
return 'text!' + moduleId + path.extname(filePath);
3939
case 'system':
40-
throw new Error('SystemJS is not yet supported');
40+
return moduleId + path.extname(filePath) + '!text';
4141
default:
4242
throw new Error(`Loader configuration style ${loderConfigType} is not supported.`);
4343
}

lib/build/loader.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const path = require('path');
23

34
exports.createLoaderCode = function createLoaderCode(platform, bundler) {
45
let loaderCode;
@@ -9,7 +10,7 @@ exports.createLoaderCode = function createLoaderCode(platform, bundler) {
910
loaderCode = 'requirejs.config(' + JSON.stringify(exports.createRequireJSConfig(platform, bundler)) + ')';
1011
break;
1112
case 'system':
12-
loaderCode = exports.createSystemJSConfig(platform);
13+
loaderCode = 'window.define=SystemJS.amdDefine; window.require=window.requirejs=SystemJS.amdRequire; SystemJS.config(' + JSON.stringify(exports.createSystemJSConfig(platform, bundler)) + ');';
1314
break;
1415
default:
1516
//TODO: Enhancement: Look at a designated folder for any custom configurations
@@ -76,8 +77,18 @@ exports.createRequireJSConfig = function createRequireJSConfig(platform, bundler
7677
return config;
7778
};
7879

79-
exports.createSystemJSConfig = function createSystemJSConfig(platform) {
80-
throw new Error('SystemJS is not yet supported');
80+
exports.createSystemJSConfig = function createSystemJSConfig(platform, bundler) {
81+
const loaderOptions = bundler.loaderOptions;
82+
const bundles = bundler.bundles;
83+
const configBundleName = loaderOptions.configTarget;
84+
const includeBundles = shouldIncludeBundleMetadata(bundles, loaderOptions);
85+
const location = platform.baseUrl || platform.output;
86+
87+
const config = bundles.map(bundle => systemJSConfigForBundle(bundle, bundler, location, includeBundles))
88+
.filter(bundle => bundle.name !== configBundleName)
89+
.reduce((config, bundle) => bundle.addBundleConfig(config), { map: { "text": "text" } });
90+
91+
return config;
8192
};
8293

8394
function shouldIncludeBundleMetadata(bundles, loaderOptions) {
@@ -96,3 +107,23 @@ function shouldIncludeBundleMetadata(bundles, loaderOptions) {
96107

97108
return setting === true;
98109
}
110+
111+
function systemJSConfigForBundle(bundle, bundler, location, includeBundles) {
112+
const buildOptions = bundler.interpretBuildOptions(bundle.config.options, bundler.buildOptions);
113+
const mapTarget = location + '/' + bundle.moduleId + (buildOptions.rev && bundle.hash ? '-' + bundle.hash : '') + path.extname(bundle.config.name);
114+
const moduleId = bundle.moduleId;
115+
const bundledModuleIds = bundle.getBundledModuleIds();
116+
117+
return {
118+
name: bundle.config.name,
119+
addBundleConfig: function(config) {
120+
config.map[moduleId] = mapTarget;
121+
if(includeBundles) {
122+
config.bundles = (config.bundles || {});
123+
config.bundles[moduleId] = bundledModuleIds;
124+
}
125+
126+
return config;
127+
}
128+
};
129+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const ProjectItem = require('../../../project-item').ProjectItem;
3+
4+
module.exports = function(project) {
5+
project.loader = project.model.loader.id;
6+
delete project.model.loader; // remove loader from model as it is actually a property of model.build
7+
8+
project.loaderTextPlugin = 'text';
9+
project.loaderScript = 'node_modules/requirejs/require.js';
10+
project.addToContent(
11+
ProjectItem.resource('index.html', 'content/require.index.html')
12+
).addToClientDependencies(
13+
'requirejs',
14+
'text'
15+
);
16+
};

lib/commands/new/loaders/system.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const ProjectItem = require('../../../project-item').ProjectItem;
3+
4+
module.exports = function(project) {
5+
project.loader = project.model.loader.id;
6+
delete project.model.loader; // remove loader from model as it is actually a property of model.build
7+
8+
project.loaderTextPlugin = {
9+
'name': 'text',
10+
'path': '../node_modules/systemjs-plugin-text',
11+
'main': 'text'
12+
};
13+
project.loaderScript = 'node_modules/systemjs/dist/system.js';
14+
project.addToContent(
15+
ProjectItem.resource('index.html', 'content/system.index.html')
16+
).addToClientDependencies(
17+
'systemjs',
18+
'systemjs-plugin-text'
19+
);
20+
};

lib/commands/new/new-application.json

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"nextActivity": 2,
88
"state": {
99
"type": "project:application",
10+
"loader": {
11+
"id": "require",
12+
"displayName": "RequireJS"
13+
},
1014
"transpiler": {
1115
"id": "babel",
1216
"displayName": "Babel",
@@ -33,7 +37,7 @@
3337
"editor": {
3438
"id": "vscode",
3539
"displayName": "Visual Studio Code"
36-
},
40+
},
3741
"platform": {
3842
"id": "web",
3943
"displayName": "Web"
@@ -52,6 +56,31 @@
5256
"id": 3,
5357
"type": "input-select",
5458
"nextActivity": 4,
59+
"question": "Which module loader would you like to use?",
60+
"stateProperty": "loader",
61+
"options": [
62+
{
63+
"displayName": "RequireJS",
64+
"description": "A file and module loader for JavaScript.",
65+
"value": {
66+
"id": "require",
67+
"displayName": "RequireJS"
68+
}
69+
},
70+
{
71+
"displayName": "SystemJS",
72+
"description": "Dynamic ES module loader",
73+
"value": {
74+
"id": "system",
75+
"displayName": "SystemJS"
76+
}
77+
}
78+
]
79+
},
80+
{
81+
"id": 4,
82+
"type": "input-select",
83+
"nextActivity": 5,
5584
"question": "Would you like to use the default setup or customize your choices?",
5685
"stateProperty": "defaultOrCustom",
5786
"options": [
@@ -73,28 +102,28 @@
73102
]
74103
},
75104
{
76-
"id": 4,
105+
"id": 5,
77106
"type": "branch-switch",
78107
"stateProperty": "defaultOrCustom",
79108
"branches": [
80109
{
81110
"case": "default-esnext",
82-
"nextActivity": 14
111+
"nextActivity": 15
83112
},
84113
{
85114
"case": "default-typescript",
86-
"nextActivity": 12
115+
"nextActivity": 13
87116
},
88117
{
89118
"case": "custom",
90-
"nextActivity": 5
119+
"nextActivity": 6
91120
}
92121
]
93122
},
94123
{
95-
"id": 5,
124+
"id": 6,
96125
"type": "input-select",
97-
"nextActivity": 6,
126+
"nextActivity": 7,
98127
"question": "What platform are you targeting?",
99128
"stateProperty": "platform",
100129
"options": [
@@ -118,9 +147,9 @@
118147
]
119148
},
120149
{
121-
"id": 6,
150+
"id": 7,
122151
"type": "input-select",
123-
"nextActivity": 7,
152+
"nextActivity": 8,
124153
"question": "What transpiler would you like to use?",
125154
"stateProperty": "transpiler",
126155
"options": [
@@ -145,9 +174,9 @@
145174
]
146175
},
147176
{
148-
"id": 7,
177+
"id": 8,
149178
"type": "input-select",
150-
"nextActivity": 8,
179+
"nextActivity": 9,
151180
"question": "How would you like to setup your template?",
152181
"stateProperty": "markupProcessor",
153182
"options": [
@@ -181,9 +210,9 @@
181210
]
182211
},
183212
{
184-
"id": 8,
213+
"id": 9,
185214
"type": "input-select",
186-
"nextActivity": 9,
215+
"nextActivity": 10,
187216
"question": "What css processor would you like to use?",
188217
"stateProperty": "cssProcessor",
189218
"options": [
@@ -235,9 +264,9 @@
235264
]
236265
},
237266
{
238-
"id": 9,
267+
"id": 10,
239268
"type": "input-select",
240-
"nextActivity": 10,
269+
"nextActivity": 11,
241270
"question": "Would you like to configure unit testing?",
242271
"stateProperty": "unitTestRunner",
243272
"options": [
@@ -260,24 +289,24 @@
260289
]
261290
},
262291
{
263-
"id": 10,
292+
"id": 11,
264293
"type": "branch-switch",
265294
"stateProperty": "platform",
266295
"branches": [
267296
{
268297
"case": "aspnetcore",
269-
"nextActivity": 13
298+
"nextActivity": 14
270299
},
271300
{
272301
"case": "default",
273-
"nextActivity": 11
302+
"nextActivity": 12
274303
}
275304
]
276305
},
277306
{
278-
"id": 11,
307+
"id": 12,
279308
"type": "input-select",
280-
"nextActivity": 14,
309+
"nextActivity": 15,
281310
"question": "What is your default code editor?",
282311
"stateProperty": "editor",
283312
"options": [
@@ -324,9 +353,9 @@
324353
]
325354
},
326355
{
327-
"id": 12,
356+
"id": 13,
328357
"type": "state-assign",
329-
"nextActivity": 14,
358+
"nextActivity": 15,
330359
"state": {
331360
"transpiler": {
332361
"id": "typescript",
@@ -336,9 +365,9 @@
336365
}
337366
},
338367
{
339-
"id": 13,
368+
"id": 14,
340369
"type": "state-assign",
341-
"nextActivity": 14,
370+
"nextActivity": 15,
342371
"state": {
343372
"editor": {
344373
"id": "visualstudio",
@@ -347,17 +376,17 @@
347376
}
348377
},
349378
{
350-
"id": 14,
379+
"id": 15,
351380
"type": "project-create",
352-
"nextActivity": 15,
353-
"restartActivity": 16
381+
"nextActivity": 16,
382+
"restartActivity": 17
354383
},
355384
{
356-
"id": 15,
385+
"id": 16,
357386
"type": "project-install"
358387
},
359388
{
360-
"id": 16,
389+
"id": 17,
361390
"type": "state-assign",
362391
"nextActivity": 1,
363392
"state": {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

33
module.exports = function(project) {
4-
project.configureVisualStudioStructure();
4+
project.configureVisualStudioStructure(project.model.loader.id);
55
project.configureDefaultSetup();
66
};

0 commit comments

Comments
 (0)