All we need is an easy explanation of the problem, so here it is.
The idea is to have 1 JS file out of all html files.
and after just to use it via require:
templateUrl: require('./views/user-list.html')
Could you share you experience please?
I was googling for it and found several loaders for webpack but not sure what to use.
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
I decided to use ng-cache loader for webpack together with ES2015 syntax: it means import from
instead of require
.
Part of my webpack config:
module: {
loaders: [
{ test: /\.js$/, loader: 'babel', include: APP },
{ test: /\.html$/, loader: 'ng-cache?prefix=[dir]/[dir]' },
]
},
and example of directive with template:
import avatarTemplate from './views/avatar.html';
const avatar = function() {
return {
restrict: 'E',
replace: true,
scope: {
user: '='
},
template: avatarTemplate
}
};
export default avatar;
Method 2
your answer is right. However just offering alternative for it:
const avatar = function() {
return {
restrict: 'E',
replace: true,
scope: {
user: '='
},
template: require("../path/to/file.html"),
}
};
export default avatar;
Method 3
In Webpack.config file you can add main html like below
plugins: [
new HtmlWebpackPlugin({
template: 'index.html'
}),
new ExtractTextPlugin('bundle.css')
],
Include html-loader in package.json and use the below method in config block alone is enough.
$stateProvider.state('app', {
url: '/app',
template: require('html-loader!root/app/Common/templates/role.html'),
controller: 'roleController'
})
So all the partials will be bundled within the bundle.js itself.No need to add the loaders in webpack-config as well
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0