fix(compiler): support css stylesheets in offline compiler
This commit is contained in:
@ -60,8 +60,7 @@ export function main() {
|
||||
templateUrl: 'someTemplateUrl',
|
||||
styles: ['someStyle'],
|
||||
styleUrls: ['someStyleUrl'],
|
||||
ngContentSelectors: ['*'],
|
||||
baseUrl: 'someBaseUrl'
|
||||
ngContentSelectors: ['*']
|
||||
});
|
||||
fullDirectiveMeta = CompileDirectiveMetadata.create({
|
||||
selector: 'someSelector',
|
||||
|
@ -21,10 +21,15 @@ import {TEST_PROVIDERS} from './test_bindings';
|
||||
export function main() {
|
||||
describe('DirectiveNormalizer', () => {
|
||||
var dirType: CompileTypeMetadata;
|
||||
var dirTypeWithHttpUrl: CompileTypeMetadata;
|
||||
|
||||
beforeEachProviders(() => TEST_PROVIDERS);
|
||||
|
||||
beforeEach(() => { dirType = new CompileTypeMetadata({name: 'SomeComp'}); });
|
||||
beforeEach(() => {
|
||||
dirType = new CompileTypeMetadata({moduleUrl: 'package:some/module/a.js', name: 'SomeComp'});
|
||||
dirTypeWithHttpUrl =
|
||||
new CompileTypeMetadata({moduleUrl: 'http://some/module/a.js', name: 'SomeComp'});
|
||||
});
|
||||
|
||||
describe('loadTemplate', () => {
|
||||
describe('inline template', () => {
|
||||
@ -36,8 +41,7 @@ export function main() {
|
||||
template: 'a',
|
||||
templateUrl: null,
|
||||
styles: [],
|
||||
styleUrls: ['test.css'],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: ['test.css']
|
||||
}))
|
||||
.then((template: CompileTemplateMetadata) => {
|
||||
expect(template.template).toEqual('a');
|
||||
@ -46,7 +50,7 @@ export function main() {
|
||||
});
|
||||
}));
|
||||
|
||||
it('should resolve styles on the annotation against the baseUrl',
|
||||
it('should resolve styles on the annotation against the moduleUrl',
|
||||
inject([AsyncTestCompleter, DirectiveNormalizer],
|
||||
(async, normalizer: DirectiveNormalizer) => {
|
||||
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
||||
@ -54,8 +58,7 @@ export function main() {
|
||||
template: '',
|
||||
templateUrl: null,
|
||||
styles: [],
|
||||
styleUrls: ['test.css'],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: ['test.css']
|
||||
}))
|
||||
.then((template: CompileTemplateMetadata) => {
|
||||
expect(template.styleUrls).toEqual(['package:some/module/test.css']);
|
||||
@ -63,7 +66,7 @@ export function main() {
|
||||
});
|
||||
}));
|
||||
|
||||
it('should resolve styles in the template against the baseUrl',
|
||||
it('should resolve styles in the template against the moduleUrl',
|
||||
inject([AsyncTestCompleter, DirectiveNormalizer],
|
||||
(async, normalizer: DirectiveNormalizer) => {
|
||||
normalizer.normalizeTemplate(dirType, new CompileTemplateMetadata({
|
||||
@ -71,8 +74,7 @@ export function main() {
|
||||
template: '<style>@import test.css</style>',
|
||||
templateUrl: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: []
|
||||
}))
|
||||
.then((template: CompileTemplateMetadata) => {
|
||||
expect(template.styleUrls).toEqual(['package:some/module/test.css']);
|
||||
@ -83,7 +85,7 @@ export function main() {
|
||||
|
||||
describe('templateUrl', () => {
|
||||
|
||||
it('should load a template from a url that is resolved against baseUrl',
|
||||
it('should load a template from a url that is resolved against moduleUrl',
|
||||
inject([AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async, normalizer: DirectiveNormalizer, xhr: MockXHR) => {
|
||||
xhr.expect('package:some/module/sometplurl.html', 'a');
|
||||
@ -92,8 +94,7 @@ export function main() {
|
||||
template: null,
|
||||
templateUrl: 'sometplurl.html',
|
||||
styles: [],
|
||||
styleUrls: ['test.css'],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: ['test.css']
|
||||
}))
|
||||
.then((template: CompileTemplateMetadata) => {
|
||||
expect(template.template).toEqual('a');
|
||||
@ -104,7 +105,7 @@ export function main() {
|
||||
xhr.flush();
|
||||
}));
|
||||
|
||||
it('should resolve styles on the annotation against the baseUrl',
|
||||
it('should resolve styles on the annotation against the moduleUrl',
|
||||
inject([AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async, normalizer: DirectiveNormalizer, xhr: MockXHR) => {
|
||||
xhr.expect('package:some/module/tpl/sometplurl.html', '');
|
||||
@ -113,8 +114,7 @@ export function main() {
|
||||
template: null,
|
||||
templateUrl: 'tpl/sometplurl.html',
|
||||
styles: [],
|
||||
styleUrls: ['test.css'],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: ['test.css']
|
||||
}))
|
||||
.then((template: CompileTemplateMetadata) => {
|
||||
expect(template.styleUrls).toEqual(['package:some/module/test.css']);
|
||||
@ -133,8 +133,7 @@ export function main() {
|
||||
template: null,
|
||||
templateUrl: 'tpl/sometplurl.html',
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: []
|
||||
}))
|
||||
.then((template: CompileTemplateMetadata) => {
|
||||
expect(template.styleUrls).toEqual(['package:some/module/tpl/test.css']);
|
||||
@ -160,50 +159,36 @@ export function main() {
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
|
||||
var viewEncapsulation = ViewEncapsulation.Native;
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: viewEncapsulation,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'', 'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata(
|
||||
{encapsulation: viewEncapsulation, styles: [], styleUrls: []}),
|
||||
'', 'package:some/module/');
|
||||
expect(template.encapsulation).toBe(viewEncapsulation);
|
||||
}));
|
||||
|
||||
it('should keep the template as html',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'a', 'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}), 'a',
|
||||
'package:some/module/');
|
||||
expect(template.template).toEqual('a')
|
||||
}));
|
||||
|
||||
it('should collect ngContent',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'<ng-content select="a"></ng-content>',
|
||||
'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<ng-content select="a"></ng-content>', 'package:some/module/');
|
||||
expect(template.ngContentSelectors).toEqual(['a']);
|
||||
}));
|
||||
|
||||
it('should normalize ngContent wildcard selector',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<ng-content></ng-content><ng-content select></ng-content><ng-content select="*"></ng-content>',
|
||||
'package:some/module/');
|
||||
expect(template.ngContentSelectors).toEqual(['*', '*', '*']);
|
||||
@ -211,91 +196,64 @@ export function main() {
|
||||
|
||||
it('should collect top level styles in the template',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template =
|
||||
normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'<style>a</style>', 'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<style>a</style>', 'package:some/module/');
|
||||
expect(template.styles).toEqual(['a']);
|
||||
}));
|
||||
|
||||
it('should collect styles inside in elements',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'<div><style>a</style></div>',
|
||||
'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<div><style>a</style></div>', 'package:some/module/');
|
||||
expect(template.styles).toEqual(['a']);
|
||||
}));
|
||||
|
||||
it('should collect styleUrls in the template',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'<link rel="stylesheet" href="aUrl">',
|
||||
'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<link rel="stylesheet" href="aUrl">', 'package:some/module/');
|
||||
expect(template.styleUrls).toEqual(['package:some/module/aUrl']);
|
||||
}));
|
||||
|
||||
it('should collect styleUrls in elements',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<div><link rel="stylesheet" href="aUrl"></div>', 'package:some/module/');
|
||||
expect(template.styleUrls).toEqual(['package:some/module/aUrl']);
|
||||
}));
|
||||
|
||||
it('should ignore link elements with non stylesheet rel attribute',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'<link href="b" rel="a">',
|
||||
'package:some/module/');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<link href="b" rel="a">', 'package:some/module/');
|
||||
expect(template.styleUrls).toEqual([]);
|
||||
}));
|
||||
|
||||
it('should ignore link elements with absolute urls but non package: scheme',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<link href="http://some/external.css" rel="stylesheet">', 'package:some/module/');
|
||||
expect(template.styleUrls).toEqual([]);
|
||||
}));
|
||||
|
||||
it('should extract @import style urls into styleAbsUrl',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: ['@import "test.css";'],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'', 'package:some/module/id');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata(
|
||||
{encapsulation: null, styles: ['@import "test.css";'], styleUrls: []}),
|
||||
'', 'package:some/module/id');
|
||||
expect(template.styles).toEqual(['']);
|
||||
expect(template.styleUrls).toEqual(['package:some/module/test.css']);
|
||||
}));
|
||||
@ -306,8 +264,7 @@ export function main() {
|
||||
dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: ['.foo{background-image: url(\'double.jpg\');'],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
styleUrls: []
|
||||
}),
|
||||
'', 'package:some/module/id');
|
||||
expect(template.styles).toEqual(['.foo{background-image: url(\'double.jpg\');']);
|
||||
@ -315,52 +272,38 @@ export function main() {
|
||||
|
||||
it('should resolve relative style urls in styleUrls',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: ['test.css'],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'', 'package:some/module/id');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata(
|
||||
{encapsulation: null, styles: [], styleUrls: ['test.css']}),
|
||||
'', 'package:some/module/id');
|
||||
expect(template.styles).toEqual([]);
|
||||
expect(template.styleUrls).toEqual(['package:some/module/test.css']);
|
||||
}));
|
||||
|
||||
it('should resolve relative style urls in styleUrls with http directive url',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: ['test.css'],
|
||||
baseUrl: 'http://some/module/a.js'
|
||||
}),
|
||||
'', 'http://some/module/id');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirTypeWithHttpUrl, new CompileTemplateMetadata(
|
||||
{encapsulation: null, styles: [], styleUrls: ['test.css']}),
|
||||
'', 'http://some/module/id');
|
||||
expect(template.styles).toEqual([]);
|
||||
expect(template.styleUrls).toEqual(['http://some/module/test.css']);
|
||||
}));
|
||||
|
||||
it('should normalize ViewEncapsulation.Emulated to ViewEncapsulation.None if there are no styles nor stylesheets',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template =
|
||||
normalizer.normalizeLoadedTemplate(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: ViewEncapsulation.Emulated,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
'', 'package:some/module/id');
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata(
|
||||
{encapsulation: ViewEncapsulation.Emulated, styles: [], styleUrls: []}),
|
||||
'', 'package:some/module/id');
|
||||
expect(template.encapsulation).toEqual(ViewEncapsulation.None);
|
||||
}));
|
||||
|
||||
it('should ignore ng-content in elements with ngNonBindable',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<div ngNonBindable><ng-content select="a"></ng-content></div>',
|
||||
'package:some/module/');
|
||||
expect(template.ngContentSelectors).toEqual([]);
|
||||
@ -369,12 +312,8 @@ export function main() {
|
||||
it('should still collect <style> in elements with ngNonBindable',
|
||||
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
|
||||
var template = normalizer.normalizeLoadedTemplate(
|
||||
dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
styles: [],
|
||||
styleUrls: [],
|
||||
baseUrl: 'package:some/module/a.js'
|
||||
}),
|
||||
dirType,
|
||||
new CompileTemplateMetadata({encapsulation: null, styles: [], styleUrls: []}),
|
||||
'<div ngNonBindable><style>div {color:red}</style></div>', 'package:some/module/');
|
||||
expect(template.styles).toEqual(['div {color:red}']);
|
||||
}));
|
||||
|
@ -62,13 +62,12 @@ export function main() {
|
||||
expect(meta.template.styleUrls).toEqual(['someStyleUrl']);
|
||||
expect(meta.template.template).toEqual('someTemplate');
|
||||
expect(meta.template.templateUrl).toEqual('someTemplateUrl');
|
||||
expect(meta.template.baseUrl).toEqual(`package:someModuleId${MODULE_SUFFIX}`);
|
||||
}));
|
||||
|
||||
it('should use the moduleUrl from the reflector if none is given',
|
||||
inject([CompileMetadataResolver], (resolver: CompileMetadataResolver) => {
|
||||
var value: string =
|
||||
resolver.getDirectiveMetadata(ComponentWithoutModuleId).template.baseUrl;
|
||||
resolver.getDirectiveMetadata(ComponentWithoutModuleId).type.moduleUrl;
|
||||
var expectedEndValue =
|
||||
IS_DART ? 'test/compiler/metadata_resolver_spec.dart' : './ComponentWithoutModuleId';
|
||||
expect(value.endsWith(expectedEndValue)).toBe(true);
|
||||
|
@ -2,15 +2,17 @@
|
||||
import {print, IS_DART} from '../src/facade/lang';
|
||||
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
||||
import {DartEmitter} from '@angular/compiler/src/output/dart_emitter';
|
||||
import {DartImportGenerator} from '@angular/compiler/src/output/dart_imports';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {compileComp, compAMetadata} from './offline_compiler_util';
|
||||
import {ComponentFactory} from '@angular/core/src/linker/component_factory';
|
||||
import {CompA} from './offline_compiler_util';
|
||||
import {CompA, SimpleJsImportGenerator} from './offline_compiler_util';
|
||||
|
||||
export const CompANgFactory: ComponentFactory<CompA> = null;
|
||||
|
||||
export function emit(): Promise<string> {
|
||||
var emitter = IS_DART ? new DartEmitter() : new TypeScriptEmitter();
|
||||
var emitter = IS_DART ? new DartEmitter(new DartImportGenerator()) :
|
||||
new TypeScriptEmitter(new SimpleJsImportGenerator());
|
||||
return compileComp(emitter, compAMetadata);
|
||||
}
|
||||
|
||||
|
@ -3,12 +3,12 @@ import {print} from '../src/facade/lang';
|
||||
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
|
||||
import {compileComp, compAMetadata} from './offline_compiler_util';
|
||||
import {ComponentFactory} from '@angular/core/src/linker/component_factory';
|
||||
import {CompA} from './offline_compiler_util';
|
||||
import {CompA, SimpleJsImportGenerator} from './offline_compiler_util';
|
||||
|
||||
export const CompANgFactory: ComponentFactory<CompA> = null;
|
||||
|
||||
export function emit() {
|
||||
var emitter = new JavaScriptEmitter();
|
||||
var emitter = new JavaScriptEmitter(new SimpleJsImportGenerator());
|
||||
return compileComp(emitter, compAMetadata);
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {print, IS_DART} from '../src/facade/lang';
|
||||
import {print, isPresent, IS_DART} from '../src/facade/lang';
|
||||
import {OutputEmitter} from '@angular/compiler/src/output/abstract_emitter';
|
||||
import {Console} from '../core_private';
|
||||
|
||||
@ -19,6 +19,7 @@ import {createOfflineCompileUrlResolver} from '@angular/compiler/src/url_resolve
|
||||
import {MockSchemaRegistry} from '../testing/schema_registry_mock';
|
||||
import {MODULE_SUFFIX} from '@angular/compiler/src/util';
|
||||
import {MockXHR} from '../testing/xhr_mock';
|
||||
import {ImportGenerator} from '@angular/compiler/src/output/path_util';
|
||||
|
||||
import {
|
||||
CompileDirectiveMetadata,
|
||||
@ -29,7 +30,7 @@ import {
|
||||
|
||||
export class CompA { user: string; }
|
||||
|
||||
var THIS_MODULE_PATH = `asset:angular2/test/compiler`;
|
||||
var THIS_MODULE_PATH = `asset:@angular/lib/compiler/test`;
|
||||
var THIS_MODULE_URL = `${THIS_MODULE_PATH}/offline_compiler_util${MODULE_SUFFIX}`;
|
||||
|
||||
export var compAMetadata = CompileDirectiveMetadata.create({
|
||||
@ -40,8 +41,7 @@ export var compAMetadata = CompileDirectiveMetadata.create({
|
||||
template: new CompileTemplateMetadata({
|
||||
templateUrl: './offline_compiler_compa.html',
|
||||
styles: ['.redStyle { color: red; }'],
|
||||
styleUrls: ['./offline_compiler_compa.css'],
|
||||
baseUrl: THIS_MODULE_URL,
|
||||
styleUrls: ['./offline_compiler_compa.css']
|
||||
})
|
||||
});
|
||||
|
||||
@ -54,7 +54,7 @@ function _createOfflineCompiler(xhr: MockXHR, emitter: OutputEmitter): OfflineCo
|
||||
normalizer, new TemplateParser(new Parser(new Lexer()), new MockSchemaRegistry({}, {}),
|
||||
htmlParser, new Console(), []),
|
||||
new StyleCompiler(urlResolver), new ViewCompiler(new CompilerConfig(true, true, true)),
|
||||
emitter);
|
||||
emitter, xhr);
|
||||
}
|
||||
|
||||
export function compileComp(emitter: OutputEmitter,
|
||||
@ -68,3 +68,15 @@ export function compileComp(emitter: OutputEmitter,
|
||||
xhr.flush();
|
||||
return result;
|
||||
}
|
||||
|
||||
export class SimpleJsImportGenerator implements ImportGenerator {
|
||||
getImportPath(moduleUrlStr: string, importedUrlStr: string): string {
|
||||
// var moduleAssetUrl = ImportGenerator.parseAssetUrl(moduleUrlStr);
|
||||
var importedAssetUrl = ImportGenerator.parseAssetUrl(importedUrlStr);
|
||||
if (isPresent(importedAssetUrl)) {
|
||||
return `${importedAssetUrl.packageName}/${importedAssetUrl.modulePath}`;
|
||||
} else {
|
||||
return importedUrlStr;
|
||||
}
|
||||
}
|
||||
}
|
@ -13,6 +13,7 @@ import {isBlank} from '../../src/facade/lang';
|
||||
import {DartEmitter} from '@angular/compiler/src/output/dart_emitter';
|
||||
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {DartImportGenerator} from '@angular/compiler/src/output/dart_imports';
|
||||
|
||||
var someModuleUrl = 'asset:somePackage/lib/somePath';
|
||||
var anotherModuleUrl = 'asset:somePackage/lib/someOtherPath';
|
||||
@ -36,7 +37,7 @@ export function main() {
|
||||
var someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new DartEmitter();
|
||||
emitter = new DartEmitter(new DartImportGenerator());
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
|
64
modules/@angular/compiler/test/output/dart_imports_spec.ts
Normal file
64
modules/@angular/compiler/test/output/dart_imports_spec.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {DartImportGenerator} from '@angular/compiler/src/output/dart_imports';
|
||||
|
||||
export function main() {
|
||||
describe('DartImportGenerator', () => {
|
||||
describe('getImportPath', () => {
|
||||
var generator: DartImportGenerator;
|
||||
beforeEach(() => { generator = new DartImportGenerator(); });
|
||||
|
||||
it('should calculate relative paths Dart', () => {
|
||||
expect(generator.getImportPath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/impPath'))
|
||||
.toEqual('impPath');
|
||||
});
|
||||
|
||||
it('should calculate relative paths for different constellations', () => {
|
||||
expect(generator.getImportPath('asset:somePkg/test/modPath', 'asset:somePkg/test/impPath'))
|
||||
.toEqual('impPath');
|
||||
expect(
|
||||
generator.getImportPath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/dir2/impPath'))
|
||||
.toEqual('dir2/impPath');
|
||||
expect(
|
||||
generator.getImportPath('asset:somePkg/lib/dir1/modPath', 'asset:somePkg/lib/impPath'))
|
||||
.toEqual('../impPath');
|
||||
expect(generator.getImportPath('asset:somePkg/lib/dir1/modPath',
|
||||
'asset:somePkg/lib/dir2/impPath'))
|
||||
.toEqual('../dir2/impPath');
|
||||
});
|
||||
|
||||
it('should calculate absolute paths', () => {
|
||||
expect(
|
||||
generator.getImportPath('asset:somePkg/lib/modPath', 'asset:someOtherPkg/lib/impPath'))
|
||||
.toEqual('package:someOtherPkg/impPath');
|
||||
});
|
||||
|
||||
it('should not allow absolute imports of non lib modules', () => {
|
||||
expect(() => generator.getImportPath('asset:somePkg/lib/modPath',
|
||||
'asset:somePkg/test/impPath'))
|
||||
.toThrowError(
|
||||
`Can't import url asset:somePkg/test/impPath from asset:somePkg/lib/modPath`);
|
||||
});
|
||||
|
||||
it('should not allow non asset urls as base url', () => {
|
||||
expect(
|
||||
() => generator.getImportPath('http:somePkg/lib/modPath', 'asset:somePkg/test/impPath'))
|
||||
.toThrowError(`Url http:somePkg/lib/modPath is not a valid asset: url`);
|
||||
});
|
||||
|
||||
it('should allow non asset urls as import urls and pass them through', () => {
|
||||
expect(generator.getImportPath('asset:somePkg/lib/modPath', 'dart:html'))
|
||||
.toEqual('dart:html');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -13,6 +13,7 @@ import {isBlank} from '../../src/facade/lang';
|
||||
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
|
||||
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {SimpleJsImportGenerator} from '../offline_compiler_util';
|
||||
|
||||
var someModuleUrl = 'asset:somePackage/lib/somePath';
|
||||
var anotherModuleUrl = 'asset:somePackage/lib/someOtherPath';
|
||||
@ -33,7 +34,7 @@ export function main() {
|
||||
var someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new JavaScriptEmitter();
|
||||
emitter = new JavaScriptEmitter(new SimpleJsImportGenerator());
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
@ -111,9 +112,10 @@ export function main() {
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt()))
|
||||
.toEqual(
|
||||
[`var import0 = re` + `quire('./someOtherPath');`, `import0.someExternalId;`].join(
|
||||
'\n'));
|
||||
.toEqual([
|
||||
`var import0 = re` + `quire('somePackage/someOtherPath');`,
|
||||
`import0.someExternalId;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
|
@ -4,8 +4,10 @@ import {unimplemented} from '../../src/facade/exceptions';
|
||||
import {codegenExportsVars, codegenStmts} from './output_emitter_util';
|
||||
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
||||
import {DartEmitter} from '@angular/compiler/src/output/dart_emitter';
|
||||
import {DartImportGenerator} from '@angular/compiler/src/output/dart_imports';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {assetUrl} from '../../src/util';
|
||||
import {SimpleJsImportGenerator} from '../offline_compiler_util';
|
||||
|
||||
export function getExpressions(): any {
|
||||
return unimplemented();
|
||||
@ -13,7 +15,8 @@ export function getExpressions(): any {
|
||||
|
||||
// Generator
|
||||
export function emit() {
|
||||
var emitter = IS_DART ? new DartEmitter() : new TypeScriptEmitter();
|
||||
var emitter = IS_DART ? new DartEmitter(new DartImportGenerator()) :
|
||||
new TypeScriptEmitter(new SimpleJsImportGenerator());
|
||||
var emittedCode =
|
||||
emitter.emitStatements(assetUrl('compiler', 'output/output_emitter_codegen_typed', 'test'),
|
||||
codegenStmts, codegenExportsVars);
|
||||
|
@ -4,6 +4,7 @@ import {unimplemented} from '../../src/facade/exceptions';
|
||||
import {codegenExportsVars, codegenStmts} from './output_emitter_util';
|
||||
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
|
||||
import {assetUrl} from '../../src/util';
|
||||
import {SimpleJsImportGenerator} from '../offline_compiler_util';
|
||||
|
||||
export function getExpressions(): any {
|
||||
return unimplemented();
|
||||
@ -11,7 +12,7 @@ export function getExpressions(): any {
|
||||
|
||||
// Generator
|
||||
export function emit() {
|
||||
var emitter = new JavaScriptEmitter();
|
||||
var emitter = new JavaScriptEmitter(new SimpleJsImportGenerator());
|
||||
var emittedCode =
|
||||
emitter.emitStatements(assetUrl('compiler', 'output/output_emitter_codegen_untyped', 'test'),
|
||||
codegenStmts, codegenExportsVars);
|
||||
|
@ -15,7 +15,7 @@ export class ExternalClass {
|
||||
|
||||
var testDataIdentifier = new CompileIdentifierMetadata({
|
||||
name: 'ExternalClass',
|
||||
moduleUrl: assetUrl('compiler', 'output/output_emitter_util'),
|
||||
moduleUrl: `asset:@angular/lib/compiler/test/output/output_emitter_util`,
|
||||
runtime: ExternalClass
|
||||
});
|
||||
|
||||
|
@ -1,69 +0,0 @@
|
||||
import {
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
xit,
|
||||
} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {getImportModulePath, ImportEnv} from '@angular/compiler/src/output/path_util';
|
||||
|
||||
export function main() {
|
||||
describe('PathUtils', () => {
|
||||
describe('getImportModulePath', () => {
|
||||
it('should calculate relative paths for JS and Dart', () => {
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('./impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toEqual('impPath');
|
||||
});
|
||||
|
||||
it('should calculate relative paths for different constellations', () => {
|
||||
expect(getImportModulePath('asset:somePkg/test/modPath', 'asset:somePkg/test/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('./impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/lib/dir2/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('./dir2/impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/dir1/modPath', 'asset:somePkg/lib/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('../impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/dir1/modPath',
|
||||
'asset:somePkg/lib/dir2/impPath', ImportEnv.JS))
|
||||
.toEqual('../dir2/impPath');
|
||||
});
|
||||
|
||||
it('should calculate absolute paths for JS and Dart', () => {
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:someOtherPkg/lib/impPath',
|
||||
ImportEnv.JS))
|
||||
.toEqual('someOtherPkg/impPath');
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'asset:someOtherPkg/lib/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toEqual('package:someOtherPkg/impPath');
|
||||
});
|
||||
|
||||
it('should not allow absolute imports of non lib modules', () => {
|
||||
expect(() => getImportModulePath('asset:somePkg/lib/modPath', 'asset:somePkg/test/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toThrowError(
|
||||
`Can't import url asset:somePkg/test/impPath from asset:somePkg/lib/modPath`);
|
||||
});
|
||||
|
||||
it('should not allow non asset urls as base url', () => {
|
||||
expect(() => getImportModulePath('http:somePkg/lib/modPath', 'asset:somePkg/test/impPath',
|
||||
ImportEnv.Dart))
|
||||
.toThrowError(`Url http:somePkg/lib/modPath is not a valid asset: url`);
|
||||
});
|
||||
|
||||
it('should allow non asset urls as import urls and pass them through', () => {
|
||||
expect(getImportModulePath('asset:somePkg/lib/modPath', 'dart:html', ImportEnv.Dart))
|
||||
.toEqual('dart:html');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -13,6 +13,7 @@ import {isBlank} from '../../src/facade/lang';
|
||||
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
||||
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {SimpleJsImportGenerator} from '../offline_compiler_util';
|
||||
|
||||
var someModuleUrl = 'asset:somePackage/lib/somePath';
|
||||
var anotherModuleUrl = 'asset:somePackage/lib/someOtherPath';
|
||||
@ -33,7 +34,7 @@ export function main() {
|
||||
var someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
emitter = new TypeScriptEmitter();
|
||||
emitter = new TypeScriptEmitter(new SimpleJsImportGenerator());
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
@ -112,8 +113,10 @@ export function main() {
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt()))
|
||||
.toEqual([`import * as import0 from './someOtherPath';`, `import0.someExternalId;`].join(
|
||||
'\n'));
|
||||
.toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`import0.someExternalId;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
@ -302,7 +305,7 @@ export function main() {
|
||||
.toEqual('var a:someLocalId = null;');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier))))
|
||||
.toEqual([
|
||||
`import * as import0 from './someOtherPath';`,
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`var a:import0.someExternalId = null;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
Reference in New Issue
Block a user