fix: svelte and css2json-loader

This commit is contained in:
Igor Randjelovic
2020-11-23 19:13:45 +01:00
parent 00a1cb5fc6
commit 86a46b46cd
4 changed files with 46 additions and 55 deletions

View File

@ -20,8 +20,7 @@ exports[`svelte configuration for android 1`] = `
symlinks: true, symlinks: true,
alias: { alias: {
'~': '<TODO>appFullPath', '~': '<TODO>appFullPath',
'@': '<TODO>appFullPath', '@': '<TODO>appFullPath'
svelte: 'svelte-native'
}, },
extensions: [ extensions: [
'.android.svelte', '.android.svelte',
@ -62,10 +61,7 @@ exports[`svelte configuration for android 1`] = `
sourceMap: true, sourceMap: true,
declaration: false declaration: false
}, },
getCustomTransformers: function () { /* omitted long function */ }, getCustomTransformers: function () { /* omitted long function */ }
appendTsSuffixTo: [
'\\\\\\\\.svelte$'
]
} }
} }
] ]
@ -123,19 +119,22 @@ exports[`svelte configuration for android 1`] = `
/* config.module.rule('svelte') */ /* config.module.rule('svelte') */
{ {
test: /\\\\.svelte$/, test: /\\\\.svelte$/,
exclude: [
/node_modules/
],
use: [ use: [
/* config.module.rule('svelte').use('svelte-loader-hot') */ /* config.module.rule('svelte').use('svelte-loader-hot') */
{ {
loader: 'svelte-loader-hot', loader: 'svelte-loader-hot',
options: { options: {
dev: false, dev: true,
preprocess: [ preprocess: [
undefined, undefined,
{ {
markup: function () { /* omitted long function */ } markup: function () { /* omitted long function */ }
} }
], ],
hotReload: false, hotReload: true,
hotOptions: { hotOptions: {
injectCss: false, injectCss: false,
'native': true 'native': true
@ -258,8 +257,7 @@ exports[`svelte configuration for ios 1`] = `
symlinks: true, symlinks: true,
alias: { alias: {
'~': '<TODO>appFullPath', '~': '<TODO>appFullPath',
'@': '<TODO>appFullPath', '@': '<TODO>appFullPath'
svelte: 'svelte-native'
}, },
extensions: [ extensions: [
'.ios.svelte', '.ios.svelte',
@ -300,10 +298,7 @@ exports[`svelte configuration for ios 1`] = `
sourceMap: true, sourceMap: true,
declaration: false declaration: false
}, },
getCustomTransformers: function () { /* omitted long function */ }, getCustomTransformers: function () { /* omitted long function */ }
appendTsSuffixTo: [
'\\\\\\\\.svelte$'
]
} }
} }
] ]
@ -361,19 +356,22 @@ exports[`svelte configuration for ios 1`] = `
/* config.module.rule('svelte') */ /* config.module.rule('svelte') */
{ {
test: /\\\\.svelte$/, test: /\\\\.svelte$/,
exclude: [
/node_modules/
],
use: [ use: [
/* config.module.rule('svelte').use('svelte-loader-hot') */ /* config.module.rule('svelte').use('svelte-loader-hot') */
{ {
loader: 'svelte-loader-hot', loader: 'svelte-loader-hot',
options: { options: {
dev: false, dev: true,
preprocess: [ preprocess: [
undefined, undefined,
{ {
markup: function () { /* omitted long function */ } markup: function () { /* omitted long function */ }
} }
], ],
hotReload: false, hotReload: true,
hotOptions: { hotOptions: {
injectCss: false, injectCss: false,
'native': true 'native': true

View File

@ -1,9 +1,10 @@
import base from './base';
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'; import svelteNativePreprocessor from 'svelte-native-preprocessor';
import Config from 'webpack-chain';
import { env as _env, IWebpackEnv } from '../index';
import { getPlatform, getProjectRootPath } from '../helpers/project';
import base from './base';
import { error } from '../helpers/log';
export default function (config: Config, env: IWebpackEnv = _env): Config { export default function (config: Config, env: IWebpackEnv = _env): Config {
base(config, env); base(config, env);
@ -20,46 +21,39 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
config.module config.module
.rule('svelte') .rule('svelte')
.test(/\.svelte$/) .test(/\.svelte$/)
.exclude.add(/node_modules/)
.end()
.use('svelte-loader-hot') .use('svelte-loader-hot')
.loader('svelte-loader-hot') .loader('svelte-loader-hot')
.tap((options) => { .tap((options) => {
return { return {
...options, ...options,
dev: production, dev: !production,
preprocess: [getSvelteConfig()?.preprocess, svelteNativePreprocessor()], preprocess: [getSvelteConfigPreprocessor(), svelteNativePreprocessor()],
hotReload: production, hotReload: !production,
hotOptions: { hotOptions: {
injectCss: false, injectCss: false,
native: true, 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; return config;
} }
function getSvelteConfig(): { preprocess: any } | null { function getSvelteConfigPreprocessor(): any {
const config = getSvelteConfig();
return config?.preprocess;
}
function getSvelteConfig(): { preprocess: any } | undefined {
try { try {
const resolvedPath = require.resolve(`./svelte.config.js`, { const resolvedPath = require.resolve(`./svelte.config.js`, {
paths: [getProjectRootPath()], paths: [getProjectRootPath()],
}); });
return require(resolvedPath); return require(resolvedPath);
} catch (e) { } catch (err) {
return null; error('Could not find svelte.config.js.', err);
} }
} }

View File

@ -26,8 +26,7 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
...options, ...options,
compiler: require('nativescript-vue-template-compiler'), compiler: require('nativescript-vue-template-compiler'),
}; };
}) });
.end();
// set up ts support in vue files // set up ts support in vue files
config.module config.module

View File

@ -16,20 +16,11 @@ export default function loader(content: string, map: any) {
// todo: revise if this is necessary // todo: revise if this is necessary
// todo: perhaps use postCSS and just build imports into a single file? // todo: perhaps use postCSS and just build imports into a single file?
let dependencies = []; let dependencies = [];
getImportRules(ast) getAndRemoveImportRules(ast)
.map(extractUrlFromRule) .map(extractUrlFromRule)
.map(createRequireUri) .map(createRequireUri)
.forEach(({ uri, requireURI }) => { .forEach(({ uri, requireURI }) => {
dependencies.push( dependencies.push(`require("${requirePrefix}${requireURI}")`);
`global.registerModule("${uri}", () => require("${requirePrefix}${requireURI}"));`
);
// Call registerModule with requireURI to handle cases like @import "~@nativescript/theme/css/blue.css";
if (uri !== requireURI) {
dependencies.push(
`global.registerModule("${requireURI}", () => require("${requirePrefix}${requireURI}"));`
);
}
}); });
const str = JSON.stringify(ast, (k, v) => (k === 'position' ? undefined : v)); const str = JSON.stringify(ast, (k, v) => (k === 'position' ? undefined : v));
@ -60,6 +51,15 @@ function getImportRules(ast: Stylesheet): Import[] {
); );
} }
function getAndRemoveImportRules(ast: Stylesheet): Import[] {
const imports = getImportRules(ast);
ast.stylesheet.rules = ast.stylesheet.rules.filter(
(rule) => rule.type !== 'import'
);
return imports;
}
/** /**
* Extracts the url from import rule (ex. `url("./platform.css")`) * Extracts the url from import rule (ex. `url("./platform.css")`)
*/ */