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.

An external img should not generate an import statement

See original GitHub issue

Describe the bug

Here’s the setup. Page1.vue contains an external image that doesn’t exist in this repo**:

<img alt="smpte bars" class="logo" src="/img/aaaSMPTE-color-bars.png" />

There’s a entry-point-1.ts file which includes the Page1.vue file, and that entry point file is specified as an input in the vite.config.ts config. The vite.config.ts also has a rollup config that specifies that PNG to be external:

export default defineConfig({
  // ...
  build: {
    rollupOptions: {
      input: [
        'src/entry-point-1.ts',
      ],
      external: [
        '/img/aaaSMPTE-color-bars.png',
      ],
      // ...
    }
  },
  // ...
})

Expected Behavior

Run npm run build.

After running npm run build, the generated .js file should not attempt to import anything for that <img> source, and the string literal /img/aaaSMPTE-color-bars.png should be left intact when rendering that <img>. That <img> is expected to be completely external (honoring the rollup config), and should be ignored by any client code.

Actual Behavior

The build does not ignore that <img> tag, and generates an import statement that expects that image file to be in a relative location, i.e. in the build output,

File dist/assets/entry-point-1.e49989cd.js:

[blahblahblah]import n from"../../img/aaaSMPTE-color-bars.png";[blahblahblah]()=>p("img",{alt:"smpte bars",class:"logo",src:n},null,-1)

That import breaks at runtime on the client side. That import should not exist in the build output, and the rendering of the "img" tag should be a static string value rather than the imported variable (n).

Reproduction

https://github.com/bseib/vue-vite-external-static-image

System Info

$ npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers

  System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
    Memory: 4.69 GB / 15.81 GB
  Binaries:
    Node: 14.15.4 - C:\Program Files (x86)\Nodist\bin\node.EXE
    Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.14.10 - C:\Program Files (x86)\Nodist\bin\npm.EXE
  Browsers:
    Chrome: 100.0.4896.127
    Edge: Spartan (44.19041.1266.0), Chromium (101.0.1210.39)
    Internet Explorer: 11.0.19041.1566

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
bseibcommented, May 22, 2022

Whoops! I did not update the @vitejs/plugin-vue, despite acknowledging that I needed to! 😳 🤦‍♂️

Using that workaround, the output looks correct now compared to the previous output, with the excess import statement removed:

image

I pushed a branch showing this working.

Thanks @LinusBorg and @bluwy .

0reactions
LinusBorgcommented, May 22, 2022

did you update @vitejs/plugin-vue (2.3.2), as that’s where the fix happened?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >
Auto import | IntelliJ IDEA Documentation - JetBrains
Basic procedures to create and optimize imports in IntelliJ IDEA. Learn more how to import the missing import or XML namespace.
Read more >
Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >
Sass: @import
Sass extends CSS's @import rule with the ability to import Sass and CSS stylesheets, providing access to mixins, functions, and variables and combining ......
Read more >
Manually import boot disks | Compute Engine Documentation
Although /dev/sda is running, you can still create an image from it. It is best to do this on a quiet system that...
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