Im trying to use webpack with my angular app but whenever I try loading in the html to the template I get errors. This is the one I'm currently getting:
Module parse failed: C:\Users\Fabian\Dropbox\FixMyCity\FixMyCity\src\FixMyCity.Web\App\settings\settings.html Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
Which makes me think that I don't have the right loader for html, but i've tried it with both raw-loader and html-load and neither of them have worked for me.
Here is my current webpack.config.js
module.exports = {
entry: './App/app.js',
output: {
filename: './wwwroot/app-bundle.js'
},
loaders: [
{ test: /\.html$/, loader: 'html' },
{ test: /\.css$/, loader: 'style!css' },
]
};
app.js
var app = angular.module('app', ['auth0', 'angular-storage', 'angular-jwt', 'ngRoute'])
.config(['$routeProvider', 'authProvider', 'jwtInterceptorProvider', '$httpProvider', configFunction])
function configFunction($routeProvider, authProvider, jwtInterceptorProvider, $httpProvider) {
// Configure routes for your application
$routeProvider
.when('/', {
controller: 'HomeCtrl',
template: require('./home/home.html'),
requiresLogin: true
})
}
You must install html-loader from npm, after that you have to put in property loader the value 'html-loader'.
Like this:
module.exports = {
entry: './App/app.js',
output: {
filename: './wwwroot/app-bundle.js'
},
loaders: [
{ test: /\.html$/, loader: 'html-loader' },
{ test: /\.css$/, loader: 'style!css' },
]};
Related
Angular-ui-bootstap import issue in angularJS code using ECMA 6 and webpack-4
**Below is my angular.js & angular-ui-bootstrap versions **
"angular": "1.5.3",
"angular-ui-bootstrap": "^0.12.1",
Below is my webpack config file:
module.exports = {
mode: "development",
entry: {
app: "./src/app/modules/index.js"
},
module: {
rules: [
{
test: /\.js$/,
use: "babel-loader",
exclude: /node_modules/
},
{
test: /\.less$/,
use: ["style-loader", "css-loader", "less-loader"]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ["file-loader"]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: ["file-loader"]
},
{
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: 'jQuery'
},{
loader: 'expose-loader',
options: '$'
}]
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: {
attributes: true,
interpolate: true,
}
}
],
}
]
},
devtool: "inline-source-map",
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./src/app/index.html",
inject: true,
appMountId: "app",
filename: "index.html"
}),
],
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "dist")
}
};
To import angular-ui-bootstrap in app.js:
import angular from 'angular';
import uirouter from 'angular-ui-router'
import uibootstrap from 'angular-ui-bootstrap'
export default angular.module('App', [uirouter, uibootstrap ]).name;
I am getting following error:
ERROR in ./src/app/modules/core/index.js
Module not found: Error: Can't resolve 'angular-ui-bootstrap'
And webpack build going failed. I have also tried with directly ui-bootstrap import in my html but its not working
I think you need to upgrade your angular-ui-bootstrap version
Refer: https://github.com/angular-ui/bootstrap
Angular Requirements
UI Bootstrap 1.0 and higher requires Angular 1.4.x or higher and it has been tested with Angular 1.4.8.
I am trying to set up a basic angularjs + webpack project. I can get it running well as long as i stick to angularjs alone (ngApp = angular.module('ngApp), [])). Whenever I try to take the step to add some angular extension (ngApp = angular.module('ngApp), ['ngRoute']) something just doesn't work. I'm fairly certain the problem is with the loading of the library. I don't want to use bower, i want to use Webpack to run it.
webpack.config.js:
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
app: './src/index.js',
print: './src/print.js'
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist'
},
plugins: [
new CleanWebpackPlugin(['dist']),
new HtmlWebpackPlugin({
title: 'Webpack Starter App',
template: './src/templates/index.html'
})
],
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.js$/,
use: [
'angular-router-loader',
'babel-loader'
]
/* These 2 js loaders were failed attempts at solving this problem*/
}
]
}
};
ngApp.js
import * as angular from 'angular';
import ngRoute from'angular-route';
import './ngApp.controller.root.js';
const ngApp = angular.module('ngApp', [ngRoute]);
ngApp.config(function($routeProvider){
$routeProvider.when('/', {
controller: 'ngAppRootController',
templateUrl: './ngApp.view.root.html'
}).otherwise({
redirectTo: '/'
});
});
ngApp.$inject = [ngRoute];
webconsle error:
angular.js:138 Uncaught Error: [$injector:modulerr] Failed to instantiate module ngApp due to:
Error: [$injector:unpr] Unknown provider: t
If you see error like 'Unknown provider' with random letter like 'e'/'t'/'x', means you need ngInject plugin to let Webpack know which dependency to inject.
Add 'ngInject' right on next line after any dependency injection in Angular file:
ngApp.config(function($routeProvider, serviceWhateverYouRequire) {
'ngInject'
$routeProvider.when('/', { bla-bla-bla
or
ngApp.config(['$routeProvider', 'serviceWhateverYouRequire', function($routeProvider, serviceWhateverYouRequire) {
$routeProvider.when('/', { bla-bla-bla
We use these libraries for devDependencies with webpack#4.12.0
"ng-annotate-loader": "0.1.0",
"ng-annotate-webpack-plugin": "^0.3.0",
"ts-ng-annotate-loader": "^0.2.1"
This is the structure of my project
This is the webpack configuration
var ExtractTextPlugin = require('extract-text-webpack-plugin'),
webpack = require('webpack');
module.exports = {
entry: [
'./src/app.js',
],
output: {
path: __dirname + '/../web/js',
filename: 'build.js',
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
{
test: /\.png$/,
loader: "url-loader?limit=100000"
},
{
test: /\.jpg$/,
loader: "file-loader"
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
},
]
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
'window.jQuery': "jquery"
}),
new ExtractTextPlugin('build.css')
]
}
This is app.js (entry point for webpack)
import 'babel-polyfill';
import $ from 'jquery';
window.$ = $;
import 'jquery-ui-dist/jquery-ui';
import 'bootstrap/dist/js/bootstrap.js';
import 'bootstrap/dist/css/bootstrap.css';
import 'angular';
import 'angular-ui-sortable';
import './styles.css'
import './controller/app.js'
This is controller/app.js (angular app)
import './racks.js'
import './deletedRacks.js'
import './switches.js'
import './deletedSwitches.js'
const myApp = angular.module(
'myApp',
[
'RacksListController',
'DeletedRacksListController',
'SwitchesListController',
'DeletedSwitchesListController',
'ui.sortable'
],
function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}
);
This is controller (for example use deletedRacks.js)
(function (window, angular) {
'use strict';
angular.module('DeletedRacksListController', []).controller('DeletedRacksListController', [
'$scope', '$http', function ($scope, $http) {
$scope.racks = [];
$scope.init = () => {
$http({
method: 'GET',
url: Routing.generate('racks_get_deleted')
}).then((response) => {
$scope.racks = response.data;
}, (e) => {
console.log(e)
});
};
$scope.restore = (rack) => {
$http.post(Routing.generate('racks_toggle_delete', {id: rack.id, token: rack.token})).then((response) => {
rack.delete = false;
}, (e) => {
console.log(e)
});
};
}
])
})(window, window.angular);
When I collect the project on the dev mode - everything works well. No errors appear in the console. If I collect the project on the the production mode (npm run build) I get the following error in the console:
Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:unpr] Unknown provider: t
http://errors.angularjs.org/1.6.4/$injector/unpr?p0=t
I ask for your help.
This is controller/app.js (angular app)
import './racks.js'
import './deletedRacks.js'
import './switches.js'
import './deletedSwitches.js'
const myApp = angular.module(
'myApp',
[
'RacksListController',
'DeletedRacksListController',
'SwitchesListController',
'DeletedSwitchesListController',
'ui.sortable'
],
function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}
);
The error is in this file. $interpolateProvider is not injected. Correctly so:
import './racks.js'
import './deletedRacks.js'
import './switches.js'
import './deletedSwitches.js'
const myApp = angular.module(
'myApp',
[
'RacksListController',
'DeletedRacksListController',
'SwitchesListController',
'DeletedSwitchesListController',
'ui.sortable'
]
);
myApp.config(['$interpolateProvider', function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}]);
I am getting started to migrate my angular app to webpack. I have a file structure as belows:
- app/
------ app.js
------ index.html
- lib/
----- angular.js
----- jquery.js
----- ...
- webpack.config.js
Due to restrictions, I cannot use npm to install libraries. All my library files are located in lib and other folders. My webpack config looks like below:
var webpack = require('webpack'),
path = require('path');
module.exports = {
context: __dirname,
entry: {
app: [ './app/app.js'],
vendors: ['angular']
},
output: {
path: __dirname + '/build',
filename: 'bundle.js'
},
resolve: {
alias: {
angular: __dirname + "/lib/angular"
}
},
debug: false,
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
},
{
test: /\.css$/,
loader: "style-loader!css-loader"},
{
test: /\.png$/,
loader: "url-loader?limit=100000"},
{
test: /\.jpg$/,
loader: "file-loader"
},
{
test: /\.json/,
loader: 'json'
}
]
},
plugins: [
new webpack.ProvidePlugin({
angular: "angular"
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
]
}
I get the error
angular.js?848f:80Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
app.js looks like below
angular.module("myApp", [])
.controller("myCtrl", function(){ ... });
Thanks for the help!
First, fix typo vendor instead of vendors in your entries. It should match name in CommonsChunkPlugin
entry: {
app: [ './app/app.js'],
vendor: ['angular']
},
Second, remove ProvidePlugin
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
]
Now it should works.
But i don't know if it's correct way to load external libs with webpack, actually. (Webpack is super black box for me, gulp is much more predictable). So now it works, but without proper DI.
I am trying to bootstrap an AngularJS app built with Webpack. But I get the following error and the module isn't set up.
TypeError: _angular.angular is undefined
I dig into the generated code chunk and find that the _angular.angular is from
var _angular = __webpack_require__(1);
var _angularUiBootstrap = __webpack_require__(3);
_angular.angular.module('app', [_angularUiBootstrap.bootstrap]).constant('_', window._).run(function ($rootScope) {
$rootScope._ = window._;
It looks like that _angular.angular.module should be _angular.module. I probably use a wrong way to bootstrap angular, or use an incorrect Webpack configuration. Here is my code:
webpack.config.js
var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var srcDir = 'static_src';
var outputDir = 'static';
module.exports = {
devtool: 'source-map',
debug: true,
entry: {
app: path.resolve(srcDir, 'app.js')
},
output: {
path: outputDir,
filename: '[name].bundle.js',
sourceMapFilename: '[name].map',
chunkFilename: '[id].chunk.js'
},
resolve: {
extensions: ['', '.js', '.less', '.css'],
alias: {
npm: __dirname + '/node_modules'
}
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
query: {
presets: ['es2015'],
plugins: ['syntax-decorators', 'ng-annotate']
},
exclude: /node_module/
},
{ test: /\.less$/, loader: 'to-string!css!less' },
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader') },
{ test: /\.(png|gif|jpg)$/, loader: 'file?name=images/[name].[ext]' }
]
},
plugins: [
new webpack.NoErrorsPlugin(),
new webpack.optimize.DedupePlugin(),
new ExtractTextPlugin('[name].css')
]
};
app.js
import { angular } from 'angular';
import { bootstrap } from 'angular-ui-bootstrap';
angular.module('app', [bootstrap]);
I am using angular 1.5.0 and webpack 1.12.14.
Thanks in advance.
your error is in the require statement. you are using
import { angular } from 'angular';
this implies that there is an angular variable inside of the exported angular module.
what you want to use is
import angular from 'angular';
try that.