First of all, I'm trying to ask the right question. It seems to me that I have missed or mixed some concepts regarding the TypeScript compiler, the new ES2015 module syntax (ES6) and module loaders.
I am trying to import the jquery module installed by npm using ES2015 module syntax. TypeScript should pass it to ES5.
I get an error when opening index.html.
system-polyfills.src.js:1276 Error: SyntaxError: Unexpected token <
Evaluating http:
Error loading http:
index.html
<html>
<head>
<title>Bare TypeScript</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/jquery/dist/jquery.js"></script>
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<body>
<div>Loading...</div>
</body>
</html>
- I'm not sure what is required
<script
src="node_modules/jquery/dist/jquery.js"></script>. - How come import B works fine when
console.log($)commented out? Should jQuery import work too? - Perhaps a problem with running npm commonjs modules with systemjs?
main.ts
import * as $ from 'jquery'
import B from './app.b'
console.log("Monkey");
let b = new B();
console.log($);
How to configure tsc to work with ES2015 module syntax? Used TypeScript 1.8.
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
package.json
"dependencies": {
"es6-shim": "^0.35.0",
"jquery": "^2.2.2",
"systemjs": "0.19.25"
},
typings.json
{
"ambientDependencies": {
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
"jquery": "registry:dt/jquery#1.10.0+20160316155526"
}
}