Skip to content

Commit 5ff5b90

Browse files
zairigimadweaverryan
authored andcommitted
add svelte support to webpack encore
1 parent 1f741c9 commit 5ff5b90

File tree

8 files changed

+983
-809
lines changed

8 files changed

+983
-809
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,19 @@ class Encore {
17261726
runtimeConfig = null;
17271727
webpackConfig = null;
17281728
}
1729+
1730+
/**
1731+
* If enabled, the SvelteJs loader is enabled.
1732+
*
1733+
* https://github.com/sveltejs/svelte-loader
1734+
*
1735+
* Encore.enableSvelte()
1736+
*/
1737+
enableSvelte() {
1738+
webpackConfig.enableSvelte();
1739+
1740+
return this;
1741+
}
17291742
}
17301743

17311744
/**

lib/WebpackConfig.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class WebpackConfig {
9595
this.useBabelTypeScriptPreset = false;
9696
this.useWebpackNotifier = false;
9797
this.useHandlebarsLoader = false;
98+
this.useSvelte = false;
9899

99100
// Features/Loaders options
100101
this.copyFilesConfigs = [];
@@ -692,6 +693,10 @@ class WebpackConfig {
692693
}
693694
}
694695

696+
enableSvelte() {
697+
this.useSvelte = true;
698+
}
699+
695700
enableTypeScriptLoader(callback = () => {}) {
696701
if (this.useBabelTypeScriptPreset) {
697702
throw new Error('Encore.enableTypeScriptLoader() can not be called when Encore.enableBabelTypeScriptPreset() has been called.');

lib/config-generator.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ConfigGenerator {
120120
config.stats = this.buildStatsConfig();
121121

122122
config.resolve = {
123-
extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx', '.vue', '.ts', '.tsx'],
123+
extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx', '.vue', '.ts', '.tsx', 'svelte'],
124124
alias: {}
125125
};
126126

@@ -403,6 +403,17 @@ class ConfigGenerator {
403403
}));
404404
}
405405

406+
if (this.webpackConfig.useSvelte) {
407+
rules.push(applyRuleConfigurationCallback('svelte', {
408+
resolve: {
409+
mainFields: ['svelte', 'browser', 'module', 'main'],
410+
extensions: ['.mjs', '.js', '.svelte'],
411+
},
412+
test: /\.svelte$/,
413+
loader: 'svelte-loader',
414+
}));
415+
}
416+
406417
if (this.webpackConfig.useVueLoader) {
407418
rules.push(applyRuleConfigurationCallback('vue', {
408419
test: /\.vue$/,

lib/features.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ const features = {
161161
{ name: '@symfony/stimulus-bridge', enforce_version: true }
162162
],
163163
description: 'enable Stimulus bridge'
164+
},
165+
svelte: {
166+
method: 'enableSvelte()',
167+
packages: [
168+
{ name: 'svelte', enforce_version: true },
169+
{ name: 'svelte-loader', enforce_version: true }
170+
],
171+
description: 'process Svelte JS files'
164172
}
165173
};
166174

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
"webpack": "^5.72",
9999
"webpack-cli": "^4.9.1",
100100
"webpack-notifier": "^1.15.0",
101+
"svelte": "^3.23.0",
102+
"svelte-loader": "^2.13.6",
101103
"zombie": "^6.1.4"
102104
},
103105
"peerDependencies": {

test/WebpackConfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,12 +1300,12 @@ describe('WebpackConfig object', () => {
13001300

13011301
expect(config.externals).to.deep.equals([]);
13021302

1303-
config.addExternals({ 'jquery': 'jQuery', 'react': 'react' });
1303+
config.addExternals({ 'jquery': 'jQuery', 'react': 'react', 'svelte': 'svelte' });
13041304
config.addExternals({ 'lodash': 'lodash' });
13051305
config.addExternals(/^(jquery|\$)$/i);
13061306

13071307
expect(config.externals).to.deep.equals([
1308-
{ 'jquery': 'jQuery', 'react': 'react' },
1308+
{ 'jquery': 'jQuery', 'react': 'react', 'svelte': 'svelte' },
13091309
{ 'lodash': 'lodash' },
13101310
/^(jquery|\$)$/i
13111311
]);

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ describe('Public API', () => {
266266

267267
});
268268

269+
describe('enableSvelte', () => {
270+
271+
it('must return the API object', () => {
272+
const returnedValue = api.enableSvelte();
273+
expect(returnedValue).to.equal(api);
274+
});
275+
276+
});
277+
269278
describe('enablePreactPreset', () => {
270279

271280
it('must return the API object', () => {

yarn.lock

Lines changed: 932 additions & 806 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)