From 0b46fea25a9c428330516f50c1fa9d0e7474bcbf Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Fri, 13 Jan 2023 13:44:03 +0900 Subject: [PATCH] chore: update README.md --- README.md | 168 +++++++++++++++++++++++++++++++++++++++++++++------ package.json | 18 +++--- 2 files changed, 159 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 7a4e74f..c1534ba 100644 --- a/README.md +++ b/README.md @@ -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 git@github.com: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`. + +
+Log (click to expand) + +### rollup.js tried to build the code `import y18n from "y18n"` (succeeded) + +```shell +$ npm run build:rollup:import-default-export-of-y18n + +> repro-y18n-default-export-broken@1.0.0 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 + +> repro-y18n-default-export-broken@1.0.0 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 + +> repro-y18n-default-export-broken@1.0.0 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 + +> repro-y18n-default-export-broken@1.0.0 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 +``` + +
+ +## 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) diff --git a/package.json b/package.json index b8c99aa..b080462 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "name": "ts-node-template", + "name": "repro-y18n-default-export-broken", "version": "1.0.0", - "main": "src/index.ts", - "repository": "https://mshrtsr@github.com/mshrtsr/ts-node-template.git", + "main": "src/index.js", + "repository": "https://mshrtsr@github.com/tasshi-playground/repro-y18n-default-export-broken.git", "author": "Masaharu TASHIRO ", "license": "MIT", "private": "true", @@ -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" },