2017-05-17 21 views
1

나는이 문서에 따라, 타이프 라이터로 SystemJs를 사용하는 것을 시도하고 실패 , Chrome에서 index.html을 위반하면 다음과 같은 오류가 표시됩니다. 증빙이 발생하지 않았 음을 나타냅니다.SystemJs는

common.js:85 Uncaught (in promise) Error: Unexpected token : 
    Evaluating http://localhost:8080/src/main.ts 
    Loading src/main.ts 
    ... 

무엇이 문제입니까? 이에 대한 검사를 SystemJS export 또는 import 같은 ES6 기능이 포함되어 있지 않습니다 - 그것은 코드가 transpiled 될 필요가 있음을 감지하지 못하기 때문에 SystemJS는 main.ts에서 소스 코드를 transpile하는 타이프 라이터를 사용하지 않기 때문에

+0

일반적으로 브라우저에서 transpile을하는 것은 좋지 않은 방법이라고 생각합니다. 일반적으로 브라우저에서'node_modules'에 도달하는 대신 브라우저가 가져 오는 서버의'dist' (또는 유사한) 폴더에'.js' 출력을 저장하여 빌드시에 typescript를 실행하는 것이 좋습니다. 예제와 같은 클라이언트 측 코드가 표시됩니다. 클라이언트 쪽 transpile이 유용하지만 특별한 경우가있을 수 있습니다. –

답변

1

오류가 발생합니다.

이것이 바로 transpiler 옵션의 작동 방식입니다. transpiler: 'typescript'을 typescript 코드로 사용하려면 SystemJS가 es6을 javascript로 변환하는 데 사용하도록 설정되어 있어야하며 typescript가 명시 적으로 지원되지 않으며 코드가 es6처럼 보이지 않으면 transpiler가 사용되지 않습니다.

그래서 코드가 es6임을 명시 적으로 SystemJS에 알릴 필요가 있습니다. 당신의 예제와 SystemJS 0.19에서 작동하는 SystemJS 설정입니다. 0.20 버전의 SystemJS에서는 작동하지 않으므로 plugin-typescript을 사용하는 것보다 you have no other option처럼 보입니다.

<!DOCTYPE html> 
<script src="node_modules/systemjs/dist/system.src.js"></script> 
<script> 
    System.config({ 
    map: {typescript: 'node_modules/typescript'}, 
    packages: { 
     typescript: {main: 'lib/typescript.js', format: 'global'}, 
     src: {defaultExtension: 'ts', format: 'esm'} 
    }, 
    transpiler: 'typescript', 
    }); 
    System.import('src/main.ts'); 
</script> 
+0

최상위'import' 또는'export'가없는 타이프 스크립트 파일은 [** 모듈 **] (https://www.typescriptlang.org/docs/handbook/modules.html)가 아니기 때문에 당연히 모듈 로더가 혼란 스러울 수 있습니다. 모듈화의 정신에서 더미 수출을 추가 할 수 있을까? –