Skip to content

Commit

Permalink
fix: allow importing relative paths in global resources
Browse files Browse the repository at this point in the history
  • Loading branch information
pmrotule committed Aug 4, 2023
1 parent 972e5aa commit 1359d88
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions e2e/2.x/style/colors.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@primary-color: "red";
1 change: 1 addition & 0 deletions e2e/2.x/style/colors.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$primary-color: #333;
4 changes: 3 additions & 1 deletion e2e/2.x/style/variables.less
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
@primary-color: "red";
@import "./colors.less";

@font-size: 16px;
4 changes: 3 additions & 1 deletion e2e/2.x/style/variables.scss
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
$primary-color: #333;
@import './colors.scss';

$font-size: 16px;
1 change: 1 addition & 0 deletions e2e/3.x/style/colors.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@primary-color: "red";
1 change: 1 addition & 0 deletions e2e/3.x/style/colors.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$primary-color: #333;
4 changes: 3 additions & 1 deletion e2e/3.x/style/variables.less
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
@primary-color: "red";
@import "./colors.less";

@font-size: 16px;
4 changes: 3 additions & 1 deletion e2e/3.x/style/variables.scss
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
$primary-color: #333;
@import './colors.scss';

$font-size: 16px;
19 changes: 14 additions & 5 deletions packages/vue2-jest/lib/process-style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const path = require('path')
const fs = require('fs')
const cssTree = require('css-tree')
const getVueJestConfig = require('./utils').getVueJestConfig
const compileStyle = require('@vue/component-compiler-utils').compileStyle
Expand All @@ -12,14 +11,23 @@ function getGlobalResources(resources, lang) {
let globalResources = ''
if (resources && resources[lang]) {
globalResources = resources[lang]
.map(resource => path.resolve(process.cwd(), resource))
.filter(resourcePath => fs.existsSync(resourcePath))
.map(resourcePath => fs.readFileSync(resourcePath).toString())
.join('\n')
.map(resource => {
const absolutePath = path.resolve(process.cwd(), resource)
return `${getImportLine(lang, absolutePath)}\n`
})
.join('')
}
return globalResources
}

function getImportLine(lang, filePath) {
const importLines = {
default: `@import "${filePath}";`,
sass: `@import "${filePath}"`
}
return importLines[lang] || importLines.default
}

function extractClassMap(cssCode) {
const ast = cssTree.parse(cssCode)

Expand All @@ -35,6 +43,7 @@ function extractClassMap(cssCode) {
function getPreprocessOptions(lang, filePath, jestConfig) {
if (lang === 'scss' || lang === 'sass') {
return {
filename: filePath,
importer: (url, prev, done) => ({
file: applyModuleNameMapper(
url,
Expand Down
19 changes: 14 additions & 5 deletions packages/vue3-jest/lib/process-style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { compileStyle } = require('@vue/compiler-sfc')
const path = require('path')
const fs = require('fs')
const cssTree = require('css-tree')
const getVueJestConfig = require('./utils').getVueJestConfig
const applyModuleNameMapper = require('./module-name-mapper-helper')
Expand All @@ -12,14 +11,23 @@ function getGlobalResources(resources, lang) {
let globalResources = ''
if (resources && resources[lang]) {
globalResources = resources[lang]
.map(resource => path.resolve(process.cwd(), resource))
.filter(resourcePath => fs.existsSync(resourcePath))
.map(resourcePath => fs.readFileSync(resourcePath).toString())
.join('\n')
.map(resource => {
const absolutePath = path.resolve(process.cwd(), resource)
return `${getImportLine(lang, absolutePath)}\n`
})
.join('')
}
return globalResources
}

function getImportLine(lang, filePath) {
const importLines = {
default: `@import "${filePath}";`,
sass: `@import "${filePath}"`
}
return importLines[lang] || importLines.default
}

function extractClassMap(cssCode) {
const ast = cssTree.parse(cssCode)

Expand All @@ -35,6 +43,7 @@ function extractClassMap(cssCode) {
function getPreprocessOptions(lang, filePath, jestConfig) {
if (lang === 'scss' || lang === 'sass') {
return {
filename: filePath,
importer: (url, prev) => ({
file: applyModuleNameMapper(
url,
Expand Down

0 comments on commit 1359d88

Please sign in to comment.