Skip to content

Commit

Permalink
refactor: fix resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 25, 2020
1 parent c09dad0 commit 549d625
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/evaluator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import path from 'path';

import Evaluator from 'stylus/lib/visitor/evaluator';

import { klona } from 'klona/full';
import { Parser, utils } from 'stylus';
import { Evaluator, Parser, utils } from 'stylus';
import DepsResolver from 'stylus/lib/visitor/deps-resolver';

import { resolveFilename, readFile } from './utils';
Expand Down
58 changes: 32 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,59 @@ export default async function stylusLoader(source) {
);
}

const shouldUseWebpackImporter =
typeof options.webpackImporter === 'boolean'
? options.webpackImporter
: true;

if (shouldUseWebpackImporter) {
styl.set('Evaluator', await createEvaluator(this, source, stylusOptions));
}

if (
typeof stylusOptions.use !== 'undefined' &&
stylusOptions.use.length > 0
) {
for (const [i, plugin] of Object.entries(stylusOptions.use)) {
if (typeof plugin === 'string') {
let { length } = stylusOptions.use;

// eslint-disable-next-line no-plusplus
while (length--) {
let [item] = stylusOptions.use.splice(length, 1);
if (typeof item === 'string') {
try {
const resolved = require.resolve(plugin);
const resolved = require.resolve(item);

// eslint-disable-next-line import/no-dynamic-require, global-require
stylusOptions.use[i] = require(resolved)(stylusOptions);
item = require(resolved)(stylusOptions);
} catch (error) {
callback(
`Failed to load "${plugin}" Stylus plugin. Are you sure it's installed?\n${error}`
`Failed to load "${item}" Stylus plugin. Are you sure it's installed?\n${error}`
);

return;
}
}

styl.use(item);
}
}

if (typeof stylusOptions.import !== 'undefined') {
for (const imported of stylusOptions.import) {
styl.import(imported);
}
}

if (typeof stylusOptions.include !== 'undefined') {
for (const included of stylusOptions.include) {
styl.include(included);
}
}

if (stylusOptions.resolveURL !== false) {
styl.define('url', urlResolver(stylusOptions.resolveURL));
}

const shouldUseWebpackImporter =
typeof options.webpackImporter === 'boolean'
? options.webpackImporter
: true;

if (shouldUseWebpackImporter) {
styl.set('Evaluator', await createEvaluator(this, source, stylusOptions));
}

if (typeof stylusOptions.define !== 'undefined') {
const definitions = Array.isArray(stylusOptions.define)
? stylusOptions.define
Expand All @@ -106,18 +124,6 @@ export default async function stylusLoader(source) {
}
}

if (typeof stylusOptions.include !== 'undefined') {
for (const included of stylusOptions.include) {
styl.include(included);
}
}

if (typeof stylusOptions.import !== 'undefined') {
for (const imported of stylusOptions.import) {
styl.import(imported);
}
}

styl.render(async (error, css) => {
if (error) {
if (error.filename) {
Expand Down

0 comments on commit 549d625

Please sign in to comment.