question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I use assets correctly?

See original GitHub issue

Hey! When running in dev mode, the following works:

// Component in src/components/
<img src="../../assets/icon.svg" />

… and using any other path here doesn’t seem to work.

But then, when running build, I get the following error:

error during build:
Error: Could not resolve '../../assets/icon.svg' from src/components/Tweets.vue
    at error (/Users/user/code/bugle-vue/node_modules/.pnpm/rollup@2.57.0/node_modules/rollup/dist/shared/rollup.js:158:30)
    at ModuleLoader.handleResolveId (/Users/user/code/bugle-vue/node_modules/.pnpm/rollup@2.57.0/node_modules/rollup/dist/shared/rollup.js:22263:24)
    at /Users/user/code/bugle-vue/node_modules/.pnpm/rollup@2.57.0/node_modules/rollup/dist/shared/rollup.js:22257:26
 ELIFECYCLE  Command failed with exit code 1.
ERROR: "build:web" exited with 1.
 ELIFECYCLE  Command failed with exit code 1.

How do I use assets correctly?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Elliot67commented, Apr 15, 2022

Couldn’t figure out how to use assets too, made some search so here is a summary :

  • extension/assets is a directory for the manifest assets only.
  • if you need to include assets in your vue components, you have to create an src/assets folder and include them with <img src="/assets/icon.svg" alt="" />. Assets included in the app will be copied to the extension/dist/assets folder on build.

At that point everything is working but you might have duplicated assets inside extension/assets and src/assets. To avoid that, you can create a script that will copy files from src/assets/static to extension/assets. It will be used the same way as manifest.ts write the manifest.json file.

Create the script to copy files, inside scripts/static-assets.ts

import fs from 'fs-extra';
import { r, log } from './utils';

export async function writeStaticAssets(): Promise<void> {
  // Name of the files
  const assetNames = ['icon-128.png', 'icon-512.png', 'icon.svg'];
  const outputDir = r('extension/assets');

  if (!fs.existsSync(outputDir)) {
    await fs.mkdir(outputDir);
  }

  const promises = assetNames.map((name) => fs.copyFile(r(`src/assets/static/${name}`), `${outputDir}/${name}`));
  await Promise.all(promises);

  log('PRE', 'write static assets : ' + assetNames.join(', '));
}

writeStaticAssets();

Then edit scripts/prepare.ts to execute our script :

function writeStaticAssets() {
  execSync('npx esno ./scripts/static-assets.ts', { stdio: 'inherit' });
}
writeStaticAssets();

if (isDev) {
  chokidar.watch(r('src/assets/static/**')).on('change', () => {
    writeStaticAssets();
  });
}

Hope this helps someone !

1reaction
jonasmerlincommented, Oct 28, 2021

@tmkx Solved it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is an Asset? Definition, Types, and Examples
An asset is a resource with economic value that an individual or corporation owns or controls with the expectation that it will provide...
Read more >
8 Tips on Managing Your Business Assets Wisely
7. Leverage Your Assets in Valuing Your Business. If you decide to take out a loan, seek funding or sell your business, your...
Read more >
How to Calculate Total Assets: Definition & Examples
It can also be used to check if your total assets figure is correct, according to The Balance. The formula is: Total Liabilities...
Read more >
Fixed-Asset Accounting Basics
These procedures include documenting financial records, calculating revenue, estimating fixed-asset valuations and complying with tax laws.
Read more >
6 Types of Business Assets and How to Record Them
The easiest, most accurate way to manage and record your assets is by using accounting software, but even if you're using a manual ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found