@kaliber/build 0.0.123 → 0.0.124

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,22 @@
1
- attempt(() => {
2
- const { evalWithSourceMap, withSourceMappedError } = require('./node-utils')
1
+ const fs = require('fs-extra')
2
+ const path = require('path')
3
+ const uuid = require('uuid')
3
4
 
4
- const messages = []
5
+ attempt(() => {
5
6
  process.on('message', handleMessage)
6
- function handleMessage(x) {
7
+
8
+ function handleMessage(source) {
7
9
  attempt(() => {
8
- messages.push(x)
9
- const [source, map] = messages
10
- if (source !== undefined && map !== undefined) {
11
- process.off('message', handleMessage)
12
- const createMap = () => map
13
- const { template, renderer } = evalWithSourceMap(source, createMap)
14
- const result = withSourceMappedError(createMap, () => renderer(template))
15
- process.send(result, e => attempt(() => {
10
+ process.off('message', handleMessage)
11
+ const { template, renderer } = evalSource(source)
12
+ const result = renderer(template)
13
+
14
+ process.send(result, e =>
15
+ attempt(() => {
16
16
  if (e) throw e
17
17
  else process.exit()
18
- }))
19
- }
18
+ })
19
+ )
20
20
  })
21
21
  }
22
22
  })
@@ -29,3 +29,9 @@ function attempt(f) {
29
29
  process.exit(1)
30
30
  }
31
31
  }
32
+
33
+ function evalSource(source) {
34
+ const file = path.resolve(`.kaliber-eval`, `${uuid.v4()}.js`)
35
+ fs.outputFileSync(file, source, { encoding: 'utf-8' })
36
+ try { return require(file) } finally { fs.removeSync(file) }
37
+ }
package/lib/node-utils.js CHANGED
@@ -1,35 +1,11 @@
1
- const { SourceMapConsumer } = require('source-map')
2
1
  const path = require('path')
3
2
  const childProcess = require('child_process')
4
3
 
5
4
  module.exports = {
6
- evalWithSourceMap,
7
- withSourceMappedError,
8
5
  evalInFork,
9
6
  }
10
7
 
11
- function evalWithSourceMap(source, createMap) {
12
- return withSourceMappedError(createMap, () => {
13
- const module = { exports: {} }
14
- eval(source) // eslint-disable-line no-eval
15
- return module.exports.default || module.exports
16
- }, { evalOnly: true })
17
- }
18
-
19
- function withSourceMappedError(createMap, fn, options) {
20
- return withRawErrorStack(() => {
21
- try {
22
- return fn()
23
- } catch (e) {
24
- const messageWithStack = e + '\n' + toMappedStack(createMap, e.stack, options)
25
- const error = new Error(messageWithStack)
26
- error.stack = messageWithStack
27
- throw error
28
- }
29
- })
30
- }
31
-
32
- async function evalInFork(source, map) {
8
+ async function evalInFork(name, source, map) {
33
9
  return new Promise((resolve, reject) => {
34
10
  const js = childProcess.fork(
35
11
  path.join(__dirname, 'eval-in-fork.js'),
@@ -49,33 +25,15 @@ async function evalInFork(source, map) {
49
25
  else resolve(messageData.join(''))
50
26
  } else reject(new Error(errData.join('')))
51
27
  })
52
- js.send(source)
53
- js.send(map)
54
- })
55
- }
56
-
57
28
 
58
- function withRawErrorStack(fn) {
59
- const $prepareStackTrace = Error.prepareStackTrace
60
- Error.prepareStackTrace = (error, stack) => stack
61
- try { return fn() } finally { Error.prepareStackTrace = $prepareStackTrace }
29
+ js.send(appendSourceMap(name, source, map))
30
+ })
62
31
  }
63
32
 
64
- function toMappedStack(createMap, stack = [], { evalOnly = false } = {}) {
65
- const sourceMap = new SourceMapConsumer(createMap())
66
- return stack
67
- .map(frame => {
68
- if (evalOnly && !frame.isEval()) return null
33
+ function appendSourceMap(name, source, map) {
34
+ map.sources = map.sources.map(source => { try { return require.resolve(source) } catch (_) { return `/.../${source}` } })
69
35
 
70
- const [frameLine, frameColumn] = [frame.getLineNumber(), frame.getColumnNumber()]
71
- if (!frameLine || !frameColumn) return ` at ${frame.getFileName()}:${frameLine}:${frameColumn}`
72
-
73
- const generated = { line: frameLine, column: frameColumn - 1 }
74
- const { source, line, column } = sourceMap.originalPositionFor(generated)
75
- return (source && !source.startsWith('webpack/'))
76
- ? ` at ${source}:${line}:${column + 1}`
77
- : null
78
- })
79
- .filter(Boolean)
80
- .join('\n')
36
+ const base64Map = Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')
37
+ const sourceMap = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
38
+ return `${source}\n${sourceMap}\n//# sourceURL=${name}`
81
39
  }
package/lib/serve.js CHANGED
@@ -59,14 +59,14 @@ app.use((err, req, res, next) => {
59
59
  if (!err) return next()
60
60
 
61
61
  console.error(err)
62
- if (reportError) reportError(err)
62
+ if (reportError) reportError(err, req)
63
63
 
64
64
  const response = res.status(500)
65
65
  if (isProduction) {
66
66
  findFile(req.path, internalServerError)
67
67
  .then(file => file ? response.sendFile(file) : next())
68
68
  .catch(next)
69
- } else response.send(`<pre>${err.toString()}</pre>`)
69
+ } else response.send(`<pre><title style='display: block;'>${err.stack || err.toString()}</title><pre>`)
70
70
  })
71
71
 
72
72
  app.listen(port, () => console.log(`Server listening at port ${port}`))
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.123",
2
+ "version": "0.0.124",
3
3
  "name": "@kaliber/build",
4
4
  "description": "Zero configuration, opinionated webpack / react build setup",
5
5
  "scripts": {
@@ -80,13 +80,13 @@
80
80
  "react": "^17.0.2",
81
81
  "react-dom": "^17.0.2",
82
82
  "rollbar": "^2.19.3",
83
- "source-map": "0.6.1",
84
83
  "stylelint": "^13.7.1",
85
84
  "stylelint-use-nesting": "^3.0.0",
86
85
  "tapable": "^2.0.0",
87
86
  "terser-webpack-plugin": "^4.2.2",
88
87
  "time-fix-plugin": "^2.0.7",
89
88
  "url-loader": "^4.1.0",
89
+ "uuid": "^8.3.2",
90
90
  "walk-sync": "^3.0.0",
91
91
  "webpack": "^4.44.2",
92
92
  "webpack-node-externals": "^2.5.2",
@@ -31,7 +31,7 @@ module.exports = function sourceMapPlugin({ sourceRoot }) {
31
31
 
32
32
  const [startComment, endComment] = name.endsWith('.css') ? ['/*', ' */'] : ['//', '']
33
33
  assets[name] = new ConcatSource(asset, `\n${startComment}# sourceMappingURL=${path.basename(name)}.map${endComment}\n`)
34
- assets[name + '.map'] = new RawSource(JSON.stringify(map))
34
+ assets[name + '.map'] = new RawSource(JSON.stringify({ ...map, sourceRoot: `${sourceRoot}/` }))
35
35
  }
36
36
  })
37
37
  })
@@ -143,11 +143,11 @@ module.exports = function templatePlugin(renderers) {
143
143
  try {
144
144
  const files = isFunction
145
145
  ? [
146
- [srcExt, createDynamicTemplate(path.basename(outputName), templateExt, map)],
147
- [templateExt, new RawSource(source)]
146
+ [srcExt, createDynamicTemplate(path.basename(outputName), templateExt)],
147
+ [templateExt, asset]
148
148
  ]
149
149
  : [
150
- [targetExt, await createStaticTemplate(source, map)]
150
+ [targetExt, await createStaticTemplate(name, source, map)]
151
151
  ]
152
152
 
153
153
  files.forEach(([ext, result]) => {
@@ -169,26 +169,22 @@ module.exports = function templatePlugin(renderers) {
169
169
  }
170
170
  }
171
171
 
172
- async function createStaticTemplate(source, map) {
173
- return new RawSource(await evalInFork(source, map))
172
+ async function createStaticTemplate(name, source, map) {
173
+ return new RawSource(await evalInFork(name, source, map))
174
174
  }
175
175
 
176
- function createDynamicTemplate(name, ext, map) {
176
+ function createDynamicTemplate(name, ext) {
177
177
  return new RawSource(
178
- `|const createMap = () => JSON.parse(${JSON.stringify((JSON.stringify(map)))})
179
- |
180
- |const { withSourceMappedError } = require('@kaliber/build/lib/node-utils')
181
- |
182
- |const envRequire = process.env.NODE_ENV === 'production' ? require : require('import-fresh')
183
- |const { template, renderer } = withSourceMappedError(createMap, () => envRequire('./${name}${ext}'))
178
+ `|const envRequire = process.env.NODE_ENV === 'production' ? require : require('import-fresh')
179
+ |const { template, renderer } = envRequire('./${name}${ext}')
184
180
  |
185
181
  |Object.assign(render, template)
186
182
  |
187
183
  |module.exports = render
188
184
  |
189
185
  |function render(props) {
190
- | return withSourceMappedError(createMap, () => renderer(template(props)))
186
+ | return renderer(template(props))
191
187
  |}
192
- |`.split(/^[ \t]*\|/m).join('')
188
+ |`.replace(/^[ \t]*\|/gm, '')
193
189
  )
194
190
  }