I have a monolith project made with AngularJS using Webpack but I'm getting an error saying: Unexpected token < main.html.
This error occurs when I have this line of code in one of my controllers:
import templateUrl from './main.html';
From my understanding, it looks to me that Webpack does not bundle correctly my HTML template. the <
symbol is coming from the main.html template, it is found with success but not parsed. I then thought about using html-loader
and use it that way:
import templateUrl from 'html-loader!./main.html';
To my great surprise, this does not solve the issue.
I am using Webpack version "webpack": "^3.4.1",
and this is my config:
const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ContextReplacementPlugin =
require('webpack/lib/ContextReplacementPlugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const nodeEnv = process.env.NODE_ENV || 'development';
const isProd = nodeEnv === 'production';
const styles = [
'css-loader?importLoaders=1',
'postcss-loader',
'sass-loader'
];
module.exports = {
entry: {
'root-application': 'src/root-application/root-application.js',
},
output: {
publicPath: '/dist/',
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.scss$/,
use: !isProd
? [
'style-loader',
...styles
]
: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: styles
}),
exclude: /(node_modules)/
},
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
use: ['url-loader?limit=100000']
},
{
test: /\.html$/,
use: [
'ngtemplate-loader',
'html-loader'
],
exclude: /(index)/
},
{
test: /\.js?$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
},
],
},
node: {
fs: 'empty'
},
resolve: {
modules: [
__dirname,
'node_modules',
],
},
plugins: [
new CleanWebpackPlugin(['dist']),
new webpack.optimize.CommonsChunkPlugin({
name: 'common-dependencies',
}),
new ContextReplacementPlugin(
/(.+)?angular(\\|\/)core(.+)?/,
path.resolve(__dirname, '../src')
)
],
devtool: 'source-map',
externals: [],
devServer: {
historyApiFallback: true
}
};
I've checked other SO topic about Unexpected token <
, I am sure that there is no 404 - Not found error in my network tab.