Hey all, I was wondering if someone could help me with an issue I’m having using TypeScript and Webpack. This is for the weather viewer, I decided to go back and build it using TypeScript. At first I wasn’t using Webpack, I was just transpiling to JS and manually linking them in the HTML. The problem there was that the TS compiler was emitting JS that was incompatible with the browser (keywords like import
and export
) even though I was targeting ES5, so I was getting reference exceptions in my browser. Then I switched to Webpack, but Webpack was unable to resolve dependencies using the ts-loader when I was importing from one file to another, even though they were in the same directory.
Where I am now is that I’m transpiling my TS to ES2015 and using the babel-loader in Webpack to transpile to ES5. Webpack passes the bundling stage with no errors, and it says that the modules were bundled successfully, but there is no output. Here is the GitHub repo, and some of the more important code is below:
webpack.config.js
var path = require('path');
//fixme: no output from operation
module.exports = {
entry: "./src/app/js/index.js",
output: {
path: __dirname + 'dist/app',
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.scss$/,
loader: "style-loader!css-loader!sass-loader",
include: [
path.resolve(__dirname, 'src/app/styles')
]
},
/*{
test: /\.ts(x?)$/,
loader: 'ts-loader',
include: [
path.resolve(__dirname, 'src/app')
]/!*,
exclude: /(node_modules)/!*!/
}*/
{
test: /\.js$/,
//exclude: /(node_modules)/,
loader: 'babel-loader',
include: [
path.resolve(__dirname, 'src/app/js')
],
query: {
presets:['es2015']
}
}
]
},
resolve: {
root:[
path.resolve('./src/app')
]
}
};
GetFromApi.ts
/// <///<reference path="../../node_modules/@types/jquery/index.d.ts"/>
/**
* This class will be used to get data from the backend API.
*/
export class GetFromApi {
constructor(private testString: string) {
}
getResponse(): string {
return this.testString;
}
}
index.ts
import {GetFromApi} from "./GetFromApi";
/// <reference path="../../node_modules/@types/jquery/index.d.ts" />
$(document).ready(function () {
let getFromApi = new GetFromApi("this is a test");
alert(getFromApi.getResponse());
});
Any help would be very appreciated!