klenod-plugin-javascript 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f6d99ba8d67935d9db3c5fdd451ced0508f9c2d9de7ae8daefc4de42cb6cf67
4
- data.tar.gz: '09df9d3181ec7af2caa74a7e9132abbfc9de4167ac41fb86743e5153c5ca96bc'
3
+ metadata.gz: 3c8464e3833b7ea07785ee149f36573ef7eecaa594cbaa0e80db676b23f7b24e
4
+ data.tar.gz: 04a644220d927ccb75dce5da97368b2f6fda74a40b2fc442899da229bce261fa
5
5
  SHA512:
6
- metadata.gz: 23c83b068ecbd2d4c630c4623f04853a68548ce205f4b0a84b37e30544c2c1f744280b5cd5e1346d295d134ce8154d4cbc3921a279e6d6517f52f024550d107e
7
- data.tar.gz: f7e0a04b3fddff8cf9cc6d62927321ed933b1c16889ec190ca922c548df2fb5d20068a2a95d018acb7fa2d59acbdbfe093790e5a37f795fe4b510767ccaaea60
6
+ metadata.gz: f5e7232182b2f214b46a9f6416700d8db21f1bada0c8b2c9abbd22a9d5b8d6abc6776d3c0e789add63ffcfbf41525bfad38214d2bb8d1a299001675843628c1f
7
+ data.tar.gz: 5f1d84b30baefca55d175f3350008850af922c6b9113230c4fc558154908f423684008f0d07071d8ce0c725fe4ae53453cdc042cc24d2e03fa6fe961c71d6764
@@ -964,9 +964,9 @@ dependencies = [
964
964
 
965
965
  [[package]]
966
966
  name = "swc_core"
967
- version = "77.1.2"
967
+ version = "78.0.0"
968
968
  source = "registry+https://github.com/rust-lang/crates.io-index"
969
- checksum = "400701fb0bc3d13d1d0a1de6f58d91489fa9007eb3c584057bb2adb637fa9a5d"
969
+ checksum = "2b3e1cf21711152ac57d0ade1fea33bf8f1cc974f7bc1f668ac0252252876803"
970
970
  dependencies = [
971
971
  "swc_allocator",
972
972
  "swc_atoms",
@@ -9,4 +9,4 @@ crate-type = ["cdylib"]
9
9
 
10
10
  [dependencies]
11
11
  magnus = "0.8.2"
12
- swc_core = { version = "77.1.2", features = ["common", "ecma_ast", "ecma_codegen", "ecma_parser", "ecma_parser_typescript", "ecma_transforms_react", "ecma_transforms_typescript", "ecma_visit"] }
12
+ swc_core = { version = "78.0.0", features = ["common", "ecma_ast", "ecma_codegen", "ecma_parser", "ecma_parser_typescript", "ecma_transforms_react", "ecma_transforms_typescript", "ecma_visit"] }
@@ -1,7 +1,8 @@
1
1
  use magnus::{function, prelude::*, Error, RArray, RHash, Ruby, TryConvert};
2
2
  use swc_core::{
3
3
  common::{
4
- comments::NoopComments, sync::Lrc, FileName, Globals, Mark, SourceMap, Span, GLOBALS,
4
+ comments::NoopComments, sync::Lrc, FileName, Globals, Mark, SourceMap, Span, Spanned,
5
+ GLOBALS,
5
6
  },
6
7
  ecma::{
7
8
  ast::{
@@ -222,9 +223,16 @@ fn parse_module(
222
223
  );
223
224
  let mut parser = Parser::new_from(lexer);
224
225
  let module = parser.parse_module().map_err(|error| {
226
+ let loc = source_map.lookup_char_pos(error.span().lo);
225
227
  Error::new(
226
228
  ruby.exception_syntax_error(),
227
- format!("{}: {}", filename, error.kind().msg()),
229
+ format!(
230
+ "{}:{}:{}: {}",
231
+ filename,
232
+ loc.line,
233
+ loc.col.0 + 1,
234
+ error.kind().msg()
235
+ ),
228
236
  )
229
237
  })?;
230
238
  Ok(ParsedProgram {
@@ -12,6 +12,7 @@ require "klenod/build/source_map"
12
12
  require "klenod/build/transform_result"
13
13
  require "klenod/runtime"
14
14
 
15
+ require_relative "../../plugin/javascript/errors"
15
16
  require_relative "../../plugin/javascript/parser"
16
17
 
17
18
  module Klenod
@@ -72,14 +73,21 @@ module Klenod
72
73
  return super unless EXTENSIONS.include?(module_id.extname)
73
74
 
74
75
  custom_element = custom_element_module?(module_id)
76
+ return analysis_transform(module_id, code, custom_element) if context.analysis?
77
+
75
78
  jsx_runtime_namespace = custom_element ? jsx_runtime_namespace(module_id) : nil
76
- transform = Parser.transform(
77
- code,
78
- filename: module_id.to_s,
79
- source_kind: source_kind(module_id),
80
- minify: minify_enabled?(context),
81
- jsx_runtime_namespace: jsx_runtime_namespace
82
- )
79
+ transform =
80
+ begin
81
+ Parser.transform(
82
+ code,
83
+ filename: module_id.to_s,
84
+ source_kind: source_kind(module_id),
85
+ minify: minify_enabled?(context),
86
+ jsx_runtime_namespace: jsx_runtime_namespace
87
+ )
88
+ rescue ScriptError => e
89
+ raise ParseError.new(e, source: code, module_id: module_id)
90
+ end
83
91
  javascript_source, imports =
84
92
  if custom_element
85
93
  inject_jsx_runtime(transform.code, transform.imports, module_id, jsx_runtime_namespace)
@@ -103,6 +111,22 @@ module Klenod
103
111
  )
104
112
  end
105
113
 
114
+ # Analysis only needs the module graph: imports are scanned without
115
+ # compiling, and the missing javascript_source makes finalize skip
116
+ # import rewriting and asset emission.
117
+ def analysis_transform(module_id, code, custom_element)
118
+ imports = Parser::FallbackScanner.new(code, module_id.to_s).imports
119
+
120
+ Klenod::Build::TransformResult.new(
121
+ module_source(nil),
122
+ build_dependencies(module_id, imports),
123
+ nil,
124
+ [],
125
+ [],
126
+ {javascript_imports: imports, javascript_custom_element: custom_element}
127
+ )
128
+ end
129
+
106
130
  def finalize(module_id, result, resolved_dependencies, dependency_records, context)
107
131
  source = result.metadata[:javascript_source]
108
132
  original_source = result.metadata[:javascript_original_source]
@@ -90,7 +90,7 @@ module Klenod
90
90
  return nil unless module_id.scheme == SCHEME
91
91
 
92
92
  path = absolute_path(module_id, context)
93
- LoadResult.new(path.read(encoding: "UTF-8"), Hashing.file_hexdigest(path), nil)
93
+ LoadResult.new(path.read(encoding: "UTF-8"), context.analysis? ? Hashing.file_key(path) : Hashing.file_hexdigest(path), nil)
94
94
  end
95
95
 
96
96
  private
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "klenod/build/source_error"
4
+
5
+ module Klenod
6
+ module Build
7
+ module Plugins
8
+ module JavaScriptPlugin
9
+ # The native parser raises a bare SyntaxError, which is a ScriptError
10
+ # rather than a StandardError, so it escapes every `rescue => e` in the
11
+ # build and takes the watcher thread down with it. Wrapping it in a
12
+ # SourceError that carries the source lets it be collected like a Haml
13
+ # parse error and rendered in the browser error dialog.
14
+ class ParseError < Klenod::Build::SourceError
15
+ # "app:/pages/thing.tsx:33:9: Expected '</', got ':'"
16
+ LOCATION = /\A(?<file>.+?):(?<line>\d+):(?<column>\d+):\s*(?<message>.*)\z/m
17
+
18
+ def kind
19
+ "JavaScript parse error"
20
+ end
21
+
22
+ private
23
+
24
+ def location(error)
25
+ found = LOCATION.match(error.message)
26
+ return nil unless found
27
+
28
+ # One-based, matching HamlPlugin::ParseError#line and the column the
29
+ # native parser reports.
30
+ Location.new(
31
+ line: found[:line].to_i,
32
+ column: found[:column].to_i,
33
+ detail: found[:message]
34
+ )
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -4,7 +4,7 @@ module Klenod
4
4
  module Build
5
5
  module Plugins
6
6
  module JavaScriptPlugin
7
- VERSION = "0.0.13"
7
+ VERSION = "0.0.15"
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klenod-plugin-javascript
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrés Alin
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.0.13
18
+ version: 0.0.15
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.0.13
25
+ version: 0.0.15
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rb_sys
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -68,6 +68,7 @@ files:
68
68
  - lib/klenod/build/plugins/node_modules_plugin.rb
69
69
  - lib/klenod/build/plugins/node_modules_plugin/package_exports.rb
70
70
  - lib/klenod/plugin/javascript.rb
71
+ - lib/klenod/plugin/javascript/errors.rb
71
72
  - lib/klenod/plugin/javascript/parser.rb
72
73
  - lib/klenod/plugin/javascript/version.rb
73
74
  homepage: https://github.com/aalin/klenod