简体   繁体   English

使用 NPM 安装 Bootstrap

[英]Installing Bootstrap with NPM

I know how to use npm to install and maintain build tools, but I realize I'm not sure how to leverage it to install frameworks (css or js I'd normally just slap the script/css links in my index.html).我知道如何使用 npm 来安装和维护构建工具,但我意识到我不确定如何利用它来安装框架(css 或 js,我通常只是在我的 index.html 中添加脚本/css 链接)。

But, let's say I want to use npm.但是,假设我想使用 npm。 I'd run an npm install bootstrap and npm install jquery , but then I'm not sure what the next step is.我会运行npm install bootstrapnpm install jquery ,但是我不确定下一步是什么。

I could then just say <link rel="stylesheet" type="text/css" href="nodemodules/boostrap/dist/css/bootstrap.css"> but that seems clunky and dumb.然后我可以只说<link rel="stylesheet" type="text/css" href="nodemodules/boostrap/dist/css/bootstrap.css">但这似乎笨拙和愚蠢。

I see a lot of people importing stuff like this with require('bootstrap');我看到很多人用require('bootstrap');导入这样的东西require('bootstrap'); but honestly I'm not even sure where to put that.但老实说,我什至不确定把它放在哪里。

Could somebody help me fill in these blanks?有人可以帮我填写这些空白吗?

A common way to handle this is via grunt tasks, which copy the relevant files out of node_modules, into a more appropriate folder.处理此问题的常用方法是通过 grunt 任务,将相关文件从 node_modules 复制到更合适的文件夹中。

This is an excerpt from a copy task which I used for a recent project...这是我用于最近项目的复制任务的摘录...

    copy: {
        types: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/@types',
                    src: ['**/*.ts'],
                    dest: 'scripts/typings'
                }
            ]
        },
        jquery: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/jquery/dist',
                    src: ['*.js'],
                    dest: 'Content/libraries/jquery'
                }
            ]
        },
        handlebars: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/handlebars/dist',
                    src: ['*.js'],
                    dest: 'Content/libraries/handlebars'
                }
            ]
        },
        bootstrap: {
            files: [
                {
                    expand: true,
                    cwd: 'node_modules/bootstrap',
                    src: ['dist/js/*.js', 'dist/fonts/*.*', 'dist/css/*.css'],
                    dest: 'Content/libraries/bootstrap'
                }
            ]
        }
    }

There may be more succinct ways to write this, but it works for me.可能有更简洁的方法来写这个,但它对我有用。

This allows me to update my website packages by doing the following...这允许我通过执行以下操作来更新我的网站包...

$ npm install
$ grunt copy

The folders /Content/libraries, and /scripts/typings are committed to my application git repo, node_modules isn't.文件夹 /Content/libraries 和 /scripts/typings 提交给我的应用程序 git repo,而 node_modules 不是。 This helps prevent issues if a module become unavailable at some stage in the future.如果模块在将来的某个阶段变得不可用,这有助于防止出现问题。 My git repo contains everything required for the site to function.我的 git repo 包含网站运行所需的一切。

If for some reason one of these modules was removed from npm, it would just mean I couldn't do upgrades, but my site will still work with the files that my git repo includes.如果出于某种原因从 npm 中删除了这些模块之一,则仅意味着我无法进行升级,但我的站点仍可使用我的 git repo 包含的文件。

In my case, I don't actually need node_modules at all in my runtime environment, as I'm not using nodejs.就我而言,我的运行时环境中实际上根本不需要 node_modules,因为我没有使用 nodejs。 I'm just using npm to manage my application's client-side module dependencies.我只是使用 npm 来管理我的应用程序的客户端模块依赖项。

This depends on your build configuration but for the sake of this answer let's say you have an app.js file where you import the rest of your styles and scripts and you have a webpack configuration that processes this file.这取决于您的构建配置,但为了这个答案,假设您有一个app.js文件,您可以在其中导入其余的样式和脚本,并且您有一个处理此文件的webpack配置。 Then you only need to install the following dependencies:那么你只需要安装以下依赖:

npm i popper.js jquery bootstrap

( popper.js is a bootstrap dependency) popper.js是一个引导依赖项)

Then you just have to import them in your app.js file like so:然后你只需要像这样在你的app.js文件中导入它们:

app.js应用程序.js

  window.Popper = require("popper.js").default;
  window.$ = window.jQuery = require("jquery");

  require("bootstrap");

You also need to import the styles of bootstrap of course.当然,您还需要导入 bootstrap 的样式。 You could do that in several ways.您可以通过多种方式做到这一点。 Let's say you have an app.scss file for your styles.假设您有一个用于样式的app.scss文件。 Then you can do it like so:然后你可以这样做:

app.scss应用程序.scss

@import "~bootstrap/scss/bootstrap";

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

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