Skip to content

Commit 5518756

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

File tree

7 files changed

+76
-39
lines changed

7 files changed

+76
-39
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: 44 additions & 12 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,27 +143,56 @@ 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');
172+
if (packages.addon.hasTypeScript) {
173+
scripts.set('build', 'concurrently "npm:build:*"');
174+
scripts.set('build:js', 'rollup --config');
175+
scripts.set(
176+
'build:types',
177+
packages.addon.hasGlint ? 'glint --declaration' : 'tsc --declaration',
178+
);
179+
} else {
180+
scripts.set('build', 'rollup --config');
181+
}
182+
162183
scripts.set('prepack', 'rollup --config');
163-
scripts.set('start', 'rollup --config --watch');
184+
185+
if (packages.addon.hasTypeScript) {
186+
scripts.set('start', 'concurrently "npm:start:*"');
187+
scripts.set('start:js', 'rollup --config --watch');
188+
scripts.set(
189+
'start:types',
190+
packages.addon.hasGlint ? 'glint --watch' : 'tsc --watch',
191+
);
192+
} else {
193+
scripts.set('start', 'rollup --config --watch');
194+
}
195+
164196
scripts.set(
165197
'test',
166198
"echo 'A v2 addon does not have tests, run tests in test-app'",
@@ -181,7 +213,7 @@ export function updateAddonPackageJson(
181213

182214
updateDependencies(packageJson, options);
183215
updateDevDependencies(packageJson, options);
184-
updateScripts(packageJson);
216+
updateScripts(packageJson, options);
185217
updateOtherFields(packageJson, context, options);
186218

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function updateCompilerOptions(tsConfigJson: TsConfigJson): void {
1111

1212
compilerOptions.delete('baseUrl');
1313
compilerOptions.delete('paths');
14+
compilerOptions.set('declarationDir', 'declarations');
1415

1516
tsConfigJson['compilerOptions'] = convertToObject(compilerOptions);
1617
}

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)