Skip to content

[BUG] Unable to import language when bundled by Vite. #31

Closed

Description

@gcoakes

Information

  • Speed-highlight.js version: 1.1.11
  • Browser version: n/a

Description

Unable to import language either dynamically or directly when bundled by Vite.

Example

import asm from "@speed-highlight/core/dist/langauges/asm.js";

const lang = await import(`./languages/${lang}.js`);

Expected behavior

Able to import without error.

Actual Behavior

Either, this when directly importing:

Missing "./dist/languages/log" specifier in "@speed-highlight/core" package

Or, this in the console when dynamically importing (which is done by tokenize when lang is of type string):

Loading module from “http://localhost:5173/node_modules/.vite/deps/languages/log.js” was blocked because of a disallowed MIME type (“”).

Activity

  1. BearToCode commented on May 7, 2023

    @BearToCode
    Contributor

    I managed to fix it using vite-plugin-dynamic-import with this config:

    // vite.config.js
    import { defineConfig } from 'vite';
    import { resolve } from 'path';
    
    import dynamicImport from 'vite-plugin-dynamic-import';
    
    // https://vitejs.dev/config/
    export default defineConfig({
    	build: {
    		lib: {
    			entry: resolve(__dirname, 'src/index.ts'),
    			name: 'myPlugin',
    			fileName: 'index'
    		}
    	},
    	plugins: [
    		dynamicImport({
    			filter(id) {
    				if (id.includes("@speed-highlight/core"))
    					return true;
    			}
    		})
    	]
    });

    this way this:

    const lang = await import(`./languages/${lang}.js`);

    is converted into this in the generated bundle:

    function b(e) {
      switch (e) {
        case "./languages/asm":
        case "./languages/asm.js":
          return import("./asm-c2d0ef48.js");
        case "./languages/bash":
        case "./languages/bash.js":
          return import("./bash-c3851741.js");
        case "./languages/bf":
        case "./languages/bf.js":
        // And many more

    And all the mentioned files are also present automatically.

  2. reopened this on Aug 6, 2023
  3. added a commit that references this issue on Aug 1, 2026
  4. added a commit that references this issue on Aug 1, 2026
  5. matubu commented on Oct 20, 2026

    @matubu
    Member

    Thanks guys! Should now be fixed since 2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions