Skip to content

Commit

Permalink
Move to ESM (#338)
Browse files Browse the repository at this point in the history
* move to esm

* update test workflow

* add esm config tests

* cleanup fixtures

* some more cleanup

* fix formatting

* fix test names
  • Loading branch information
lev875 committed Oct 23, 2023
1 parent 71e974c commit a015e3e
Show file tree
Hide file tree
Showing 101 changed files with 1,384 additions and 2,047 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run unit tests
run: ./node_modules/.bin/jest --forceExit
run: ./node_modules/.bin/vitest --run
17 changes: 5 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"type": "module",
"private": true,
"scripts": {
"bump": "./scripts/bump",
"release": "./scripts/each pnpm clean-publish --access public",
"clean": "rm -Rf packages/*/node_modules/ node_modules/ node-warnings.logs out/ coverage/",
"test": "jest --coverage && eslint . && sh scripts/max-listeners-check.sh"
"test": "vitest run --coverage && eslint . && sh scripts/max-listeners-check.sh"
},
"devDependencies": {
"@logux/eslint-config": "^51.0.2",
Expand All @@ -18,6 +19,7 @@
"@size-limit/time": "workspace:^",
"@size-limit/webpack": "workspace:^",
"@size-limit/webpack-why": "workspace:^",
"@vitest/coverage-istanbul": "^0.34.6",
"clean-publish": "^4.2.0",
"cross-spawn": "^7.0.3",
"dual-publish": "^3.0.1",
Expand All @@ -27,10 +29,10 @@
"eslint-plugin-n": "^16.0.2",
"eslint-plugin-prefer-let": "^3.0.1",
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.6.4",
"print-snapshots": "^0.4.2",
"redux": "^4.2.1",
"size-limit-node-esbuild": "^0.2.0"
"size-limit-node-esbuild": "^0.2.0",
"vitest": "^0.34.6"
},
"eslintConfig": {
"extends": "@logux/eslint-config",
Expand All @@ -44,15 +46,6 @@
"files": "*.test.js",
"rules": {
"n/no-extraneous-require": "off"
},
"globals": {
"describe": "readonly",
"it": "readonly",
"expect": "readonly",
"beforeAll": "readonly",
"beforeEach": "readonly",
"afterEach": "readonly",
"jest": "readonly"
}
},
{
Expand Down
108 changes: 54 additions & 54 deletions packages/dual-publish/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
let SizeLimitError = require('size-limit/size-limit-error')
let { spawn } = require('child_process')
let { join } = require('path')
let rm = require('size-limit/rm')
import { spawn } from 'child_process'
import { join } from 'path'
import rm from 'size-limit/rm'
import { SizeLimitError } from 'size-limit/size-limit-error'

let self = {
async all10(config) {
let stderr = ''
let cmd = spawn(
process.platform === 'win32' ? 'npx.cmd' : 'npx',
['-y', '-q', 'dual-publish', '--check'],
{ cwd: config.cwd }
)
cmd.stderr.on('data', data => {
stderr += data.toString().replace(/^ ERROR {2}/, '')
})
await new Promise((resolve, reject) => {
cmd.on('close', code => {
if (code !== 0 || stderr) {
reject(new SizeLimitError('cmdError', 'dual-publish', stderr))
} else {
resolve()
}
})
cmd.on('error', reject)
})
for (let check of config.checks) {
check.files = check.files.map(i => {
return join(
config.cwd,
'dual-publish-tmp',
i.slice(config.cwd.length + 1)
)
export default [
{
async all10(config) {
let stderr = ''
let cmd = spawn(
process.platform === 'win32' ? 'npx.cmd' : 'npx',
['-y', '-q', 'dual-publish', '--check'],
{ cwd: config.cwd }
)
cmd.stderr.on('data', data => {
stderr += data.toString().replace(/^ ERROR {2}/, '')
})
if (check.import) {
let imports = {}
for (let i in check.import) {
if (!i.includes('node_modules')) {
let changed = join(
config.cwd,
'dual-publish-tmp',
i.slice(config.cwd.length + 1)
)
imports[changed] = check.import[i]
await new Promise((resolve, reject) => {
cmd.on('close', code => {
if (code !== 0 || stderr) {
reject(new SizeLimitError('cmdError', 'dual-publish', stderr))
} else {
imports[i] = check.import[i]
resolve()
}
})
cmd.on('error', reject)
})
for (let check of config.checks) {
check.files = check.files.map(i => {
return join(
config.cwd,
'dual-publish-tmp',
i.slice(config.cwd.length + 1)
)
})
if (check.import) {
let imports = {}
for (let i in check.import) {
if (!i.includes('node_modules')) {
let changed = join(
config.cwd,
'dual-publish-tmp',
i.slice(config.cwd.length + 1)
)
imports[changed] = check.import[i]
} else {
imports[i] = check.import[i]
}
}
check.import = imports
}
check.import = imports
}
}
},

async finally(config) {
await rm(join(config.cwd, 'dual-publish-tmp'))
},
name: '@size-limit/dual-publish',
},

wait10: 'Compiling files to ESM'
}
async finally(config) {
await rm(join(config.cwd, 'dual-publish-tmp'))
},
name: '@size-limit/dual-publish',

module.exports = [self]
wait10: 'Compiling files to ESM'
}
]
1 change: 1 addition & 0 deletions packages/dual-publish/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@size-limit/dual-publish",
"version": "9.0.0",
"description": "dual-publish plugin for Size Limit",
"type": "module",
"keywords": [
"size-limit",
"plugin",
Expand Down
10 changes: 6 additions & 4 deletions packages/dual-publish/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
let SizeLimitError = require('size-limit/size-limit-error')
let { join } = require('path')
import { join } from 'path'
import { SizeLimitError } from 'size-limit/size-limit-error'
import { expect, it, vi } from 'vitest'

let [dualPublish] = require('../')
import dualPublishPkg from '../'
const [dualPublish] = dualPublishPkg

jest.mock('child_process', () => ({
vi.mock('child_process', () => ({
spawn(cmd, args, opts) {
return {
on(type, cb) {
Expand Down
44 changes: 22 additions & 22 deletions packages/esbuild-why/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
let { visualizer } = require('esbuild-visualizer')
let { join } = require('path')
let { writeFileSync } = require('fs')
let open = require('open')
import { visualizer } from 'esbuild-visualizer'
import { writeFileSync } from 'fs'
import open from 'open'
import { join } from 'path'

let { getReportName } = require('./report')
import { getReportName } from './report'

let self = {
async finally(config, check) {
let {esbuildVisualizerFile} = check
export default [
{
async finally(config, check) {
let { esbuildVisualizerFile } = check

if (esbuildVisualizerFile) {
await open(esbuildVisualizerFile)
}
},
if (esbuildVisualizerFile) {
await open(esbuildVisualizerFile)
}
},

name: '@size-limit/esbuild-why',
async step81(config, check) {
if (config.why && check.esbuildMetafile) {
let result = await visualizer(check.esbuildMetafile)
let file = join(config.saveBundle ?? '', getReportName(config, check))
check.esbuildVisualizerFile = file;
writeFileSync(file, result)
name: '@size-limit/esbuild-why',
async step81(config, check) {
if (config.why && check.esbuildMetafile) {
let result = await visualizer(check.esbuildMetafile)
let file = join(config.saveBundle ?? '', getReportName(config, check))
check.esbuildVisualizerFile = file
writeFileSync(file, result)
}
}
}
}

module.exports = [self]
]
1 change: 1 addition & 0 deletions packages/esbuild-why/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@size-limit/esbuild-why",
"version": "9.0.0",
"description": "esbuild plugin for Size Limit",
"type": "module",
"keywords": [
"size-limit",
"plugin",
Expand Down
14 changes: 6 additions & 8 deletions packages/esbuild-why/report.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
let { isValidFilename } = require('./valid-filename')
import { isValidFilename } from './valid-filename'

module.exports = {
getReportName(config, check) {
if (config.checks.length === 1) return `esbuild-why.html`
if (isValidFilename(check.name)) return `esbuild-why-${check.name}.html`
let index = config.checks.findIndex(c => c.name === check.name)
return `esbuild-why-${index + 1}.html`
}
export function getReportName(config, check) {
if (config.checks.length === 1) return `esbuild-why.html`
if (isValidFilename(check.name)) return `esbuild-why-${check.name}.html`
let index = config.checks.findIndex(c => c.name === check.name)
return `esbuild-why-${index + 1}.html`
}
31 changes: 18 additions & 13 deletions packages/esbuild-why/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
let {readFile} = require('fs').promises
let [esbuild] = require('@size-limit/esbuild')
let {join} = require('path')
let rm = require('size-limit/rm')
let open = require('open')
import esbuildPkg from '@size-limit/esbuild'
import { readFile } from 'fs/promises'
import open from 'open'
import { join } from 'path'
import rm from 'size-limit/rm'
import { afterEach, expect, it, vi } from 'vitest'

jest.mock('open');
import esbuildWhyPkg from '..'
const [esbuild] = esbuildPkg

let [esbuildWhy] = require('..')
vi.mock('open')
const [esbuildWhy] = esbuildWhyPkg

const DIST = join(process.cwd(), 'out')

Expand All @@ -16,13 +19,13 @@ function fixture(name) {

afterEach(async () => {
await rm(DIST)
jest.clearAllMocks()
vi.clearAllMocks()
})

it('supports --why', async () => {
jest.spyOn(console, 'log').mockImplementation(() => true)
vi.spyOn(console, 'log').mockImplementation(() => true)
let config = {
checks: [{files: [fixture('big.js')]}],
checks: [{ files: [fixture('big.js')] }],
project: 'superProject',
saveBundle: DIST,
why: true
Expand All @@ -43,7 +46,7 @@ it('supports --why', async () => {

it('supports open esbuild visualizer on complete', async () => {
let config = {
checks: [{files: [fixture('big.js')]}],
checks: [{ files: [fixture('big.js')] }],
project: 'superProject',
saveBundle: DIST,
why: true
Expand All @@ -58,6 +61,8 @@ it('supports open esbuild visualizer on complete', async () => {
await esbuildWhy.finally(config, config.checks[0])
}

expect(open).toHaveBeenCalledTimes(1);
expect(open).toHaveBeenCalledWith(expect.stringMatching( /.*\/out\/esbuild-why.html$/));
expect(open).toHaveBeenCalledTimes(1)
expect(open).toHaveBeenCalledWith(
expect.stringMatching(/.*\/out\/esbuild-why.html$/)
)
})
8 changes: 6 additions & 2 deletions packages/esbuild-why/test/report.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
let { getReportName } = require('../report')
import { expect, it } from 'vitest'

import { getReportName } from '../report'

let plain = {
name: 'plain'
Expand Down Expand Up @@ -27,7 +29,9 @@ it('returns name if the name is a valid file name', () => {

it('returns plain name if there is only one check', () => {
expect(getReportName({ checks: [plain] }, plain)).toEqual(`esbuild-why.html`)
expect(getReportName({ checks: [withSlash] }, withSlash)).toEqual(`esbuild-why.html`)
expect(getReportName({ checks: [withSlash] }, withSlash)).toEqual(
`esbuild-why.html`
)
})

it('returns one-based index if name is not a valid file name', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/esbuild-why/test/valid-filename.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
let { isValidFilename } = require('../valid-filename')
import { expect, it } from 'vitest'

import { isValidFilename } from '../valid-filename'

it('main', () => {
expect(isValidFilename('foo-bar')).toBe(true)
Expand All @@ -16,4 +18,4 @@ it('main', () => {
expect(isValidFilename('.')).toBe(false)
expect(isValidFilename('..')).toBe(false)
expect(isValidFilename('...')).toBe(true)
});
})
16 changes: 7 additions & 9 deletions packages/esbuild-why/valid-filename.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module.exports = {
isValidFilename(string) {
if (!string || string.length > 255 || string === '.' || string === '..') {
return false
}
return (
!/[<>:"/\\|?*\u0000-\u001F]/g.test(string) &&
!/^(con|prn|aux|nul|com\d|lpt\d)$/i.test(string)
)
export function isValidFilename(string) {
if (!string || string.length > 255 || string === '.' || string === '..') {
return false
}
return (
!/[<>:"/\\|?*\u0000-\u001F]/g.test(string) &&
!/^(con|prn|aux|nul|com\d|lpt\d)$/i.test(string)
)
}
2 changes: 1 addition & 1 deletion packages/esbuild/convert-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function convertConfig(config) {
export function convertConfig(config) {
config.metafile = true
}
4 changes: 2 additions & 2 deletions packages/esbuild/get-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let processImport = require('size-limit/process-import')
import processImport from 'size-limit/process-import'

module.exports = async function getConfig(limitConfig, check, output) {
export async function getConfig(_limitConfig, check, output) {
await processImport(check, output)

let config = {
Expand Down

0 comments on commit a015e3e

Please sign in to comment.