Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
fix: allow using custom importers
Browse files Browse the repository at this point in the history
  • Loading branch information
hevele-moda committed Feb 17, 2021
1 parent a03c7bf commit eace811
Show file tree
Hide file tree
Showing 5 changed files with 3,135 additions and 142 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install Dependencies
run: yarn
- name: Semantic Release
run: npx semantic-release
run: yarn semantic-release || true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rollup-plugin-postcss",
"name": "@moda/rollup-plugin-postcss",
"version": "3.0.0",
"description": "Seamless integration between Rollup and PostCSS",
"main": "dist/index.js",
Expand All @@ -16,7 +16,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/egoist/rollup-plugin-postcss.git"
"url": "git+https://github.com/ModaOperandi/rollup-plugin-postcss.git"
},
"keywords": [
"postcss",
Expand Down Expand Up @@ -44,8 +44,10 @@
"jest": "^26.6.3",
"less": "^3.12.2",
"node-sass": "^5.0.0",
"node-sass-package-importer": "^5.3.2",
"postcss": "^8.1.14",
"rollup": "^2.34.2",
"semantic-release": "^17.3.9",
"stylus": "^0.54.8",
"sugarss": "^3.0.3",
"xo": "^0.35.0"
Expand Down Expand Up @@ -102,5 +104,8 @@
"master",
"next"
]
},
"publishConfig": {
"access": "public"
}
}
}
57 changes: 4 additions & 53 deletions src/sass-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import path from 'path'
import pify from 'pify'
import resolve from 'resolve'
import PQueue from 'p-queue'
import { loadModule } from './utils/load-module'

Expand All @@ -9,15 +7,6 @@ import { loadModule } from './utils/load-module'
const threadPoolSize = process.env.UV_THREADPOOL_SIZE || 4
const workQueue = new PQueue({ concurrency: threadPoolSize - 1 })

const moduleRe = /^~([a-z\d]|@).+/i

const getUrlOfPartial = url => {
const parsedUrl = path.parse(url)
return `${parsedUrl.dir}${path.sep}_${parsedUrl.base}`
}

const resolvePromise = pify(resolve)

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

Expand All @@ -37,46 +26,7 @@ export default {
data: data + code,
indentedSyntax: /\.sass$/.test(this.id),
sourceMap: this.sourceMap,
importer: [
(url, importer, done) => {
if (!moduleRe.test(url)) return done({ file: url })

const moduleUrl = url.slice(1)
const partialUrl = getUrlOfPartial(moduleUrl)

const options = {
basedir: path.dirname(importer),
extensions: ['.scss', '.sass', '.css']
}
const finishImport = id => {
done({
// Do not add `.css` extension in order to inline the file
file: id.endsWith('.css') ? id.replace(/\.css$/, '') : id
})
}

const next = () => {
// Catch all resolving errors, return the original file and pass responsibility back to other custom importers
done({ file: url })
}

// Give precedence to importing a partial
resolvePromise(partialUrl, options)
.then(finishImport)
.catch(error => {
if (
error.code === 'MODULE_NOT_FOUND' ||
error.code === 'ENOENT'
) {
resolvePromise(moduleUrl, options)
.then(finishImport)
.catch(next)
} else {
next()
}
})
}
].concat(this.options.importer || [])
importer: this.options.importer || []
})
.then(result => {
for (const file of result.stats.includedFiles) {
Expand Down Expand Up @@ -106,7 +56,8 @@ function loadSassOrThrow() {
// 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'
sassModuleIds.map(moduleId => `"${moduleId}"`).join(', ') +
' ' +
'in order to process SASS files'
)
}
8 changes: 7 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path'
import fs from 'fs-extra'
import { rollup } from 'rollup'
import packageImporter from 'node-sass-package-importer'
import postcss from '../src'

process.env.ROLLUP_POSTCSS_TEST = true
Expand Down Expand Up @@ -407,7 +408,12 @@ snapshotMany('sass', [
},
{
title: 'import',
input: 'sass-import/index.js'
input: 'sass-import/index.js',
options: {
use: {
sass: { importer: [packageImporter({ cwd: fixture('sass-import') })] }
}
}
}
])

Expand Down
Loading

0 comments on commit eace811

Please sign in to comment.