How come Webpack is not transferring my ng2 template files?

Here is the simplified structure of my Angular 2 app folder:

https://i.sstatic.net/jrzE9.jpg

Also, here is how my webpack.config.js file looks like:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        'polyfills': './polyfills.ts',
        'vendor': './vendor.ts',        
        'app': './app/main.ts'},
    output: {
        path: './dist',
        filename: '[name].js',
        chunkFilename: "[id].bundle.js",
        sourcemapFilename: '[name].map'
    },
    module: {
        loaders: [
            {test: /\.ts$/, loaders: ['ts', 'angular2-template-loader']},
            {
            test: /\.html$/,
            loader: 'html'
            },
            {
            test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
            loader: 'file?name=assets/[name].[hash].[ext]'
            },
            ,{ test: /\.css$/, loaders: 'style!css' }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.ts']
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: ['app', 'vendor', 'polyfills']
            }),       
        new HtmlWebpackPlugin({
            template: './index.html'
        })
    ]
}

After executing webpack build, I noticed that only index.html, app.js, polyfills.js, and vendor.js are copied to the dist folder. The components such as app.component.html, css files, and users component html files in their respective folders under app/users are not being transferred to the dist directory.

https://i.sstatic.net/On6BT.jpg

Can you tell me what I might be missing?

EDIT:

This is a screenshot from Dev Tools:

https://i.sstatic.net/haF0u.jpg

Answer №1

Explaining the process: the plugin angular2-template-loader searches for the templateUrl and transforms the path in the component

from

templateUrl; 'path'

to

 `require("templatePath")`

which webpack interprets and includes it in bundle.js along with the component. You can inspect your build to find something similar to this

 template: __webpack_require__(id).

Instead of creating separate HTML files, everything is bundled into one and served upon request. In your webpack configuration, you will typically have app.js, pollyfills.js , vendor.js, and index.html only.

Here's an example of a complete component and how it appears:

@Component({
    templateUrl: "./home.template.html",
})

and in your webpack setup

{test: /\.ts$/, loaders: ["ts-loader","angular2-template-loader"]},

Answer №2

After struggling to convert my app and get it working, I decided to start fresh by creating a new project and following the steps outlined in the Angular Docs for Webpack Webpack: an introduction. I then slowly transferred over my components one at a time, fixing any errors that arose during the build process - mostly related to incorrect path locations. So far, it seems like everything is running smoothly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining the dimensions of an Angular component: Height and Width

I'm dealing with a component that is manually resized, so I've set up a listener: @HostListener('window:resize') public detectResize(): void { // extract the height and width of the component this.theHeight = // the height of ...

I'm trying to use the Tailwindcss Outline feature, but it doesn't seem to be working properly

I'm struggling to understand why the background color is displaying properly while the outline color is not. Here's the code snippet I'm referring to: <span className={ ` block w-4 h-4 bg-${legend[key as keyof typeof legend].col ...

The typescript is throwing an error with columnrange: Highcharts error #17 is occurring, and it seems to be related to a missing module for columnrange. For more information, visit www.highcharts.com

Hello there: I am currently working on implementing a column chart in TypeScript and keep encountering this error message: Highcharts error #17: www.highcharts.com/errors/17/?missingModuleFor=columnrange - missingModuleFor: columnrange I understand that ...

angular input elements with no spacing between them

In my project, I have a square component identified by the selector 'app-square': <input type="text"/> I have applied specific styles to this component: input[type=text] { width: 40px; height: 40px; font-size: 15pt; ...

Maintain the text layout when copying and pasting from the Angular Application

After noticing that copying and pasting text from an Angular Application to a text editor like Microsoft Word results in losing the original format, I decided to investigate further. An example I used was the angular material website: https://material.ang ...

Exploring the versatility of type extensions in TypeScript

Currently, there is an existing type named GeometryFeature: type GeometryFeature = { type: "Feature"; properties: { id: string; refId: string | null; rootZoneId: string; name: string; description: s ...

error: encountering issue with Vue TypeScript Jest tests - '$store' property is undefined

I've been facing issues with my tests failing after a recent npm install. I've tried adjusting versions up and down, but haven't had any success. Interestingly, the $store isn't directly used in any of the components or tests. Despit ...

Having trouble with declaring an upgraded AngularJS component in the App Module of Angular 2?

Check out this Gist I am currently working on a project that involves running AngularJS and Angular together, utilizing the UpgradeModule. I have successfully "upgraded" an AngularJS component (angular.component) for use in our Angular components (@Compon ...

Node JS application facing compatibility issues with Typescript not working as intended

I've been diving into Typescript and recently followed a tutorial on how to integrate it with an express api app. However, I encountered the following error: D:\Development\Node\Ed\TypeScript\src\app.ts:5 const app: Appl ...

Calls to webApi with an Angular disorderly (asynchronous) pattern

Whenever I type in a textbox, it triggers a call to a webapi. The issue is that if I type too quickly, the calls and responses get mixed up. For example, when typing "hello": call with h call with "hel" call with "hello" call with "hell" call with "he" ...

When deploying an Angular 4 application in IIS, the command router.navigate(['/']) may not function as expected when the browser is refreshed

app.component.ts @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], providers: [BureauApiService,BureauStateWorkflowComponent,CommondataApiService] }) export class ...

Tips for importing resources in Webpack and Express.js without relying on a front-end framework

Currently, I'm developing a basic Express.js application without incorporating any client-side frameworks such as React, Vue, or Angular. The HTML templates are rendered from the server in the following manner: app.get('/', function (req, r ...

The OPTIONS API is triggered just once during the initial loading of the Application on the server

When the server is started for the first time, the OPTIONS API is only called once with a 200 status code. However, upon refreshing the page or stopping and restarting the server, only the GET API is called. According to expectations, when using Spring Sec ...

Reorganizing the layout of a React grid in response to changes in screen size

Currently, I'm facing a challenge in creating a container for a React app that includes a Highcharts chart and three textboxes. My struggle lies in getting the three textboxes to adjust and rearrange beneath the chart component when resizing the scree ...

Only the final defined document is instantiated by the Swagger-ui-express module

Currently, I am working on a Typescripted nodejs server and facing an issue with defining different swagger paths for separated controllers. The problem is that the swagger-ui-express module only displays the last defined document in the specific route. I ...

How can we restrict a user from moving away from the current tab in Angular?

I am looking for a solution to restrict users from switching tabs or opening new ones during an exam on our application for security purposes. What recommendations do you have to accomplish this? ...

Issue with ngOnInit not triggering upon opening dialog

Upon opening the dialog, the ngOnInit function is not triggered as expected. Strangely, the only way I am able to get it to fire is by resizing the window. I attempted to use detectChanges and zone.run() but unfortunately, it did not have any effect. Belo ...

The error message "JSX.Element' cannot be assigned to type 'ReactNode' within a React functional HOC" appeared when attempting to pass JSX.Element

I'm in need of creating a Higher Order Component (HOC) that will encase my components within Suspense. The plan is to provide the component and fallback as props to the HOC. This is the structure of my HOC: export const withSuspense = ({ Component, ...

Converting an array of objects to an array of JSON objects in TypeScript

My dilemma lies in the data I have uploaded under the _attachments variable: https://i.sstatic.net/jnFNH.png My aim is to format this data for insertion in the following structure: "_attachments": [ { "container": "string", "fileName": "string" ...

Challenges arise when attempting to merge declarations in Typescript involving passport, passport-local, and express-session

I'm currently facing challenges trying to integrate passport, passport-local, and express-session with Typescript. After installing all four necessary libraries - @types/passport, @types/express-session @types/passport-local, and @types/express - I in ...