Skip to content

Commit b2a3689

Browse files
committed
feature: Updated the codemod so that v2 addons do not rely on rollup-plugin-ts
1 parent 4e9d33c commit b2a3689

File tree

7 files changed

+105
-46
lines changed

7 files changed

+105
-46
lines changed

src/blueprints/ember-addon/__addonLocation__/.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/blueprints/*/files/
33

44
# compiled output
5+
/declarations/
56
/dist/
67

78
# misc
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The authoritative copies of these live in the monorepo root (because they're
2+
# more useful on github that way), but the build copies them into here so they
3+
# will also appear in published NPM packages.
4+
/LICENSE.md
5+
/README.md
6+
7+
# compiled output
8+
/declarations/
9+
/dist/
10+
11+
# dependencies
12+
/node_modules/

src/blueprints/ember-addon/__addonLocation__/rollup.config.mjs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
<% if (options.packages.addon.hasTypeScript) { %>import typescript from 'rollup-plugin-ts';<% } else { %>import { babel } from '@rollup/plugin-babel';<% } %>
2-
import copy from 'rollup-plugin-copy';
31
import { Addon } from '@embroider/addon-dev/rollup';
2+
import { babel } from '@rollup/plugin-babel';
3+
import { nodeResolve } from '@rollup/plugin-node-resolve';
4+
import copy from 'rollup-plugin-copy';
45

56
const addon = new Addon({
67
srcDir: 'src',
78
destDir: 'dist',
89
});
910

11+
// Add extensions here, such as ts, gjs, etc that you may import
12+
const extensions = ['.js'<% if (options.packages.addon.hasTypeScript) { %>, '.ts'<% } %>];
13+
1014
export default {
1115
// This provides defaults that work well alongside `publicEntrypoints` below.
1216
// You can augment this if you need to.
@@ -27,22 +31,22 @@ export default {
2731
// package names.
2832
addon.dependencies(),
2933

30-
<% if (options.packages.addon.hasTypeScript) { %> // compile TypeScript to latest JavaScript, including Babel transpilation
31-
typescript({
32-
transpiler: 'babel',
33-
browserslist: false,
34-
transpileOnly: false,
35-
}),
36-
<% } else { %> // This babel config should *not* apply presets or compile away ES modules.
34+
// This babel config should *not* apply presets or compile away ES modules.
3735
// It exists only to provide development niceties for you, like automatic
3836
// template colocation.
3937
//
4038
// By default, this will load the actual babel config from the file
4139
// babel.config.json.
4240
babel({
4341
babelHelpers: 'bundled',
42+
extensions,
4443
}),
45-
<% } %>
44+
45+
// Allows rollup to resolve imports of files with the specified extensions
46+
nodeResolve({
47+
extensions,
48+
}),
49+
4650
// Ensure that standalone .hbs files are properly integrated as Javascript.
4751
addon.hbs(),
4852

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
# compiled output
2-
/dist/
2+
dist/
33

44
# dependencies
5-
/node_modules/
5+
node_modules/
66

77
# misc
88
/.env*
9-
/.pnp*
10-
/.eslintcache
11-
/coverage/
9+
/.pnpm-debug.log
1210
/npm-debug.log*
13-
/testem.log
1411
/yarn-error.log
15-
16-
# ember-try
17-
/.node_modules.ember-try/
18-
/npm-shrinkwrap.json.ember-try
19-
/package.json.ember-try
20-
/package-lock.json.ember-try
21-
/yarn.lock.ember-try
22-
23-
# broccoli-debug
24-
/DEBUG/

src/migration/ember-addon/steps/update-addon-package-json.ts

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ function updateDevDependencies(
6262
'@babel/runtime',
6363
'@embroider/addon-dev',
6464
'@rollup/plugin-babel',
65+
'@rollup/plugin-node-resolve',
66+
'concurrently',
6567
'rollup',
6668
'rollup-plugin-copy',
6769
]);
6870

6971
if (packages.addon.hasTypeScript) {
7072
packagesToInstall.add('@babel/preset-typescript');
71-
packagesToInstall.add('rollup-plugin-ts');
72-
packagesToInstall.delete('@rollup/plugin-babel');
7373
}
7474

7575
Array.from(packagesToInstall)
@@ -122,12 +122,15 @@ function updateOtherFields(
122122

123123
if (packages.addon.hasTypeScript) {
124124
packageJson['exports'] = {
125-
'.': './dist/index.js',
125+
'.': {
126+
types: './declarations/index.d.ts',
127+
default: './dist/index.js',
128+
},
126129
'./*': {
127130
/*
128131
This object has an order dependency. The `default` key must appear last.
129132
*/
130-
types: './dist/*.d.ts',
133+
types: './declarations/*.d.ts',
131134
default: './dist/*.js',
132135
},
133136
'./addon-main.js': './addon-main.cjs',
@@ -140,31 +143,69 @@ function updateOtherFields(
140143
};
141144
}
142145

146+
const files = new Set(['addon-main.cjs', 'dist']);
147+
143148
if (hasPublicAssets) {
144-
packageJson['files'] = ['addon-main.cjs', 'dist', 'public'];
145-
} else {
146-
packageJson['files'] = ['addon-main.cjs', 'dist'];
149+
files.add('public');
147150
}
148151

152+
if (packages.addon.hasTypeScript) {
153+
files.add('declarations');
154+
}
155+
156+
packageJson['files'] = Array.from(files).sort();
157+
149158
if (packages.addon.hasTypeScript) {
150159
packageJson['typesVersions'] = {
151160
'*': {
152-
'*': ['dist/*'],
161+
'*': ['declarations/*'],
153162
},
154163
};
155164
}
156165
}
157166

158-
function updateScripts(packageJson: PackageJson): void {
167+
function updateScripts(packageJson: PackageJson, options: Options): void {
168+
const { packages } = options;
169+
159170
const scripts = convertToMap(packageJson['scripts']);
160171

161-
scripts.set('build', 'rollup --config');
162-
scripts.set('prepack', 'rollup --config');
163-
scripts.set('start', 'rollup --config --watch');
164-
scripts.set(
165-
'test',
166-
"echo 'A v2 addon does not have tests, run tests in test-app'",
167-
);
172+
if (packages.addon.hasTypeScript) {
173+
scripts.set('build', 'concurrently "npm:build:*" --names "build:"');
174+
scripts.set('build:js', 'rollup --config');
175+
scripts.set(
176+
'build:types',
177+
packages.addon.hasGlint ? 'glint --declaration' : 'tsc',
178+
);
179+
180+
scripts.set(
181+
'lint:types',
182+
packages.addon.hasGlint
183+
? 'glint'
184+
: 'tsc --emitDeclarationOnly false --noEmit',
185+
);
186+
187+
scripts.set('prepack', 'rollup --config');
188+
189+
scripts.set('start', 'concurrently "npm:start:*" --names "start:"');
190+
scripts.set('start:js', 'rollup --config --watch --no-watch.clearScreen');
191+
scripts.set(
192+
'start:types',
193+
packages.addon.hasGlint ? 'glint --declaration --watch' : 'tsc --watch',
194+
);
195+
196+
scripts.set(
197+
'test',
198+
"echo 'A v2 addon does not have tests, run tests in test-app'",
199+
);
200+
} else {
201+
scripts.set('build', 'rollup --config');
202+
scripts.set('prepack', 'rollup --config');
203+
scripts.set('start', 'rollup --config --watch');
204+
scripts.set(
205+
'test',
206+
"echo 'A v2 addon does not have tests, run tests in test-app'",
207+
);
208+
}
168209

169210
packageJson['scripts'] = convertToObject(scripts);
170211
}
@@ -181,7 +222,7 @@ export function updateAddonPackageJson(
181222

182223
updateDependencies(packageJson, options);
183224
updateDevDependencies(packageJson, options);
184-
updateScripts(packageJson);
225+
updateScripts(packageJson, options);
185226
updateOtherFields(packageJson, context, options);
186227

187228
const destination = join(projectRoot, locations.addon, 'package.json');

src/migration/ember-addon/steps/update-addon-tsconfig-json.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@ import { convertToMap, convertToObject } from '@codemod-utils/json';
66
import type { Options, TsConfigJson } from '../../../types/index.js';
77
import { sanitizeJson } from '../../../utils/json.js';
88

9-
function updateCompilerOptions(tsConfigJson: TsConfigJson): void {
9+
function updateCompilerOptions(
10+
tsConfigJson: TsConfigJson,
11+
options: Options,
12+
): void {
13+
const { packages } = options;
14+
1015
const compilerOptions = convertToMap(tsConfigJson['compilerOptions']);
1116

1217
compilerOptions.delete('baseUrl');
1318
compilerOptions.delete('paths');
1419

20+
if (packages.addon.hasGlint) {
21+
compilerOptions.set('declarationDir', 'declarations');
22+
} else {
23+
compilerOptions.set('declaration', true);
24+
compilerOptions.set('declarationDir', 'declarations');
25+
compilerOptions.set('emitDeclarationOnly', true);
26+
compilerOptions.set('noEmit', false);
27+
}
28+
1529
tsConfigJson['compilerOptions'] = convertToObject(compilerOptions);
1630
}
1731

@@ -30,7 +44,7 @@ export function updateAddonTsConfigJson(options: Options): void {
3044
const oldFile = readFileSync(oldPath, 'utf8');
3145
const tsConfigJson = JSON.parse(sanitizeJson(oldFile));
3246

33-
updateCompilerOptions(tsConfigJson);
47+
updateCompilerOptions(tsConfigJson, options);
3448
updateInclude(tsConfigJson);
3549

3650
const newFile = JSON.stringify(tsConfigJson, null, 2) + '\n';

src/utils/blueprints/get-version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const latestVersions = new Map([
1111
['@embroider/addon-dev', '3.1.1'],
1212
['@embroider/addon-shim', '1.8.5'],
1313
['@rollup/plugin-babel', '6.0.3'],
14+
['@rollup/plugin-node-resolve', '15.1.0'],
1415
['concurrently', '8.2.0'],
1516
['ember-auto-import', '2.6.3'],
1617
['ember-cli-babel', '7.26.11'],
1718
['ember-cli-htmlbars', '6.2.0'],
1819
['rollup', '3.26.0'],
1920
['rollup-plugin-copy', '3.4.0'],
20-
['rollup-plugin-ts', '3.2.0'],
2121
]);
2222

2323
export function getVersion(packageName: string, options: Options): string {

0 commit comments

Comments
 (0)