From 897874d984690a940b8452456a803f4702026d36 Mon Sep 17 00:00:00 2001 From: glromeo Date: Tue, 20 Jun 2023 23:26:27 +0100 Subject: [PATCH] fixes & formatting --- src/index.ts | 2 +- src/plugin.ts | 12 ++++++------ src/render.ts | 6 +++--- src/utils.ts | 4 ++-- test/bugfixes.test.ts | 18 +++++++++--------- test/e2e.test.ts | 6 ++---- test/test-toolkit.ts | 3 ++- test/unit.test.ts | 37 +++++++++++++++++++------------------ 8 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/index.ts b/src/index.ts index 164a236..16e66a8 100755 --- a/src/index.ts +++ b/src/index.ts @@ -75,7 +75,7 @@ export type SassPluginOptions = StringOptions<'sync'> & { /** * */ - prefer?: "sass" | "style" | "main" + prefer?: 'sass' | 'style' | 'main' } export default sassPlugin diff --git a/src/plugin.ts b/src/plugin.ts index a60ed06..93517f2 100755 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -51,17 +51,17 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin { const transform = options.transform ? options.transform.bind(options) : null - const cssChunks:Record = {} + const cssChunks: Record = {} if (transform) { - const namespace = 'esbuild-sass-plugin'; + const namespace = 'esbuild-sass-plugin' - onResolve({filter: /^css-chunk:/}, ({path})=>({ + onResolve({filter: /^css-chunk:/}, ({path}) => ({ path, namespace })) - onLoad({filter: /./, namespace}, ({path})=>({ + onLoad({filter: /./, namespace}, ({path}) => ({ contents: cssChunks[path], loader: 'css' })) @@ -92,11 +92,11 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin { } } let {contents, pluginData} = out - if (type === "css") { + if (type === 'css') { let name = posixRelative(path) cssChunks[name] = contents contents = `import '${name}';` - } else if (type === "style") { + } else if (type === 'style') { contents = makeModule(String(contents), 'style', nonce) } else { return { diff --git a/src/render.ts b/src/render.ts index a9eb9ef..ec921d4 100644 --- a/src/render.ts +++ b/src/render.ts @@ -86,7 +86,7 @@ export function createRenderer(options: SassPluginOptions = {}, sourcemap: boole const logger = options.logger ?? { warn: function (message, opts) { if (!opts.span) { - warnings.push({ text: `sass warning: ${message}` }) + warnings.push({text: `sass warning: ${message}`}) } else { const filename = opts.span.url?.pathname ?? path const esbuildMsg = { @@ -95,11 +95,11 @@ export function createRenderer(options: SassPluginOptions = {}, sourcemap: boole file: filename, line: opts.span.start.line, column: opts.span.start.column, - lineText: opts.span.text, + lineText: opts.span.text }, detail: { deprecation: opts.deprecation, - stack: opts.stack, + stack: opts.stack } } diff --git a/src/utils.ts b/src/utils.ts index 97b5808..d8ef9b5 100755 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,7 +9,7 @@ import {SyncOpts} from 'resolve' const cwd = process.cwd() -export const posixRelative = require("path").sep === '/' +export const posixRelative = require('path').sep === '/' ? (path: string) => `css-chunk:${relative(cwd, path)}` : (path: string) => `css-chunk:${relative(cwd, path).replace(/\\/g, '/')}` @@ -167,7 +167,7 @@ export function postcssModules(options: PostcssModulesParams, plugins: AcceptedP return { contents: css, - pluginData: { exports: cssModule }, + pluginData: {exports: cssModule}, loader: 'js' } } diff --git a/test/bugfixes.test.ts b/test/bugfixes.test.ts index af6137b..eaa6920 100644 --- a/test/bugfixes.test.ts +++ b/test/bugfixes.test.ts @@ -33,7 +33,7 @@ describe('tests covering github issues', function () { it('#20 Plugin stops working after a SASS failure', async function () { const options = useFixture('../issues/20') - this.timeout(10000); + this.timeout(10000) writeTextFile('dep.scss', `$primary-color: #333; body { padding: 0; color: $primary-color; }`) writeTextFile('tmp.scss', `@use 'dep'; body {background-color: dep.$primary-color }`) @@ -45,9 +45,9 @@ describe('tests covering github issues', function () { entryPoints: ['./tmp.scss'], outfile: './tmp.css', plugins: [sassPlugin(), { - name: "listener", + name: 'listener', setup({onEnd}) { - onEnd(({errors, warnings})=>{ + onEnd(({errors, warnings}) => { const [failure] = errors switch (step) { case 0: @@ -56,7 +56,7 @@ describe('tests covering github issues', function () { step++ return case 1: - expect(failure.pluginName).to.eq("sass-plugin") + expect(failure.pluginName).to.eq('sass-plugin') writeTextFile('dep.scss', `$primary-color: #333; body { padding: 0; color: $primary-color; }`) step++ return @@ -66,16 +66,16 @@ describe('tests covering github issues', function () { step++ return case 3: - expect(failure.pluginName).to.eq("sass-plugin") + expect(failure.pluginName).to.eq('sass-plugin') writeTextFile('tmp.scss', `@use 'dep'; body {background-color: dep.$primary-color; color: red }`) step++ return case 4: expect(failure).to.be.undefined expect(warnings.length).to.equal(0) - setTimeout(()=>{ + setTimeout(() => { ctx!.dispose() - },100) + }, 100) step++ return } @@ -91,7 +91,7 @@ describe('tests covering github issues', function () { await new Promise((resolve, reject) => { writeTextFile('tmp.scss', `@use 'dep'; body {background-color: dep.$primary-color; color: red }`) const interval = setInterval(() => { - console.log("interval", step) + console.log('interval', step) if (step === 5) { clearInterval(interval) try { @@ -301,7 +301,7 @@ describe('tests covering github issues', function () { }) let map = readJsonFile('./dist/index.css.map') - map.sourcesContent[0] = map.sourcesContent[0].replace(/\r\n/g, "\n") + map.sourcesContent[0] = map.sourcesContent[0].replace(/\r\n/g, '\n') expect(map).to.eql({ 'version': 3, diff --git a/test/e2e.test.ts b/test/e2e.test.ts index 3af928d..cd6d65e 100644 --- a/test/e2e.test.ts +++ b/test/e2e.test.ts @@ -51,9 +51,9 @@ describe('e2e tests', function () { prefer: 'sass', precompile(source, path, isRoot) { if (path.endsWith('_functions.scss')) { - return source.replace('opacity($foreground) * 100)', 'opacity($foreground) * 100%)'); + return source.replace('opacity($foreground) * 100)', 'opacity($foreground) * 100%)') } else { - return source; + return source } } }) @@ -190,8 +190,6 @@ describe('e2e tests', function () { ' padding: 20px;\n' + '}`;\n') - expect(bundle).to.have.string(`__publicField(HelloWorld, "styles", hello_world_default);`) - expect(bundle).to.have.string( `document.head.appendChild(document.createElement("style")).appendChild(document.createTextNode(css));` ) diff --git a/test/test-toolkit.ts b/test/test-toolkit.ts index 84574b9..edab5f6 100644 --- a/test/test-toolkit.ts +++ b/test/test-toolkit.ts @@ -19,7 +19,8 @@ export function useFixture(name: string): BuildOptions { } catch (ignored) { } return { - absWorkingDir // esbuild cwd is initialized when imported, we have to change it at each test case! + absWorkingDir, // esbuild cwd is initialized when imported, we have to change it at each test case! + target: 'chrome100' } } diff --git a/test/unit.test.ts b/test/unit.test.ts index 6fb50f6..a13f6ee 100644 --- a/test/unit.test.ts +++ b/test/unit.test.ts @@ -101,7 +101,8 @@ describe('unit tests', function () { `) writeTextFile('./dependency.sass', readTextFile('./dependency-v2.sass')) - await ctx.rebuild().catch(ignored => {}) + await ctx.rebuild().catch(ignored => { + }) writeTextFile('./dependency.sass', readTextFile('./dependency-v3.sass')) await ctx.rebuild() @@ -152,7 +153,7 @@ describe('unit tests', function () { expect(readTextFile('out/index.js')).to.equalIgnoreSpaces(readTextFile('snapshot.js')) }) - it('captures warnings in entrypoint', async function() { + it('captures warnings in entrypoint', async function () { const options = useFixture('warnings') let warnings = [] @@ -163,11 +164,11 @@ describe('unit tests', function () { outdir: './out', bundle: true, plugins: [ - sassPlugin({ syntax: 'nested' }), + sassPlugin({syntax: 'nested'}), { - name: "capture-build-end-warnings", + name: 'capture-build-end-warnings', setup: function (build) { - build.onEnd(async function(result) { + build.onEnd(async function (result) { warnings = result.warnings }) } @@ -177,13 +178,13 @@ describe('unit tests', function () { expect(warnings.length).to.equal(1) - expect(warnings[0].text).to.include("This selector doesn't have any properties") - expect(warnings[0].location.file).to.equal("index.sass") + expect(warnings[0].text).to.include('This selector doesn\'t have any properties') + expect(warnings[0].location.file).to.equal('index.sass') expect(warnings[0].location.line).to.equal(3) - expect(warnings[0].location.lineText).to.equal("p") + expect(warnings[0].location.lineText).to.equal('p') }) - it('captures warnings in imports', async function() { + it('captures warnings in imports', async function () { const options = useFixture('warnings') let warnings = [] @@ -194,11 +195,11 @@ describe('unit tests', function () { logLevel: 'silent', outdir: './out', plugins: [ - sassPlugin({ syntax: 'nested' }), + sassPlugin({syntax: 'indented'}), { - name: "capture-build-end-warnings", + name: 'capture-build-end-warnings', setup: function (build) { - build.onEnd(async function(result) { + build.onEnd(async function (result) { warnings = result.warnings }) } @@ -208,14 +209,14 @@ describe('unit tests', function () { expect(warnings.length).to.equal(2) - const indexWarning = warnings.find(w => w.location.file === "index.sass") - expect(indexWarning.text).to.include("This selector doesn't have any properties") + const indexWarning = warnings.find(w => w.location.file.endsWith('index.sass')) + expect(indexWarning.text).to.include('This selector doesn\'t have any properties') expect(indexWarning.location.line).to.equal(3) - expect(indexWarning.location.lineText).to.equal("p") + expect(indexWarning.location.lineText).to.equal('p') - const partialWarning = warnings.find(w => w.location.file === "_partial.sass") - expect(partialWarning.text).to.include("This selector doesn't have any properties") + const partialWarning = warnings.find(w => w.location.file.endsWith('_partial.sass')) + expect(partialWarning.text).to.include('This selector doesn\'t have any properties') expect(partialWarning.location.line).to.equal(0) - expect(partialWarning.location.lineText).to.equal("div") + expect(partialWarning.location.lineText).to.equal('div') }) })