@kaliber/build 0.0.121 → 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
@@ -8,7 +8,7 @@ const morgan = require('morgan')
8
8
 
9
9
  const templateRenderers = require('./getTemplateRenderers')
10
10
 
11
- const { kaliber: { serveMiddleware, helmetOptions, publicPath = '/' } = {} } = require('@kaliber/config')
11
+ const { kaliber: { serveMiddleware, helmetOptions, publicPath = '/', reportError } = {} } = require('@kaliber/config')
12
12
 
13
13
  const recognizedTemplates = Object.keys(templateRenderers)
14
14
  const blockedTemplateFiles = recognizedTemplates.reduce(
@@ -59,12 +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, req)
63
+
62
64
  const response = res.status(500)
63
65
  if (isProduction) {
64
66
  findFile(req.path, internalServerError)
65
67
  .then(file => file ? response.sendFile(file) : next())
66
68
  .catch(next)
67
- } else response.send(`<pre>${err.toString()}</pre>`)
69
+ } else response.send(`<pre><title style='display: block;'>${err.stack || err.toString()}</title><pre>`)
68
70
  })
69
71
 
70
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.121",
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",
@@ -4,7 +4,7 @@ const {
4
4
  withRootRules, withNestedRules,
5
5
  isPseudoElement,
6
6
  } = require('../../machinery/ast')
7
- const { flexChildProps, gridChildProps } = require('../../machinery/css')
7
+ const { flexChildProps, gridChildProps, flexOrGridChildProps } = require('../../machinery/css')
8
8
 
9
9
  const intrinsicUnits = ['px', 'em', 'rem', 'vw', 'vh']
10
10
  const intrinsicProps = ['width', 'height', 'max-width', 'min-width', 'max-height', 'min-height']
@@ -25,6 +25,7 @@ const layoutRelatedProps = [ // only allowed in child
25
25
  'justify-self', 'align-self',
26
26
  ...flexChildProps,
27
27
  ...gridChildProps,
28
+ ...flexOrGridChildProps,
28
29
  ...allowedInRootAndChild,
29
30
  ]
30
31
  const layoutRelatedPropsWithValues = extractPropsWithValues(layoutRelatedProps)
@@ -36,9 +37,9 @@ const messages = {
36
37
  `move to a nested selector in a another root rule, if you are forced by a third party ` +
37
38
  `library, you can rename your selector to \`_rootXyz\` or \`component_rootXyz\`` + (
38
39
  intrinsicProps.includes(prop)
39
- ? `\nif you are trying to define an intrinsic ${prop}, make sure you set the unit to ` +
40
+ ? `\nif you are trying to define an intrinsic ${prop}, make sure you set the unit to ` +
40
41
  `one of \`${intrinsicUnits.join('`, `')}\` and add \`!important\``
41
- : ''
42
+ : ''
42
43
  ),
43
44
  'nested - only layout related props in nested': prop =>
44
45
  `illegal non-layout related prop\n` +
@@ -127,6 +127,7 @@ Examples of *correct* code for this rule:
127
127
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
128
128
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
129
129
  justify-self: 0; align-self: 0;
130
+ order: 0;
130
131
  }
131
132
  }
132
133
  ```
@@ -143,6 +144,7 @@ Examples of *incorrect* code for this rule:
143
144
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
144
145
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
145
146
  justify-self: 0; align-self: 0;
147
+ order: 0;
146
148
  max-width: 0; min-width: 0; max-height: 0; min-height: 0;
147
149
  }
148
150
  ```
@@ -19,6 +19,7 @@ test('layout-related-properties', {
19
19
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
20
20
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
21
21
  justify-self: 0; align-self: 0;
22
+ order: 0;
22
23
  }
23
24
  }
24
25
  `,
@@ -101,6 +102,7 @@ test('layout-related-properties', {
101
102
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
102
103
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
103
104
  justify-self: 0; align-self: 0;
105
+ order: 0;
104
106
  max-width: 0; min-width: 0; max-height: 0; min-height: 0;
105
107
  }
106
108
  `,
@@ -113,6 +115,7 @@ test('layout-related-properties', {
113
115
  'grid', 'grid-area', 'grid-column', 'grid-row',
114
116
  'grid-column-start', 'grid-column-end', 'grid-row-start', 'grid-row-end',
115
117
  'justify-self', 'align-self',
118
+ 'order',
116
119
  'max-width', 'min-width', 'max-height', 'min-height',
117
120
  ])
118
121
  },
@@ -133,6 +136,7 @@ test('layout-related-properties', {
133
136
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
134
137
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
135
138
  justify-self: 0; align-self: 0;
139
+ order: 0;
136
140
  }
137
141
  }
138
142
  `,
@@ -145,6 +149,7 @@ test('layout-related-properties', {
145
149
  'grid', 'grid-area', 'grid-column', 'grid-row',
146
150
  'grid-column-start', 'grid-column-end', 'grid-row-start', 'grid-row-end',
147
151
  'justify-self', 'align-self',
152
+ 'order',
148
153
  ])
149
154
  },
150
155
  {
@@ -138,6 +138,7 @@ test('naming-policy', {
138
138
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
139
139
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
140
140
  justify-self: 0; align-self: 0;
141
+ order: 0;
141
142
  }
142
143
 
143
144
  .component_rootTest {
@@ -149,6 +150,7 @@ test('naming-policy', {
149
150
  grid: 0; grid-area: 0; grid-column: 0; grid-row: 0;
150
151
  grid-column-start: 0; grid-column-end: 0; grid-row-start: 0; grid-row-end: 0;
151
152
  justify-self: 0; align-self: 0;
153
+ order: 0;
152
154
  }
153
155
  `,
154
156
  },
@@ -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
  }