Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"rules": {
"exclude": [
"no-explicit-any",
"camelcase"
"camelcase",
"no-import-prefix"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run into linter issues with the existing code using inline imports. This rule got added to the default ruleset in Deno 2.5.

My assumption is the project's okay with inline imports, so I decided to exclude the rule instead. Let me know if I should rather refactor inline imports instead.

]
}
},
Expand Down
3 changes: 2 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/package_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Deno.test("single entrypoint", () => {
assertEquals(getPackageJson(props), {
name: "package",
version: "0.1.0",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
types: "./types/mod.d.ts",
Expand Down Expand Up @@ -93,6 +94,7 @@ Deno.test("single entrypoint", () => {
{
name: "package",
version: "0.1.0",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
types: "./types/mod.d.ts",
Expand Down Expand Up @@ -128,6 +130,7 @@ Deno.test("single entrypoint", () => {
{
name: "package",
version: "0.1.0",
type: "module",
main: undefined,
module: "./esm/mod.js",
types: "./types/mod.d.ts",
Expand Down Expand Up @@ -184,6 +187,7 @@ Deno.test("single entrypoint", () => {
{
name: "package",
version: "0.1.0",
type: "module",
main: undefined,
module: "./esm/mod.js",
types: undefined,
Expand Down Expand Up @@ -216,6 +220,7 @@ Deno.test("single entrypoint", () => {
{
name: "package",
version: "0.1.0",
type: "module",
main: undefined,
module: "./esm/mod.js",
types: undefined,
Expand Down Expand Up @@ -324,6 +329,7 @@ Deno.test("multiple entrypoints", () => {
version: "0.1.0",
main: "./script/mod.js",
module: "./esm/mod.js",
type: "module",
types: "./types/mod.d.ts",
dependencies: {
"@deno/shim-deno": "~0.1.0",
Expand Down Expand Up @@ -401,6 +407,7 @@ Deno.test("binary entrypoints", () => {
version: "0.1.0",
main: "./script/mod.js",
module: "./esm/mod.js",
type: "module",
types: "./types/mod.d.ts",
bin: {
my_bin: "./esm/bin.js",
Expand Down Expand Up @@ -501,6 +508,7 @@ Deno.test("peer dependencies", () => {
scripts: {
test: "node test_runner.js",
},
type: "module",
dependencies: {
dep: "^1.0.0",
},
Expand Down
9 changes: 9 additions & 0 deletions lib/package_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export function getPackageJson({
path: e.replace(/\.tsx?$/i, ".js"),
types: e.replace(/\.tsx?$/i, ".d.ts"),
}));
// include "type" field if we are including esm
const type = {
...(includeEsModule
? {
type: "module",
}
: {}),
};
Comment on lines +39 to +45
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Includes "type": "module" if esModule: true, otherwise doesn't add the type field at all.
It also still allows the user to explictly overwrite the type field via the build function's config.

const exports = finalEntryPoints.filter((e) => e.kind === "export");
const binaries = finalEntryPoints.filter((e) => e.kind === "bin");
const dependencies = {
Expand Down Expand Up @@ -111,6 +119,7 @@ export function getPackageJson({
const final: Record<string, unknown> = {
...mainExport,
...binaryExport,
...type,
...packageJsonObj,
scripts: {},
...deleteEmptyKeys({
Expand Down
15 changes: 12 additions & 3 deletions lib/test_runner/get_test_runner_code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ Deno.test("gets code when no shim used", () => {
});
assertEquals(
code,
`const pc = require("picocolors");
`import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const __dirname = new URL(".", import.meta.url).pathname;
const pc = require("picocolors");
const process = require("process");

const filePaths = [
Expand Down Expand Up @@ -60,7 +63,10 @@ Deno.test("gets code when shim used", () => {
});
assertEquals(
code,
`const pc = require("picocolors");
`import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const __dirname = new URL(".", import.meta.url).pathname;
const pc = require("picocolors");
const process = require("process");
const { pathToFileURL } = require("url");
const { testDefinitions } = require("test-shim-package/test-internals");
Expand Down Expand Up @@ -123,7 +129,10 @@ Deno.test("gets code when cjs is not used", () => {
});
assertEquals(
code,
`const pc = require("picocolors");
`import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const __dirname = new URL(".", import.meta.url).pathname;
const pc = require("picocolors");
const process = require("process");

const filePaths = [
Expand Down
10 changes: 10 additions & 0 deletions lib/test_runner/get_test_runner_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export function getTestRunnerCode(options: {
}) {
const usesDenoTest = options.denoTestShimPackageName != null;
const writer = createWriter();

if (options.includeEsModule) {
// ensure compatibility with esm ("type": "module")
writer.writeLine(`import { createRequire } from 'module';`);
writer.writeLine(`const require = createRequire(import.meta.url);`);
writer.writeLine(
`const __dirname = new URL(".", import.meta.url).pathname;`,
);
}
Comment on lines +15 to +22
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered polyfilling the safer way to enable ESM compatibility instead of outputting ES modules directly.


writer.writeLine(`const pc = require("picocolors");`)
.writeLine(`const process = require("process");`);
if (usesDenoTest) {
Expand Down
10 changes: 10 additions & 0 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Deno.test("should build test project - basic", async () => {
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
exports: {
Expand Down Expand Up @@ -211,6 +212,7 @@ Deno.test("should build with all options off", async () => {
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
module: "./esm/mod.js",
exports: {
".": {
Expand Down Expand Up @@ -288,6 +290,7 @@ Deno.test("should build test project with declarations inline by default", async
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
exports: {
Expand Down Expand Up @@ -369,6 +372,7 @@ Deno.test("should build bin project", async () => {
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
bin: {
add: "./esm/mod.js",
},
Expand Down Expand Up @@ -414,6 +418,7 @@ Deno.test("should build bin project with a shebang", async () => {
assertEquals(output.packageJson, {
name: "hello",
version: "1.0.0",
type: "module",
bin: {
hello: "./esm/main.js",
},
Expand Down Expand Up @@ -505,6 +510,7 @@ Deno.test("not error for TLA when not using CommonJS", async () => {
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
module: "./esm/mod.js",
exports: {
".": {
Expand Down Expand Up @@ -621,6 +627,7 @@ Deno.test("should build with package mappings", async () => {
assertEquals(output.packageJson, {
name: "mappings",
version: "1.2.3",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
exports: {
Expand Down Expand Up @@ -690,6 +697,7 @@ Deno.test("should build with peer dependencies in mappings", async () => {
assertEquals(output.packageJson, {
name: "mappings",
version: "1.2.3",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
exports: {
Expand Down Expand Up @@ -1162,6 +1170,7 @@ Deno.test("should build jsr project", async () => {
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
exports: {
Expand Down Expand Up @@ -1232,6 +1241,7 @@ Deno.test("should build workspace project", async () => {
assertEquals(output.packageJson, {
name: "add",
version: "1.0.0",
type: "module",
main: "./script/mod.js",
module: "./esm/mod.js",
exports: {
Expand Down
2 changes: 1 addition & 1 deletion tests/jsr_project/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. MIT license.

import { parse } from "jsr:@std/csv/parse";
import { parse } from "jsr:@std/csv@1.0.6/parse";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a version specifier to fix a linter error. The version used is based on the latest @std/csv version specified in deno.lock.

import { assertEquals } from "jsr:@std/[email protected]/assert-equals";
import * as fs from "node:fs";

Expand Down
Loading