How to use foundation 6 with angular cli.I tried with plain scss but was unable to proceed with foundation 6 scss.How should I proceed with this. Thanks in advance.
Let’s get into it! When creating a new Angular app using the ng new app-name command, the CLI will ask you “ Which stylesheet format would you like to use? ” — select SCSS. When choosing this option when creating a new project, the main things the CLI does is the following:
To get the CLI to generate .scss files (or .sass / .less) is an easy matter. You can also set the --style flag with the following: If you’ve already created your Angular CLI app with the default .css files, it will take a bit more work to convert it over.
Using Sass with the Angular CLI 1 Starting an Angular CLI Project with Sass. Normally, when we run ng new my-app, our app will have .css files. ... 2 Converting a Current App to Sass. ... 3 Using Sass Imports. ... 4 Importing Sass Files Into Angular Components. ... 5 Sass Include Paths. ... 6 Using Bootstrap Sass Files. ... 7 Conclusion. ...
The other thing you need to do is update your CLI configuration to create SCSS files instead of CSS files when you create new components. There are two ways to do this — using the CLI command or adding the reference manually. To use the command type the following into your terminal: ng config schematics.@schematics/angular:component.styleext scss
Create a new "sassy" Project with the Angular CLI:
ng new sassy-project --style=sass
Then install Foundation Sites via NPM:
npm install foundation-sites --save
Finally import the Foundation SASS-File in the projects styles.scss file:
@import "../node_modules/foundation-sites/assets/foundation";
For more Details: https://github.com/angular/angular-cli#css-preprocessor-integration
Do you use the angular cli or the webpack starter package?
With webpack it's very straightforward to implement foundation. At the moment i'm using Angular2 Webpack Starter.
Import foundation-sites in your vendor.ts file. Like so:
import 'foundation-sites';
Import foundation scss file in your app.scss like this:
@import '~foundation-sites/scss/foundation'
Include your preferred mixins.
@include foundation-global-styles; @include foundation-typography;
Angular CLI (without webpack)
To include external sass files in your project, you have to change the angular cli build file. The angular cli is based on the ember cli and uses broccoli to compile your assets. There is a excellent article about this on the codementor website.
In short you have to install extra node_modules for broccoli and change your angular-cli-build.js file.
Run the following command to install the extra node_modules:
npm i broccoli-sass broccoli-merge-trees lodash glob broccoli-postcss postcss-cssnext cssnano --save
For reference here is my angular-cli-build.js
const Angular2App = require('angular-cli/lib/broccoli/angular2-app');
const compileSass = require('broccoli-sass');
const compileCSS = require('broccoli-postcss');
const cssnext = require('postcss-cssnext');
const cssnano = require('cssnano');
const mergeTrees = require('broccoli-merge-trees');
const _ = require('lodash');
const glob = require('glob');
var options = {
plugins: [
{
module: cssnext,
options: {
browsers: ['> 1%'],
warnForDuplicates: false
}
},
{
module: cssnano,
options: {
safe: true,
sourcemap: true
}
}
]
};
module.exports = function (defaults) {
let appTree = new Angular2App(defaults, {
sassCompiler: {
cacheExclude: [/\/_[^\/]+$/],
includePaths: [
'node_modules/foundation-sites/scss/util/util',
'node_modules/foundation-sites/scss/foundation',
'src/assets/styles'
]
},
vendorNpmFiles: [
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'zone.js/dist/**/*.+(js|js.map)',
'es6-shim/es6-shim.js',
'reflect-metadata/**/*.+(js|js.map)',
'rxjs/**/*.+(js|js.map)',
'@angular/**/*.+(js|js.map)'
]
});
let sass = mergeTrees(_.map(glob.sync('src/**/*.scss'), function (sassFile) {
sassFile = sassFile.replace('src/', '');
return compileSass(['src'], sassFile, sassFile.replace(/.scss$/, '.css'));
}));
let css = compileCSS(sass, options);
return mergeTrees([appTree, sass, css], { overwrite: true });
};
In your .scss file you can include foundation sass file like this:
@import "node_modules/foundation-sites/scss/foundation";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With