Skip to content

Commit 9f807ef

Browse files
committed
feat(webpack): use svelte-loader and fallback to svelte-loader-hot
Allows running older projects
1 parent 59d9271 commit 9f807ef

2 files changed

Lines changed: 70 additions & 34 deletions

File tree

‎packages/webpack5/__tests__/configuration/__snapshots__/svelte.spec.ts.snap‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,18 @@ exports[`svelte configuration for android 1`] = `
194194
/node_modules/
195195
],
196196
use: [
197-
/* config.module.rule('svelte').use('svelte-loader') */
197+
/* config.module.rule('svelte').use('svelte-loader-hot') */
198198
{
199-
loader: 'svelte-loader',
199+
loader: 'svelte-loader-hot',
200200
options: {
201-
compilerOptions: {
202-
dev: true
203-
},
201+
dev: true,
204202
preprocess: undefined,
205203
hotReload: true,
206204
hotOptions: {
207205
injectCss: false,
208206
'native': true
209-
}
207+
},
208+
onwarn: function () { /* omitted long function */ }
210209
}
211210
}
212211
]
@@ -523,19 +522,18 @@ exports[`svelte configuration for ios 1`] = `
523522
/node_modules/
524523
],
525524
use: [
526-
/* config.module.rule('svelte').use('svelte-loader') */
525+
/* config.module.rule('svelte').use('svelte-loader-hot') */
527526
{
528-
loader: 'svelte-loader',
527+
loader: 'svelte-loader-hot',
529528
options: {
530-
compilerOptions: {
531-
dev: true
532-
},
529+
dev: true,
533530
preprocess: undefined,
534531
hotReload: true,
535532
hotOptions: {
536533
injectCss: false,
537534
'native': true
538-
}
535+
},
536+
onwarn: function () { /* omitted long function */ }
539537
}
540538
}
541539
]

‎packages/webpack5/src/configuration/svelte.ts‎

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getPlatformName } from '../helpers/platform';
55
import { env as _env, IWebpackEnv } from '../index';
66
import { error } from '../helpers/log';
77
import base from './base';
8+
import { hasDependency } from '../helpers/dependencies';
89

910
export default function (config: Config, env: IWebpackEnv = _env): Config {
1011
base(config, env);
@@ -28,33 +29,70 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
2829
config.module.rule('workers').test(/\.(js|ts|svelte)$/);
2930

3031
// add a rule for .svelte files
31-
config.module
32-
.rule('svelte')
33-
.test(/\.svelte$/)
34-
.exclude.add(/node_modules/)
35-
.end()
36-
.use('svelte-loader')
37-
.loader('svelte-loader')
38-
.tap((options) => {
39-
const svelteConfig = getSvelteConfig();
40-
return {
41-
...options,
42-
compilerOptions: {
43-
...svelteConfig?.compilerOptions,
32+
if (hasDependency('svelte-loader')) {
33+
// configure using svelte-loader
34+
config.module
35+
.rule('svelte')
36+
.test(/\.svelte$/)
37+
.exclude.add(/node_modules/)
38+
.end()
39+
.use('svelte-loader')
40+
.loader('svelte-loader')
41+
.tap((options) => {
42+
const svelteConfig = getSvelteConfig();
43+
return {
44+
...options,
45+
compilerOptions: {
46+
...svelteConfig?.compilerOptions,
47+
dev: !production,
48+
},
49+
preprocess: svelteConfig?.preprocess,
50+
hotReload: !production,
51+
hotOptions: {
52+
injectCss: false,
53+
native: true,
54+
},
55+
};
56+
});
57+
} else {
58+
// fallback for projects still using svelte-loader-hot
59+
config.module
60+
.rule('svelte')
61+
.test(/\.svelte$/)
62+
.exclude.add(/node_modules/)
63+
.end()
64+
.use('svelte-loader-hot')
65+
.loader('svelte-loader-hot')
66+
.tap((options) => {
67+
const svelteConfig = getSvelteConfig();
68+
return {
69+
...options,
4470
dev: !production,
45-
},
46-
preprocess: svelteConfig?.preprocess,
47-
hotReload: !production,
48-
hotOptions: {
49-
injectCss: false,
50-
native: true,
51-
},
52-
};
53-
});
71+
preprocess: getSvelteConfigPreprocessor(),
72+
hotReload: !production,
73+
hotOptions: {
74+
injectCss: false,
75+
native: true,
76+
},
77+
// Suppress A11y warnings
78+
onwarn(warning, warn) {
79+
if (!/A11y:/.test(warning.message)) {
80+
warn(warning);
81+
}
82+
},
83+
};
84+
});
85+
}
5486

5587
return config;
5688
}
5789

90+
function getSvelteConfigPreprocessor(): any {
91+
const config = getSvelteConfig();
92+
93+
return config?.preprocess;
94+
}
95+
5896
interface ISvelteConfig {
5997
preprocess: any;
6098
compilerOptions: any;

0 commit comments

Comments
 (0)