feat: svelte config (#9061)

* wip: svelte webpack5

* added project svelte config

* svelte snapshot
This commit is contained in:
Janos Hrubos
2020-11-23 18:13:05 +01:00
committed by GitHub
parent e544b4506f
commit 00a1cb5fc6
4 changed files with 556 additions and 3 deletions

View File

@ -1,10 +1,65 @@
import base from './base';
import { IWebpackEnv } from '@nativescript/webpack';
import { env as _env, IWebpackEnv } from '../index';
import Config from 'webpack-chain';
import { getPlatform, getProjectRootPath } from '../helpers/project';
import { merge } from 'webpack-merge';
import svelteNativePreprocessor from 'svelte-native-preprocessor';
// todo: add base configuration for svelte
export default function (config: Config, env: IWebpackEnv): Config {
export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env);
const platform = getPlatform();
const mode = env.production ? 'production' : 'development';
const production = mode === 'production';
// resolve .svelte files
// the order is reversed because we are using prepend!
config.resolve.extensions.prepend('.svelte').prepend(`.${platform}.svelte`);
// add a rule for .svelte files
config.module
.rule('svelte')
.test(/\.svelte$/)
.use('svelte-loader-hot')
.loader('svelte-loader-hot')
.tap((options) => {
return {
...options,
dev: production,
preprocess: [getSvelteConfig()?.preprocess, svelteNativePreprocessor()],
hotReload: production,
hotOptions: {
injectCss: false,
native: true,
},
};
})
.end();
// set up ts support in svelte files
config.module
.rule('ts')
.use('ts-loader')
.loader('ts-loader')
.tap((options = {}) => {
return merge(options, {
appendTsSuffixTo: ['\\.svelte$'],
});
});
// add an alias for svelte, since some plugins may try to import it
config.resolve.alias.set('svelte', 'svelte-native');
return config;
}
function getSvelteConfig(): { preprocess: any } | null {
try {
const resolvedPath = require.resolve(`./svelte.config.js`, {
paths: [getProjectRootPath()],
});
return require(resolvedPath);
} catch (e) {
return null;
}
}