Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnoukhov committed Oct 20, 2023
1 parent e9f6748 commit b06850b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
17 changes: 11 additions & 6 deletions packages/addon-dev/src/rollup-app-reexports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function appReexports(opts: {
to: string;
include: string[];
mapFilename?: (filename: string) => string;
defaultExport?: string;
exports?: (filename: string) => string[] | string | undefined;
}): Plugin {
return {
name: 'app-reexports',
Expand All @@ -17,6 +17,12 @@ export default function appReexports(opts: {
let appJS: Record<string, string> = {};
for (let addonFilename of Object.keys(bundle)) {
let appFilename = opts.mapFilename?.(addonFilename) ?? addonFilename;
let appExports = opts.exports?.(addonFilename) || ['default'];

let computedExports =
typeof appExports === 'string'
? appExports
: `{ ${appExports.join(', ')} }`;

if (
opts.include.some((glob) => minimatch(addonFilename, glob)) &&
Expand All @@ -26,21 +32,20 @@ export default function appReexports(opts: {
this.emitFile({
type: 'asset',
fileName: `_app_/${appFilename}`,
source: `export ${opts.defaultExport} from "${
source: `export ${computedExports} from "${
pkg.name
}/${addonFilename.slice(0, -extname(addonFilename).length)}";\n`,
});
}
}
let originalAppJS = pkg['ember-addon']?.['app-js'];
let newAppJs = Object.assign({}, originalAppJS || {}, appJS);
let hasChanges =
JSON.stringify(originalAppJS) !== JSON.stringify(newAppJs);

let hasChanges = JSON.stringify(originalAppJS) !== JSON.stringify(appJS);

// Don't cause a file i/o event unless something actually changed
if (hasChanges) {
pkg['ember-addon'] = Object.assign({}, pkg['ember-addon'], {
'app-js': newAppJs,
'app-js': appJS,
});
writeJsonSync('package.json', pkg, { spaces: 2 });
}
Expand Down
9 changes: 2 additions & 7 deletions packages/addon-dev/src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { readJsonSync, writeJsonSync } from 'fs-extra';
import { default as hbs } from './rollup-hbs-plugin';
import { default as gjs } from './rollup-gjs-plugin';
import { default as publicEntrypoints } from './rollup-public-entrypoints';
Expand All @@ -17,10 +16,6 @@ export class Addon {
constructor(params: { srcDir?: string; destDir?: string } = {}) {
this.#srcDir = params.srcDir ?? 'src';
this.#destDir = params.destDir ?? 'dist';

let pkg = readJsonSync('package.json');
delete pkg['ember-addon']['app-js'];
writeJsonSync('package.json', pkg, { spaces: 2 });
}

// Given a list of globs describing modules in your srcDir, this generates
Expand All @@ -30,15 +25,15 @@ export class Addon {
patterns: string[],
opts: {
mapFilename?: (fileName: string) => string;
defaultExport?: string;
exports?: (filename: string) => string[] | string | undefined;
} = {}
): Plugin {
return appReexports({
from: this.#srcDir,
to: this.#destDir,
include: patterns,
mapFilename: opts.mapFilename,
defaultExport: opts.defaultExport || '{ default }',
exports: opts.exports,
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"ember-addon": {
"version": 2,
"type": "addon",
"main": "addon-main.js"
"main": "addon-main.js",
"app-js": {}
},
"exports": {
".": "./dist/index.js",
Expand Down
22 changes: 19 additions & 3 deletions tests/scenarios/v2-addon-dev-typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,24 @@ appScenarios
addon.publicEntrypoints([
'components/**/*.js',
'initializers/**/*.js',
'utils/**/*.js',
]),
addon.appReexports([
'components/demo/index.js',
'components/demo/out.js',
'components/demo/namespace-me.js',
'initializers/**/*.js',
'utils/**/*.js',
], {
mapFilename: (name) => reexportMappings[name] || name,
}),
addon.appReexports(['initializers/**/*.js'], {
defaultExport: '{ default, initialize }',
exports: (name) => {
if (name.startsWith('initializers/')) {
return ['default', 'initialize'];
} else if (name.startsWith('utils/')) {
return '*';
}
}
}),
addon.dependencies(),
Expand Down Expand Up @@ -186,6 +193,13 @@ appScenarios
};
`,
},
utils: {
'demo-util.js': `
export function demoUtil() {
return 42;
}
`,
}
},
});

Expand Down Expand Up @@ -270,6 +284,7 @@ appScenarios
'./components/demo/out.js': './dist/_app_/components/demo/out.js',
'./components/demo/namespace/namespace-me.js': './dist/_app_/components/demo/namespace/namespace-me.js',
'./initializers/demo.js': './dist/_app_/initializers/demo.js',
'./utils/demo-util.js': './dist/_app_/utils/demo-util.js',
});
});

Expand All @@ -281,6 +296,7 @@ appScenarios
'./dist/_app_/components/demo/namespace/namespace-me.js':
'export { default } from "v2-addon/components/demo/namespace-me";\n',
'./dist/_app_/initializers/demo.js': 'export { default, initialize } from "v2-addon/initializers/demo";\n',
'./dist/_app_/utils/demo-util.js': 'export * from "v2-addon/utils/demo-util";\n',
};

assert.strictEqual(
Expand Down

0 comments on commit b06850b

Please sign in to comment.