Is it possible to create a VueJS 3 application in a unified ES Module bundle format?

In my Vue3 project, I have configured it with typescript and a main.ts entry file that has a single default export.

import { App, createApp } from "vue";
import { createIntl } from "vue-intl";

import Application from "./App.vue";
import { AppProps, Settings } from "types";

let appRef = {} as App<Element>;

const AppLifecycle = {
  mount: (container: HTMLElement, appProps: AppProps, settings: Settings) => {
    const { themeUrl, userPreferences } = settings;
    const { language } = userPreferences;

    appRef = createApp(Application, { ...appProps, themeUrl });
    appRef.use(
      createIntl({
        locale: language,
        defaultLocale: "en",
        messages: messages[language],
      })
    );
    appRef.mount(container);
  },
  unmount: (_: HTMLElement) => {
    appRef.unmount();
  },
};

export default AppLifecycle;

I am looking to create a single ES Module bundle to integrate into a private platform with specific requirements:

The app’s bundle must be a JavaScript ES Module;

The default export of the app must be an object to handle the app’s lifecycle (the AppLifecycle object above)

A boilerplate project (React + Typescript) provided me with the following webpack configuration:

const path = require("path");

module.exports = {
  mode: "production",
  entry: "./src/index.tsx",
  experiments: {
    outputModule: true,
  },
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, "dist"),
    library: {
      type: "module",
    },
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: "css-loader",
      },
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
};

Vue3 uses webpack4 underneath, and the configuration can be adjusted using a webpack chain in vue.config.js. Additionally, vue-cli can specify a target like --target lib, but I am unsure if ES modules are supported this way. I have tried the following configuration, but I am unsure if it is the correct approach.

module.exports = {
  chainWebpack: (config) => {
    config.optimization.set("splitChunks", false);
    config.plugins.delete("prefetch");
    config.plugins.delete("preload");
  },
  css: {
    extract: false,
  },
  filenameHashing: false,
};

I have not found detailed resources on how to specifically build a single ES Module with a single typescript entry file using Vue3, so I am reaching out for guidance here. Thank you in advance.

Answer №1

I managed to resolve this issue by updating vue-cli to version 5. This update includes compatibility with Webpack 5, which assists in ES module generation. You can find more information about this update at

I made adjustments to the vue.config.js file to align it with the provided boilerplate. Here is the modified configuration:

module.exports = {
  configureWebpack: {
    entry: "./src/main.ts",
    experiments: {
      outputModule: true,
    },
    optimization: {
      splitChunks: false,
    },
    output: {
      library: {
        type: "module",
      },
    },
  },
  chainWebpack: (config) => {
    config.plugins.delete("prefetch");
    config.plugins.delete("preload");
  },
  css: {
    extract: false,
  },
  filenameHashing: false,
};

Instead of utilizing Vite, as suggested by Estus Flask, I opted for this solution as I am more comfortable with Webpack, which aligns with the target platform's requirements.

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

tips for optimizing pdf size using nodeJs and vueJs

I am searching for a library for either NodeJS or VueJS that can help reduce the size of PDF uploads by users. Which library should I choose? Alternatively, I came across pdftron but it is not widely used and not free. I tried installing ghostscript4js ...

Steps for retrieving the URL using the getDownloadURL() method

After exploring various options, I have come across multiple solutions but none seem to be suitable for my specific requirements! I have managed to successfully upload a photo to Firebase along with all necessary information. However, when attempting to r ...

Utilizing TypeScript with Vue3 to Pass a Pinia Store as a Prop

My current stack includes Typescript, Pinia, and Vue3. I have a MenuButton component that I want to be able to pass a Pinia store for managing the menu open state and related actions. There are multiple menus in the application, each using the same store f ...

How to apply a single pipe to filter columns in Angular 2 with an array of values

I need to sort through an array of objects using multiple array string values. Here is an example of how my array of objects looks like: [{ "name": "FULLY MAINTAINED MARUTI SUZUKI SWIFT VDI 2008", "model": "Swift" }, { "name": "maruti suzuki ...

Receiving intelligent suggestions for TypeScript interfaces declared within function parameters

Here is a simple example I put together: https://i.sstatic.net/Fdtfa.png In this example, intellisense provides suggestions for the interface of the object named test in the foo function. It works perfectly and I love it! However, if you declare that in ...

Exploring the Module System of TypeScript

I am working with a TypeScript module structured like this: let function test(){ //... } export default test; My goal is for tsc to compile it in the following way: let function test(){ //... } module.exports = test; However, upon compilation, ...

Tips for validating Enum Strings using the newest version of Joi?

Is there a way to validate Enum String? In the past, I followed this advice from: https://github.com/hapijs/joi/issues/1449 enum UserRole { Admin = 'admin', Staff = 'staff' } const validator = { create: Joi.object().keys({ ...

Webpack FileAddPlugin: move file to specified directory

How can I include the current module folder in my webpack compilation output to the dist/ directory? Currently, my /dist folder contains the following: const toCopy = [ './../../../../node_modules/flatpickr/dist/flatpickr.min.js', './../ ...

Vue JS - Unable to locate module (datepicker)

Today marks my first time delving into the world of vue.js, and as expected, I've encountered an error that has me stumped. I recently incorporated the v-md-date-range-picker module into my project: (. The setup instructions provided me with the f ...

Exploring the possibilities of utilizing package.json exports within a TypeScript project

I have a local Typescript package that I am importing into a project using npm I ./path/to/midule. The JSON structure of the package.json for this package is as follows: { "name": "my_package", "version": "1.0.0&q ...

Using Vue 3's emit in place of v-model

I am facing a challenge with integrating a custom dropdown select component. The idea is to use v-model to retrieve data from the parent component, but I am unsure how to pass that through an emit. Can anyone clarify this for me? Here is my parent compone ...

React Functional Component not working properly following package update

After a 4-month hiatus from programming, I decided to update this project using npm but encountered issues with all my stateless functions. interface INotFoundPageContainerProps { history: any; } class NotFoundPag ...

Problems with the duration of Shadcn Toasts (Inspired by the react-hot-toast library)

Within a 13.4 Nextjs project (app router), utilizing Typescript and TailwindCSS. I am currently exploring the usage of toasts provided by the remarkable shadcnUI Library, which draws inspiration from react-hot-toast while adding its own unique flair. Imp ...

No contains operator found in Material UI Datagrid

While working on a project, I utilized Material UI's datagrid and successfully implemented filters such as contains and isEmpty. However, I am struggling to find information on how to create a notContains filter. Does Material UI natively support this ...

Vue: The computed property cannot be set

I'm having trouble with utilizing computed properties and ajax calls in Vue.js. The "filterFactories" variable stores a list of factories. A computed property named "filterFactories" generates this list of factories. Now, I am looking to implement a ...

What is the best way to transfer data received from an observable function to use as an input for another observable function?

After carefully declaring all the variables, I am facing an issue with passing the value obtained from the first observable function (this.acNum) as a parameter to resolve the second observable function within the ngOnInit method. Despite displaying correc ...

Creating a variable that is not defined and then converting it into

I have an issue with a function that returns an Observable. The problem is that when the function is called, the parameter works fine, but its value becomes undefined within the Observable. This is the function in question: import {Observable} from &apos ...

When working with Nuxt 3, the referrer header may sometimes return as undefined

I am looking to capture the referrer header and store it in a cookie so that I can later use it to populate an axios request during the user's journey on my website. In my app.vue, I currently have the following code snippet: const headers = useReque ...

Using the currency pipe with a dynamic variable in Angular 2

My application utilizes CurrencyPipe, The current implementation is functional, <div class="price">{{123 | currConvert | currency:'USD':true:'3.2-2'}}</div> Now, I need to dynamically pass the currency from a model varia ...

Combine strings in the webpack.DefinePlugin

I am currently utilizing the webpack.DefinePlugin, and this is my current setup: new webpack.DefinePlugin({ 'REST_URL': REST_URL[nodeEnv], 'process.env': { NODE_ENV: JSON.stringify(nodeEnv) } }) I am looking to concatenate a ...