Skip to content

Commit ccb7880

Browse files
committed
wip
1 parent 663f170 commit ccb7880

File tree

8 files changed

+103
-19
lines changed

8 files changed

+103
-19
lines changed

.github/workflows/build_rails.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ jobs:
3737
run: bundle exec rake test
3838
- name: Run acceptance test
3939
run: BUNDLE_GEMFILE='' ruby -Ilib:test test/acceptance/superglue_installation_acceptance.rb
40+
- name: Run acceptance test w/ typescript
41+
run: BUNDLE_GEMFILE='' ruby -Ilib:test test/acceptance/superglue_installation_acceptance.rb --typescript
4042
- name: Upload artifact
4143
if: always()
4244
uses: actions/upload-artifact@v4

lib/generators/superglue/install/install_generator.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def create_files
4343
run "yarn add react react-dom @reduxjs/toolkit react-redux @thoughtbot/superglue@2.0.0-alpha.8"
4444

4545
if use_typescript
46-
run "yarn add -D @types/react-dom @types/react @types/node @thoughtbot/candy_wrapper@0.0.4"
46+
run "yarn add -D @types/react-dom @types/react @types/node @deepkit/type @deepkit/core @deepkit/type-compiler @thoughtbot/candy_wrapper@0.0.4 typescript"
4747
end
4848

4949
say "Superglue is Installed! 🎉", :green
@@ -113,6 +113,12 @@ def copy_ts_files
113113

114114
say "Copying tsconfig.json file to #{app_js_path}"
115115
copy_file "#{__dir__}/templates/ts/tsconfig.json", "tsconfig.json"
116+
117+
say "Copying esbuild plugin for Deepkit"
118+
copy_file "#{__dir__}/templates/esbuild/plugin.js", "deepkit.mjs"
119+
120+
say "Adding build.mjs for TypeScript compilation"
121+
copy_file "#{__dir__}/templates/build.mjs", "build.mjs"
116122
end
117123

118124
def copy_js_files
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as esbuild from 'esbuild'
2+
import deepkitType from './deepkit.mjs'
3+
4+
const isWatch = process.argv.includes('--watch')
5+
6+
const buildOptions = {
7+
entryPoints: [
8+
'app/javascript/application.tsx',
9+
],
10+
bundle: true,
11+
sourcemap: true,
12+
format: 'esm',
13+
outdir: 'app/assets/builds',
14+
publicPath: '/assets',
15+
plugins: process.env.NODE_ENV === 'production' ? [] : [deepkitType()] ,
16+
metafile: true,
17+
conditions: process.env.NODE_ENV === 'production' ? ['production'] : [],
18+
}
19+
20+
if (isWatch) {
21+
const ctx = await esbuild.context(buildOptions)
22+
await ctx.watch()
23+
console.log('Watching for changes...')
24+
} else {
25+
const result = await esbuild.build(buildOptions)
26+
console.log(await esbuild.analyzeMetafile(result.metafile))
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as ts from "typescript";
2+
import { cwd } from "process";
3+
import { declarationTransformer, transformer } from "@deepkit/type-compiler";
4+
import { readFile } from "fs/promises";
5+
6+
export default function deepkitType(options = {}){
7+
return {
8+
name: "Deepkit",
9+
setup(build) {
10+
const transformers = options.transformers || {
11+
before: [transformer],
12+
after: [declarationTransformer],
13+
};
14+
15+
build.onLoad({ filter: /\.tsx?$/ }, async (args) => {
16+
const configFilePath = options.tsConfig || cwd() + "/tsconfig.json";
17+
18+
const code = await readFile(args.path, { encoding: 'utf8' });
19+
const transformed = ts.transpileModule(code, {
20+
compilerOptions: Object.assign(
21+
{
22+
target: ts.ScriptTarget.ESNext,
23+
module: ts.ModuleKind.ESNext,
24+
sourceMap: false,
25+
skipDefaultLibCheck: true,
26+
skipLibCheck: true,
27+
configFilePath,
28+
},
29+
options || {},
30+
),
31+
fileName: args.path,
32+
moduleName: args.namespace,
33+
transformers,
34+
});
35+
36+
return {
37+
contents: transformed.outputText,
38+
loader: args.path.endsWith('.tsx') ? 'tsx' : 'ts'
39+
};
40+
});
41+
},
42+
};
43+
}

lib/generators/superglue/install/templates/ts/page_to_page_mapping.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
// }
2929
// ```
3030
//
31-
const pageIdentifierToPageComponent = {};
31+
const pageIdentifierToPageComponent = {
32+
// Add your mappings here
33+
};
3234

3335
export { pageIdentifierToPageComponent };

lib/generators/superglue/install/templates/ts/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"strictPropertyInitialization": false,
88
"noUnusedLocals": true,
99
"noUnusedParameters": true,
10-
"jsx": "react",
10+
"jsx": "preserve",
1111
"allowJs": false,
1212
"target": "ES2021",
1313
"allowSyntheticDefaultImports": true,
@@ -24,4 +24,5 @@
2424
"app/javascript/**/*",
2525
"app/views/**/*"
2626
],
27+
"reflection": true
2728
}

lib/generators/superglue/view_collection/templates/ts/new.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type ContentProps = {
1717
<%= js_plural_table_name %>Path: string
1818
<%= js_singular_table_name %>Form: FormProps<{
1919
<%- attributes.each do |attr| -%>
20-
<%= attr.column_name.camelize(:lower)%>: <%= Railsjs_component(attr)%>Props
20+
<%= attr.column_name.camelize(:lower)%>: Rails<%= js_component(attr)%>Props
2121
<%- end -%>
2222
submit: RailsSubmitButtonProps
2323
}>

test/acceptance/superglue_installation_acceptance.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
require "capybara/minitest"
44
require "selenium-webdriver"
55
require "git"
6+
require "byebug"
67

78
ROOT_DIR = File.expand_path("../../../", __FILE__)
89
TMP_DIR = File.join(ROOT_DIR, "tmp")
910
SUPERGLUE_RAILS_PATH = ROOT_DIR
10-
SUPERGLUE_SUPERGLUE_PATH = File.join(ROOT_DIR, "superglue/superglue")
11+
SUPERGLUE_SUPERGLUE_PATH = File.join(ROOT_DIR, "../superglue/superglue")
1112
VERSION = File.read(File.expand_path("../../VERSION", __dir__)).strip
1213

1314
SERVER_PORT = "3000"
1415

16+
USE_TYPESCRIPT = ENV["USE_TYPESCRIPT"] == "1"
17+
1518
Minitest.load_plugins
1619

1720
class << Minitest
@@ -63,28 +66,28 @@ def update_package_json
6366
end
6467

6568
def install_superglue
66-
# Dir.chdir(ROOT_DIR) do
67-
# successfully "rm -rf ./superglue"
68-
# Git.clone("https://github.com/thoughtbot/superglue.git")
69-
# end
70-
71-
# Dir.chdir(SUPERGLUE_SUPERGLUE_PATH) do
72-
# successfully "npm install"
73-
# successfully "npm run build"
74-
# successfully "npm pack"
75-
# end
69+
Dir.chdir(ROOT_DIR) do
70+
successfully "rm -rf ./superglue"
71+
Git.clone("https://github.com/thoughtbot/superglue.git", branch: "v2")
72+
end
73+
74+
Dir.chdir(SUPERGLUE_SUPERGLUE_PATH) do
75+
successfully "npm install"
76+
successfully "npm run build"
77+
successfully "npm pack"
78+
end
7679
successfully "echo \"gem 'superglue', path: '#{SUPERGLUE_RAILS_PATH}'\" >> Gemfile"
7780
successfully "bundle install"
7881

7982
FileUtils.rm_f("app/javascript/application.js")
8083

81-
successfully "bundle exec rails generate superglue:install"
82-
# update_package_json
84+
successfully "bundle exec rails generate superglue:install #{"--typescript" if USE_TYPESCRIPT}"
85+
update_package_json
8386
successfully "yarn install --cache-folder /tmp/.junk; rm -rf /tmp/.junk"
8487
end
8588

8689
def add_esbuild_cmd
87-
build_script = "esbuild app/javascript/*.* --bundle --loader:.js=jsx --sourcemap --outdir=app/assets/builds --public-path=assets"
90+
build_script = "node build.mjs"
8891
successfully %(npm pkg set scripts.build="#{build_script}")
8992
end
9093

@@ -99,7 +102,7 @@ def generate_test_app_7(app_name)
99102
end
100103

101104
def generate_scaffold
102-
successfully "bundle exec rails generate superglue:scaffold post body:string --force"
105+
successfully "bundle exec rails generate superglue:scaffold post body:string --force #{"--typescript" if USE_TYPESCRIPT}"
103106
end
104107

105108
def reset_db

0 commit comments

Comments
 (0)