A combination of Webpack CSS modules with Angular templates

I am currently trying to integrate webpack CSS modules into my angular/ionic project. I'm wondering if it's possible for the CSS module class definitions to be correctly appended to an external template instead of an inline template.

When I embed the template directly into the route, everything works as expected:

var styles = require('./css.css')

module.exports = function(ngModule) {

  ngModule.config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider
      .state('tab.two', {
        url: '/two',
        views: {
          'module-two': {
            template:`<ion-view view-title="Module Two"> <ion-content class="padding moduleTwo"> <h2 class="${styles.title}">Welcome to Module Two</h2> </ion-content> </ion-view>`,
            controller: 'TwoCtrl'
          }
        }
      })
  })
}

Is there a way to simply require my template like this?

var styles = require('./css.css')
module.exports = function(ngModule) {

    ngModule.config(function ($stateProvider, $urlRouterProvider) {

        $stateProvider
                .state('tab.two', {
                    url: '/two',
                    views: {
                        'module-two': {
                            template:require('./myTemplate.html'),
                            controller: 'TwoCtrl'
                        }
                    }
                })
    })
}

I assume that I may need to adjust my webpack configuration, but I am uncertain about how to proceed.

var webpack = require('webpack');
var path = require('path');

module.exports = {
  entry: './index.js',

  output: {
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {test: /\.js$/, loader: "ng-annotate!babel", exclude: /node_modules/},
      {test: /\.html$/, loader: "raw", exclude: /node_modules/},
      {test: /\.json$/, loader: "json", exclude: /node_modules/},
      {
        test: /\.css$/,
        loader: "style!css-loader?modules&localIdentName=[name]__[local]___[hash:base64:5]",
        exclude: [
          path.resolve(__dirname, 'node_modules')
        ]
      }]
  }
};

Answer №1

I have encountered a similar issue in the past. The short answer is yes, but you may need to use a template engine such as jade.

Here is an example of external jade code that needs to be incorporated into your code:

'module-two': {
            template () => {
              console.log(require('./view.jade'));
              return require('./view.jade')(styles);
            }:,
            controller: 'TwoCtrl'
          }

When you run console.log, you will see something like this:

function template(locals) {
    var buf = [];
    var jade_mixins = {};
    var jade_interp;

    buf.push("<div" + (jade.cls(['text-center',"" + (locals.test) + ""], [null,true])) + ">dsadsadadas</div>");;return buf.join("");
    }

After passing styles to the template function, you can then utilize it within the template as locals.

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

Step-by-step guide to achieving a File div effect using CSS

Wondering how to achieve this specific effect within a div using CSS? click here for visual reference I attempted it using the before and after elements, but I struggle with these tags: check out this image for details Apologies if my question is unclear ...

I prefer to disable the toggle feature if the target remains unchanged

My goal is to create a step-by-step ordering system using the data-id="x" attribute to determine which content should be visible when selected. I want to make sure that if two elements have the same data-id, clicking on one will deselect the other. Any su ...

Anticipated that the absence of a value would match the presence of an Object

Struggling with running AngularJS unit tests for $resource using Jasmine v2.0 and Karma v0.13, I managed to convert custom actions to newer syntax successfully. However, a specific type of test is giving me trouble, which I suspect is related to $httpBacke ...

What is the best way to replicate floats using flexbox?

Currently, I am exploring the possibility of using flexbox in a similar manner as floats. My main aim is to have one child element create a column on the right side, while the rest of the children form another column on the left side. The challenge is that ...

Instructions on how to load an HTTP file inside a Bootstrap modal

Is it possible to load a file located at an http:// address into a modal window? Something like this: <a class="btn btn-info btn-large" data-toggle="modal" href="#http://localhost/login.html"> <i class="icon-user icon-white"></i> Login ...

Website search bar - easily find what you're looking for

I've tested this code and it works well on the basintap page. However, I'm interested to find out how I can search for something when I'm on a different page instead? <?php $query = $_GET['query']; $min_length = 2; if(strlen($ ...

The ngMessage feature in Angular bootstrap ui tabs seems to be malfunctioning specifically within the initial tab

Encountered a peculiar issue with ngMessage While using angular ui bootstrap tabs, the validation is not functioning correctly in the first tab This is the code snippet js angular .module('app', ['ngMessages','ui.boots ...

Using Angular's ng-repeat directive to iterate over an array from JavaScript

I am attempting to display an array in the HTML HTML: <div class="personWrapper" ng-repeat="message in messages"> <p>{{message}}</p> </div> JS: var app = angular.module('matcherApp', [ "ngRoute", "ngStorage" ] ...

rearranging the positioning of links in the HTML navbar

Is there a way to prevent other links in the navbar from moving up slightly when hovering over one link and creating a line (border bottom)? ` .n24{ margin-top: 2px; padding: 14px 14px; } .n25{ margin-top: 2px; padding: 14px 14px; } . ...

What is the best way to transfer the chosen ID from a Kendo combo box to an Angular function

I am currently utilizing a table with the table body being generated using ng-repeat. My issue is that when I try to pass the ID of the selected item through the kendo combo box, it always ends up passing the ID of the last item to the function. <tb ...

Top location for Cordova/Phonegap events in AngularJS

Currently, I am working on an AngularJS Cordova app and so far everything is progressing smoothly. My next objective is to integrate Cordova plugins into the application, specifically the Cordova Connect plugin, which will allow me to monitor network conne ...

What is the process for transforming my JSON array into a FirebaseArray and importing it into Firebase?

Currently, I am dealing with a situation where I have a large JSON file that contains an array. My app's backend is powered by Firebase and I intend to use FirebaseArray to store this data. Creating a FirebaseArray from my Angular app and adding data ...

Maintaining focus on UI grid cellTemplate editableCellTemplate is not a problem

Currently, I am utilizing angular-ui-grid and attempting to display a mix of various cell templates. Specifically, I want certain grid cells to change their class/color based on the entity's property. Additionally, some cells should display a button t ...

A div positioned in front of a centrally-located div

Managing a website with numerous headlines can be a challenge. When a user clicks on a headline, a button located 10px to the left of the headline should appear. However, the catch is that the headlines must always remain centered, regardless of whether th ...

Increasing box width in Django

I'm currently working on a website using Django all-auth for authentication. Everything is functioning perfectly, but I'm wondering if there's a way to increase the size of the username and password input boxes using HTML. Below is the code ...

There seems to be an issue with the HighCharts chart export feature as it is not showing the Navigator graph

We are currently using HighCharts version 4.2.2 http://api.highcharts.com/highcharts/exporting While going through their exporting documentation, I made a decision to not utilize their default menu dropdown. Instead, I only needed access to the .exportCh ...

Is there a way to determine if a container is overflowing at the top?

Currently, I am using Puppeteer to scrape Slack. My goal is to confirm whether I have scrolled to the very top of the channel feed. The issue arises because the channel feed does not actually scroll, making it impossible for me to utilize the method outli ...

The synergy between Object.prototype and the bind() method

Currently, I am delving into ngInfiniteScroll while being a novice in JavaScript. Upon reviewing the demo of ngInfiniteScroll, I find it quite challenging to comprehend why Reddit.nextPage has been altered to Reddit.prototype.nextPage, and how the bind() m ...

Retrieving data from MongoDB and saving it to your computer's hard drive

I've asked a variety of questions and received only limited answers, but it has brought me this far: Mongoose code : app.get('/Download/:file(*)', function (req, res) { grid.mongo = mongoose.mongo; var gfs = grid(conn.db); var fi ...

Problem with Bootstrap container-fluid overlapping with floated element

I am struggling with the layout of my card body, which includes a floating logo and rows enclosed in a container-fluid. While everything looks great in Chrome, I am facing alignment issues in Edge and Firefox. I have considered using absolute positioning ...