Skip to content

feat: add scoping css prefix option in compiler #4367

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export default class Component {
source,
ast,
compile_options.filename,
compile_options.dev
compile_options.dev,
compile_options.cssPrefix
);
this.stylesheet.validate(this);

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compile/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ export default class Stylesheet {

nodes_with_css_class: Set<CssNode> = new Set();

constructor(source: string, ast: Ast, filename: string, dev: boolean) {
constructor(source: string, ast: Ast, filename: string, dev: boolean, cssPrefix: string) {
this.source = source;
this.ast = ast;
this.filename = filename;
this.dev = dev;

if (ast.css && ast.css.children.length) {
this.id = `svelte-${hash(ast.css.content.styles)}`;
this.id = `${cssPrefix}-${hash(ast.css.content.styles)}`;

this.has_styles = true;

Expand Down
5 changes: 3 additions & 2 deletions src/compiler/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const valid_options = [
'css',
'loopGuardTimeout',
'preserveComments',
'preserveWhitespace'
'preserveWhitespace',
'cssPrefix'
];

function validate_options(options: CompileOptions, warnings: Warning[]) {
Expand Down Expand Up @@ -68,7 +69,7 @@ function validate_options(options: CompileOptions, warnings: Warning[]) {
}

export default function compile(source: string, options: CompileOptions = {}) {
options = assign({ generate: 'dom', dev: false }, options);
options = assign({ generate: 'dom', dev: false, cssPrefix: 'svelte' }, options);

const stats = new Stats();
const warnings = [];
Expand Down
1 change: 1 addition & 0 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface CompileOptions {

preserveComments?: boolean;
preserveWhitespace?: boolean;
cssPrefix?: string;
}

export interface ParserOptions {
Expand Down
4 changes: 3 additions & 1 deletion test/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ describe('css', () => {
css: read(`${__dirname}/samples/${dir}/expected.css`)
};

const actual_css = dom.css.code.replace(/svelte(-ref)?-[a-z0-9]+/g, (m, $1) => $1 ? m : 'svelte-xyz');
const cssPrefix = config.compileOptions && config.compileOptions.cssPrefix || 'svelte';
const regexp = new RegExp(`${cssPrefix}(-ref)?-[a-z0-9]+`, 'g');
const actual_css = dom.css.code.replace(regexp, (m, $1) => $1 ? m : 'svelte-xyz');
try {
assert.equal(actual_css, expected.css);
} catch (error) {
Expand Down
5 changes: 5 additions & 0 deletions test/css/samples/custom-css-prefix/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
compileOptions: {
cssPrefix: 'sv'
}
};
1 change: 1 addition & 0 deletions test/css/samples/custom-css-prefix/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div.svelte-xyz{color:red}
7 changes: 7 additions & 0 deletions test/css/samples/custom-css-prefix/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>red</div>

<style>
div {
color: red;
}
</style>