actionview-svelte-handler 0.5.7 → 0.7.0

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.
@@ -7,14 +7,12 @@
7
7
  <%= result.dig(:server, :css) %>
8
8
  </style>
9
9
  <template data-island>
10
- <script type="module">
11
- <%= result.dig(:client) %>
12
-
13
- new App({
14
- target: document.getElementById("hydratee-<%= digest %>"),
15
- props: JSON.parse('<%= locals.to_json %>'),
16
- hydrate: true
17
- });
18
- </script>
10
+ <script type="module"><%= result.dig(:client).strip %>;
11
+ new App({
12
+ target: document.getElementById("hydratee-<%= digest %>"),
13
+ props: JSON.parse('<%= locals.to_json %>'),
14
+ hydrate: true
15
+ });
16
+ </script>
19
17
  </template>
20
18
  </is-land>
@@ -1,3 +1,3 @@
1
1
  module Svelte
2
- VERSION = "0.5.7"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/svelte.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  require "active_support/core_ext"
2
- require "svelte/variabilization"
3
- require "svelte/railtie"
4
- require "svelte/errors"
5
- require "svelte/helpers"
6
- require "svelte/handler"
7
- require "svelte/version"
8
2
  require "active_support/isolated_execution_state"
3
+ require "zeitwerk"
4
+
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.ignore("#{__dir__}/generators")
7
+ loader.ignore("#{__dir__}/actionview-svelte-handler.rb")
8
+
9
+ loader.setup
9
10
 
10
11
  module Svelte
11
12
  include ActionView::Helpers::JavaScriptHelper
@@ -38,3 +39,5 @@ module Svelte
38
39
  nil
39
40
  end
40
41
  end
42
+
43
+ loader.eager_load
data/lib/ts/builder.ts CHANGED
@@ -1,10 +1,16 @@
1
1
  import { readable, Stores } from 'svelte/store'
2
2
  import { importFromStringSync } from 'module-from-string'
3
- import * as esbuild from 'esbuild'
4
- import sveltePlugin from 'esbuild-svelte'
5
3
  import { sveltePreprocess } from 'svelte-preprocess'
6
4
  import type { Warning } from 'svelte/types/compiler/interfaces'
7
5
  import * as recast from 'recast'
6
+ import { rollup } from 'rollup';
7
+ import svelte from "rollup-plugin-svelte"
8
+ import alias from '@rollup/plugin-alias';
9
+ import commonjs from '@rollup/plugin-commonjs';
10
+ import { nodeResolve } from '@rollup/plugin-node-resolve';
11
+ import virtual from '@rollup/plugin-virtual';
12
+ import swc from '@rollup/plugin-swc';
13
+ import path from "node:path"
8
14
 
9
15
  export interface BuildSuccess {
10
16
  client: string
@@ -40,19 +46,24 @@ class Builder {
40
46
  ssr: boolean
41
47
  workingDir: string
42
48
  preprocess: object
49
+ pathAliases?: object
50
+ root: string
43
51
 
44
52
  constructor (
45
53
  path: string,
54
+ root: string,
46
55
  props: object,
47
56
  locals: object,
48
57
  client: string | null,
49
58
  server: string | null,
50
59
  ssr: boolean,
51
60
  workingDir: string,
52
- preprocess: object
61
+ preprocess: object,
62
+ pathAliases: object
53
63
  ) {
54
64
  this.path = path
55
- this.props = readable(props)
65
+ this.root = root
66
+ this.props = readable(Object.assign(props, locals, props))
56
67
  this.locals = locals
57
68
  this.compiled = {
58
69
  client,
@@ -61,55 +72,82 @@ class Builder {
61
72
  this.ssr = ssr
62
73
  this.workingDir = workingDir
63
74
  this.preprocess = preprocess
75
+ this.pathAliases = pathAliases
64
76
  }
65
77
 
66
- async bundle (generate: 'ssr' | 'dom', sveltePath = 'svelte'): Promise<string> {
67
- const bundle = await esbuild.build({
68
- entryPoints: [this.path],
69
- mainFields: ['svelte', 'browser', 'module', 'main'],
70
- conditions: ['svelte', 'browser'],
71
- absWorkingDir: this.workingDir,
72
- write: false,
73
- outfile: 'component.js',
74
- bundle: true,
75
- format: 'esm',
76
- metafile: true,
78
+ async bundle (generate: 'ssr' | 'dom'): Promise<string> {
79
+ const bundle = (await (await rollup({
80
+ input: 'entry',
81
+ output: {
82
+ format: "esm",
83
+ sourcemap: true
84
+ },
85
+ watch: {
86
+ skipWrite: true
87
+ },
77
88
  plugins: [
78
- // @ts-expect-error
79
- sveltePlugin({
89
+ // @ts-expect-error see https://github.com/rollup/plugins/issues/1662
90
+ virtual({
91
+ entry: `import App from '${this.path}'; export default App`
92
+ }),
93
+ // @ts-expect-error see https://github.com/rollup/plugins/issues/1662
94
+ svelte({
80
95
  compilerOptions: {
81
96
  generate,
82
97
  css: 'injected',
83
98
  hydratable: true,
84
- sveltePath
85
99
  },
100
+ emitCss: false,
86
101
  preprocess: sveltePreprocess(this.preprocess),
87
- filterWarnings: (warning: Warning) => {
102
+ onwarn: (warning: Warning, handler: Function) => {
88
103
  if (
89
104
  warning.code === 'missing-declaration' &&
90
105
  warning.message.includes("'props'")
91
- ) {
92
- return false
93
- }
94
- return true
106
+ ) return
107
+
108
+ throw new Error(warning.message);
109
+ }
110
+ }),
111
+ // @ts-expect-error see https://github.com/rollup/plugins/issues/1662
112
+ alias({
113
+ entries: Object.entries(this.pathAliases || {}).map((k, v) => { return new Object({ find: k, replacement: v }) })
114
+ }),
115
+ // @ts-expect-error see https://github.com/rollup/plugins/issues/1662
116
+ commonjs(),
117
+ nodeResolve({
118
+ browser: true,
119
+ exportConditions: ['svelte'],
120
+ extensions: ['.svelte']
121
+ }),
122
+ // @ts-expect-error see https://github.com/rollup/plugins/issues/1662
123
+ swc({
124
+ swc: {
125
+ jsc: {
126
+ target: "es6",
127
+ minify: {
128
+ format: {
129
+ comments: 'all'
130
+ },
131
+ }
132
+ },
133
+ minify: true,
134
+ sourceMaps: true,
135
+ inlineSourcesContent: true
95
136
  }
96
137
  })
97
138
  ]
139
+ })).generate({ format: "esm", sourcemap: true })).output
140
+
141
+ bundle[0].map!.sources = bundle[0].map!.sources.map((el) => {
142
+ return path.relative(this.path, this.root) + "/" + path.relative(this.root, el)
98
143
  })
99
-
100
- // @ts-expect-error
101
- const throwables = [].concat(bundle.errors, bundle.warnings)
102
-
103
- if (throwables.length > 0) {
104
- throw throwables[0] // eslint-disable-line @typescript-eslint/no-throw-literal
105
- }
106
-
107
- return bundle.outputFiles[0].text
144
+
145
+ return `//# sourceMappingURL=${bundle[0].map!.toUrl()}\n//# sourceURL=${path.relative(this.path, this.root) + "/" + path.relative(this.root, this.path) }\n${bundle[0].code}`.trim()
108
146
  }
109
147
 
110
148
  async client (): Promise<string> {
111
149
  return (
112
- this.compiled?.client || (this.standardizeClient(await this.bundle('dom', 'https://esm.sh/svelte'))) // eslint-disable-line
150
+ this.compiled?.client || this.standardizeClient(await this.bundle("dom")) // eslint-disable-line
113
151
  )
114
152
  }
115
153
 
@@ -135,7 +173,7 @@ class Builder {
135
173
  return false
136
174
  }
137
175
  })
138
-
176
+
139
177
  return recast.print(ast).code
140
178
  }
141
179
 
@@ -29,8 +29,10 @@ declare class Builder {
29
29
  ssr: boolean;
30
30
  workingDir: string;
31
31
  preprocess: object;
32
- constructor(path: string, props: object, locals: object, client: string | null, server: string | null, ssr: boolean, workingDir: string, preprocess: object);
33
- bundle(generate: 'ssr' | 'dom', sveltePath?: string): Promise<string>;
32
+ pathAliases?: object;
33
+ root: string;
34
+ constructor(path: string, root: string, props: object, locals: object, client: string | null, server: string | null, ssr: boolean, workingDir: string, preprocess: object, pathAliases: object);
35
+ bundle(generate: 'ssr' | 'dom'): Promise<string>;
34
36
  client(): Promise<string>;
35
37
  standardizeClient(code: string): string;
36
38
  server(): Promise<{