Skip to content

Commit

Permalink
feat: adds maxBuffer option that sets buffer limit for preprocess
Browse files Browse the repository at this point in the history
fixes #20
  • Loading branch information
ilyavf authored and mihar-22 committed Dec 9, 2020
1 parent a0f5fb1 commit bd80185
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,17 @@ The default mode is to load `svelte.config.js` from the current project root to
When `upward` is set it will stop at the first config file it finds above the file being transformed, but will walk up the directory structure all the way to the filesystem root if it cannot find any config file. This means that if there is no `svelte.config.js` file in the project above the file being transformed, it is always possible that someone will have a forgotten `svelte.config.js` in their home directory which could cause unexpected errors in your builds.
`maxBuffer` (default: 10485760): Sets limit for buffer when `preprocess` is true. It defines the largest amount of data in bytes allowed on stdout or stderr for [child_process.spawnSync](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options). If exceeded, the child process is terminated and any output is truncated. The default value of 10Mb overrides Node's default value of 1Mb.

```json
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.svelte$": ["svelte-jester", {
"preprocess": false,
"debug": false,
"compilerOptions": {},
"rootMode": ""
"rootMode": "",
"maxBuffer": 15000000
}]
}
```
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ describe('transformer', () => {
const esInterop = 'Object.defineProperty(exports, "__esModule", { value: true });'
expect(global.window.console.log).toHaveBeenCalledWith(code.replace(esInterop, ''))
})

it('should accept maxBuffer option for preprocess buffer limit', () => {
expect(
() => runTransformer('SassComp', { preprocess: true, maxBuffer: 1 })
).toThrow('spawnSync /bin/sh ENOBUFS');
runTransformer('SassComp', { preprocess: true, maxBuffer: 5 * 1024 * 1024 })
})
})
5 changes: 3 additions & 2 deletions src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ const svelte = require('svelte/compiler')
const { getSvelteConfig } = require('./svelteconfig.js')

const transformer = (options = {}) => (source, filename) => {
const { debug, compilerOptions, preprocess, rootMode } = options
const { debug, compilerOptions, preprocess, rootMode, maxBuffer } = options

let processed = source

if (preprocess) {
const svelteConfig = getSvelteConfig(rootMode, filename)
const preprocessor = require.resolve('./preprocess.js')
processed = execSync(`node --unhandled-rejections=strict --abort-on-uncaught-exception "${preprocessor}"`, {
env: { PATH: process.env.PATH, source, filename, svelteConfig }
env: { PATH: process.env.PATH, source, filename, svelteConfig },
maxBuffer: maxBuffer || 10 * 1024 * 1024
}).toString()
}

Expand Down

0 comments on commit bd80185

Please sign in to comment.