I need to use an external script file in my application.I added that script in my vendor.ts file as follows
import '../src/app/forms/form-signature/SigWebTablet.js';
and my webpack configuration file looks like
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');
module.exports = {
entry: {
'polyfills': './src/polyfills.ts',
'vendor': './src/vendor.ts',
'app': './src/main.ts'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'awesome-typescript-loader'
},
{
loader: 'angular2-template-loader'
}
]
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader'
}
]
},
{
test: /\.json$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].json'
}
}
]
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
use: [
{
loader: 'file-loader?name=assets/[name].[ext]',
}
]
},
{
test: /\.css$/,
use: ['style-loader','css-loader'],
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
{
loader: 'raw-loader'
},
{
loader: 'sass-loader'
}
]
}
]
},
plugins: [
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)@angular/,
helpers.root('./src'), // location of your src
{} // a map of your routes
),
new webpack.optimize.CommonsChunkPlugin({
name: ['app', 'vendor', 'polyfills']
}),
new HtmlWebpackPlugin({
template: 'src/index.html'
}),
/* http://stackoverflow.com/a/38825995/184509 */
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
]
};
when I tried call a function from that script I got a reference Error. But when I put that script in index.html page it works fine.
import './SigWebTablet.js';
declare let [ SetTabletState]: any;
..........
export class FormSignatureComponent implements OnInit {
ngOnInit() {
let canvas = <HTMLCanvasElement>document.getElementById("mycanvas");
this.ctx = canvas.getContext("2d");
SetTabletState(0, this.tmr);
}
}
I dont know how to resolve this.I thought to keep the external js files in the vendor.ts file. And also I checked that vendor.js bundled file in the browser console and found that the function is there. But somehow the script file is not referenced. My question is about why it is not referenced when I added the script in vendor.ts but not in index.html Trying to figure out where I made the mistake.