From 4b4be64bd11754d992fe108691284188743ba409 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 26 Oct 2024 10:23:34 +0100 Subject: [PATCH] chore: remove references to `__dirname` --- .../beasties-webpack-plugin/test/_helpers.js | 13 +++++++--- packages/beasties/src/index.js | 2 +- packages/beasties/test/beasties.bench.ts | 26 +++++++------------ packages/beasties/test/beasties.test.ts | 24 +++++++---------- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/packages/beasties-webpack-plugin/test/_helpers.js b/packages/beasties-webpack-plugin/test/_helpers.js index e544b3e..e814e09 100644 --- a/packages/beasties-webpack-plugin/test/_helpers.js +++ b/packages/beasties-webpack-plugin/test/_helpers.js @@ -16,11 +16,16 @@ import fs from 'node:fs' import path from 'node:path' +import { fileURLToPath } from 'node:url' import { promisify } from 'node:util' + import { JSDOM } from 'jsdom' import webpack from 'webpack' + import BeastiesWebpackPlugin from '../src/index.js' +const cwd = fileURLToPath(new URL('.', import.meta.url)) + const { window } = new JSDOM() // parse a string into a JSDOM Document @@ -30,24 +35,24 @@ export function parseDom(html) { // returns a promise resolving to the contents of a file export function readFile(file) { - return promisify(fs.readFile)(path.resolve(__dirname, file), 'utf8') + return promisify(fs.readFile)(path.resolve(cwd, file), 'utf-8') } // invoke webpack on a given entry module, optionally mutating the default configuration export function compile(entry, configDecorator) { return new Promise((resolve, reject) => { - const context = path.dirname(path.resolve(__dirname, entry)) + const context = path.dirname(path.resolve(cwd, entry)) entry = path.basename(entry) let config = { context, entry: path.resolve(context, entry), output: { - path: path.resolve(__dirname, path.resolve(context, 'dist')), + path: path.resolve(cwd, path.resolve(context, 'dist')), filename: 'bundle.js', chunkFilename: '[name].chunk.js', }, resolveLoader: { - modules: [path.resolve(__dirname, '../node_modules')], + modules: [path.resolve(cwd, '../node_modules')], }, // Needed to resolve `Error: error:0308010C:digital envelope routines::unsupported` in webpack 4. // Should remove when we drop support for webpack 4. diff --git a/packages/beasties/src/index.js b/packages/beasties/src/index.js index 44ed836..3bb05ff 100644 --- a/packages/beasties/src/index.js +++ b/packages/beasties/src/index.js @@ -151,7 +151,7 @@ export default class Beasties { fs.readFile(filename, callback) } else { - readFile(filename, 'utf8', callback) + readFile(filename, 'utf-8', callback) } }) } diff --git a/packages/beasties/test/beasties.bench.ts b/packages/beasties/test/beasties.bench.ts index 69d9506..152d7f4 100644 --- a/packages/beasties/test/beasties.bench.ts +++ b/packages/beasties/test/beasties.bench.ts @@ -1,9 +1,12 @@ import fs from 'node:fs' -import path from 'node:path' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' import { bench, describe } from 'vitest' import Beasties from '../src/index' +const fixtureDir = fileURLToPath(new URL('./src', import.meta.url)) + function trim(s: TemplateStringsArray) { return s[0]! .trim() @@ -38,15 +41,12 @@ describe('beasties', () => { `) }) - const basicHTML = fs.readFileSync( - path.join(__dirname, 'src/index.html'), - 'utf8', - ) + const basicHTML = fs.readFileSync(join(fixtureDir, 'index.html'), 'utf-8') bench('run on HTML file', async () => { const beasties = new Beasties({ reduceInlineStyles: false, - path: path.join(__dirname, 'src'), + path: fixtureDir, }) await beasties.process(basicHTML) @@ -147,28 +147,22 @@ describe('beasties', () => { `) }) - const mediaValidationHtml = fs.readFileSync( - path.join(__dirname, 'src/media-validation.html'), - 'utf8', - ) + const mediaValidationHtml = fs.readFileSync(join(fixtureDir, 'media-validation.html'), 'utf-8') bench('prevent injection via media attr', async () => { const beasties = new Beasties({ reduceInlineStyles: false, - path: path.join(__dirname, 'src'), + path: fixtureDir, preload: 'media', }) await beasties.process(mediaValidationHtml) }) - const invalidPathHtml = fs.readFileSync( - path.join(__dirname, 'src/subpath-validation.html'), - 'utf8', - ) + const invalidPathHtml = fs.readFileSync(join(fixtureDir, 'subpath-validation.html'), 'utf-8') bench('skip invalid path', async () => { const beasties = new Beasties({ reduceInlineStyles: false, - path: path.join(__dirname, 'src'), + path: fixtureDir, }) await beasties.process(invalidPathHtml) diff --git a/packages/beasties/test/beasties.test.ts b/packages/beasties/test/beasties.test.ts index 0232903..14d260f 100644 --- a/packages/beasties/test/beasties.test.ts +++ b/packages/beasties/test/beasties.test.ts @@ -16,11 +16,14 @@ import fs from 'node:fs' import path from 'node:path' +import { fileURLToPath } from 'node:url' import { describe, expect, it, vi } from 'vitest' import Beasties from '../src/index' +const fixtureDir = fileURLToPath(new URL('./src', import.meta.url)) + function trim(s: TemplateStringsArray) { return s[0]! .trim() @@ -61,13 +64,10 @@ describe('beasties', () => { it('run on HTML file', async () => { const beasties = new Beasties({ reduceInlineStyles: false, - path: path.join(__dirname, 'src'), + path: fixtureDir, }) - const html = fs.readFileSync( - path.join(__dirname, 'src/index.html'), - 'utf8', - ) + const html = fs.readFileSync(path.join(fixtureDir, 'index.html'), 'utf-8') const result = await beasties.process(html) expect(result).toMatchSnapshot() @@ -187,14 +187,11 @@ describe('beasties', () => { it('prevent injection via media attr', async () => { const beasties = new Beasties({ reduceInlineStyles: false, - path: path.join(__dirname, 'src'), + path: fixtureDir, preload: 'media', }) - const html = fs.readFileSync( - path.join(__dirname, 'src/media-validation.html'), - 'utf8', - ) + const html = fs.readFileSync(path.join(fixtureDir, 'media-validation.html'), 'utf-8') const result = await beasties.process(html) expect(result).toContain( @@ -208,13 +205,10 @@ describe('beasties', () => { const beasties = new Beasties({ reduceInlineStyles: false, - path: path.join(__dirname, 'src'), + path: fixtureDir, }) - const html = fs.readFileSync( - path.join(__dirname, 'src/subpath-validation.html'), - 'utf8', - ) + const html = fs.readFileSync(path.join(fixtureDir, 'subpath-validation.html'), 'utf-8') const result = await beasties.process(html) expect(consoleSpy).not.toHaveBeenCalledWith( -- 2.51.2