[READ-ONLY] Mirror of https://github.com/danielroe/siroc. Zero-config build tooling for Node
bundle node package rollup typescript
0

Configure Feed

Select the types of activity you want to include in your feed.

fix: support cjs stubbing (#220)

authored by

Daniel Roe and committed by
GitHub
(Aug 24, 2021, 12:32 PM +0200) c61f3147 638fe753

+34 -14
+18 -9
src/core/package/index.spec.ts
··· 59 59 // TODO: move to fixture 60 60 test('should generate package stub', async () => { 61 61 const defaultPath = getFixturePath('default') 62 - const files = [ 63 - resolve(defaultPath, 'dist/index.js'), 64 - resolve(defaultPath, 'dist/index.es.js'), 65 - resolve(defaultPath, 'dist/index.d.ts'), 66 - ] 67 - for (const file of files) { 62 + const files = { 63 + [resolve(defaultPath, 'dist/index.js')]: [ 64 + `const jiti = require('jiti')()`, 65 + `module.exports = jiti('./../src/index')`, 66 + ].join('\n'), 67 + [resolve( 68 + defaultPath, 69 + 'dist/index.es.js' 70 + )]: `export * from './../src/index'`, 71 + [resolve( 72 + defaultPath, 73 + 'dist/index.d.ts' 74 + )]: `export * from './../src/index'`, 75 + } 76 + for (const file in files) { 68 77 await remove(file) 69 78 expect(existsSync(file)).toBeFalsy() 70 79 } 71 80 72 81 await core.createStubs() 73 82 74 - for (const file of files) { 83 + for (const file in files) { 75 84 expect( 76 85 readFileSync(file) 77 86 .toString() 78 - .replace(/from '.*\/fixture/, "from '/fixture") 79 - ).toBe(`export * from './../src/index'`) 87 + .replace(/'.*\/fixture/, "'/fixture") 88 + ).toBe(files[file]) 80 89 } 81 90 }) 82 91
+16 -5
src/core/package/index.ts
··· 342 342 const absPath = entrypoint.replace(/(\.[jt]s)$/, '') 343 343 await writeFile( 344 344 binary, 345 - `#!/usr/bin/env node\nconst jiti = require('jiti')()\nmodule.exports = jiti('${absPath}')` 345 + [ 346 + `#!/usr/bin/env node`, 347 + `const jiti = require('jiti')()`, 348 + `module.exports = jiti('${absPath}')`, 349 + ].join('\n') 346 350 ) 347 351 await this.setBinaryPermissions() 348 352 }) 349 353 } 350 354 351 - async createStub(path: string | undefined) { 355 + async createStub(path: string | undefined, cjs = false) { 352 356 if (!path || !this.entrypoint || !this.options.build) return 353 357 354 358 const outFile = this.resolvePath(path) 355 359 const outDir = dirname(outFile) 356 360 if (!existsSync(outDir)) await mkdirp(outDir) 357 361 const relativeEntrypoint = relative(outDir, this.entrypoint).replace( 358 - /(\.[jt]s)$/, 362 + /(\.[cm]?[jt]s)$/, 359 363 '' 360 364 ) 361 - await writeFile(outFile, `export * from './${relativeEntrypoint}'`) 365 + const stub = cjs 366 + ? [ 367 + `const jiti = require('jiti')()`, 368 + `module.exports = jiti('./${relativeEntrypoint}')`, 369 + ].join('\n') 370 + : `export * from './${relativeEntrypoint}'` 371 + 372 + await writeFile(outFile, stub) 362 373 } 363 374 364 375 async createStubs() { 365 376 return Promise.all([ 366 377 this.createBinaryStubs(), 367 - this.createStub(this.pkg.main), 378 + this.createStub(this.pkg.main, true), 368 379 this.createStub(this.pkg.module), 369 380 this.createStub(this.pkg.types), 370 381 ])