Skip to content

[fix] remove lang tag when packaging #2486

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

Merged
merged 3 commits into from
Sep 24, 2021
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 .changeset/stupid-schools-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Remove lang tag when packaging
32 changes: 31 additions & 1 deletion packages/kit/src/packaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function make_package(config, cwd = process.cwd()) {
// it's a Svelte component
out_file = file.slice(0, -svelte_ext.length) + '.svelte';
out_contents = config.preprocess
? (await preprocess(source, config.preprocess, { filename })).code
? strip_lang_tags((await preprocess(source, config.preprocess, { filename })).code)
: source;
contains_svelte_files = true;
} else if (ext === '.ts' && file.endsWith('.d.ts')) {
Expand Down Expand Up @@ -187,6 +187,36 @@ function resolve_$lib_alias(file, content, config) {
return content;
}

/**
* Strip out lang="X" or type="text/X" tags. Doing it here is only a temporary solution.
* See https://github.com/sveltejs/kit/issues/2450 for ideas for places where it's handled better.
*
* @param {string} content
*/
function strip_lang_tags(content) {
strip_lang_tag('script');
strip_lang_tag('style');
return content;

/**
* @param {string} tagname
*/
function strip_lang_tag(tagname) {
const regexp = new RegExp(
`/<!--[^]*?-->|<${tagname}(\\s[^]*?)?(?:>([^]*?)<\\/${tagname}>|\\/>)`,
'g'
);
content = content.replace(regexp, (tag, attributes) => {
const idx = tag.indexOf(attributes);
return (
tag.substring(0, idx) +
attributes.replace(/\s(type|lang)=(["']).*?\2/, ' ') +
tag.substring(idx + attributes.length)
);
});
}
}

/**
* @param {string} filename
* @param {string} source
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script lang="ts">
<script>
import { foo } from './sub/foo';
export let bar = foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script lang="ts">
<script>
import { createEventDispatcher } from 'svelte';
export const astring;
const dispatch = createEventDispatcher();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script lang="ts">
<script>
export let foo;
</script>