@@ -136,19 +136,19 @@ async function enhanceCSS(module) {
136136 }
137137
138138 // 预处理器编译
139- let processedCSS = inputCSS
139+ let processedCSS = normalizeRootStyleImports ( inputCSS )
140140 const ext = path . extname ( absolutePath ) . toLowerCase ( )
141141
142142 try {
143143 if ( ext === '.less' ) {
144- const result = await less . render ( inputCSS , {
144+ const result = await less . render ( processedCSS , {
145145 filename : absolutePath ,
146- paths : [ path . dirname ( absolutePath ) ] ,
146+ paths : [ path . dirname ( absolutePath ) , getWorkPath ( ) ] ,
147147 } )
148148 processedCSS = result . css
149149 } else if ( ext === '.scss' || ext === '.sass' ) {
150- const result = sass . compileString ( inputCSS , {
151- loadPaths : [ path . dirname ( absolutePath ) ] ,
150+ const result = sass . compileString ( processedCSS , {
151+ loadPaths : [ path . dirname ( absolutePath ) , getWorkPath ( ) ] ,
152152 syntax : ext === '.sass' ? 'indented' : 'scss' ,
153153 } )
154154 processedCSS = result . css
@@ -175,7 +175,7 @@ async function enhanceCSS(module) {
175175 // @import 样式导入
176176 // 替换字符串首尾的引号
177177 const str = node . params . replace ( / ^ [ ' " ] | [ ' " ] $ / g, '' )
178- const importFullPath = path . resolve ( absolutePath , `../ ${ str } ` )
178+ const importFullPath = resolveStyleImportPath ( absolutePath , str )
179179
180180 node . remove ( )
181181
@@ -267,6 +267,19 @@ function getAbsolutePath(modulePath) {
267267 }
268268}
269269
270+ function resolveStyleImportPath ( absolutePath , importPath , workPath = getWorkPath ( ) ) {
271+ if ( importPath . startsWith ( '/' ) ) {
272+ return path . join ( workPath , importPath )
273+ }
274+ return path . resolve ( path . dirname ( absolutePath ) , importPath )
275+ }
276+
277+ function normalizeRootStyleImports ( source , workPath = getWorkPath ( ) ) {
278+ return source . replace ( / ( @ i m p o r t \s + (?: \( .* ?\) \s * ) ? (?: u r l \( ) ? [ ' " ] ) ( \/ [ ^ ' " ) ] + ) ( [ ' " ] \) ? ) / g, ( _ , prefix , importPath , suffix ) => {
279+ return `${ prefix } ${ path . join ( workPath , importPath ) } ${ suffix } `
280+ } )
281+ }
282+
270283/**
271284 * 移除基础组件选择器的 scoped 属性
272285 * @param {string } css - 包含 scoped 属性的 CSS
@@ -332,4 +345,4 @@ function processHostSelector(selector, moduleId) {
332345 . replace ( / : h o s t (? = \. | # | : ) / g, `[data-v-${ moduleId } ]` )
333346}
334347
335- export { compileSS , ensureImportSemicolons , processHostSelector , removeBaseComponentScope }
348+ export { compileSS , ensureImportSemicolons , normalizeRootStyleImports , processHostSelector , removeBaseComponentScope , resolveStyleImportPath }
0 commit comments