From f919ccf0c9b9d3d615269f54c33ff3f3086ef1d0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Feb 2019 18:10:31 -0500 Subject: [PATCH 1/3] fix tests for v3 --- index.js | 49 +++++++++++++------ package-lock.json | 8 ++-- package.json | 2 +- test/fixtures/css.html | 18 +++---- test/fixtures/es2015.html | 10 +--- test/fixtures/good.html | 16 ++----- test/fixtures/parent.html | 12 ++--- test/fixtures/parse-error.html | 3 +- test/fixtures/validation-error.html | 12 ++--- test/loader.spec.js | 74 ++++++----------------------- 10 files changed, 75 insertions(+), 129 deletions(-) diff --git a/index.js b/index.js index 4aa5768a..988c4d48 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,20 @@ const { compile, preprocess } = major_version >= 3 ? require('svelte/compiler') : require('svelte'); +const pluginOptions = { + externalDependencies: true, + hotReload: true, + hotOptions: true, + preprocess: true, + emitCss: true, + + // legacy + shared: true, + style: true, + script: true, + markup: true +}; + function makeHot(id, code, hotOptions) { const options = JSON.stringify(hotOptions); const replacement = ` @@ -99,22 +113,27 @@ module.exports = function(source, map) { const isServer = this.target === 'node' || (options.generate && options.generate == 'ssr'); const isProduction = this.minimize || process.env.NODE_ENV === 'production'; - options.filename = this.resourcePath; - if (!('format' in options)) { - options.format = major_version >= 3 ? 'esm' : 'es'; + const compileOptions = { + filename: this.resourcePath, + format: options.format || (major_version >= 3 ? 'esm' : 'es') + }; + + if (major_version >= 3) { + // TODO anything? + } else { + compileOptions.shared = options.shared || 'svelte/shared.js'; + compileOptions.name = capitalize(sanitize(options.filename)); } - if (!('shared' in options)) { - const shared = (major_version >= 3 ? 'svelte/internal.js' : 'svelte/shared.js'); - options.shared = (options.format === 'es' || options.format === 'esm') && - requireRelative.resolve(shared, process.cwd()); + + for (const option in options) { + if (!pluginOptions[option]) compileOptions[option] = options[option]; } - if (!('name' in options)) options.name = capitalize(sanitize(options.filename)); - if (!('onwarn' in options)) options.onwarn = warning => this.emitWarning(new Error(warning)); - if (options.emitCss) options.css = false; - if (options.externalDependencies) options.externalDependencies.forEach(dep => this.addDependency(dep)); + + // if (!('onwarn' in options)) options.onwarn = warning => this.emitWarning(new Error(warning)); + deprecatePreprocessOptions(options); - options.preprocess.filename = options.filename; + options.preprocess.filename = compileOptions.filename; preprocess(source, options.preprocess).then(processed => { if (processed.dependencies && this.addDependency) { @@ -123,16 +142,16 @@ module.exports = function(source, map) { } } - let { js, css } = normalize(compile(processed.toString(), options)); + let { js, css } = normalize(compile(processed.toString(), compileOptions)); if (options.hotReload && !isProduction && !isServer) { const hotOptions = Object.assign({}, options.hotOptions); - const id = JSON.stringify(relative(process.cwd(), options.filename)); + const id = JSON.stringify(relative(process.cwd(), compileOptions.filename)); js.code = makeHot(id, js.code, hotOptions); } if (options.emitCss && css.code) { - const cssFilepath = options.filename.replace( + const cssFilepath = compileOptions.filename.replace( /\.[^/.]+$/, `.svelte.css` ); diff --git a/package-lock.json b/package-lock.json index 34245a68..faa646c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "svelte-loader", - "version": "2.11.0", + "version": "2.13.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1249,9 +1249,9 @@ "dev": true }, "svelte": { - "version": "2.16.0", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-2.16.0.tgz", - "integrity": "sha512-1x1zCZpNnuwQdH+9tTeM0QYv33TVEFIxrrd88Mv80EcaomIlA9IvFLIThc8OFsCMHwrjllbIRNmsVfwlOxI7MA==", + "version": "3.0.0-beta.5", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.0.0-beta.5.tgz", + "integrity": "sha512-uPDvJ37NGCdIi/Nk2RvtYcrNuVOO34SA+T4ZDJMyb5JiRQ4xHXtbBpxl6ytzftHewfQqKGcEeB+T3LVLIwj6OQ==", "dev": true }, "svelte-dev-helper": { diff --git a/package.json b/package.json index cb4e4929..da1a8a75 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "mocha": "^5.2.0", "sinon": "^6.1.5", "sinon-chai": "^3.2.0", - "svelte": "^2.9.5" + "svelte": "^3.0.0-beta.5" }, "peerDependencies": { "svelte": ">1.44.0" diff --git a/test/fixtures/css.html b/test/fixtures/css.html index 51e72fa4..df3da44f 100644 --- a/test/fixtures/css.html +++ b/test/fixtures/css.html @@ -1,19 +1,13 @@

Count: {count}

- + \ No newline at end of file diff --git a/test/fixtures/es2015.html b/test/fixtures/es2015.html index a0335f9c..106e3230 100644 --- a/test/fixtures/es2015.html +++ b/test/fixtures/es2015.html @@ -1,13 +1,7 @@

{hi}

\ No newline at end of file diff --git a/test/fixtures/good.html b/test/fixtures/good.html index 69b8c091..d84b36cc 100644 --- a/test/fixtures/good.html +++ b/test/fixtures/good.html @@ -1,12 +1,6 @@ -

Count: {count}

- -​ \ No newline at end of file + let count = 1; + + +

Count: {count}

+ diff --git a/test/fixtures/parent.html b/test/fixtures/parent.html index 72f28e3d..9492f620 100644 --- a/test/fixtures/parent.html +++ b/test/fixtures/parent.html @@ -1,11 +1,5 @@ - - - export default { - components: { - Nested - } - }; - \ No newline at end of file + \ No newline at end of file diff --git a/test/fixtures/parse-error.html b/test/fixtures/parse-error.html index e3cfb287..695cc0c8 100644 --- a/test/fixtures/parse-error.html +++ b/test/fixtures/parse-error.html @@ -1,2 +1 @@ -

Count: {count}

{/if} - \ No newline at end of file +

Count: {count}

{/if} \ No newline at end of file diff --git a/test/fixtures/validation-error.html b/test/fixtures/validation-error.html index 16e1f4ab..d91ce13a 100644 --- a/test/fixtures/validation-error.html +++ b/test/fixtures/validation-error.html @@ -1,9 +1,5 @@ -

Count: {count}

- \ No newline at end of file + export default {}; + + +

Hello world

\ No newline at end of file diff --git a/test/loader.spec.js b/test/loader.spec.js index 8526ffcc..b86a0391 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -71,32 +71,7 @@ describe('loader', () => { expect(err.message).to.eql(d` ParseError: Unexpected block closing tag (1:23) 1:

Count: {count}

{/if} - ^ - 2: `); - - expect(code).not.to.exist; - expect(map).not.to.exist; - }) - ); - - it( - 'should handle wrong export', - testLoader('test/fixtures/export-error.html', function( - err, - code, - map, - context - ) { - expect(err).to.exist; - - expect(err.message).to.eql(d` - ParseError: Unexpected token (5:7) - 3: `); + ^`); expect(code).not.to.exist; expect(map).not.to.exist; @@ -113,14 +88,13 @@ describe('loader', () => { ) { expect(err).to.exist; - expect(err.message).to.eql(d` - ValidationError: Computed properties can be function expressions or arrow function expressions (6:11) - 4: export default { - 5: computed: { - 6: foo: 'BAR' - ^ - 7: } - 8: };`); + expect(err.message.trim()).to.eql(d` + ValidationError: A component cannot have a default export (2:1) + 1: + 4:`); expect(code).not.to.exist; expect(map).not.to.exist; @@ -138,8 +112,7 @@ describe('loader', () => { expect(map).to.exist; // es2015 statements remain - expect(code).to.contain(`import { hello } from './utils';`); - expect(code).to.contain('data() {'); + expect(code).to.contain(`import { hello } from "./utils";`); }) ); @@ -149,7 +122,7 @@ describe('loader', () => { expect(err).not.to.exist; // es2015 statements remain - expect(code).to.contain(`import Nested from './nested';`); + expect(code).to.contain(`import Nested from "./nested";`); expect(code).to.exist; expect(map).to.exist; @@ -180,33 +153,18 @@ describe('loader', () => { ); }); - describe('shared', () => { - it( - 'should configure shared=false (default)', - testLoader( - 'test/fixtures/good.html', - function(err, code, map) { - expect(err).not.to.exist; - - expect(code).not.to.contain('import {'); - expect(code).not.to.contain('svelte/shared.js'); - }, - { shared: false }, - 1 - ) - ); - + describe('sveltePath', () => { it( - 'should configure shared=true', + 'should configure sveltePath', testLoader( 'test/fixtures/good.html', function(err, code, map) { expect(err).not.to.exist; expect(code).to.contain('import {'); - expect(code).to.contain('svelte/shared.js'); + expect(code).to.contain('custom-svelte/internal'); }, - { shared: true } + { sveltePath: 'custom-svelte' } ) ); }); @@ -230,9 +188,7 @@ describe('loader', () => { function(err, code, map) { expect(err).not.to.exist; - expect(code).to.contain( - '.render = function(state, options = {}) {' - ); + expect(code).to.contain('create_ssr_component'); }, { generate: 'ssr' } ) From a18c7cd9d37e021459865598fcdaebb96e24a816 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Feb 2019 18:21:23 -0500 Subject: [PATCH 2/3] reinstate onwarn --- index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 988c4d48..76b4d973 100644 --- a/index.js +++ b/index.js @@ -76,7 +76,7 @@ function normalize(compiled) { ? compiled.css : { code: compiled.css, map: compiled.cssMap }; - return { js, css, ast: compiled.ast }; + return { js, css, ast: compiled.ast, warnings: compiled.warnings || compiled.stats.warnings || [] }; } const warned = {}; @@ -118,20 +118,20 @@ module.exports = function(source, map) { format: options.format || (major_version >= 3 ? 'esm' : 'es') }; + const handleWarning = warning => this.emitWarning(new Error(warning)); + if (major_version >= 3) { // TODO anything? } else { compileOptions.shared = options.shared || 'svelte/shared.js'; compileOptions.name = capitalize(sanitize(options.filename)); + compileOptions.onwarn = options.onwarn || handleWarning; } for (const option in options) { if (!pluginOptions[option]) compileOptions[option] = options[option]; } - // if (!('onwarn' in options)) options.onwarn = warning => this.emitWarning(new Error(warning)); - - deprecatePreprocessOptions(options); options.preprocess.filename = compileOptions.filename; @@ -142,7 +142,15 @@ module.exports = function(source, map) { } } - let { js, css } = normalize(compile(processed.toString(), compileOptions)); + let { js, css, warnings } = normalize(compile(processed.toString(), compileOptions)); + + if (major_version >= 3) { + warnings.forEach( + options.onwarn + ? warning => options.onwarn(warning, handleWarning) + : handleWarning + ); + } if (options.hotReload && !isProduction && !isServer) { const hotOptions = Object.assign({}, options.hotOptions); From a25ccf0846b2a775399ec9664163232abc55e571 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Tue, 19 Feb 2019 19:14:38 -0500 Subject: [PATCH 3/3] remove require-relative --- index.js | 1 - package-lock.json | 5 ----- package.json | 1 - 3 files changed, 7 deletions(-) diff --git a/index.js b/index.js index 76b4d973..767fafed 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ const { basename, extname, relative } = require('path'); const { getOptions } = require('loader-utils'); const VirtualModules = require('./lib/virtual'); -const requireRelative = require('require-relative'); const hotApi = require.resolve('./lib/hot-api.js'); diff --git a/package-lock.json b/package-lock.json index faa646c4..9ab8f4ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1063,11 +1063,6 @@ "integrity": "sha512-g2FAVtR8Uh8GO1Nv5wpxW7VFVwHcCEr4wyA8/MHiRkO8uHoR5ntAA8Uq3P1vvMTX/BeQiRVSpDGLd+Wn5HNOTA==", "dev": true }, - "require-relative": { - "version": "0.8.7", - "resolved": "https://registry.npmjs.org/require-relative/-/require-relative-0.8.7.tgz", - "integrity": "sha1-eZlTn8ngR6N5KPoZb44VY9q9Nt4=" - }, "require-uncached": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/require-uncached/-/require-uncached-1.0.3.tgz", diff --git a/package.json b/package.json index da1a8a75..f93df75d 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,6 @@ ], "dependencies": { "loader-utils": "^1.1.0", - "require-relative": "^0.8.7", "svelte-dev-helper": "^1.1.9" }, "devDependencies": {