webpack - compile one TypeScript file separately (2 actions)


In summary...

When my Vue files are combined with background.ts, webpack processes them to create bundled vue files along with background.js

  • I'm unable to run the background.js script

  • I expected background.js to only contain "console.log('test');


I have a Vue project with webpack and TypeScript.

My goal is to generate a separate "background.js" file during the build process, in addition to the [Vue JS related files].

I have a typescript source file named "background.ts".

In vue.config.js, I added a webpack entry called "background".

Although it does create a "background.js" file as intended,

it seems to be bundled and cannot be executed by a Chrome plugin.

Right now, I simply want a "background.js" file that executes the instruction "console.log('test');" when called.

Thank you, webpack can be quite tricky!


Edit: Including files:

// vue.config.js
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    filenameHashing: false,
    chainWebpack: config => {
        // add your custom entry point
        config
            .entry('background')
            .add('./src/background.ts');
    },
    configureWebpack: {
        plugins: [
            new CopyWebpackPlugin([
                { from: 'manifest.json', to: 'manifest.json', flatten: true },
            ]),
        ]
    }
}

Content of "$vue inspect" $vue inspect > https://pastebin.com/6F3zwLhC


What I've attempted:

  • Exporting a function rather than plain code:

    export default function() {
        console.log("gboDebug: background.ts dans export function");
    }
    

    // Instead of just

    console.log("gboDebug: background.ts dans export function");
    
  • Adding this at the end of the file after seeing it elsewhere:

       export default null;
    
  • Verifying that my console.log was present in the bundled background.js file

  • Pasting the resulting background.js script into the browser
  • Experimenting with the webpackJsonp global variable created by the scripts

My considerations:

  • Creating an npm script to 1-bundle-vue-webpack and then 2-transpile my file using babel-loader
  • Exploring the library output option in webpack, although I believe it allows code to be available for use in a variable rather than automatically executing upon load

Answer №1

To put it simply - when working on transpiling a single typescript file, there is no need for a bundler. Just utilize tsc.

Especially in the case where the Vue app is integrated into a chrome extension, it might be beneficial to separate the process of building the app from handling the extension specific files.

Alternatively, one can consider implementing something like the Vue CLI Browser Extension Plugin as a potential solution.

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

Unable to change the color of InputBase component's placeholder in React.js

I've been attempting to modify the color of the placeholder in an inputbase. I came across several methods online and tried implementing them, but none have been successful. Below are the codes I have tried. <InputBase id="input-id&quo ...

The functions firebase.auth().currentUser and onAuthStateChanged consistently return null even when the user is logged in

Despite the fact that my front end Swift application indicates that the user is signed in, I am encountering an issue where firebase.auth().currentUser and the onAuthStateChanged listener in my node.js code return null. The backend service is called from t ...

Utilize the jsTimezoneDetect script to showcase a three-letter time zone code

I'm currently utilizing the jsTimezoneDetect script to identify the user's current timezone. The code below shows the result as America/Chicago. Is there a way to display CDT/CST instead (based on today's date)? var timezone = jstz.determin ...

How can I handle moving to button code-behind when validation fails without relying on Page.IsValid?

Recently, I encountered a challenge with an ASP.NET page that contains both ASP.NET validators and JavaScript checks. As I delved into the button code behind the scenes: protected void Button2_Click(object sender, EventArgs e) { if (Page.IsVal ...

Direct your attention to the final item in a visible array within a ReactJS component

Currently, I'm in the process of developing a chat application using reactjs and am faced with the challenge of adjusting focus to the latest message whenever a new one is added to the array. The structure of my react chat window is as follows: < ...

I encountered an issue with route handlers in Next.js version 13.2. Sadly, they are not

I am trying to implement an API on my website with the endpoint /api/popular-movie. Here is an overview of my file structure: https://i.stack.imgur.com/e8Pf8.png Additionally, this is my route.ts code: import { NextResponse } from "next/server"; ...

Problem with Raphael Sketch and Request to Ajax

Utilizing Raphael.js and jQuery Ajax, I am attempting to display some dots (circles) on the map in this [Demo][1]. I have a PHP file called econo.php which looks like this: <?PHP include 'conconfig.php'; $con = new mysqli(DB_HOST,DB_USER,DB_P ...

The Javascript setTimeout Function Prolongs Its Operation

Currently, I have a web app index page that performs two main tasks. Firstly, it geocodes the user's location and stores it in a JavaScript variable called "variableToSend." Secondly, it sends this data to mySQL using a PHP script after a delay of 10 ...

Browsing a container with JavaScript

I am attempting to display one div at a time and scroll through them repeatedly. I found and modified a Fiddle that works as intended, but when I try to implement it on my own test page, the divs do not scroll as expected. Here is the Fiddle example: http ...

Exploring the Wonderful World of Styled Components

I have a query regarding styled components and how they interact when one is referenced within another. While I've looked at the official documentation with the Link example, I'm still unclear on the exact behavior when one styled component refe ...

I continuously encounter an issue in Vite version 3.2.4 where an error pops up stating `[vite:esbuild] The service has stopped running: write EPIPE`

When I finished creating a Vite app, I ran the command npm run dev and encountered the following error: [vite:esbuild] The service is no longer running: write EPIPE https://i.stack.imgur.com/MZuyK.png I need help solving this error. Can anyone provide gu ...

React - Exploring the depths of functional components

Picture this: a straightforward form that showcases several unique components with customized layouts. The custom components include: <UsernameInput />, <PasswordInput />, <DateTimePicker />, <FancyButton />, <Checkbox /> Th ...

Upgrading to Postgres 9.3 for Improved JSON Handling and Date Object Support

Currently working with PostgreSQL 9.3 and exploring options for utilizing TIMESTAMPTZ as authentic JavaScript date objects. Aware of JavaScript lacking a Date literal notation, I considered '{ "key" : new Date(datevalue) }'::JSON as a possibl ...

Using EJS to Render a Function Expression?

Has anyone been able to successfully render a function call in express using EJS? Here's what I've tried so far: res.render("page", { test: test() }); Can someone confirm if this is possible, or provide guidance on how to call a function fr ...

What is the best way to update a page and retrieve fresh data from a service in Angular?

On my webpage, there is a table and two charts coming from three different controllers. I am looking for a way to refresh all of them simultaneously by clicking on a single refresh button. This will allow the data to be fetched from the webservice again an ...

The selected jQuery plugin is not functioning properly within CodeIgniter framework

I recently downloaded the jQuery Chosen plugin to use the simple "multiselect" version on my website. I followed all the necessary steps and even copied and pasted the code into CodeIgniter. Despite my experience with jQuery, I am facing an issue where the ...

Encountering a TypeError indicating that the asterisk is not functioning as a proper function

Whenever I run my server.js file, an error keeps popping up: TypeError: routing is not a function This occurs in the following line of code: routing(app); In my routing.js file, the content looks like this: // JavaScript source code var friends = requ ...

Dealing with Typescript (at-loader) compilation issues within a WebPack environment

Currently, I am using Visual Studio 2017 for writing an Angular SPA, but I rely on WebPack to run it. The current setup involves VS building the Typescript into JS, which is then utilized by WebPack to create the build artifact. However, I am looking to t ...

javascript best practice for processing form data with arrays

As a newcomer to javascript, I've been exploring more efficient ways to handle certain situations. For example, would it be possible to utilize an array for this particular scenario? Here's the challenge: I have an HTML form with 6 fields that a ...

Strategies for displaying error messages in case of zero search results

I am currently developing a note-taking application and facing an issue with displaying error messages. The error message is being shown even when there are no search results, which is not the intended behavior. Can someone help me identify what I am doing ...