|
1 | 1 | const path = require('path')
|
2 | 2 | const fs = require('fs')
|
3 | 3 | const sass = require('node-sass')
|
4 |
| -const cwd = process.cwd() |
5 | 4 |
|
6 |
| -module.exports = (content, dir, config) => { |
7 |
| - const getRelativeImportPath = (oldImportPath, absoluteImportPath) => (/^\~/.test(oldImportPath)) |
8 |
| - ? oldImportPath |
9 |
| - : path.relative(cwd, absoluteImportPath) |
| 5 | +const rewriteImports = (content, filePath) => content.replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => { |
| 6 | + const oldImportPath = single || double || unquoted |
| 7 | + const absoluteImportPath = path.join(path.dirname(filePath), oldImportPath) |
| 8 | + const lastCharacter = entire[entire.length - 1] |
| 9 | + const quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : '' |
| 10 | + const importPath = path.relative(process.cwd(), absoluteImportPath) |
| 11 | + return '@import ' + quote + importPath + quote |
| 12 | +}) |
10 | 13 |
|
| 14 | +module.exports = (content, filePath, config) => { |
11 | 15 | let scssResources = ''
|
12 | 16 | if (config.resources && config.resources.scss) {
|
13 | 17 | scssResources = config.resources.scss
|
14 |
| - .map(scssResource => path.resolve(cwd, scssResource)) |
| 18 | + .map(scssResource => path.resolve(process.cwd(), scssResource)) |
15 | 19 | .filter(scssResourcePath => fs.existsSync(scssResourcePath))
|
16 |
| - .map(scssResourcePath => fs.readFileSync(scssResourcePath).toString() |
17 |
| - .replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => { |
18 |
| - const oldImportPath = single || double || unquoted |
19 |
| - const absoluteImportPath = path.join(path.dirname(scssResourcePath), oldImportPath) |
20 |
| - const relImportPath = getRelativeImportPath(oldImportPath, absoluteImportPath) |
21 |
| - const newImportPath = relImportPath.split(path.sep).join('/') |
22 |
| - const lastCharacter = entire[entire.length - 1] |
23 |
| - const quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : '' |
24 |
| - return '@import ' + quote + newImportPath + quote |
25 |
| - }) |
26 |
| - ) |
| 20 | + .map(scssResourcePath => rewriteImports(fs.readFileSync(scssResourcePath).toString(), scssResourcePath)) |
27 | 21 | .join('\n')
|
28 | 22 | }
|
29 | 23 |
|
30 | 24 | return sass.renderSync({
|
31 |
| - data: scssResources + content, |
| 25 | + data: scssResources + rewriteImports(content, filePath), |
32 | 26 | outputStyle: 'compressed'
|
33 | 27 | }).css.toString()
|
34 | 28 | }
|
0 commit comments