Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
- run: npm ci
- run: npm run playwright:install
- run: npm run test:ci
- run: npm run build
- run: npm run test:node
- name: Archive reference files
if: ${{ failure() }}
uses: actions/upload-artifact@v6
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@
"LICENSE"
],
"type": "module",
"main": "dist/svg2pdf.umd.min.js",
"main": "dist/svg2pdf.umd.min.cjs",
"module": "dist/svg2pdf.es.min.js",
"browser": "dist/svg2pdf.es.min.js",
"types": "types.d.ts",
"exports": {
".": {
"types": "./types.d.ts",
"import": "./dist/svg2pdf.es.min.js",
"require": "./dist/svg2pdf.umd.min.cjs"
},
"./dist/*": "./dist/*"
},
"scripts": {
"version": "npm run build && git add -A dist",
"build": "rollup -c rollup.config.mjs",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"test:node": "node --test test/node-entry/node-entry.test.mjs",
"test:watch": "vitest",
"test:ui": "vitest --ui",
"test:update": "vitest run -u",
Expand Down
8 changes: 8 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ const umd = {
sourcemap: true,
globals,
plugins: [terser({})]
},
{
// "type": "module" makes Node treat *.js as ESM, so require() needs a .cjs file
file: pkg.browser.replace('.es.min.js', '.umd.min.cjs'),
format: 'cjs',
exports: 'named',
sourcemap: true,
plugins: [terser({})]
}
],
external: umdExternals,
Expand Down
117 changes: 117 additions & 0 deletions test/node-entry/node-entry.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

// Simulate a consumer project: a temp dir whose node_modules contains this
// package (symlinked to the repo root, built files in dist/) and jspdf, then
// run plain `node` against it like the issue's repro does.
const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..')

function withConsumer(files, fn) {
const dir = mkdtempSync(join(tmpdir(), 'svg2pdf-consumer-'))
try {
mkdirSync(join(dir, 'node_modules'))
symlinkSync(root, join(dir, 'node_modules', 'svg2pdf.js'), 'dir')
symlinkSync(join(root, 'node_modules', 'jspdf'), join(dir, 'node_modules', 'jspdf'), 'dir')
for (const [name, source] of Object.entries(files)) writeFileSync(join(dir, name), source)
return fn(dir)
} finally {
rmSync(dir, { recursive: true, force: true })
}
}

const run = (dir, file) =>
execFileSync(process.execPath, [file], { cwd: dir, encoding: 'utf8', stdio: 'pipe' }).trim()

test('ESM consumer: import registers jsPDF.API.svg (issue #394 repro)', () => {
const out = withConsumer(
{
'repro.mjs': `
import { jsPDF } from 'jspdf'
import 'svg2pdf.js'
console.log(typeof jsPDF.API.svg)
`
},
dir => run(dir, 'repro.mjs')
)
assert.equal(out, 'function')
})

test('ESM consumer: named export svg2pdf is available', () => {
const out = withConsumer(
{
'repro.mjs': `
import { svg2pdf } from 'svg2pdf.js'
console.log(typeof svg2pdf)
`
},
dir => run(dir, 'repro.mjs')
)
assert.equal(out, 'function')
})

test('CommonJS consumer: require() loads the package', () => {
const out = withConsumer(
{
'repro.cjs': `
const { jsPDF } = require('jspdf')
const { svg2pdf } = require('svg2pdf.js')
console.log(typeof svg2pdf, typeof jsPDF.API.svg)
`
},
dir => run(dir, 'repro.cjs')
)
assert.equal(out, 'function function')
})

for (const [module, moduleResolution] of [
['Node16', 'Node16'],
['NodeNext', 'NodeNext'],
['ESNext', 'Bundler']
]) {
test(`TypeScript consumer: tsc --noEmit (${moduleResolution})`, () => {
withConsumer(
{
'package.json': JSON.stringify({ type: 'module' }),
'consumer.ts': `
import { jsPDF } from 'jspdf'
import { svg2pdf, type Svg2pdfOptions } from 'svg2pdf.js'
import 'svg2pdf.js'

const pdf = new jsPDF()
const element = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
const options: Svg2pdfOptions = { width: 100, loadImages: false }
const rendered: Promise<jsPDF> = svg2pdf(element, pdf, options)
const augmented: Promise<jsPDF> = pdf.svg(element, options)
// @ts-expect-error width must be numeric (also guards against untyped exports)
svg2pdf(element, pdf, { width: '100' })
`,
'tsconfig.json': JSON.stringify({
compilerOptions: {
strict: true,
noEmit: true,
module,
moduleResolution,
target: 'ES2020',
types: []
},
files: ['consumer.ts']
})
},
dir =>
execFileSync(
process.execPath,
[join(root, 'node_modules/typescript/bin/tsc'), '--noEmit'],
{
cwd: dir,
encoding: 'utf8',
stdio: 'pipe'
}
)
)
})
}