Skip to content

Commit

Permalink
fixes & formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
glromeo authored and glromeo committed Jun 20, 2023
1 parent 31a9453 commit 897874d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type SassPluginOptions = StringOptions<'sync'> & {
/**
*
*/
prefer?: "sass" | "style" | "main"
prefer?: 'sass' | 'style' | 'main'
}

export default sassPlugin
Expand Down
12 changes: 6 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ export function sassPlugin(options: SassPluginOptions = {}): Plugin {

const transform = options.transform ? options.transform.bind(options) : null

const cssChunks:Record<string, string | Uint8Array | undefined> = {}
const cssChunks: Record<string, string | Uint8Array | undefined> = {}

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'
}))
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/')}`

Expand Down Expand Up @@ -167,7 +167,7 @@ export function postcssModules(options: PostcssModulesParams, plugins: AcceptedP

return {
contents: css,
pluginData: { exports: cssModule },
pluginData: {exports: cssModule},
loader: 'js'
}
}
Expand Down
18 changes: 9 additions & 9 deletions test/bugfixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
})
Expand Down Expand Up @@ -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));`
)
Expand Down
3 changes: 2 additions & 1 deletion test/test-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
37 changes: 19 additions & 18 deletions test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = []

Expand All @@ -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
})
}
Expand All @@ -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 = []

Expand All @@ -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
})
}
Expand All @@ -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')
})
})

0 comments on commit 897874d

Please sign in to comment.