|
1 | 1 | const path = require('path') |
2 | 2 | const fs = require('fs') |
3 | 3 | const ensureRequire = require('../ensure-require') |
4 | | -const cwd = process.cwd() |
| 4 | +const getVueJestConfig = require('../get-vue-jest-config') |
5 | 5 | const logger = require('../logger') |
6 | 6 |
|
7 | | -const rewriteImports = (content, filePath) => content.replace(/@import\s+(?:'([^']+)'|"([^"]+)"|([^\s;]+))/g, (entire, single, double, unquoted) => { |
8 | | - const oldImportPath = single || double || unquoted |
9 | | - const absoluteImportPath = path.join(path.dirname(filePath), oldImportPath) |
10 | | - const lastCharacter = entire[entire.length - 1] |
11 | | - const quote = lastCharacter === "'" || lastCharacter === '"' ? lastCharacter : '' |
12 | | - const importPath = path.relative(cwd, absoluteImportPath) |
13 | | - return '@import ' + quote + importPath + quote |
14 | | -}) |
| 7 | +const applyModuleNameMapper = require('./helpers/module-name-mapper-helper') |
| 8 | + |
| 9 | +/** |
| 10 | + * This module is meant to compile scss |
| 11 | + * |
| 12 | + * @param {String} content - the content of the scss string that should be compiled |
| 13 | + * @param {String} filePath - the path of the file holding the scss |
| 14 | + * @param {Object} jestConfig - the complete jest config |
| 15 | + * @returns {String} styles - the compiled scss |
| 16 | + */ |
| 17 | +module.exports = (content, filePath, jestConfig = {}) => { |
| 18 | + const vueJestConfig = getVueJestConfig(jestConfig) |
15 | 19 |
|
16 | | -module.exports = (content, filePath, config) => { |
17 | 20 | ensureRequire('scss', ['node-sass']) |
18 | 21 | const sass = require('node-sass') |
19 | 22 |
|
20 | 23 | let scssResources = '' |
21 | | - if (config && config.resources && config.resources.scss) { |
22 | | - scssResources = config.resources.scss |
| 24 | + if (vueJestConfig.resources && vueJestConfig.resources.scss) { |
| 25 | + scssResources = vueJestConfig.resources.scss |
23 | 26 | .map(scssResource => path.resolve(process.cwd(), scssResource)) |
24 | 27 | .filter(scssResourcePath => fs.existsSync(scssResourcePath)) |
25 | | - .map(scssResourcePath => rewriteImports(fs.readFileSync(scssResourcePath).toString(), scssResourcePath)) |
| 28 | + .map(scssResourcePath => fs.readFileSync(scssResourcePath).toString()) |
26 | 29 | .join('\n') |
27 | 30 | } |
28 | | - let result |
| 31 | + |
29 | 32 | try { |
30 | | - result = sass.renderSync({ |
31 | | - data: scssResources + rewriteImports(content, filePath), |
32 | | - outputStyle: 'compressed' |
| 33 | + return sass.renderSync({ |
| 34 | + data: scssResources + content, |
| 35 | + outputStyle: 'compressed', |
| 36 | + importer: (url, prev, done) => ({ file: applyModuleNameMapper(url, prev === 'stdin' ? filePath : prev, jestConfig) }) |
33 | 37 | }).css.toString() |
34 | 38 | } catch (err) { |
35 | | - config.hideStyleWarn && |
36 | | - logger.warn(`There was an error rendering the SCSS in ${filePath}. SCSS is not fully supported by vue-jest, so some features will throw errors. Webpack aliases are a common cause of errors.`) |
| 39 | + if (!vueJestConfig.hideStyleWarn) { |
| 40 | + logger.warn(`There was an error rendering the SCSS in ${filePath}. SCSS is fully supported by vue-jest. Still some features might throw errors. Webpack aliases are a common cause of errors. If you use Webpack aliases, please use jest's suggested way via moduleNameMapper which is supported.`) |
| 41 | + logger.warn(`Error while compiling styles: ${err}`) |
| 42 | + } |
37 | 43 | } |
38 | | - return result || '' |
| 44 | + |
| 45 | + return '' |
39 | 46 | } |
0 commit comments