包裹没有将JS和CSS代码注入结果的HTML

发布于 2025-01-17 20:10:22 字数 1941 浏览 5 评论 0 原文

总的来说,我对 Parcel 和 Web 开发都是新手。

我正在观看此视频来了解包裹。 在这个视频中,他使用了我现在所理解的parcel v1。 当我尝试安装它时(package: Parcel-bundler),我收到了大量警告和错误,从中我了解到 Parcel v2 现在可用,并且可以更好地使用,在 包名称:parcel所以我:

npm uninstall Parcel-bundler

然后

npm install --save-dev -g Parcel

构建项目

安装的包裹数量:parcel --version 2.4.0

我的示例项目很简单: index.html 包含脚本和 css 文件。

构建项目后,

parcel build client/index.html --no-optimize --no-cache  

我希望输出 html 包含 js、css 文件中的代码。 但结果是看起来只是对js、css文件进行了重命名,并没有将代码注入到html文件中。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document title</title>
    <link rel="stylesheet" href="/index.ef1c6e2b.css">
</head>
<body>
    <h1 id="idh1">main header</h1>
    <script src="/index.a61d77df.js"></script>
</body>
</html>

package.json 是

{
  "name": "webappsetup",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/google-apps-script": "^1.0.45",
    "parcel-plugin-inliner": "^1.0.16"
  },
  "devDependencies": {
    "@parcel/transformer-sass": "^2.4.0"
  }
}

我的问题是,我期望 dist/index.html 包含 js 和 css 代码是否正确,如视频中所示(使用 parcel-bundler),或 parcel 的工作方式不同吗?

I am new to Parcel and to web dev in general.

I am watching this video to learn about parcel.
In this video he uses, what I understand now, parcel v1.
When I tried to install it (package: parcel-bundler) I got huge list of warnings and errors, from which I have learned that parcel v2 is now available, and better to use, under the package name: parcel so I:

npm uninstall parcel-bundler

and then

npm install --save-dev -g parcel.

building the project

installed parcel number: parcel --version 2.4.0

My sample project is simple:
index.html with script and css files.

<script src="js/main.js"></script>

<link rel="stylesheet" href="scss/main.scss">

After building the project with

parcel build client/index.html --no-optimize --no-cache  

I expected the output html to include the code within the js,css files.
But the result was that it looks like it only renamed the js,css files, and didn't inject the code to the html file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document title</title>
    <link rel="stylesheet" href="/index.ef1c6e2b.css">
</head>
<body>
    <h1 id="idh1">main header</h1>
    <script src="/index.a61d77df.js"></script>
</body>
</html>

the package.json is

{
  "name": "webappsetup",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/google-apps-script": "^1.0.45",
    "parcel-plugin-inliner": "^1.0.16"
  },
  "devDependencies": {
    "@parcel/transformer-sass": "^2.4.0"
  }
}

My question is, am I right to expect the dist/index.html include the js and css code, as in the video (which is using parcel-bundler), or parcel works differently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦行僧 2025-01-24 20:10:22

如果我正确理解您的问题,您正在问为什么包裹构建的输出 dist/index.html 仅仅是参考输出CSS文件(&lt; link link; link rel = rel = rel = “ stylesheet” href =“/index.ef1c6e2b.css”&gt; )和输出js file(&lt; script src =“/index.a61d77df.js” /code>),而不是嵌入内容。那对吗?我假设该应用程序可以正确运行,但是如果不是这样,请发布其他详细信息。

因此,假设我做到了,那么对于大多数用例,这可能是期望的。

如果出于某种原因,您的用例要求您具有内联脚本和样式,则可以这样做(请参阅 docs ):

<style>
   @import "./style.scss";
 </style>
 <script type="module">
   import value from "./other.ts";
   console.log(value);
 </script>

但是,正如文档所建议的...

注意:很少使用此,因为大的内联脚本/样式可能不利于(感知的)负载速度。

If I understand your question correctly, you are asking why the output dist/index.html that parcel builds merely references the output css file (<link rel="stylesheet" href="/index.ef1c6e2b.css">) and the output js file (<script src="/index.a61d77df.js"></script>), rather than inlining the contents. Is that right? I'm assuming the app will run correctly, but please post additional details if that's not the case.

So, assuming I got that right, this is expected behavior probably desirable for most use cases.

If for some reason your use case requires that you have inline scripts and styles, you could do it like this (see docs):

<style>
   @import "./style.scss";
 </style>
 <script type="module">
   import value from "./other.ts";
   console.log(value);
 </script>

But, as the docs recommend...

Note: Use this sparingly, as big inline scripts/styles can be detrimental to the (perceived) load speed.

失眠症患者 2025-01-24 20:10:22

您尚未将脚本命令添加到package.json文件中。在脚本中删除测试,然后添加 - public-url ./到您的build命令。
像这样;

"scripts": {
    "dev": "parcel src/*.html --open",
    "build": "parcel build client/index.html --no-optimize --no-cache --public-url ./"
  },

You haven't added script commands to your package.json file. Delete test in scripts and add --public-url ./ to your build command.
Like this;

"scripts": {
    "dev": "parcel src/*.html --open",
    "build": "parcel build client/index.html --no-optimize --no-cache --public-url ./"
  },

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文