Skip to content

Commit

Permalink
fix: fix for include path
Browse files Browse the repository at this point in the history
  • Loading branch information
yibn2008 committed Apr 13, 2017
1 parent a3b579d commit eaf59fb
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
37 changes: 24 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function getImportsToResolve (original, includePaths) {
let extname = path.extname(original)
let basename = path.basename(original, extname)
let dirname = path.dirname(original)
let basedirs = [dirname].concat(includePaths)

let imports = []
let names = [basename]
Expand All @@ -34,30 +33,44 @@ function getImportsToResolve (original, includePaths) {

for (let i = 0; i < names.length; i++) {
for (let j = 0; j < exts.length; j++) {
for (let basedir of basedirs) {
imports.push(path.join(basedir, names[i] + exts[j]))
// search relative to original file
imports.push(path.join(dirname, names[i] + exts[j]))

// search in includePaths
for (let includePath of includePaths) {
imports.push(path.join(includePath, dirname, names[i] + exts[j]))
}
}
}

return imports
}

function getLoaderConfig (loaderContext) {
let query = loaderUtils.getOptions(loaderContext) || {}
let configKey = query.config || 'sassLoader'
let config = loaderContext.options[configKey] || {}
function getLoaderConfig (ctx) {
let options = loaderUtils.getOptions(ctx) || {}
let includePaths = options.includePaths || []
let basedir = ctx.options.context || process.cwd()

delete query.config
// convert relative to absolute
for (let i = 0; i < includePaths.length; i++) {
if (!path.isAbsolute(includePaths[i])) {
includePaths[i] = path.join(basedir, includePaths[i])
}
}

return Object.assign({}, config, query)
return {
basedir,
includePaths,
baseEntryDir: path.dirname(ctx.resourcePath),
root: options.root
}
}

function * mergeSources (opts, entry, resolve, dependencies, level) {
level = level || 0
dependencies = dependencies || []

let includePaths = opts.includePaths || []
let includePaths = opts.includePaths
let content = false

if (typeof entry === 'object') {
Expand All @@ -83,7 +96,7 @@ function * mergeSources (opts, entry, resolve, dependencies, level) {
// test again
if (loaderUtils.isUrlRequest(file)) {
let absoluteFile = path.normalize(path.resolve(entryDir, file))
let relativeFile = path.relative(opts.baseDir, absoluteFile).replace(/\\/g, '/') // fix for windows path
let relativeFile = path.relative(opts.baseEntryDir, absoluteFile).replace(/\\/g, '/') // fix for windows path

if (relativeFile[0] !== '.') {
relativeFile = './' + relativeFile
Expand Down Expand Up @@ -180,8 +193,6 @@ module.exports = function (content) {
// for webpack 1
this.cacheable()

options.baseDir = path.dirname(entry)

function resolver (ctx) {
return function (dir, importFile) {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"loader-utils": "^1.1.0"
},
"peerDependencies": {
"node-sass": "^4.0.0",
"node-sass": "4.x || 3.x",
"webpack": "1.x || 2.x"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/normal/actual/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
*/

// include test
@import "include-test";
@import "include-test";
@import "foobaz/index";
3 changes: 3 additions & 0 deletions test/fixtures/normal/expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ body {

.include-test {
color: #FFF; }

.web_foobaz {
content: "web_foobaz"; }
3 changes: 3 additions & 0 deletions test/fixtures/normal/sass_modules/foobaz/foobaz.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.web_foobaz {
content: "web_foobaz";
}
1 change: 1 addition & 0 deletions test/fixtures/normal/sass_modules/foobaz/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "foobaz.scss";
2 changes: 1 addition & 1 deletion test/fixtures/normal/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
{
loader: loader,
options: {
includePaths: [ path.join(__dirname, 'extra')]
includePaths: [ path.join(__dirname, 'extra'), 'sass_modules']
}
}
]
Expand Down

0 comments on commit eaf59fb

Please sign in to comment.