Angular 5 CLI中如何将Bootstrap 4 CSS设为外部链接并自动复制?
Hey there! Let’s break down your two Angular 5 migration questions step by step— I’ve messed around with these exact config tweaks before, so I’ve got you covered.
1. Configure Build to Generate External CSS Link Tags Instead of Inlining CSS
When you import Bootstrap via style.css or the styles array in .angular-cli.json, Angular CLI defaults to inlining CSS into the HTML in development mode (production builds usually extract CSS to external files automatically). To force external CSS files for all environments, adjust your CLI config:
- Open your
.angular-cli.jsonfile and locate theappsarray (your app’s config is typically the first item in this array). - Add or update the
extractCssproperty totrue:"apps": [ { // Other existing configs... "extractCss": true, // Other existing configs... } ] - After this change, both
ng serve(development) andng build --prod(production) will extract all compiled CSS into separate.cssfiles, which the CLI will reference via<link>tags in your final HTML. Note: Production mode already hasextractCssenabled by default, so this tweak is mostly for making development builds match production’s behavior.
2. Automatically Copy Bootstrap CSS from node_modules to Assets/Vendor Directory
Manual copying definitely goes against the CLI’s automation philosophy— here’s how to set up automatic copying via the CLI’s built-in assets configuration:
- Open
.angular-cli.jsonagain, and find theassetsarray under your app’s config. - Add a new object to this array to define the copy rule:
"assets": [ "assets", "favicon.ico", { "glob": "bootstrap.min.css", "input": "../node_modules/bootstrap/dist/css", "output": "./assets/vendor/css" } ] - Let’s break down the properties:
glob: The filename pattern to match (use*.cssif you want to copy all Bootstrap CSS files, not just the minified one).input: The source directory relative to yoursrcfolder. Sincenode_moduleslives at the project root (outsidesrc), we use../to navigate up one level.output: The target directory relative to thedistfolder after build. This example will placebootstrap.min.cssindist/assets/vendor/css/.
- Now every time you run
ng serveorng build, the CLI will automatically copy the specified Bootstrap CSS file to your target directory— no manual work required.
内容的提问来源于stack exchange,提问作者Max Bündchen
相关产品推荐
相关产品推荐

