From c37b7cfd5f9b1e0ac9698eeff0bfc8f6ffc81cf7 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Fri, 6 Apr 2018 10:40:09 -0400 Subject: [PATCH] deprecate options.style etc - fixes #41 --- index.js | 22 +++++++++++++++++++++- test/loader.spec.js | 44 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 9742a463..53c51517 100644 --- a/index.js +++ b/index.js @@ -63,6 +63,23 @@ function normalize(compiled) { return { js, css, ast: compiled.ast }; } +const warned = {}; +function deprecatePreprocessOptions(options) { + const preprocessOptions = {}; + + ['markup', 'style', 'script'].forEach(kind => { + if (options[kind]) { + if (!warned[kind]) { + console.warn(`[svelte-loader] DEPRECATION: options.${kind} is now options.preprocess.${kind}`); + warned[kind] = true; + } + preprocessOptions[kind] = options[kind]; + } + }); + + options.preprocess = options.preprocess || preprocessOptions; +} + module.exports = function(source, map) { this.cacheable(); @@ -86,7 +103,10 @@ module.exports = function(source, map) { if (!options.onwarn) options.onwarn = warning => this.emitWarning(new Error(warning)); - preprocess(source, options).then(processed => { + deprecatePreprocessOptions(options); + options.preprocess.filename = options.filename; + + preprocess(source, options.preprocess).then(processed => { let { js, css, ast } = normalize(compile(processed.toString(), options)); if (options.emitCss && css.code) { diff --git a/test/loader.spec.js b/test/loader.spec.js index d795fd1d..a3ccb244 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -294,10 +294,12 @@ describe('loader', () => { }); const callbackSpy = spy(cb); const options = { - style: ({ content }) => { - return { - code: content.replace(/\$size/gi, '50px'), - }; + preprocess: { + style: ({ content }) => { + return { + code: content.replace(/\$size/gi, '50px'), + }; + } }, }; @@ -321,9 +323,11 @@ describe('loader', () => { const cacheableSpy = spy(() => { }); const options = { - style: () => { - throw new Error('Error while preprocessing'); - }, + preprocess: { + style: () => { + throw new Error('Error while preprocessing'); + } + } }; loader.call( @@ -342,6 +346,32 @@ describe('loader', () => { }); }); + + describe('deprecations', () => { + it('should warn on options.style', done => { + const { warn } = console; + const warnings = []; + + console.warn = (msg) => { + warnings.push(msg); + }; + + testLoader('test/fixtures/style-valid.html', (err, code, map) => { + expect(code).to.contain('50px'); + expect(warnings).to.deep.equal([ + '[svelte-loader] DEPRECATION: options.style is now options.preprocess.style' + ]); + console.warn = warn; + }, { + style: ({ content }) => { + return { + code: content.replace(/\$size/gi, '50px'), + }; + } + })(done); + }); + }); + describe('hotReload', () => { it( 'should configure hotReload=false (default)',