我是pro npm / gulp风格的工作流程的诺言,我正在使用一个名为react-starterify的repo来构建一个简单的webapp。
我正在尝试使用@import“变量”添加部分scss文件;在我的main.scss文件中。但是,在最终编译的css文件中,它实际上是打印“@import'变量';”在文件的顶部,而不是将其作为文件的一部分拉入。
我错过了什么?
这是我的gulpfile.babel.js:
import gulp from 'gulp';
import autoprefixer from 'autoprefixer';
import browserify from 'browserify';
import watchify from 'watchify';
import source from 'vinyl-source-stream';
import buffer from 'vinyl-buffer';
import eslint from 'gulp-eslint';
import babelify from 'babelify';
import uglify from 'gulp-uglify';
import rimraf from 'rimraf';
import notify from 'gulp-notify';
import browserSync, { reload } from 'browser-sync';
import sourcemaps from 'gulp-sourcemaps';
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import nested from 'postcss-nested';
import vars from 'postcss-simple-vars';
import extend from 'postcss-simple-extend';
import cssnano from 'cssnano';
import htmlReplace from 'gulp-html-replace';
import imagemin from 'gulp-imagemin';
import pngquant from 'imagemin-pngquant';
import runSequence from 'run-sequence';
import ghPages from 'gulp-gh-pages';
const paths = {
bundle: 'app.js',
entry: 'src/Index.js',
srcCss: 'src/**/main.scss',
srcImg: 'src/images/**',
srcLint: ['src/**/*.js', 'test/**/*.js'],
dist: 'dist',
distJs: 'dist/js',
distImg: 'dist/images',
distDeploy: './dist/**/*'
};
const customOpts = {
entries: [paths.entry],
debug: true,
cache: {},
packageCache: {}
};
const opts = Object.assign({}, watchify.args, customOpts);
gulp.task('clean', cb => {
rimraf('dist', cb);
});
gulp.task('browserSync', () => {
browserSync({
server: {
baseDir: './'
}
});
});
gulp.task('watchify', () => {
const bundler = watchify(browserify(opts));
function rebundle() {
return bundler.bundle()
.on('error', notify.onError())
.pipe(source(paths.bundle))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.distJs))
.pipe(reload({ stream: true }));
}
bundler.transform(babelify)
.on('update', rebundle);
return rebundle();
});
gulp.task('browserify', () => {
browserify(paths.entry, { debug: true })
.transform(babelify)
.bundle()
.pipe(source(paths.bundle))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.distJs));
});
gulp.task('styles', () => {
gulp.src(paths.srcCss)
.pipe(rename({ extname: '.css' }))
.pipe(sourcemaps.init())
.pipe(postcss([vars, extend, nested, autoprefixer, cssnano]))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dist))
.pipe(reload({ stream: true }));
});
gulp.task('htmlReplace', () => {
gulp.src('index.html')
.pipe(htmlReplace({ css: 'styles/main.css', js: 'js/app.js' }))
.pipe(gulp.dest(paths.dist));
});
gulp.task('images', () => {
gulp.src(paths.srcImg)
.pipe(imagemin({
progressive: true,
svgoPlugins: [{ removeViewBox: false }],
use: [pngquant()]
}))
.pipe(gulp.dest(paths.distImg));
});
gulp.task('lint', () => {
gulp.src(paths.srcLint)
.pipe(eslint())
.pipe(eslint.format());
});
gulp.task('watchTask', () => {
gulp.watch(paths.srcCss, ['styles']);
gulp.watch(paths.srcLint, ['lint']);
});
gulp.task('deploy', () => {
gulp.src(paths.distDeploy)
.pipe(ghPages());
});
gulp.task('watch', cb => {
runSequence('clean', ['browserSync', 'watchTask', 'watchify', 'styles', 'lint', 'images'], cb);
});
gulp.task('build', cb => {
process.env.NODE_ENV = 'production';
runSequence('clean', ['browserify', 'styles', 'htmlReplace', 'images'], cb);
});
我还注意到另一个问题,它将@mixin中的变量视为未定义。例如
@mixin blacklinks ($link, $visit, $hover, $active, $disabled) {
a,
.text-btn-black {
@include a1;
text-decoration: none !important;
color: $link;
&:visited {
color: $visit;
}
&:hover {
color: $hover;
}
&:active {
color: $active;
}
&:disabled {
color: $disabled
}
}
}
它在$ link处失败,其中该var从另一个部分_base.scss发送。
@include blacklinks($black, $black, $mediumgray, $darkgray, $disabledtext);
答案 0 :(得分:1)
您需要安装并使用postcss-import
包。
这是一个简化但有效的例子:
<强> gulpfile.js 强>:
import atImport from 'postcss-import';
gulp.task('styles', () => {
gulp.src(paths.srcCss)
.pipe(rename({ extname: '.css' }))
.pipe(sourcemaps.init())
.pipe(postcss([atImport, vars, extend, nested, autoprefixer, cssnano]))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(paths.dist))
.pipe(reload({ stream: true }));
});
<强>的src /风格/ main.scss 强>:
@import 'variables.scss';
.foo {
color:$c;
}
<强>的src /风格/ variables.scss 强>:
$c: blue;