我有一个这样的样式表模块:
import autoPrefixer from "gulp-autoprefixer"
import groupCssMediaQueries from "gulp-group-css-media-queries"
import GulpCleanCss from "gulp-clean-css"
import dartSass from "sass"
import gulpSass from "gulp-sass"
const sass = gulpSass(dartSass);
export const scss = () => {
return app.gulp.src(app.path.src.css, { sourcemaps: true })
.pipe(app.plugins.cached("css"))
.pipe(app.plugins.newer(app.path.build.css))
.pipe(
sass({
outputStyle: "expanded"
})
)
.pipe(
groupCssMediaQueries()
)
.pipe(
autoPrefixer({
overrideBrowserslist: ["last 5 version"],
cascade: true
})
)
.pipe(app.plugins.remember("css"))
.pipe(webpcss())
.pipe(dest(app.path.build.css))
.pipe(GulpCleanCss())
.pipe(
app.plugins.rename({
extname: ".min.css"
})
)
.pipe(app.gulp.dest(path.build.css))
.pipe(app.plugins.browsersync.stream())
}
里面有app.plugins
一些通用的插件,但这并不重要。这里
const sass = gulpSass(dartSass);
我保存了 gulpSass 函数并将编译器传递给它。也就是说,sass
理论上是一个函数,但我得到一个错误sass is not a function
。同时,本课(51:25) 中的一切对作者都有效,并且文档建议以这种方式连接和使用模块。
要在 ECMAScript 模块中使用 gulp-sass(在较新的 Node.js 14 及更高版本中受支持),请执行以下操作:
import dartSass from 'sass'; import gulpSass from 'gulp-sass'; const sass = gulpSass(dartSass);
用法
gulp-sass 必须在 Gulp 任务中使用。您的任务可以调用 sass()(异步渲染 CSS)或 sass.sync()(同步渲染 CSS)。
为什么我会收到错误消息?我该如何解决这个问题,我忽略了什么?
PS 对象位于sass
:
DestroyableTransform {
_readableState: ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: BufferList { length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null
},
readable: true,
_events: [Object: null prototype] {
end: [Function: bound onceWrapper] { listener: [Function: onend] },
prefinish: [Function: prefinish]
},
_eventsCount: 2,
_maxListeners: undefined,
_writableState: WritableState {
objectMode: true,
highWaterMark: 16,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function (anonymous)],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: CorkedRequest {
next: null,
entry: null,
finish: [Function (anonymous)]
}
},
writable: true,
allowHalfOpen: true,
_transformState: {
afterTransform: [Function: bound afterTransform],
needTransform: false,
transforming: false,
writecb: null,
writechunk: null,
writeencoding: null
},
_destroyed: false,
_transform: [Function (anonymous)],
[Symbol(kCapture)]: false
}
节点 js 版本 - 14.17.6
Gulp4.0.2
我不知道是什么问题。我清除了 npm 缓存,再次删除并安装了 sass 和 gulp-sass,没有任何帮助。
我通过创建了一个新项目
npm init
,将其切换到模块模式,并在那里只导入了该功能所需的模块——一切正常。显然,该模块与我旧程序集中的其他模块冲突。剩下的我会慢慢安装,如果出现同样的错误,我会补充答案。