Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add esm support #3

Closed
wants to merge 3 commits into from
Closed

Conversation

adrianhunter
Copy link

add basic esm support, package.json should be improved further with exports stuff

@msinkec
Copy link
Contributor

msinkec commented Feb 9, 2023

@adrianhunter Can you give an example of an explicit export of a submodule here?

@adrianhunter
Copy link
Author

@msinkec The Solid Start package.json has many explicit exports. Even source files: https://github.com/solidjs/solid-start/blob/main/packages/start/package.json

Here is an example "tsup" config file, which generates .cjs files and modern esm files and also updates all the files as package.json exports:

import { defineConfig } from 'tsup'
import fs from "node:fs/promises"
const entry = ["./src/**/*.ts"]
const outDir = "./dist/"
import { createRequire } from "node:module"
const require = createRequire(import.meta.url)
import type * as Egp from "esbuild-plugin-external-global"
const egp: typeof Egp = require("esbuild-plugin-external-global");

export default defineConfig({
    "dts": true,
    outDir,
    "esbuildOptions": (opts) => {
        opts.allowOverwrite = true
        opts.bundle = false
        opts.outdir = "dist"
        opts.outbase = "src"
        opts.entryNames = "[dir]/[name]"
        opts.plugins = [
            egp.externalGlobalPlugin({
                'global': 'globalThis',
            },)
        ]
    },
    "platform": "browser",
    "entry": entry,
    shims: true,
    format: ["esm", "cjs"],
})

import type * as Pke from "package-json-exports"
const pke: typeof Pke = require("package-json-exports");
const packageJson = JSON.parse(await fs.readFile("package.json", "utf-8"));
packageJson.main = "./dist/index.js";
packageJson.module = "./dist/esm/index.js";
packageJson.types = "./dist/index.d.ts";
packageJson.exports = {
    ".": {
        "import": "./dist/index.js",
        "require": "./dist/index.cjs",
        "types": "./dist/index.d.ts"
    },
    ...await pke.generateExports({
        defaultConditionKey: "import",
        "rule": [
            {
                pattern: `${outDir}/*.js`,
                exports: (matchedFilePath) => {
                    // matchedFilePath
                    const changeFolder = matchedFilePath.replace("./dist/", "./");
                    const modulePath = changeFolder.replace(".js", "")
                    return [
                        [
                            modulePath, "import",
                        ],
                        [
                            modulePath, "require",
                        ],
                        [
                            modulePath, "types",
                        ],
                    ];
                },
            },

        ]
    })
};
await fs.writeFile("package.json", JSON.stringify(packageJson, null, 2));

Its not 100% correct and scrypt-ts transformer part is missing

@xhliu
Copy link
Contributor

xhliu commented Oct 4, 2023

Outdated

@xhliu xhliu closed this Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants