|
| 1 | +/* eslint-env node */ |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { execFileSync } from 'node:child_process'; |
| 4 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { dirname, join } from 'node:path'; |
| 7 | +import { fileURLToPath } from 'node:url'; |
| 8 | +import { rollup } from 'rollup'; |
| 9 | +import configs from '../rollup.config.mjs'; |
| 10 | + |
| 11 | +const root = dirname(dirname(fileURLToPath(import.meta.url))); |
| 12 | +const consumer = mkdtempSync(join(tmpdir(), 'gcode-preview-packaging-')); |
| 13 | +const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')); |
| 14 | +const lock = JSON.parse(readFileSync(join(root, 'package-lock.json'), 'utf8')); |
| 15 | +const log = join(consumer, 'packaging.log'); |
| 16 | + |
| 17 | +function run(command, args, cwd = consumer) { |
| 18 | + console.log(`Packaging: ${command} ${args.join(' ')}`); |
| 19 | + try { |
| 20 | + const output = execFileSync(command, args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }); |
| 21 | + writeFileSync(log, output, { flag: 'a' }); |
| 22 | + return output; |
| 23 | + } catch (error) { |
| 24 | + writeFileSync(log, `${error.stdout}\n${error.stderr}`, { flag: 'a' }); |
| 25 | + throw error; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +try { |
| 30 | + // Exercise sharing before the standalone parser becomes a public entry point (#492). |
| 31 | + for (const { output, ...config } of configs) { |
| 32 | + console.log(`Packaging: verify shared ${output.entryFileNames} chunks`); |
| 33 | + const bundle = await rollup({ |
| 34 | + ...config, |
| 35 | + input: { ...config.input, parser: 'src/parser/gcode-parser.ts' }, |
| 36 | + plugins: config.plugins.filter((plugin) => plugin.name !== 'clean-dist') |
| 37 | + }); |
| 38 | + try { |
| 39 | + const { output: chunks } = await bundle.generate(output); |
| 40 | + const owners = chunks.filter( |
| 41 | + (chunk) => chunk.type === 'chunk' && join(root, 'src/parser/gcode-parser.ts') in chunk.modules |
| 42 | + ); |
| 43 | + assert.equal(owners.length, 1, 'parser implementation must not be duplicated'); |
| 44 | + assert( |
| 45 | + chunks.some((chunk) => chunk.type === 'chunk' && chunk.imports.includes(owners[0].fileName)), |
| 46 | + 'the preview entry must reuse the parser chunk' |
| 47 | + ); |
| 48 | + assert.equal(chunks.filter((chunk) => chunk.type === 'chunk' && chunk.isEntry).length, 2); |
| 49 | + assert(owners[0].fileName.endsWith(output.entryFileNames.endsWith('.d.ts') ? '.d.ts' : '.js')); |
| 50 | + } finally { |
| 51 | + await bundle.close(); |
| 52 | + } |
| 53 | + } |
| 54 | + const [packed] = JSON.parse(run('npm', ['pack', '--json', '--pack-destination', consumer], root)); |
| 55 | + assert(!packed.files.some(({ path }) => path === 'dist/gcode-preview.js'), 'UMD build must not ship'); |
| 56 | + writeFileSync(join(consumer, 'package.json'), JSON.stringify({ private: true, type: 'module' })); |
| 57 | + run('npm', ['install', '--ignore-scripts', '--no-audit', '--no-fund', join(consumer, packed.filename)]); |
| 58 | + writeFileSync( |
| 59 | + join(consumer, 'smoke.mjs'), |
| 60 | + `import assert from 'node:assert/strict'; |
| 61 | +import { createRequire } from 'node:module'; |
| 62 | +import { GCodePreview, Parser, Job, SceneManager } from 'gcode-preview'; |
| 63 | +const require = createRequire(import.meta.url); |
| 64 | +const pkg = require('gcode-preview/package.json'); |
| 65 | +assert.equal(pkg.type, 'module'); |
| 66 | +assert.equal(pkg.sideEffects, false); |
| 67 | +assert.equal(pkg.dependencies['lil-gui'], undefined); |
| 68 | +assert.equal(typeof GCodePreview, 'function'); |
| 69 | +assert.equal(typeof Job, 'function'); |
| 70 | +assert.equal(typeof SceneManager, 'function'); |
| 71 | +assert.equal(new Parser().parseCommand('G1 X42').params.x, 42); |
| 72 | +assert.throws(() => require.resolve('lil-gui'), { code: 'MODULE_NOT_FOUND' }); |
| 73 | +for (const path of ['dist/gcode-preview.es.js', 'dist/gcode-preview.d.ts', 'src/gcode-preview', 'parser']) { |
| 74 | + await assert.rejects(import('gcode-preview/' + path), { code: 'ERR_PACKAGE_PATH_NOT_EXPORTED' }); |
| 75 | +} |
| 76 | +` |
| 77 | + ); |
| 78 | + run(process.execPath, ['smoke.mjs']); |
| 79 | + |
| 80 | + // Install the declaration dependencies explicitly, as a TypeScript consumer would. |
| 81 | + run('npm', [ |
| 82 | + 'install', |
| 83 | + '--ignore-scripts', |
| 84 | + '--no-audit', |
| 85 | + '--no-fund', |
| 86 | + `typescript@${lock.packages['node_modules/typescript'].version}`, |
| 87 | + `@types/three@${pkg.devDependencies['@types/three']}`, |
| 88 | + `@webgpu/types@${pkg.devDependencies['@webgpu/types']}` |
| 89 | + ]); |
| 90 | + writeFileSync( |
| 91 | + join(consumer, 'consumer.ts'), |
| 92 | + `import { GCodePreview, Parser, type GCodePreviewOptions } from 'gcode-preview'; |
| 93 | +const options: GCodePreviewOptions = { canvas: document.createElement('canvas') }; |
| 94 | +new GCodePreview(options).processGCode('G1 X42'); |
| 95 | +const x: number | undefined = new Parser().parseCommand('G1 X42')?.params.x; |
| 96 | +void x; |
| 97 | +` |
| 98 | + ); |
| 99 | + for (const [moduleResolution, module] of [ |
| 100 | + ['node', 'esnext'], |
| 101 | + ['node16', 'node16'], |
| 102 | + ['nodenext', 'nodenext'], |
| 103 | + ['bundler', 'esnext'] |
| 104 | + ]) { |
| 105 | + console.log(`Packaging: typecheck with moduleResolution=${moduleResolution}`); |
| 106 | + writeFileSync( |
| 107 | + join(consumer, 'tsconfig.json'), |
| 108 | + JSON.stringify({ |
| 109 | + compilerOptions: { |
| 110 | + moduleResolution, |
| 111 | + module, |
| 112 | + target: 'es2020', |
| 113 | + strict: true, |
| 114 | + noEmit: true, |
| 115 | + types: ['@webgpu/types'] |
| 116 | + }, |
| 117 | + files: ['consumer.ts'] |
| 118 | + }) |
| 119 | + ); |
| 120 | + run(process.execPath, ['node_modules/typescript/bin/tsc', '-p', 'tsconfig.json']); |
| 121 | + } |
| 122 | + console.log('Packaging smoke test passed (runtime, exports, and four TypeScript resolution modes).'); |
| 123 | + rmSync(consumer, { recursive: true, force: true }); |
| 124 | +} catch (error) { |
| 125 | + console.error(`Packaging fixture and command log retained at ${consumer}`); |
| 126 | + throw error; |
| 127 | +} |
0 commit comments