-
Notifications
You must be signed in to change notification settings - Fork 18
/
transformer.js
44 lines (36 loc) · 1.25 KB
/
transformer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// const log = require('why-is-node-running')
const { basename } = require('path')
const { execSync } = require('child_process')
const svelte = require('svelte/compiler')
const { getSvelteConfig } = require('./svelteconfig.js')
const transformer = (options = {}) => (source, filename) => {
const { debug, compilerOptions, preprocess, rootMode, maxBuffer } = options
let processed = source
if (preprocess) {
const svelteConfig = getSvelteConfig(rootMode, filename, preprocess)
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 },
maxBuffer: maxBuffer || 10 * 1024 * 1024
}).toString()
}
const result = svelte.compile(processed, {
filename: basename(filename),
css: true,
accessors: true,
dev: true,
format: 'cjs',
...compilerOptions
})
if (debug) {
console.log(result.js.code)
}
const esInterop = 'Object.defineProperty(exports, "__esModule", { value: true });'
return {
code: result.js.code + esInterop,
map: result.js.map
}
}
exports.createTransformer = (options) => ({
process: transformer(options)
})