Giter Site home page Giter Site logo

Comments (5)

Menci avatar Menci commented on July 17, 2024 2

Your publicPathExpression is set to /static/sig/ (without quotes), which is NOT a valid JS expression.

You shouldn't use /static/sig/ as publicPathExpression, instead use a JS expression like window.publicPath and assign to it on your <head>:

<script>
  window.publicPath = "http://cdn.your.site/static/sig/";
</script>

from vite-plugin-public-path.

Menci avatar Menci commented on July 17, 2024

Just refer to the test. In your HTML file:

<script>
window.publicPath = __public_path__;
</script>

If you public path is decided staticly in client side, just use a string literal expression here. If your server decides which CDN to use, replace a placeholder when serving the file like:

// Replace the dynamic public path value in `index.html`
if (filePath === "index.html") {
return (fileContent as string).replace("__public_path__", JSON.stringify(servers[Server.Dynamic]));
}

from vite-plugin-public-path.

Menci avatar Menci commented on July 17, 2024

Also you can provide a minimum working example to reproduce your problem if you encountered one.

from vite-plugin-public-path.

TrendAlan avatar TrendAlan commented on July 17, 2024

Can you help to take a look?
Thank you so much

vite.config.js

  base: process.env.NODE_ENV === "production" ? "/__vite_base__/" : "/",
  plugins:[
    react(),
    publicPath({
      // A JS expression evaluates on client side each time when loading a new file
      // Should evaluate to a string ending with "/"
      publicPathExpression: "/static/sig/",
      // See below for explanation of `options.html`
      html: true
    }),
  ],

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite App</title>
    <script> 
      window.publicPath = __public_path__; 
    </script>    
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.jsx"></script>
  </body>
</html>

build error:

[vite-plugin-public-path] error: Unexpected token `<eof>`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp, ` for template literal, (, or an identifier
 --> <anon>:1:12
  |
1 | /static/sig/
  |            ^



Caused by:
    Syntax Error
error during build:
Error: error: Unexpected token `<eof>`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp, ` for template literal, (, or an identifier
 --> <anon>:1:12
  |
1 | /static/sig/

from vite-plugin-public-path.

TrendAlan avatar TrendAlan commented on July 17, 2024

works in this way:

vite.config.js:

  base: process.env.NODE_ENV === "production" ? "/__vite_base__/" : "/",
  plugins:[
    react(),
    publicPath({
      // A JS expression evaluates on client side each time when loading a new file
      // Should evaluate to a string ending with "/"
      publicPathExpression: "window.publicPath",
      // See below for explanation of `options.html`
      html: true
    }),
  ],

indext.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite App</title>
    <script> 
      window.publicPath = "/static/sig/"; 
    </script>    
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.jsx"></script>
  </body>
</html>

Thank you so much

from vite-plugin-public-path.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.