简体   繁体   English

如何将单个依赖项与 Webpack 捆绑在一起?

[英]How do I bundle a single dependency with Webpack?

I'm trying to load a new dependency into my rather sizable project, namely Glide.js.我正在尝试将一个新的依赖项加载到我相当大的项目中,即 Glide.js。 There are multiple nested foo.html files on which this dependency must be able to run.有多个嵌套的 foo.html 文件,此依赖项必须能够在其上运行。 Initially, I was going to just put:最初,我只想说:

<link rel="stylesheet" href="node_modules/@glidejs/glide/dist/css/glide.core.min.css">
<link rel="stylesheet" href="node_modules/@glidejs/glide/dist/css/glide.theme.min.css">
<script src="https://cdn.jsdelivr.net/npm/@glidejs/glide"></script>

However, I want to use Webpack. I do have a webpack.config.js file, and Glide.js's website says I should be able to use Webpack. But it doesn't say how to do it.但是,我想使用 Webpack。我有一个webpack.config.js文件, Glide.js 的网站说我应该可以使用 Webpack。但它没有说明如何做。 How do I do what I tried to do early but in Webpack?我如何做我在 Webpack 时尝试做的事情?

I tried putting this in the webpack.config.js file:我试着把它放在 webpack.config.js 文件中:

import Glide from '@glidejs/glide'

new Glide('.glide').mount()

and all I get is我得到的只是

import Glide from '@glidejs/glide';
       ^^^^^

SyntaxError: Unexpected identifier

How do I fix this?我该如何解决?

first you should understand how Webpack works, checkout the guide: https://webpack.js.org/guides/getting-started/首先你应该了解 Webpack 是如何工作的,查看指南: https://webpack.js.org/guides/getting-started/

webpack.config.js is webpack's configuration file and you shouldn't put there your app code. webpack.config.js是 webpack 的配置文件,你不应该把你的应用程序代码放在那里。

Move your code:移动你的代码:

import Glide from '@glidejs/glide'

new Glide('.glide').mount()

into one of your bundle entrypoint, ex.进入您的捆绑入口点之一,例如。 src/index,js , then you should run webpack to build your assets, and use generated assets in your templates. src/index,js ,那么你应该运行 webpack 来构建你的资产,并在你的模板中使用生成的资产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM