From 60299f07ede8fc5df538dd4e25304d9f0c2327bf Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 19 Oct 2020 16:55:24 -0700 Subject: [PATCH 1/9] fix: drop `major_version < 3` branches --- index.js | 61 +++++++++++--------------------------------------------- 1 file changed, 12 insertions(+), 49 deletions(-) diff --git a/index.js b/index.js index 8e03c1c..f134e3b 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,12 @@ const path = require('path'); const { existsSync } = require('fs'); const relative = require('require-relative'); -const { version } = require('svelte/package.json'); const { createFilter } = require('rollup-pluginutils'); +const { compile, preprocess } = require('svelte/compiler'); const { encode, decode } = require('sourcemap-codec'); -const major_version = +version[0]; const pkg_export_errors = new Set(); -const { compile, preprocess } = major_version >= 3 - ? require('svelte/compiler.js') - : require('svelte'); - function sanitize(input) { return path .basename(input) @@ -23,7 +18,7 @@ function sanitize(input) { } function capitalize(str) { - return str[0].toUpperCase() + str.slice(1); + return str[0].toUpperCase() + str.substring(1); } const pluginOptions = { @@ -132,13 +127,8 @@ module.exports = function svelte(options = {}) { fixed_options[key] = options[key]; }); - if (major_version >= 3) { - fixed_options.format = 'esm'; - fixed_options.sveltePath = options.sveltePath || 'svelte'; - } else { - fixed_options.format = 'es'; - fixed_options.shared = require.resolve(options.shared || 'svelte/shared.js'); - } + fixed_options.format = 'esm'; + fixed_options.sveltePath = options.sveltePath || 'svelte'; // handle CSS extraction if ('css' in options) { @@ -223,53 +213,26 @@ module.exports = function svelte(options = {}) { const dependencies = []; let preprocessPromise; if (options.preprocess) { - if (major_version < 3) { - const preprocessOptions = {}; - for (const key in options.preprocess) { - preprocessOptions[key] = (...args) => { - return Promise.resolve(options.preprocess[key](...args)).then( - resp => { - if (resp && resp.dependencies) { - dependencies.push(...resp.dependencies); - } - return resp; - } - ); - }; + preprocessPromise = preprocess(code, options.preprocess, { filename }).then(processed => { + if (processed.dependencies) { + dependencies.push(...processed.dependencies); } - preprocessPromise = preprocess( - code, - Object.assign(preprocessOptions, { filename }) - ).then(code => code.toString()); - } else { - preprocessPromise = preprocess(code, options.preprocess, { filename }).then(processed => { - if (processed.dependencies) { - dependencies.push(...processed.dependencies); - } - return processed.toString(); - }); - } + return processed.toString(); + }); } else { preprocessPromise = Promise.resolve(code); } return preprocessPromise.then(code => { let warnings = []; - - const base_options = major_version < 3 - ? { - onwarn: warning => warnings.push(warning) - } - : {}; + const base_options = {}; const compiled = compile( code, - Object.assign(base_options, fixed_options, { filename }, major_version >= 3 ? null : { - name: capitalize(sanitize(id)) - }) + Object.assign(base_options, fixed_options, { filename }) ); - if (major_version >= 3) warnings = compiled.warnings || compiled.stats.warnings; + warnings = compiled.warnings || compiled.stats.warnings; warnings.forEach(warning => { if ((!options.css && !options.emitCss) && warning.code === 'css-unused-selector') return; From 5ce59ba0863ec34e8b65d2537ae77973bce54838 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 19 Oct 2020 16:56:36 -0700 Subject: [PATCH 2/9] fix: remove unused helpers --- index.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/index.js b/index.js index f134e3b..05232c7 100644 --- a/index.js +++ b/index.js @@ -7,20 +7,6 @@ const { encode, decode } = require('sourcemap-codec'); const pkg_export_errors = new Set(); -function sanitize(input) { - return path - .basename(input) - .replace(path.extname(input), '') - .replace(/[^a-zA-Z_$0-9]+/g, '_') - .replace(/^_/, '') - .replace(/_$/, '') - .replace(/^(\d)/, '_$1'); -} - -function capitalize(str) { - return str[0].toUpperCase() + str.substring(1); -} - const pluginOptions = { include: true, exclude: true, From 90c2daeed6e1e55dc5ec3a284a976ac5d6f0d9af Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 19 Oct 2020 16:57:39 -0700 Subject: [PATCH 3/9] fix: drop `.html` default extension --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 05232c7..2c89f21 100644 --- a/index.js +++ b/index.js @@ -103,7 +103,7 @@ class CssWriter { module.exports = function svelte(options = {}) { const filter = createFilter(options.include, options.exclude); - const extensions = options.extensions || ['.html', '.svelte']; + const extensions = options.extensions || ['.svelte']; const fixed_options = {}; From 94c3e4f0793095704d6c9e42322ceab403b9a547 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 19 Oct 2020 21:10:19 -0700 Subject: [PATCH 4/9] break: remove outdated code branches --- index.js | 295 +++++++++++++++++++++----------------------------- test/index.js | 2 +- 2 files changed, 123 insertions(+), 174 deletions(-) diff --git a/index.js b/index.js index 2c89f21..a131a35 100644 --- a/index.js +++ b/index.js @@ -7,43 +7,24 @@ const { encode, decode } = require('sourcemap-codec'); const pkg_export_errors = new Set(); -const pluginOptions = { - include: true, - exclude: true, - extensions: true, - emitCss: true, - preprocess: true, - - // legacy — we might want to remove/change these in a future version - onwarn: true, - shared: true -}; - -function tryRequire(id) { - try { - return require(id); - } catch (err) { - return null; - } -} - -function tryResolve(pkg, importer) { - try { - return relative.resolve(pkg, importer); - } catch (err) { - if (err.code === 'MODULE_NOT_FOUND') return null; - if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') { - pkg_export_errors.add(pkg.replace(/\/package.json$/, '')); - return null; - } - throw err; +const plugin_options = new Set([ + 'include', 'exclude', 'extensions', + 'emitCss', 'preprocess', + // legacy — remove in future? + 'onwarn', 'shared' +]); + +function to_entry_css(bundle) { + for (let file in bundle) { + let { name } = path.parse(file); + return name + '.css'; } } class CssWriter { - constructor(context, bundle, isDev, code, filename, map) { + constructor(context, bundle, isDev, code, map) { this.code = code; - this.filename = filename; + this.filename = to_entry_css(bundle); this.map = map && { version: 3, @@ -100,212 +81,180 @@ class CssWriter { } } -module.exports = function svelte(options = {}) { +/** @returns {import('rollup').Plugin} */ +module.exports = function (options = {}) { const filter = createFilter(options.include, options.exclude); + const extensions = options.extensions || ['.html', '.svelte']; - const extensions = options.extensions || ['.svelte']; - - const fixed_options = {}; + /** @type {import('svelte/types/compiler/interfaces').ModuleFormat} */ + const format = 'esm', config = { format }; - Object.keys(options).forEach(key => { - // add all options except include, exclude, extensions, and shared - if (pluginOptions[key]) return; - fixed_options[key] = options[key]; - }); - - fixed_options.format = 'esm'; - fixed_options.sveltePath = options.sveltePath || 'svelte'; - - // handle CSS extraction - if ('css' in options) { - if (typeof options.css !== 'function' && typeof options.css !== 'boolean') { - throw new Error('options.css must be a boolean or a function'); - } + for (let key in options) { + // forward `svelte/compiler` options + if (plugin_options.has(key)) continue; + config[key] = config[key] || options[key]; } - let css = options.css && typeof options.css === 'function' - ? options.css - : null; - - // A map from css filename to css contents - // If css: true we output all contents - // If emitCss: true we virtually resolve these imports - const cssLookup = new Map(); + const css_cache = new Map(); // [filename]:[chunk] + const { css, emitCss, onwarn } = options; - if (css || options.emitCss) { - fixed_options.css = false; + const ctype = typeof css; + const toWrite = ctype === 'function' && css; + if (css != null && !toWrite && ctype !== 'boolean') { + throw new Error('options.css must be a boolean or a function'); } + // block svelte's inline CSS if writer + const external_css = !!(toWrite || emitCss); + if (external_css) config.css = false; + return { name: 'svelte', /** - * Returns CSS contents for an id - */ - load(id) { - if (!cssLookup.has(id)) return null; - return cssLookup.get(id); - }, - - /** - * Returns id for import + * Resolve an import's full filepath. */ resolveId(importee, importer) { - if (cssLookup.has(importee)) { return importee; } - if (!importer || importee[0] === '.' || importee[0] === '\0' || path.isAbsolute(importee)) - return null; + if (css_cache.has(importee)) return importee; + if (!importer || importee[0] === '.' || importee[0] === '\0' || path.isAbsolute(importee)) return null; // if this is a bare import, see if there's a valid pkg.svelte const parts = importee.split('/'); - let name = parts.shift(); - if (name[0] === '@') name += `/${parts.shift()}`; - const resolved = tryResolve( - `${name}/package.json`, - path.dirname(importer) - ); - if (!resolved) return null; - const pkg = tryRequire(resolved); - if (!pkg) return null; + let dir, pkg, name = parts.shift(); + if (name[0] === '@') { + name += `/${parts.shift()}`; + } - const dir = path.dirname(resolved); + try { + const file = `${name}/package.json`; + const resolved = relative.resolve(file, path.dirname(importer)); + dir = path.dirname(resolved); + pkg = require(resolved); + } catch (err) { + if (err.code === 'MODULE_NOT_FOUND') return null; + if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') { + pkg_export_errors.add(name); + return null; + } + throw err; + } if (parts.length === 0) { // use pkg.svelte if (pkg.svelte) { return path.resolve(dir, pkg.svelte); } - } else { - if (pkg['svelte.root']) { - // TODO remove this. it's weird and unnecessary - const sub = path.resolve(dir, pkg['svelte.root'], parts.join('/')); - if (existsSync(sub)) return sub; - } + } else if (pkg['svelte.root']) { + // TODO remove this. it's weird and unnecessary + const sub = path.resolve(dir, pkg['svelte.root'], parts.join('/')); + if (existsSync(sub)) return sub; } }, /** - * Transforms a .svelte file into a .js file - * Adds a static import for virtual css file when emitCss: true + * Returns CSS contents for a file, if ours + */ + load(id) { + return css_cache.get(id) || null; + }, + + /** + * Transforms a `.svelte` file into a `.js` file. + * NOTE: If `emitCss: true`, appends a static `import` for virtual CSS file. */ - transform(code, id) { + async transform(code, id) { if (!filter(id)) return null; const extension = path.extname(id); if (!~extensions.indexOf(extension)) return null; + const dependencies = []; const filename = path.relative(process.cwd(), id); - const dependencies = []; - let preprocessPromise; if (options.preprocess) { - preprocessPromise = preprocess(code, options.preprocess, { filename }).then(processed => { - if (processed.dependencies) { - dependencies.push(...processed.dependencies); - } - return processed.toString(); - }); - } else { - preprocessPromise = Promise.resolve(code); + const processed = await preprocess(code, options.preprocess, { filename }); + if (processed.dependencies) dependencies.push(...processed.dependencies); + code = processed.code; } - return preprocessPromise.then(code => { - let warnings = []; - const base_options = {}; - - const compiled = compile( - code, - Object.assign(base_options, fixed_options, { filename }) - ); - - warnings = compiled.warnings || compiled.stats.warnings; - - warnings.forEach(warning => { - if ((!options.css && !options.emitCss) && warning.code === 'css-unused-selector') return; + const compiled = compile(code, { ...config, filename }); - if (options.onwarn) { - options.onwarn(warning, warning => this.warn(warning)); - } else { - this.warn(warning); - } - }); - - if ((css || options.emitCss) && compiled.css.code) { - let fname = id.replace(new RegExp(`\\${extension}$`), '.css'); + (compiled.warnings || []).forEach(warning => { + if (!css && !emitCss && warning.code === 'css-unused-selector') return; + if (onwarn) onwarn(warning, this.warn); + else this.warn(warning); + }); - if (options.emitCss) { - const source_map_comment = `/*# sourceMappingURL=${compiled.css.map.toUrl()} */`; - compiled.css.code += `\n${source_map_comment}`; + if (external_css && compiled.css.code) { + const fname = id.replace(new RegExp(`\\${extension}$`), '.css'); - compiled.js.code += `\nimport ${JSON.stringify(fname)};\n`; - } - - cssLookup.set(fname, compiled.css); + if (emitCss) { + compiled.js.code += `\nimport ${JSON.stringify(fname)};\n`; + compiled.css.code += `\n/*# sourceMappingURL=${compiled.css.map.toUrl()} */`; } - if (this.addWatchFile) { - dependencies.forEach(dependency => this.addWatchFile(dependency)); - } else { - compiled.js.dependencies = dependencies; - } + css_cache.set(fname, compiled.css); + } - return compiled.js; - }); + if (this.addWatchFile) { + dependencies.forEach(this.addWatchFile); + } else { + compiled.js.dependencies = dependencies; + } + + return compiled.js; }, /** - * If css: true then outputs a single file with all CSS bundled together + * Write to CSS file if given `options.css` function. + * TODO: is there a better way to concat/append into Rollup asset? */ generateBundle(config, bundle) { - if (css) { - // TODO would be nice if there was a more idiomatic way to do this in Rollup - let result = ''; + if (pkg_export_errors.size > 0) { + console.warn('\nrollup-plugin-svelte: The following packages did not export their `package.json` file so we could not check the `svelte` field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.\n'); + console.warn(Array.from(pkg_export_errors, s => `- ${s}`).join('\n') + '\n'); + } - const mappings = []; - const sourcesContent = []; - const sources = []; + if (!toWrite) return; - const chunks = Array.from(cssLookup.keys()).sort().map(key => cssLookup.get(key)); + let result = ''; + const sources = []; + const sourcesContent = []; + const mappings = []; - for (let chunk of chunks) { - if (!chunk.code) continue; - result += chunk.code + '\n'; + [...css_cache.keys()].sort().forEach(file => { + const chunk = css_cache.get(file); + if (!chunk.code) return; + result += chunk.code + '\n'; - if (config.sourcemap && chunk.map) { - const len = sources.length; - config.sourcemapExcludeSources || sources.push(chunk.map.sources[0]); - config.sourcemapExcludeSources || sourcesContent.push(chunk.map.sourcesContent[0]); + if (config.sourcemap && chunk.map) { + const len = sources.length; + config.sourcemapExcludeSources || sources.push(chunk.map.sources[0]); + config.sourcemapExcludeSources || sourcesContent.push(chunk.map.sourcesContent[0]); - const decoded = decode(chunk.map.mappings); + const decoded = decode(chunk.map.mappings); - if (len > 0) { - decoded.forEach(line => { - line.forEach(segment => { - segment[1] = len; - }); + if (len > 0) { + decoded.forEach(line => { + line.forEach(segment => { + segment[1] = len; }); - } - - mappings.push(...decoded); + }); } - } - const filename = Object.keys(bundle)[0].split('.').shift() + '.css'; + mappings.push(...decoded); + } + }); - const writer = new CssWriter(this, bundle, !!options.dev, result, filename, config.sourcemap && { + toWrite( + new CssWriter(this, bundle, !!options.dev, result, config.sourcemap && { sources, sourcesContent, mappings: encode(mappings) - }); - - css(writer); - } - - if (pkg_export_errors.size < 1) return; - - console.warn('\nrollup-plugin-svelte: The following packages did not export their `package.json` file so we could not check the `svelte` field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.\n'); - console.warn(Array.from(pkg_export_errors).map(s => `- ${s}`).join('\n') + '\n'); + }) + ); } }; }; diff --git a/test/index.js b/test/index.js index 90ba29e..ca9a671 100644 --- a/test/index.js +++ b/test/index.js @@ -70,7 +70,7 @@ test('supports component name assignment', async () => { test('creates a {code, map, dependencies} object, excluding the AST etc', async () => { const { transform } = plugin(); - const compiled = await transform('', 'test.html') + const compiled = await transform('', 'test.html'); assert.equal(Object.keys(compiled), ['code', 'map', 'dependencies']); }); From 1fbae1cc076d7cee6f27f6b8940a3e7fc0edd20f Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 20 Oct 2020 08:03:47 -0700 Subject: [PATCH 5/9] fix: remove `shared` option --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 5709b21..6cfc647 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,7 @@ const pkg_export_errors = new Set(); const plugin_options = new Set([ 'include', 'exclude', 'extensions', - 'emitCss', 'preprocess', - // legacy — remove in future? - 'onwarn', 'shared' + 'emitCss', 'preprocess', 'onwarn', ]); function to_entry_css(bundle) { From dd8ae555473588a959e02d92cd3f2b0b24fd2021 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 20 Oct 2020 08:06:06 -0700 Subject: [PATCH 6/9] break: remove `svelte.root` resolution --- index.js | 13 +++---------- test/index.js | 8 -------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 6cfc647..7175ca2 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ const path = require('path'); -const { existsSync } = require('fs'); const relative = require('require-relative'); const { createFilter } = require('rollup-pluginutils'); const { compile, preprocess } = require('svelte/compiler'); @@ -138,15 +137,9 @@ module.exports = function (options = {}) { throw err; } - if (parts.length === 0) { - // use pkg.svelte - if (pkg.svelte) { - return path.resolve(dir, pkg.svelte); - } - } else if (pkg['svelte.root']) { - // TODO remove this. it's weird and unnecessary - const sub = path.resolve(dir, pkg['svelte.root'], parts.join('/')); - if (existsSync(sub)) return sub; + // use pkg.svelte + if (parts.length === 0 && pkg.svelte) { + return path.resolve(dir, pkg.svelte); } }, diff --git a/test/index.js b/test/index.js index 586d2b9..d6eaaa6 100644 --- a/test/index.js +++ b/test/index.js @@ -20,14 +20,6 @@ test('resolves using pkg.svelte', () => { ); }); -test('resolves using pkg.svelte.root', () => { - const { resolveId } = plugin(); - assert.is( - resolveId('widgets/Foo.html', path.resolve('test/foo/main.js')), - path.resolve('test/node_modules/widgets/src/Foo.html') - ); -}); - test('ignores built-in modules', () => { const { resolveId } = plugin(); assert.ok( From a50307bdcb52380c119c66ad23ed1960bc1dbbce Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 20 Oct 2020 08:18:30 -0700 Subject: [PATCH 7/9] fix(action): set `timeout-minutes` value --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e5fc9ec..6497489 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,7 @@ jobs: test: name: Node.js v${{ matrix.nodejs }} (${{ matrix.os }}) runs-on: ${{ matrix.os }} + timeout-minutes: 3 strategy: matrix: nodejs: [10, 12, 14] From f87be655b677eaa54505083ed0cb9b4f2f1e9164 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 20 Oct 2020 08:35:14 -0700 Subject: [PATCH 8/9] break: remove `.html` default extension --- README.md | 2 +- index.d.ts | 4 +- index.js | 2 +- test/index.js | 89 ++++++++++++++++--- test/node_modules/widget/index.js | 5 ++ test/node_modules/widget/package.json | 4 +- .../src/{Bar.html => Bar.svelte} | 2 +- .../src/{Foo.html => Foo.svelte} | 4 +- test/sourcemap-test/src/main.js | 4 +- 9 files changed, 92 insertions(+), 24 deletions(-) rename test/sourcemap-test/src/{Bar.html => Bar.svelte} (86%) rename test/sourcemap-test/src/{Foo.html => Foo.svelte} (65%) diff --git a/README.md b/README.md index 2411336..4367c85 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ export default { }, plugins: [ svelte({ - // By default, all .svelte and .html files are compiled + // By default, all ".svelte" files are compiled extensions: ['.my-custom-extension'], // You can restrict which files are compiled diff --git a/index.d.ts b/index.d.ts index 973a784..25f9e05 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,8 +19,8 @@ type CssEmitter = (css: CssWriter) => any; interface Options extends CompileOptions { /** - * By default, all .svelte and .html files are compiled - * @default ['.html', '.svelte'] + * By default, all ".svelte" files are compiled + * @default ['.svelte'] */ extensions?: string[]; diff --git a/index.js b/index.js index 7175ca2..fad4c49 100644 --- a/index.js +++ b/index.js @@ -80,8 +80,8 @@ class CssWriter { /** @returns {import('rollup').Plugin} */ module.exports = function (options = {}) { + const extensions = options.extensions || ['.svelte']; const filter = createFilter(options.include, options.exclude); - const extensions = options.extensions || ['.html', '.svelte']; /** @type {import('svelte/types/compiler/interfaces').ModuleFormat} */ const format = 'esm', config = { format }; diff --git a/test/index.js b/test/index.js index d6eaaa6..ca47b22 100644 --- a/test/index.js +++ b/test/index.js @@ -16,7 +16,7 @@ test('resolves using pkg.svelte', () => { const { resolveId } = plugin(); assert.is( resolveId('widget', path.resolve('test/foo/main.js')), - path.resolve('test/node_modules/widget/src/Widget.html') + path.resolve('test/node_modules/widget/src/Widget.svelte') ); }); @@ -62,7 +62,7 @@ test('supports component name assignment', async () => { test('creates a {code, map, dependencies} object, excluding the AST etc', async () => { const { transform } = plugin(); - const compiled = await transform('', 'test.html'); + const compiled = await transform('', 'test.svelte'); assert.equal(Object.keys(compiled), ['code', 'map', 'dependencies']); }); @@ -141,7 +141,7 @@ test('can generate a CSS sourcemap – a la Rollup config', async () => { name: originalFooLoc.name }, { - source: 'Foo.html', + source: 'Foo.svelte', line: 5, column: 1, name: null @@ -162,7 +162,7 @@ test('can generate a CSS sourcemap – a la Rollup config', async () => { name: originalBarLoc.name }, { - source: 'Bar.html', + source: 'Bar.svelte', line: 4, column: 1, name: null @@ -201,7 +201,7 @@ test('respects `sourcemapExcludeSources` Rollup option', async () => { assert.ok(css.map); assert.is(css.map.sources.length, 2); assert.is(css.map.sourcesContent, null); - assert.equal(css.map.sources, ['Bar.html', 'Foo.html']); + assert.equal(css.map.sources, ['Bar.svelte', 'Foo.svelte']); }); test('produces readable sourcemap output when `dev` is truthy', async () => { @@ -252,7 +252,7 @@ test('squelches CSS warnings if css: false', () => { color: red; } - `, 'test.html'); + `, 'test.svelte'); }); test('preprocesses components', async () => { @@ -274,10 +274,10 @@ test('preprocesses components', async () => {

Hello __REPLACEME__!

file: __FILENAME__

- `, 'test.html'); + `, 'test.svelte'); assert.is(code.indexOf('__REPLACEME__'), -1, 'content not modified'); - assert.is.not(code.indexOf('file: test.html'), -1, 'filename not replaced'); + assert.is.not(code.indexOf('file: test.svelte'), -1, 'filename not replaced'); assert.equal(dependencies, ['foo']); }); @@ -292,7 +292,7 @@ test('emits a CSS file', async () => { h1 { color: red; } - `, `path/to/Input.html`); + `, `path/to/Input.svelte`); assert.ok(transformed.code.indexOf(`import "path/to/Input.css";`) !== -1); @@ -305,7 +305,7 @@ test('emits a CSS file', async () => { column: 0 }); - assert.is(loc.source, 'Input.html'); + assert.is(loc.source, 'Input.svelte'); assert.is(loc.line, 4); assert.is(loc.column, 2); }); @@ -321,7 +321,7 @@ test('properly escapes CSS paths', async () => { h1 { color: red; } - `, `path\\t'o\\Input.html`); + `, `path\\t'o\\Input.svelte`); assert.ok(transformed.code.indexOf(`import "path\\\\t'o\\\\Input.css";`) !== -1); @@ -334,7 +334,7 @@ test('properly escapes CSS paths', async () => { column: 0 }); - assert.is(loc.source, 'Input.html'); + assert.is(loc.source, 'Input.svelte'); assert.is(loc.line, 4); assert.is(loc.column, 2); }); @@ -360,7 +360,7 @@ test('intercepts warnings', async () => { }, `

Hello world!

wheee!!! - `, 'test.html'); + `, 'test.svelte'); assert.equal(warnings.map(w => w.code), ['a11y-hidden', 'a11y-distracting-elements']); assert.equal(handled.map(w => w.code), ['a11y-hidden']); @@ -535,4 +535,67 @@ test('handles filenames that happen to contain .svelte', async () => { ); }); +test('ignores ".html" extension by default', async () => { + sander.rimrafSync('test/node_modules/widget/dist'); + sander.mkdirSync('test/node_modules/widget/dist'); + + try { + const bundle = await rollup({ + input: 'test/node_modules/widget/index.js', + external: ['svelte/internal'], + plugins: [ + plugin({ + css: false + }) + ] + }); + + await bundle.write({ + format: 'iife', + file: 'test/node_modules/widget/dist/bundle.js', + globals: { 'svelte/internal': 'svelte' }, + assetFileNames: '[name].[ext]', + sourcemap: true, + }); + + assert.unreachable('should have thrown PARSE_ERROR'); + } catch (err) { + assert.is(err.code, 'PARSE_ERROR'); + assert.match(err.message, 'Note that you need plugins to import files that are not JavaScript'); + assert.match(err.loc.file, 'test/node_modules/widget/src/Widget.html'); + } +}); + +test('allows ".html" extension if configured', async () => { + sander.rimrafSync('test/node_modules/widget/dist'); + sander.mkdirSync('test/node_modules/widget/dist'); + + try { + const bundle = await rollup({ + input: 'test/node_modules/widget/index.js', + external: ['svelte/internal'], + plugins: [ + plugin({ + extensions: ['.html'], + css: false + }) + ] + }); + + await bundle.write({ + format: 'iife', + file: 'test/node_modules/widget/dist/bundle.js', + globals: { 'svelte/internal': 'svelte' }, + assetFileNames: '[name].[ext]', + sourcemap: true, + }); + } catch (err) { + console.log(err); + throw err; + } + + assert.ok(fs.existsSync('test/node_modules/widget/dist/bundle.js')); + assert.ok(fs.existsSync('test/node_modules/widget/dist/bundle.js.map')); +}); + test.run(); diff --git a/test/node_modules/widget/index.js b/test/node_modules/widget/index.js index e69de29..015904d 100644 --- a/test/node_modules/widget/index.js +++ b/test/node_modules/widget/index.js @@ -0,0 +1,5 @@ +import Widget from './src/Widget.html'; + +new Widget({ + target: document.body +}); diff --git a/test/node_modules/widget/package.json b/test/node_modules/widget/package.json index 7b86f24..174d949 100644 --- a/test/node_modules/widget/package.json +++ b/test/node_modules/widget/package.json @@ -1,4 +1,4 @@ { "main": "./index.js", - "svelte": "src/Widget.html" -} \ No newline at end of file + "svelte": "src/Widget.svelte" +} diff --git a/test/sourcemap-test/src/Bar.html b/test/sourcemap-test/src/Bar.svelte similarity index 86% rename from test/sourcemap-test/src/Bar.html rename to test/sourcemap-test/src/Bar.svelte index 9fbfc53..015c490 100644 --- a/test/sourcemap-test/src/Bar.html +++ b/test/sourcemap-test/src/Bar.svelte @@ -4,4 +4,4 @@ .bar { color: blue; } - \ No newline at end of file + diff --git a/test/sourcemap-test/src/Foo.html b/test/sourcemap-test/src/Foo.svelte similarity index 65% rename from test/sourcemap-test/src/Foo.html rename to test/sourcemap-test/src/Foo.svelte index 4a2dbc8..7dee3bf 100644 --- a/test/sourcemap-test/src/Foo.html +++ b/test/sourcemap-test/src/Foo.svelte @@ -8,5 +8,5 @@ \ No newline at end of file + import Bar from './Bar.svelte'; + diff --git a/test/sourcemap-test/src/main.js b/test/sourcemap-test/src/main.js index 39610f9..7faa337 100644 --- a/test/sourcemap-test/src/main.js +++ b/test/sourcemap-test/src/main.js @@ -1,5 +1,5 @@ -import Foo from './Foo.html'; +import Foo from './Foo.svelte'; new Foo({ target: document.body -}); \ No newline at end of file +}); From 823594aa3b65f056d9570ba70c1b6c352b0a9160 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Tue, 20 Oct 2020 08:39:21 -0700 Subject: [PATCH 9/9] ok windows --- test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.js b/test/index.js index ca47b22..9448b39 100644 --- a/test/index.js +++ b/test/index.js @@ -562,7 +562,7 @@ test('ignores ".html" extension by default', async () => { } catch (err) { assert.is(err.code, 'PARSE_ERROR'); assert.match(err.message, 'Note that you need plugins to import files that are not JavaScript'); - assert.match(err.loc.file, 'test/node_modules/widget/src/Widget.html'); + assert.match(err.loc.file, /widget[\\\/]+src[\\\/]+Widget.html$/); } });