Ways to maximize your final export value

My data file, named data.ts, contains a large dataset:

export data = [
  ... // huge data 
]

The lib.ts file only utilizes a portion of the data:

import { data } from './data.ts';

const fitteredData = data.splice(0,2) // only use some of them

export { fitteredData };

I am using fitteredData in my Vue component:

<script setup lang="ts">
import { fitteredData } from "./lib";
</script>

<template>
  <div>{{ fitteredData }}</div>
</template>

When I run pnpm build, I noticed that all the data from data.ts is included in the final built file. How can I ensure that the final file only contains the fitteredData to reduce its size?

Is there a way to optimize the final built files so that they only include the fitteredData and nothing else?

Answer №1

lib/filter-data.ts: 1

import fs from 'fs'
import { data } from './data'
fs.writeFileSync(
  `${__dirname}/filteredData.json`,
  JSON.stringify(data.splice(0, 2), null, 2)
)

In your vue component:

// import { filteredData } from "./lib";
import filteredData from "./lib/filteredData.json"

And execute the following command in your terminal:

npx ts-node ./filter-data.ts

If you need to update filteredData.json, run this command with the appropriate path.
You can also automate this process by adding a script in your package.json:

For example: 2

{
  "scripts": {
    "dev": ...,
    "buiild": ...,
    "test": ...,
    "filter-data": "yarn --cwd lib npx ts-node ./filter-data.ts",
    "predev": "yarn filter-data",
    "prebuild": "yarn filter-data",
    "pretest": "yarn filter-data"
  }
}

Your build will only include the filtered data from filteredData.json.


1 - You can save the output as a .ts file instead of .json, just ensure it's valid TypeScript code
2 - If you use npm instead of yarn, replace "yarn" with "npm run"

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

Trigger an Ajax form submission upon a change occurring

My Ajax form needs to be submitted as soon as the user selects an image, but I'm encountering an issue with the form not submitting. Any guidance on resolving this problem would be greatly appreciated. -- Below is the form --- <form id="bgimagefo ...

Having trouble converting svg to png, thinking it might be due to discrepancies in svg elements

I am facing a puzzling issue where two different SVG elements are causing my JavaScript to work in one scenario but not the other. I have replaced the SVG elements in both examples, and surprisingly, only one works while the other does not. You can view th ...

Incorporate an external object not native to the Angular framework within a factory

We're in the midst of a debate and I'm hoping you can help us reach an agreement. Imagine I have a basic factory set up like this: angular.module('myModule', []) .factory('Fact', function() { var Fact = function() { ...

Initiate an AJAX request within an existing AJAX request

On one of my pages, page A, I have a form that passes parameters to a script using AJAX. The results are loaded into div B on the same page. This setup is functioning properly. Now, I want to add another form in div B that will pass parameters to a differe ...

"422 (Unprocessable Entity) Error When Submitting a Form in Rails Application

I recently delved into the world of ruby on rails a few days back. My current challenge involves transferring data from html tags to a ruby function using ajax. Below is the error message that has been giving me trouble: POST http://localhost:3000/ajax/o ...

Having trouble with some JS and Jquery codes not functioning properly in IE until the page is refreshed a few times. Any suggestions on how to fix this issue?

My code seems to be experiencing issues in Internet Explorer where some of the JS and jQuery codes are not functioning correctly initially, but start working after refreshing the page a few times. Any thoughts on why this could be happening? Here is the ...

Error occurred when sending form data while uploading a file

Upon trying to upload a file using the formData.append(key, value);, an error message is displayed in the value section: The argument of type 'unknown' cannot be assigned to a parameter of type 'string | Blob'. Type '{}' is ...

Learning how to retrieve the ID and Title using Jquery

Hello, I'm having an issue with my UL and LI elements. They work fine with the click function, but I can't seem to get the title when trying to access it. Example: // Works fine, but can't access the title $("#list li").on('click&ap ...

What is the best way to store multiple values in local storage using useState in react?

Currently, I am exploring how to utilize multiple values in the useState hook and perform CRUD operations in local storage. Here is an example of my code: const [Data,setData]=[[{ name:'luis', pass:'1234', //....... }]] // ...... ...

Issue with customizing border color in Mui text field

Why is the border color of the Mui textField not remaining black when the user enters or selects data for the input fields? It still shows blue as in the photo. Here is the code: enter image description here.Here is the code. I'm wondering why this i ...

Are your Express routes failing to function properly?

I recently embarked on creating a new Express app by following the tutorial from Code Magazine. Below are my app and the defined route for /img. https://i.sstatic.net/G6PUG.png Upon trying to access http://localhost:3000/img or http://localhost:3000/img/ ...

A guide on displaying a JSON object using the ng-repeat directive

Looking to create a dynamic treeview menu with angularJS? Wondering how to achieve the desired results using a controller ($scope.results) and JSON data? Check out the code snippet below for an example of how to structure your treeview: <ul> < ...

Using redux action in the onPaginationChange function instead of setPaginationState in the given example for the TanStack table - is there a way to

Provided this sample Is there a way to utilize by dispatching a redux action rather than using useState - setPaginationState? onPaginationChange: state => dispatch(browseItemModalActions.setPagination(state)) An error is appearing in the console: ...

Struggling to determine data type in Typescript

My goal is to create an interface for my realm Database using TypeScript. Essentially, I have an automation bot and I want to monitor and track how users are utilizing it. To achieve this, I have designed specific schemas that will be integrated into an i ...

Adding a CSS file in nuxt.js allows you to easily customize

I recently set up a new Nuxt project by using the command: vue init nuxt/starter <project-name> After creating my project, I made changes to the nuxt.config.js file by including the following code: css: [ { src: '~assets/css/style.css& ...

How can I iterate through a directory containing files and extract the exported JavaScript object from each one?

In my current project using nodejs / nextjs, I have file system access and a folder containing multiple React files: content - blog-post-1.jsx - blog-post-2.jsx - blog-post-3.jsx The task at hand is to extract the frontmatter from each file. My init ...

What is the best way to create a universal variable that can be accessed across all routes in an Express/

Exploring the world of nodejs and express, I have turned to the Parse API for my backend database needs. At the moment, I have an ajax post triggered on page load to one of my routers /getuser, which retrieves the current user if they are logged in. I am ...

What causes me to create components with incorrect paths?

Can someone assist me with creating a new component in the dynamic-print folder instead of it being created in the app? Thank you ...

The art of combining Angular 6 with CSS styling for dynamic

Can we dynamically set a value in an scss file from the ts component like demonstrated below? public display: "none" | "block"; ngOnInit(): void { this.display = "none"; } ::ng-deep #clear { display: {{display}} !imp ...

Inquiry about Date and Time Selection Tool

I am working on a PHP project that includes two textboxes: one for selecting dates and the other for choosing a specific time. What I need assistance with is disabling any times before the selected date in the second timepicker textbox if today's dat ...