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

add svelte support to webpack encore #781

Merged
merged 7 commits into from
Oct 10, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/js/hello_world.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let name = 'world';
</script>

<h1>Hello {name}!</h1>
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,19 @@ class Encore {
runtimeConfig = null;
webpackConfig = null;
}

/**
* If enabled, the SvelteJs loader is enabled.
*
* https://github.com/sveltejs/svelte-loader
*
* @returns {Encore}
*/
enableSvelte() {
webpackConfig.enableSvelte();

return this;
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class WebpackConfig {
this.useBabelTypeScriptPreset = false;
this.useWebpackNotifier = false;
this.useHandlebarsLoader = false;
this.useSvelte = false;

// Features/Loaders options
this.copyFilesConfigs = [];
Expand Down Expand Up @@ -150,6 +151,7 @@ class WebpackConfig {
eslint: () => {},
typescript: () => {},
handlebars: () => {},
svelte: () => {},
};

// Plugins options
Expand Down Expand Up @@ -692,6 +694,10 @@ class WebpackConfig {
}
}

enableSvelte() {
this.useSvelte = true;
}

enableTypeScriptLoader(callback = () => {}) {
if (this.useBabelTypeScriptPreset) {
throw new Error('Encore.enableTypeScriptLoader() can not be called when Encore.enableBabelTypeScriptPreset() has been called.');
Expand Down
13 changes: 12 additions & 1 deletion lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class ConfigGenerator {
config.stats = this.buildStatsConfig();

config.resolve = {
extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx', '.vue', '.ts', '.tsx'],
extensions: ['.wasm', '.mjs', '.js', '.json', '.jsx', '.vue', '.ts', '.tsx', '.svelte'],
alias: {}
};

Expand Down Expand Up @@ -403,6 +403,17 @@ class ConfigGenerator {
}));
}

if (this.webpackConfig.useSvelte) {
rules.push(applyRuleConfigurationCallback('svelte', {
Copy link
Contributor

@Kocal Kocal May 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes after the merge :)

resolve: {
mainFields: ['svelte', 'browser', 'module', 'main'],
extensions: ['.mjs', '.js', '.svelte'],
},
test: /\.svelte$/,
loader: 'svelte-loader',
}));
}

if (this.webpackConfig.useVueLoader) {
rules.push(applyRuleConfigurationCallback('vue', {
test: /\.vue$/,
Expand Down
8 changes: 8 additions & 0 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ const features = {
{ name: '@symfony/stimulus-bridge', enforce_version: true }
],
description: 'enable Stimulus bridge'
},
svelte: {
method: 'enableSvelte()',
packages: [
{ name: 'svelte', enforce_version: true },
{ name: 'svelte-loader', enforce_version: true }
],
description: 'process Svelte JS files'
}
};

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
"webpack": "^5.72",
"webpack-cli": "^4.9.1",
"webpack-notifier": "^1.15.0",
"svelte": "^3.50.0",
"svelte-loader": "^3.1.0",
"zombie": "^6.1.4"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions test/WebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1300,12 +1300,12 @@ describe('WebpackConfig object', () => {

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

config.addExternals({ 'jquery': 'jQuery', 'react': 'react' });
config.addExternals({ 'jquery': 'jQuery', 'react': 'react', 'svelte': 'svelte' });
config.addExternals({ 'lodash': 'lodash' });
config.addExternals(/^(jquery|\$)$/i);

expect(config.externals).to.deep.equals([
{ 'jquery': 'jQuery', 'react': 'react' },
{ 'jquery': 'jQuery', 'react': 'react', 'svelte': 'svelte' },
{ 'lodash': 'lodash' },
/^(jquery|\$)$/i
]);
Expand Down Expand Up @@ -1494,7 +1494,7 @@ describe('WebpackConfig object', () => {

expect(() => {
config.configureLoaderRule('reason');
}).to.throw('Loader "reason" is not configurable. Valid loaders are "javascript", "css", "images", "fonts", "sass", "less", "stylus", "vue", "eslint", "typescript", "handlebars" and the aliases "js", "ts", "scss".');
}).to.throw('Loader "reason" is not configurable. Valid loaders are "javascript", "css", "images", "fonts", "sass", "less", "stylus", "vue", "eslint", "typescript", "handlebars", "svelte" and the aliases "js", "ts", "scss".');
});

it('Call method with not a valid callback', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,23 @@ module.exports = {
});
});

it('When enabled, svelte is transformed', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
config.addEntry('main', './js/hello_world.svelte');
config.enableSvelte();

testSetup.runWebpack(config, (webpackAssert) => {
// check that babel transformed the svelte files
webpackAssert.assertOutputFileContains(
'main.js',
'SvelteComponent'
);

done();
});
});

it('When enabled, preact JSX is transformed with preact-compat!', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ describe('Public API', () => {

});

describe('enableSvelte', () => {

it('must return the API object', () => {
const returnedValue = api.enableSvelte();
expect(returnedValue).to.equal(api);
});

});

describe('enablePreactPreset', () => {

it('must return the API object', () => {
Expand Down
Loading