diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 9822529ece61..d4b446f440a2 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -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); diff --git a/src/compiler/compile/css/Stylesheet.ts b/src/compiler/compile/css/Stylesheet.ts index 246dab0f1219..cf99fc88e7a9 100644 --- a/src/compiler/compile/css/Stylesheet.ts +++ b/src/compiler/compile/css/Stylesheet.ts @@ -291,14 +291,14 @@ export default class Stylesheet { nodes_with_css_class: Set = 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; diff --git a/src/compiler/compile/index.ts b/src/compiler/compile/index.ts index 12b161aeeb0e..24e18430e45d 100644 --- a/src/compiler/compile/index.ts +++ b/src/compiler/compile/index.ts @@ -26,7 +26,8 @@ const valid_options = [ 'css', 'loopGuardTimeout', 'preserveComments', - 'preserveWhitespace' + 'preserveWhitespace', + 'cssPrefix' ]; function validate_options(options: CompileOptions, warnings: Warning[]) { @@ -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 = []; diff --git a/src/compiler/interfaces.ts b/src/compiler/interfaces.ts index a5e286462ff3..2fb1ebcb0e37 100644 --- a/src/compiler/interfaces.ts +++ b/src/compiler/interfaces.ts @@ -126,6 +126,7 @@ export interface CompileOptions { preserveComments?: boolean; preserveWhitespace?: boolean; + cssPrefix?: string; } export interface ParserOptions { diff --git a/test/css/index.js b/test/css/index.js index dc17314597fa..4845018b3f9f 100644 --- a/test/css/index.js +++ b/test/css/index.js @@ -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) { diff --git a/test/css/samples/custom-css-prefix/_config.js b/test/css/samples/custom-css-prefix/_config.js new file mode 100644 index 000000000000..b85e3ae5a77e --- /dev/null +++ b/test/css/samples/custom-css-prefix/_config.js @@ -0,0 +1,5 @@ +export default { + compileOptions: { + cssPrefix: 'sv' + } +}; \ No newline at end of file diff --git a/test/css/samples/custom-css-prefix/expected.css b/test/css/samples/custom-css-prefix/expected.css new file mode 100644 index 000000000000..d466e8df9658 --- /dev/null +++ b/test/css/samples/custom-css-prefix/expected.css @@ -0,0 +1 @@ +div.svelte-xyz{color:red} \ No newline at end of file diff --git a/test/css/samples/custom-css-prefix/input.svelte b/test/css/samples/custom-css-prefix/input.svelte new file mode 100644 index 000000000000..c87862c09306 --- /dev/null +++ b/test/css/samples/custom-css-prefix/input.svelte @@ -0,0 +1,7 @@ +
red
+ + \ No newline at end of file