Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added amd-loader #1271

Open
wants to merge 12 commits into
base: wysiwyg
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import * as code from './code';

class MyResourceLoader extends ResourceLoader {
get(url: string): Promise<string> {
let searchString = url.replace(/[\/\.-]/gi, '_');

// TODO(sancheez): fix load templates with relative url (relative url in Component)
if (url.startsWith(".")) {
searchString = searchString.substring(2);
}

const templateId = Object.keys(code).find((key) =>
key.includes(url.replace(/[\/\.-]/gi, '_'))
key.includes(searchString)
);
const template = code[templateId];
if (!template) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import * as code from './code';

class MyResourceLoader extends ResourceLoader {
get(url: string): Promise<string> {
let searchString = url.replace(/[\/\.-]/gi, '_');

// TODO(sancheez): fix load templates with relative url (relative url in Component)
if (url.startsWith(".")) {
searchString = searchString.substring(2);
}

const templateId = Object.keys(code).find((key) =>
key.includes(url.replace(/[\/\.-]/gi, '_'))
key.includes(searchString)
);
const template = code[templateId];
if (!template) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ import * as code from './code';

class MyResourceLoader extends ResourceLoader {
get(url: string): Promise<string> {

let searchString = url.replace(/[\/\.-]/gi, '_');

// TODO(sancheez): fix load templates with relative url (relative url in Component)
if (url.startsWith(".")) {
searchString = searchString.substring(2);
}

const templateId = Object.keys(code).find((key) =>
key.includes(url.replace(/[\/\.-]/gi, '_'))
key.includes(searchString)
);

const template = code[templateId];
if (!template) {
console.log(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,11 @@ import {
SimpleChanges,
ViewChild,
} from '@angular/core';
import babel_traverse from '@babel/traverse';
import * as babylon from 'babylon';
import * as babel_types from 'babel-types';
import { addMetaInformation, createSystemJsSandbox } from '@codelab/code-demos';
import { ScriptLoaderService } from '@codelab/sandbox-runner';
import { BehaviorSubject, Subscription } from 'rxjs';
import { handleTestMessage } from './tests';
import { TestRunResult } from '@codelab/utils';
import { createSystemJsSandbox } from '@codelab/code-demos';
import { getTypeScript, ScriptLoaderService } from '@codelab/sandbox-runner';

const ts = getTypeScript();

// TODO(kirjs): This is a duplicate
export function addMetaInformation(sandbox, files: { [key: string]: string }) {
// sandbox.evalJs(`System.registry.delete(System.normalizeSync('./code'));`);

(sandbox.iframe.contentWindow as any).System.register(
'code',
[],
function (exports) {
return {
setters: [],
execute: function () {
exports('ts', ts);
exports('babylon', babylon);
exports('babel_traverse', babel_traverse);
exports('babel_types', babel_types);
Object.entries(files)
.filter(([moduleName]) => moduleName.match(/\.ts$/))
.forEach(([path, code]) => {
exports(path.replace(/[\/.-]/gi, '_'), code);
exports(
path.replace(/[\/.-]/gi, '_') + '_AST',
ts.createSourceFile(path, code, ts.ScriptTarget.ES5)
);
});

Object.entries(files)
.filter(([moduleName]) => moduleName.match(/\.html/))
.forEach(([path, code]) => {
const templatePath = path.replace(/[\/.-]/gi, '_');
exports(templatePath, code);
});
},
};
}
);
}
import { handleTestMessage } from './tests';

@Component({
selector: 'codelab-simple-angular-test-runner',
Expand Down Expand Up @@ -123,6 +81,11 @@ export class SimpleAngularTestRunnerComponent
{
id: 'testing',
url: '/assets/runner',
},
({ evalJs }) => {
evalJs(this.scriptLoaderService.getScript('chai'));
evalJs(this.scriptLoaderService.getScript('zone'));
evalJs(this.scriptLoaderService.getScript('reflectMetadata'));
}
);

Expand All @@ -131,21 +94,19 @@ export class SimpleAngularTestRunnerComponent
'<app-root></app-root><my-app></my-app><div class="error"></div>'
);

sandbox.evalJs(this.scriptLoaderService.getScript('chai'));
sandbox.evalJs(this.scriptLoaderService.getScript('mocha'));
sandbox.evalJs(this.scriptLoaderService.getScript('test-bootstrap'));
sandbox.evalJs(this.scriptLoaderService.getScript('shim'));
sandbox.evalJs(this.scriptLoaderService.getScript('zone'));
// sandbox.evalJs(this.scriptLoaderService.getScript('system-config'));
sandbox.evalJs(this.scriptLoaderService.getScript('ng-bundle'));

this.subscription = this.changedFilesSubject.subscribe((files) => {
addMetaInformation(sandbox, this.code);


const hasErrors = Object.entries(files)
.filter(([path]) => path.match(/\.js$/))
.filter(([path]) => path.match(/\.js$/) && !path.startsWith('code'))
.map(([path, code]) => {
try {
sandbox.evalJs(`console.log('I remove modul')`);
addMetaInformation(sandbox, this.code);

sandbox.evalJs(`console.log('I remove module')`);
sandbox.evalJs(code);
} catch (e) {
console.groupCollapsed(e.message);
Expand All @@ -158,7 +119,7 @@ export class SimpleAngularTestRunnerComponent
.some((a) => a);

if (!hasErrors) {
sandbox.evalJs(`System.import('${this.bootstrap}')`);
sandbox.evalJs(`System.import('./${this.bootstrap}')`);
}
});
}
Expand Down
10 changes: 9 additions & 1 deletion apps/codelab/src/app/shared/angular-code/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { AppModule } from './app.module';

class MyResourceLoader extends ResourceLoader {
get(url: string): Promise<string> {

let searchString = url.replace(/[\/\.-]/gi, '_');

// TODO(sancheez): fix load templates with relative url (relative url in Component)
if (url.startsWith(".")) {
searchString = searchString.substring(2);
}

const templateId = Object.keys(code).find((key) =>
key.includes(url.replace(/[\/\.-]/gi, '_'))
key.includes(searchString)
);
const template = code[templateId];
if (!template) {
Expand Down
Loading