Skip to content

Commit

Permalink
Implemented more robust module loading (#211)
Browse files Browse the repository at this point in the history
- added loadModule() utility function
- updated loaders to use new module loading mechanism
- added more entries to gitignore file
- refactored gitignore to be more explicit
  • Loading branch information
slavafomin authored and egoist committed Jan 22, 2020
1 parent 56257f3 commit 3fc6c44
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/node_modules
dist
coverage
/**/dist/
/.idea/
/coverage/
/node_modules/
/package-lock.json
7 changes: 5 additions & 2 deletions src/less-loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import importCwd from 'import-cwd'
import pify from 'pify'
import humanlizePath from './utils/humanlize-path'
import { loadModule } from './utils/load-module'

export default {
name: 'less',
test: /\.less$/,
async process({ code }) {
const less = importCwd('less')
const less = loadModule('less')
if (!less) {
throw new Error(`You need to install "less" packages in order to process Less files`)
}

let { css, map, imports } = await pify(less.render.bind(less))(code, {
...this.options,
Expand Down
29 changes: 23 additions & 6 deletions src/sass-loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import pify from 'pify'
import resolve from 'resolve'
import importCwd from 'import-cwd'
import PQueue from 'p-queue'
import { loadModule } from './utils/load-module'

// This queue makes sure node-sass leaves one thread available for executing fs tasks
// See: https://github.com/sass/node-sass/issues/857
Expand All @@ -18,15 +18,15 @@ const getUrlOfPartial = url => {

const resolvePromise = pify(resolve)

// List of supported SASS modules in the order of preference
const sassModuleIds = ['node-sass', 'sass']

export default {
name: 'sass',
test: /\.s[ac]ss$/,
test: /\.(sass|scss)$/,
process({ code }) {
return new Promise((resolve, reject) => {
const sass = importCwd.silent('node-sass') || importCwd.silent('sass')
if (!sass) {
throw new Error(`You need to install either node-sass or sass in order to process Sass files`)
}
const sass = loadSassOrThrow()
const render = pify(sass.render.bind(sass))
return workQueue.add(() =>
render({
Expand Down Expand Up @@ -90,3 +90,20 @@ export default {
})
}
}

function loadSassOrThrow() {
// Loading one of the supported modules
for (const moduleId of sassModuleIds) {
const module = loadModule(moduleId)
if (module) {
return module
}
}

// Throwing exception if module can't be loaded
throw new Error(
`You need to install one of the following packages: ` +
sassModuleIds.map(moduleId => `"${moduleId}"`).join(', ') + ' ' +
`in order to process SASS files`
)
}
7 changes: 5 additions & 2 deletions src/stylus-loader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import importCwd from 'import-cwd'
import pify from 'pify'
import { loadModule } from './utils/load-module'

export default {
name: 'stylus',
test: /\.(styl|stylus)$/,
async process({ code }) {
const stylus = importCwd('stylus')
const stylus = loadModule('stylus')
if (!stylus) {
throw new Error(`You need to install "stylus" packages in order to process Stylus files`)
}

const style = stylus(code, {
...this.options,
Expand Down
12 changes: 12 additions & 0 deletions src/utils/load-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import importCwd from 'import-cwd'

export function loadModule(moduleId) {
// Trying to load module normally (relative to plugin directory)
const path = require.resolve(moduleId)
if (path) {
return require(path)
}

// Then, trying to load it relative to CWD
return importCwd.silent(moduleId)
}

0 comments on commit 3fc6c44

Please sign in to comment.