Skip to content

Commit

Permalink
moduleResolution nodenext
Browse files Browse the repository at this point in the history
This updates the monorepo tsconfig to use module: nodenext, moduleResolution: nodenext.

Under this setting, typescript respect the Node's ideas about which files are CJS vs ESM. Most of our modules still compile to CJS, this PR isn't doing that conversion.

This PR does convert the `@embroider/vite` package to build as ESM, because vite itself makes it impossible to consume it's types from a CJS packge, and `@embroider/vite`'s outbound deps aren't too bad. This does mean that when `@embroider/vite` imports from `@embroider/core` it needs to actually treat core as CJS, which is weird-looking but acceptable for now.
  • Loading branch information
ef4 committed Sep 21, 2024
1 parent 2e5e933 commit 0540cad
Show file tree
Hide file tree
Showing 23 changed files with 93 additions and 93 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,5 @@
"unlabeled": ":question: Unlabeled"
},
"wildcardLabel": "unlabeled"
},
"packageManager": "[email protected]+sha256.691fe176eea9a8a80df20e4976f3dfb44a04841ceb885638fe2a26174f81e65e"
}
}
4 changes: 2 additions & 2 deletions packages/addon-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"dependencies": {
"@embroider/core": "workspace:^",
"@rollup/pluginutils": "^4.1.1",
"content-tag": "^2.0.1",
"@rollup/pluginutils": "^5.1.0",
"content-tag": "^2.0.2",
"fs-extra": "^10.0.0",
"minimatch": "^3.0.4",
"rollup-plugin-copy-assets": "^2.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"@types/resolve": "^1.20.0",
"@types/semver": "^7.3.6",
"broccoli-node-api": "^1.7.0",
"code-equality-assertions": "^0.9.0",
"code-equality-assertions": "^1.0.1",
"ember-engines": "^0.8.19",
"scenario-tester": "^3.0.1",
"typescript": "^5.4.5"
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"debug": "^4.3.2",
"escape-string-regexp": "^4.0.0",
"fast-sourcemap-concat": "^2.1.1",
"filesize": "^10.0.7",
"fs-extra": "^9.1.0",
"fs-tree-diff": "^2.0.1",
"handlebars": "^4.7.7",
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/measure-concat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type SourceMapConcat from 'fast-sourcemap-concat';
import { join } from 'path';
import { statSync } from 'fs';
import { filesize } from 'filesize';

export default class MeasureConcat {
stats: { [filename: string]: number } = {};
Expand All @@ -19,7 +18,7 @@ export default class MeasureConcat {
console.log(
Object.entries(this.stats)
.sort((a, b) => b[1] - a[1])
.map(([name, bytes]) => ` ${name}: ${filesize(bytes, {})}`)
.map(([name, bytes]) => ` ${name}: ${bytes} bytes`)
.join('\n')
);
return await this.concat.end();
Expand Down
2 changes: 1 addition & 1 deletion packages/macros/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/resolve": "^1.20.0",
"@types/semver": "^7.3.6",
"babel-plugin-ember-template-compilation": "^2.3.0",
"code-equality-assertions": "^0.9.0",
"code-equality-assertions": "^1.0.1",
"scenario-tester": "^3.0.1",
"typescript": "^5.4.5"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/macros/src/macros-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function gatherAddonCacheKey(project: any): string {
}

const babelCacheBustingPluginPath: string = require.resolve(
'@embroider/shared-internals/src/babel-plugin-cache-busting.js'
'@embroider/shared-internals/src/babel-plugin-cache-busting'
);

export default class MacrosConfig {
Expand Down
3 changes: 1 addition & 2 deletions packages/shared-internals/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"browser": "./src/browser-index.js",
"default": "./src/index.js"
},
"./src/babel-plugin-cache-busting.js": "./src/babel-plugin-cache-busting.js",
"./src/template-colocation-plugin": "./src/template-colocation-plugin.js"
"./src/*": "./src/*.js"
},
"license": "MIT",
"author": "Edward Faulkner",
Expand Down
12 changes: 9 additions & 3 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"name": "@embroider/vite",
"version": "0.2.0",
"main": "index.mjs",
"type": "module",
"exports": {
".": {
"default": "./index.mjs",
"types": "./index.d.ts"
}
},
"peerDependencies": {
"@embroider/core": "workspace:^",
"vite": "^5.2.0"
Expand All @@ -13,9 +19,9 @@
"@babel/core": "^7.22.9",
"@embroider/macros": "workspace:*",
"@embroider/reverse-exports": "workspace:*",
"@rollup/pluginutils": "^4.1.1",
"@rollup/pluginutils": "^5.1.0",
"assert-never": "^1.2.1",
"content-tag": "^2.0.1",
"content-tag": "^2.0.2",
"debug": "^4.3.2",
"esbuild": "^0.17.19",
"fast-glob": "^3.3.2",
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { Resolver } from '@embroider/core';
import { ResolverLoader } from '@embroider/core';
import core from '@embroider/core';
const { ResolverLoader } = core;
import type { Plugin } from 'vite';
import * as process from 'process';
import { join, posix } from 'path';
import { existsSync, readFileSync, lstatSync } from 'fs-extra';
import fs from 'fs-extra';
const { existsSync, readFileSync, lstatSync } = fs;
import send from 'send';
import type { Readable } from 'stream';

Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/content-for.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Plugin } from 'vite';
import { readJSONSync } from 'fs-extra';
import fs from 'fs-extra';
const { readJSONSync } = fs;
import { join } from 'path';
import { locateEmbroiderWorkingDir } from '@embroider/core';
import core from '@embroider/core';
const { locateEmbroiderWorkingDir } = core;

export function contentFor(): Plugin {
return {
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/esbuild-request.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type ModuleRequest, cleanUrl, packageName } from '@embroider/core';
import core from '@embroider/core';
const { cleanUrl, packageName } = core;
import type { ImportKind, OnResolveResult, PluginBuild } from 'esbuild';
import { dirname } from 'path';

import type { PackageCache as _PackageCache, Resolution } from '@embroider/core';
import type { PackageCache as _PackageCache, Resolution, ModuleRequest } from '@embroider/core';
import { externalName } from '@embroider/reverse-exports';

type PublicAPI<T> = { [K in keyof T]: T[K] };
Expand Down
10 changes: 6 additions & 4 deletions packages/vite/src/esbuild-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { Plugin as EsBuildPlugin, OnLoadResult, PluginBuild, ResolveResult } from 'esbuild';
import { transform } from '@babel/core';
import { ResolverLoader, virtualContent, needsSyntheticComponentJS, isInComponents } from '@embroider/core';
import { readFileSync } from 'fs-extra';
import { EsBuildModuleRequest } from './esbuild-request';
import assertNever from 'assert-never';
import core from '@embroider/core';
const { ResolverLoader, virtualContent, needsSyntheticComponentJS, isInComponents } = core;
import fs from 'fs-extra';
const { readFileSync } = fs;
import { EsBuildModuleRequest } from './esbuild-request.js';
import { assertNever } from 'assert-never';
import { hbsToJS } from '@embroider/core';
import { Preprocessor } from 'content-tag';
import { extname } from 'path';
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/optimize-deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { esBuildResolver } from './esbuild-resolver';
import { esBuildResolver } from './esbuild-resolver.js';

export interface OptimizeDeps {
exclude?: string[];
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ModuleRequest, Resolution } from '@embroider/core';
import { cleanUrl, getUrlQueryParams } from '@embroider/core';
import core from '@embroider/core';
const { cleanUrl, getUrlQueryParams } = core;
import type { PluginContext, ResolveIdResult } from 'rollup';

export const virtualPrefix = 'embroider_virtual:';
Expand Down
9 changes: 5 additions & 4 deletions packages/vite/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Plugin, ViteDevServer } from 'vite';
import { virtualContent, ResolverLoader } from '@embroider/core';
import { RollupModuleRequest, virtualPrefix } from './request';
import assertNever from 'assert-never';
import core from '@embroider/core';
const { virtualContent, ResolverLoader } = core;
import { RollupModuleRequest, virtualPrefix } from './request.js';
import { assertNever } from 'assert-never';
import makeDebug from 'debug';
import { resolve } from 'path';
import { writeStatus } from './esbuild-request';
import { writeStatus } from './esbuild-request.js';
import type { PluginContext } from 'rollup';

const debug = makeDebug('embroider:vite');
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Plugin } from 'vite';
import type { EmittedFile } from 'rollup';
import { JSDOM } from 'jsdom';
import { readFileSync, readJSONSync, existsSync } from 'fs-extra';
import fs from 'fs-extra';
const { readFileSync, readJSONSync, existsSync } = fs;
import { dirname, posix, resolve } from 'path';

// This is a type-only import, so it gets compiled away. At runtime, we load
Expand Down Expand Up @@ -93,7 +94,7 @@ class ScriptOptimizer {
terserOpts.sourceMap = { content, url: fileRelativeSourceMapURL };
}
}
let { code: outCode, map: outMap } = await Terser.default.minify(inCode, terserOpts);
let { code: outCode, map: outMap } = await Terser.minify(inCode, terserOpts);
let finalFilename = await this.getFingerprintedFilename(script, outCode!);
let emit: EmittedFile[] = [];
emit.push({
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/tests/optimize-deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { optimizeDeps } from '../src/optimize-deps';
import { optimizeDeps } from '../src/optimize-deps.js';

describe('optimizeDeps', function () {
test('should produce default output when invoked without arguments', function () {
Expand Down
13 changes: 8 additions & 5 deletions packages/webpack/src/ember-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ import { EmbroiderPlugin } from './webpack-resolver-plugin';

const debug = makeDebug('embroider:debug');

// This is a type-only import, so it gets compiled away. At runtime, we load
// terser lazily so it's only loaded for production builds that use it. Don't
// add any non-type-only imports here.
import type { MinifyOptions } from 'terser';
// this function is never called. It exists to workaround typescript being
// obtuse. https://github.com/microsoft/TypeScript/pull/53426
async function loadTerser() {
let Terser = await import('terser');
return Terser.minify;
}
type MinifyOptions = NonNullable<Parameters<Awaited<ReturnType<typeof loadTerser>>>[1]>;

interface AppInfo {
entrypoints: HTMLEntrypoint[];
Expand Down Expand Up @@ -331,7 +334,7 @@ const Webpack: PackagerConstructor<Options> = class Webpack implements Packager
terserOpts.sourceMap = { content, url: fileRelativeSourceMapURL };
}
}
let { code: outCode, map: outMap } = await Terser.default.minify(inCode, terserOpts);
let { code: outCode, map: outMap } = await Terser.minify(inCode, terserOpts);
let finalFilename = this.getFingerprintedFilename(script, outCode!);
outputFileSync(join(this.outputPath, finalFilename), outCode!);
written.add(script);
Expand Down
Loading

0 comments on commit 0540cad

Please sign in to comment.