Skip to content

Commit

Permalink
chore: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Jan 13, 2023
1 parent 02ea502 commit 0b46fea
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 27 deletions.
168 changes: 150 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,164 @@
# ts-node-template
# repro-y18n-default-export-broken

[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg)](https://github.com/RichardLitt/standard-readme)
minimal reproduction of \_ .

> Template project for ts-node
## Summary

## Table of Contents
In the [`package.json`](https://github.com/yargs/y18n/blob/ad1a31b730a5034218b546e8542bb691b84992b2/package.json), the files specified in `module` and `exports` are mismatched.

- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)
- Referring `exports`, `import("y18n")` refers `./index.mjs` of y18n.
- Referring `module`, `import("y18n")` refers `./build/lib/index.js` of y18n.

## Install
```json
{
"exports": {
".": [
{
"import": "./index.mjs",
"require": "./build/index.cjs"
},
"./build/index.cjs"
]
},
"type": "module",
"module": "./build/lib/index.js"
}
```

As a result, some bundler which doesn't support the `exports` field fails to build with yargs as a dependency.

That's because yargs uses the default export of y18n on [`lib/platform-shims/esm.mjs`](https://github.com/yargs/yargs/blob/f37ee6f7da386a1244bf0a0c21b9572f2bb3131b/lib/platform-shims/esm.mjs),
and [`./build/lib/index.js`](https://github.com/yargs/y18n/blob/ad1a31b730a5034218b546e8542bb691b84992b2/lib/index.ts) on y18n doesn't provide the default export.

## Reproduction

```bash
$ yarn
I created the minimum reproduction in https://github.com/tasshi-playground/repro-y18n-default-export-broken .

```shell
$ git clone [email protected]:tasshi-playground/repro-y18n-default-export-broken
$ cd repro-y18n-default-export-broken
$ npm install
$ npm run build
```

## Usage
I examined rollup.js and Vite.
rollup supports `exports` of package.json, and Vite doesn't support due to the bug of https://github.com/vitejs/vite/issues/11676 .

- rollup refers `node_modules/y18n/index.mjs` via `exports`.
- vite refers `node_modules/y18n/build/lib/index.js` via `module`.

<details>
<summary>Log (click to expand)</summary>

### rollup.js tried to build the code `import y18n from "y18n"` (succeeded)

```shell
$ npm run build:rollup:import-default-export-of-y18n

> [email protected] build:rollup:import-default-export-of-y18n
> rollup --config ./rollup.config.default-export.mjs


src/importing-default-export.mjs → dist...
created dist in 49ms

```bash
$ yarn start
```

## Contribute
### rollup.js tried to build the code `import { y18n } from "y18n"` (failed)

PRs accepted.
```shell
$ npm run build:rollup:import-named-export-of-y18n

> [email protected] build:rollup:import-named-export-of-y18n
> rollup --config ./rollup.config.named-export.mjs


src/importing-named-export.mjs → dist...
[!] RollupError: "y18n" is not exported by "node_modules/y18n/index.mjs", imported by "src/importing-named-export.mjs".
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
src/importing-named-export.mjs (1:9)
1: import { y18n } from "y18n";
^
2:
3: console.log(y18n);
at error (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:210:30)
at Module.error (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:13578:16)
at Module.traceVariable (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:13961:29)
at ModuleScope.findVariable (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:12442:39)
at Identifier.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:8371:40)
at CallExpression.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:6165:28)
at CallExpression.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:9888:15)
at ExpressionStatement.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:6169:23)
at Program.bind (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:6165:28)
at Module.bindReferences (/Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/shared/rollup.js:13574:18)
```
### Vite tried to build the code `import y18n from "y18n"` (failed)
```shell
$ npm run build:vite:import-default-export-of-y18n

> [email protected] build:vite:import-default-export-of-y18n
> vite build --config ./vite.config.default-export.mjs

vite v4.0.4 building for production...
✓ 2 modules transformed.
"default" is not exported by "node_modules/y18n/build/lib/index.js", imported by "src/importing-default-export.mjs".
file: /Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/src/importing-default-export.mjs:1:7
1: import y18n from "y18n";
^
2:
3: console.log(y18n);
error during build:
RollupError: "default" is not exported by "node_modules/y18n/build/lib/index.js", imported by "src/importing-default-export.mjs".
at error (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:2041:30)
at Module.error (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:13062:16)
at Module.traceVariable (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:13445:29)
at ModuleScope.findVariable (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:11926:39)
at Identifier.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:7855:40)
at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:5649:28)
at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:9372:15)
at ExpressionStatement.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:5653:23)
at Program.bind (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:5649:28)
at Module.bindReferences (file:///Users/tasshi/git/mshrtsr/repro-y18n-default-export-broken/node_modules/rollup/dist/es/shared/rollup.js:13058:18)
```
### Vite tried to build the code `import { y18n } from "y18n"` (succeeded)
```shell
$ npm run build:vite:import-named-export-of-y18n

> [email protected] build:vite:import-named-export-of-y18n
> vite build --config ./vite.config.named-export.mjs

vite v4.0.4 building for production...
✓ 2 modules transformed.
dist/importing-named-export.vite.cjs 2.84 kB │ gzip: 1.10 kB
```
</details>
## Idea
The file specified in the `module` field should be the same as `exports`.
```diff
{
"exports": {
".": [
{
"import": "./index.mjs",
"require": "./build/index.cjs"
},
"./build/index.cjs"
]
},
"type": "module",
- "module": "./build/lib/index.js",
+ "module": "./index.mjs",
}
```
## License
## Lisence
MIT © mshrtsr
This project is licensed under the [MIT license.](./LICENSE)
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "ts-node-template",
"name": "repro-y18n-default-export-broken",
"version": "1.0.0",
"main": "src/index.ts",
"repository": "https://[email protected]/mshrtsr/ts-node-template.git",
"main": "src/index.js",
"repository": "https://[email protected]/tasshi-playground/repro-y18n-default-export-broken.git",
"author": "Masaharu TASHIRO <[email protected]>",
"license": "MIT",
"private": "true",
Expand All @@ -12,16 +12,16 @@
"scripts": {
"prebuild": "run-s clean",
"build": "run-s -l -c build:rollup:* build:vite:*",
"build:rollup:default-export": "rollup --config ./rollup.config.default-export.mjs",
"build:rollup:named-export": "rollup --config ./rollup.config.named-export.mjs",
"build:vite:default-export": "vite build --config ./vite.config.default-export.mjs",
"build:vite:named-export": "vite build --config ./vite.config.named-export.mjs",
"build:rollup:import-default-export-of-y18n": "rollup --config ./rollup.config.default-export.mjs",
"build:rollup:import-named-export-of-y18n": "rollup --config ./rollup.config.named-export.mjs",
"build:vite:import-default-export-of-y18n": "vite build --config ./vite.config.default-export.mjs",
"build:vite:import-named-export-of-y18n": "vite build --config ./vite.config.named-export.mjs",
"lint": "run-p lint:eslint lint:prettier",
"lint:eslint": "eslint --ext .js,.mjs,.cjs,.ts,.mts,.cts ./",
"lint:prettier": "prettier --check ./**/*.md",
"fix": "run-s fix:eslint fix:prettier",
"fix:eslint": "yarn lint:eslint --fix",
"fix:prettier": "yarn lint:prettier --write",
"fix:eslint": "run-s 'lint:eslint -- --fix'",
"fix:prettier": "run-s 'lint:prettier -- --write'",
"test": "echo 'no test'",
"clean": "rimraf dist"
},
Expand Down

0 comments on commit 0b46fea

Please sign in to comment.