Creating a standalone module using webpack that can be accessed through a script element

When setting the library target to 'umd' in the package.json file, the expectation was that the outputs would be usable via a <script> tag and if no module system was available it would be attached to the window. However, this does not seem to be the case, or there may have been a mistake somewhere. It's time for another set of eyes to take a look.

The content of the package.json file is as follows:

{
  "name": "scoped-css",
  "version": "2.1.9",
  "description": "Scoped CSS in two easy steps.",
  "main": "lib/index.js",
  "author": "Joshua Robinson",
  ...

The webpack.config.js file contains the configuration:

// => webpage.config.js

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

However, when including the module via a <script> tag and trying to use the code, an error is encountered:

standalone.html:21 Uncaught ReferenceError: Style is not defined

The standalone.html file includes the following code:

<!-- standalone.html -->

<html>
    <head>
        <script src="../dist/scoped-css.js"></script>
    ...

Suspicion arises that the issue might be related to how exports are handled in the src/index.js file before building to dist/scoped-css.js.

At the bottom of src/index.js (before build) lies:

// Index.js
...
...
...
var Style = {
        scoped: scoped,
        CSS: CSS
    };

module.exports = Style;

The goal is for ../dist/scoped-css.js to be included in a script tag and be called as Style.CSS({}) or Style.scoped() (attached to window). Additionally, jest testing confirms that the module loads and is callable without any issues, indicating that the code itself is functioning correctly. The question remains whether something is amiss with the webpack configuration?

This situation calls for an extra pair of eyes to identify any oversight. Your time and expertise in resolving this puzzle are greatly appreciated.

Answer №1

The issue lies in the fact that the UMD build is generating output using scoped-css on a global scale because of the library: 'scoped-css' parameter. To resolve this, simply update it to library: 'Style', and your code will function as intended.

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

What is the best way to access the functions within an object?

I am looking to execute a function that is an object attribute. For instance, let's take a look at the following code snippet: var obj={ a:"One", b:"two", c:'three', d:function f(){ console.log("Hello World"); } } I am trying ...

Ways to execute function when state changes, excluding initial loading

I need to trigger a function when the state's data changes, excluding the initial load. Below is my code snippet: const Page = (props) => { const { data } = props; const arrowDirection = (item) => { if (item.arrow === 1) { return ...

Troubleshooting AJAX, PHP, and mySQL Interactions

I am experiencing issues with my password change script on my website. I am utilizing an AJAX connection to my php file. While my database connection seems stable and variables are being sent correctly (based on firebug), they seem to disappear along the w ...

Having trouble loading CSS file in node js after submitting form

Before diving into more details, let me explain my current situation. I am utilizing node's nodemailer to facilitate the process of sending emails based on the information submitted through a form. Initially, everything was functioning smoothly when t ...

Getting the country code using the ng-intl-tel-input directive

How to Extract Country Code from Input in AngularJS I am a newcomer to AngularJS and currently working on a project involving a dropdown for country code and an input field for a mobile number. Upon submitting the form, I am able to retrieve the mobile nu ...

Next.js server component allows for the modification of search parameters without causing a re-fetch of the data

I have a situation where I need to store form values in the URL to prevent data loss when the page is accidentally refreshed. Here's how I am currently handling it: // Form.tsx "use client" export default function Form(){ const pathname ...

Total number of requests made since the previous reset

I'm currently working on developing an API and I need to set up a route like api/v1/status in order to check the server status. This route should return a JSON response with the total number of requests made to the API since it became active. However, ...

How can I set up the datepicker to only display dates that are exactly 5 days ahead of the start date and 14 days before the end date?

Is there a way to display the date in the jQuery UI datepicker (specifically in the #id_erinnerung_am field) only if the date falls within "startdate + 5 days" and "enddate - 14 days"? Any other dates should be disabled. $(function() { $( ".vDateField ...

Angular2 recursive template navigation

Is it possible to create a recursive template in Angular 2 without using ng-include? It's puzzling why this feature seems to be missing in Angular 2... HTML: <div ng-app="app" ng-controller='AppCtrl'> <script type="text/ng-templat ...

Exploring the World of 2D Array Animation

I'm in the process of creating a Pacman ghost using visual 2D arrays. I would like the ghost to move similar to this: https://i.sstatic.net/KPmUt.gif I am considering implementing CSS transitions for the movement, but I'm unsure about how to do ...

Encountering an issue when trying to update a document in MongoDB using Node.js

Encountered an error while attempting to update a document using MongoDB and Node.js. Error: TypeError: db.profile.updateOne is not a function at exports.updateProfile (C:\xampp\htdocs\cit_node\route\route.js:101:13) at L ...

Styling the cursor in an input box: Using CSS and Javascript

After spending some time delving into the code of wallbase.cc, I've been trying to uncover the secret behind styling the input box similar to the one featured on their homepage. My main curiosity lies in how they achieve the quickly blinking cursor an ...

The router.push function does not properly redirect to the specified path; instead, it just reloads the current page

I am a newcomer to NextJS and facing a challenge where I need to transfer data from the current page to another page. However, instead of loading the defined path in router.push as pathname: "/booking/checkout/", it loads the current page. I wan ...

Enhancing the Rendering of React's props.children

I have set up my code like this: // Parent.js class Parent extends React.Component { render() { return { this.props.children } } } // Child.js class Child extends React.Component { render() { let message; const greet = ...

Vue.js - Capturing a scroll event within a vuetify v-dialog component

Currently, I am working on a JavaScript project that involves implementing a 'scroll-to-top' button within a Vuetify v-dialog component. The button should only appear after the user has scrolled down by 20px along the y-axis. Within the v-dialog, ...

React Redux has encountered an error as mapStateToProps and a variable are not defined in the code

I encountered the following error message: ./src/components/Playing.jsx Line 15: 'aaa' is not defined no-undef within my Playing.jsx file: import React, { Component } from 'react' console.log(aaa); Referencing Token.jsx: i ...

How can I trigger a series of functions in sequence when a button is clicked using Vue.js?

When I click, I need to call 3 functions in a specific order: <CButton @click=" getBillPrice(); updateBill(); postData();" type="submit" color="primary">Save</CButton> However, the func ...

The error message "Trying to use b.replace as a function when it is not"

Hey there! I've encountered an uncaught type error in my ajax file console, even though everything seems to be functioning correctly... Here is the HTML code: <div id="deletepropertybutton"><a href = "editproperty.php?property_id=<?php e ...

The location.reload function keeps reloading repeatedly. It should only reload once when clicked

Is there a way to reload a specific div container without using ajax when the client requests it? I attempted to refresh the page with the following code: $('li.status-item a').click(function() { window.location.href=window.location.href; ...

Unique alphanumeric code following the inclusion of a JavaScript file

I'm encountering an issue with a webpage that incorporates two JavaScript files. When inspecting it using firebug, I noticed that every time the page loads, these two files are included with the prefix ?_=someRandomNumber I'm unsure about the or ...