Skip to content

Commit 09f9bc1

Browse files
committed
fix: more robust conditionNames detection
fixes #226
1 parent c1adccc commit 09f9bc1

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"space-before-function-paren": [ 2, "never" ],
99
"semi": [ 2, "always" ],
1010
"no-console": 0,
11-
"mocha/no-exclusive-tests": 2
11+
"mocha/no-exclusive-tests": 2,
12+
"no-inner-declarations": 0
1213
},
1314
"extends": "eslint:recommended",
1415
"parserOptions": {

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# svelte-loader changelog
22

3+
## 3.1.7
4+
5+
* More robust `conditionNames: ['svelte']` detection ([#226](https://github.com/sveltejs/svelte-loader/pull/226))
6+
37
## 3.1.6
48

59
* Detect missing `conditionNames: ['svelte']` ([#225](https://github.com/sveltejs/svelte-loader/pull/225))

index.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const fs = require('fs');
23
const { getOptions } = require('loader-utils');
34
const { buildMakeHot } = require('./lib/make-hot.js');
45
const { compile, preprocess } = require('svelte/compiler');
@@ -24,10 +25,30 @@ for (let i = 0; i < process.argv.length; i++) {
2425
}
2526

2627
try {
27-
const config = require(path.resolve(process.cwd(), configFile));
28-
if (!config.resolve || !config.resolve.conditionNames || !config.resolve.conditionNames.includes('svelte')) {
28+
const configPath = path.resolve(process.cwd(), configFile);
29+
const config = require(configPath);
30+
let found = false;
31+
if (Array.isArray(config)) {
32+
found = config.some(check);
33+
} else {
34+
found = check(config);
35+
}
36+
37+
if (!found) {
2938
console.warn('\n\u001B[1m\u001B[31mWARNING: You should add "svelte" to the "resolve.conditionNames" array in your webpack config. See https://github.com/sveltejs/svelte-loader#resolveconditionnames for more information\u001B[39m\u001B[22m\n');
3039
}
40+
41+
function check(config) {
42+
if (typeof config === 'function') {
43+
// We could try to invoke it but that could maybe have side unintended side effects
44+
// and/or fail due to missing parameters, so we read the file instead
45+
const configString = fs.readFileSync(configPath, 'utf-8');
46+
const result = /conditionNames\s*:[\s|\n]*\[([^\]]+?)\]/.exec(configString);
47+
return !!result && !!result[1].includes('svelte');
48+
} else {
49+
return !!config.resolve && !!config.resolve.conditionNames && config.resolve.conditionNames.includes('svelte');
50+
}
51+
}
3152
} catch (e) {
3253
// do nothing and hope for the best
3354
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-loader",
3-
"version": "3.1.6",
3+
"version": "3.1.7",
44
"author": "Nico Rehwaldt <git_nikku@nixis.de>",
55
"description": "A webpack loader for svelte",
66
"license": "MIT",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy