Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow importing relative paths in global resources #548

Merged
merged 1 commit into from Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/2.x/style/colors.less
@@ -0,0 +1 @@
@primary-color: "red";
1 change: 1 addition & 0 deletions e2e/2.x/style/colors.scss
@@ -0,0 +1 @@
$primary-color: #333;
4 changes: 3 additions & 1 deletion e2e/2.x/style/variables.less
@@ -1 +1,3 @@
@primary-color: "red";
@import "./colors.less";

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

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

@font-size: 16px;
4 changes: 3 additions & 1 deletion e2e/3.x/style/variables.scss
@@ -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
@@ -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
@@ -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