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

+2-1
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

+4
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

+23-2
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
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 <[email protected]>",
55
"description": "A webpack loader for svelte",
66
"license": "MIT",

0 commit comments

Comments
 (0)