Skip to content

Commit

Permalink
fix: improve astro compatibilty
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Aug 6, 2023
1 parent 00b65d5 commit c45a8d9
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
".": {
"types": "./dist/index.d.ts",
"source": "./src/node/index.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion playground/babel-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down
2 changes: 1 addition & 1 deletion playground/custom-dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
".": {
"types": "./lib/src/index.d.ts",
"source": "./src/index.ts",
"require": "./lib/index.cjs",
"import": "./lib/index.js",
"require": "./lib/index.cjs",
"default": "./lib/index.js"
},
"./package.json": "./package.json"
Expand Down
1 change: 1 addition & 0 deletions playground/default-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"exports": {
".": {
"source": "./src/index.js",
"module": "./dist/index.js",
"require": "./dist/index.cjs",
"node": {
"import": "./dist/index.cjs.js"
Expand Down
2 changes: 1 addition & 1 deletion playground/dummy-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"import": "./dist/index.browser.mjs"
},
"module": "./dist/index.mjs",
"require": "./dist/index.js",
"node": {
"import": "./node/index.mjs",
"require": "./node/index.js"
},
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"default": "./dist/index.mjs"
},
Expand Down
2 changes: 2 additions & 0 deletions playground/dummy-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"import": "./dist/index.browser.js"
},
"require": "./dist/index.cjs",
"module": "./dist/index.js",
"node": {
"import": "./node/index.js",
"require": "./node/index.cjs"
Expand All @@ -30,6 +31,7 @@
"import": "./dist/extra.browser.js"
},
"require": "./dist/extra.cjs",
"module": "./dist/extra.js",
"node": {
"import": "./node/extra.js",
"require": "./node/extra.cjs"
Expand Down
57 changes: 49 additions & 8 deletions src/node/core/pkg/loadPkgWithReporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function loadPkgWithReporting(options: {

try {
const pkg = await loadPkg({cwd})
let shouldError = false

// validate exports
if (pkg.exports) {
Expand All @@ -28,33 +29,73 @@ export async function loadPkgWithReporting(options: {
const keys = Object.keys(exp)

if (!assertFirst('types', keys)) {
logger.warn(`exports["${expPath}"]: the \`types\` property should be the first property`)
shouldError = true
logger.error(`exports["${expPath}"]: the \`types\` property should be the first property`)
}

if (!assertOrder('require', 'import', keys)) {
if (!exp.node && !assertOrder('import', 'require', keys)) {
logger.warn(
`exports["${expPath}"]: the \`require\` property should come before the \`import\` property`,
`exports["${expPath}"]: the \`import\` property should come before the \`require\` property`,
)
}

if (!assertOrder('require', 'node', keys)) {
logger.warn(
`exports["${expPath}"]: the \`require\` property should come before the \`node\` property`,
if(exp.require && exp.node?.require && exp.require === exp.node.require) {
shouldError = true
logger.error(`exports["${expPath}"]: the \`node.require\` property isn't necessary as it's identical to \`require\``)
}

if (exp.require && exp.node?.require && !assertOrder('node', 'require', keys)) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node\` property should come before the \`require\` property`,
)
}

if (!assertOrder('node', 'import', keys)) {
logger.warn(
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node\` property should come before \`import\` property`,
)
}

if (!assertOrder('module', 'require', keys)) {
logger.warn(
`exports["${expPath}"]: the \`module\` property should come before \`require\` property`,
)
}

if(exp.node?.import && exp.import && !exp.module) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`module\` property should be added so bundlers don't unintentionally try to bundle \`node.import\``,
)
}

if (!assertOrder('module', 'node', keys)) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`module\` property should come before \`node\` property`,
)
}

if(exp.module && exp.import && exp.node?.import && exp.module !== exp.import) {
shouldError = true
logger.error(`exports["${expPath}"]: the \`module\` property shouold be identical to \`import\``)
}

if (!assertLast('default', keys)) {
logger.warn(`exports["${expPath}"]: the \`default\` property should be the last property`)
shouldError = true
logger.error(
`exports["${expPath}"]: the \`default\` property should be the last property`,
)
}
}
}

if(shouldError) {
process.exit(1)
}

return pkg
} catch (err) {
if (err instanceof ZodError) {
Expand Down
5 changes: 3 additions & 2 deletions src/node/core/pkg/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export interface PackageJSON {
types?: string
browser?: {
source: string
require?: string
import?: string
require?: string
}
module?: string
node?: {
source?: string
require?: string
import?: string
require?: string
}
import?: string
require?: string
Expand Down
34 changes: 31 additions & 3 deletions src/node/core/pkg/validatePkg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {z} from 'zod'

import {PackageJSON} from './types'
import { assertFirst } from './helpers';

const pkgSchema = z.object({
type: z.optional(z.enum(['commonjs', 'module'])),
Expand All @@ -23,23 +24,50 @@ const pkgSchema = z.object({
z.object({
types: z.optional(z.string()),
source: z.string(),
require: z.optional(z.string()),
browser: z.optional(
z.object({
source: z.string(),
require: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
),
module: z.optional(z.string()),
node: z.optional(
z.object({
source: z.optional(z.string()),
require: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
),
import: z.optional(z.string()),
require: z.optional(z.string()),
default: z.string(),
}).superRefine((val, ctx) => {
const keys = Object.keys(val)

if (!assertFirst('types', keys)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:`the \`types\` property should be the first property`,
});
}

// if (val.length > 3) {
// ctx.addIssue({
// code: z.ZodIssueCode.too_big,
// maximum: 3,
// type: "array",
// inclusive: true,
// message: "Too many items 😡",
// });
// }

// if (val.length !== new Set(val).size) {
// ctx.addIssue({
// code: z.ZodIssueCode.custom,
// message: `No duplicates allowed.`,
// });
// }
}),
]),
),
Expand Down

0 comments on commit c45a8d9

Please sign in to comment.