Skip to content

Commit

Permalink
feat: add support for npm packages using {"type": "module"}
Browse files Browse the repository at this point in the history
  • Loading branch information
evenchange4 authored Jul 1, 2024
1 parent ad108e8 commit 64ef9e7
Show file tree
Hide file tree
Showing 83 changed files with 543 additions and 240 deletions.
3 changes: 2 additions & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"start": "shuvi start"
},
"dependencies": {
"@shuvi/package-esmodule": "workspace:*",
"shuvi": "workspace:*"
},
"devDependencies": {
"@types/node": "^18.6.1",
"@types/react": "18.0.9",
"@types/react-dom": "18.0.6",
"typescript": "^4.7.4"
"typescript": "^5.5.2"
}
}
8 changes: 8 additions & 0 deletions examples/basic/src/routes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { useEffect } from 'react';
import esModule from '@shuvi/package-esmodule';
import { consoleLog } from '@shuvi/package-esmodule/utils';
import styles from './style.css';

const Home = () => {
useEffect(() => {
esModule(`from '@shuvi/package-esmodule'`);
consoleLog(`Hello from '@shuvi/package-esmodule/utils'`);
}, []);

return (
<div className={styles.hello}>
<p>Hello World</p>
Expand Down
5 changes: 3 additions & 2 deletions examples/basic/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"esModuleInterop": true,
"noImplicitThis": true,
"module": "esnext",
"moduleResolution": "node16",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
Expand All @@ -27,7 +27,8 @@
"@shuvi/runtime/*": [
".shuvi/app/runtime/*"
]
}
},
"incremental": true
},
"include": [
".shuvi/app/shuvi-app.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions examples/package-esmodule/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/esm
/lib
10 changes: 10 additions & 0 deletions examples/package-esmodule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is a simple package that exports an ES module.

https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_package_json_type_field

```json
// package.json
{
"type": "module"
}
```
34 changes: 34 additions & 0 deletions examples/package-esmodule/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"private": true,
"name": "@shuvi/package-esmodule",
"version": "1.0.0",
"type": "module",
"main": "lib/index.js",
"module": "esm/index.js",
"types": "esm/index.d.ts",
"exports": {
".": {
"import": "./esm/index.js",
"require": "./lib/index.js",
"types": "./esm/index.d.ts"
},
"./utils": {
"import": "./esm/utils.js",
"require": "./lib/utils.js",
"types": "./esm/utils.d.ts"
}
},
"files": [
"lib",
"esm"
],
"scripts": {
"dev": "run-p watch:*",
"watch:esm": "tsc -p tsconfig.build.esm.json -w",
"watch:cjs": "tsc -p tsconfig.build.cjs.json -w",
"prebuild": "rimraf lib esm",
"build": "run-p build:*",
"build:esm": "tsc -p tsconfig.build.esm.json",
"build:cjs": "tsc -p tsconfig.build.cjs.json"
}
}
5 changes: 5 additions & 0 deletions examples/package-esmodule/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { consoleLog } from './utils.js';

export default (data?: string) => {
consoleLog(`Hello from package-esmodule ${data}`);
};
3 changes: 3 additions & 0 deletions examples/package-esmodule/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function consoleLog(data: string) {
console.log(data);
}
11 changes: 11 additions & 0 deletions examples/package-esmodule/tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "DOM"],
"outDir": "lib"
},
"include": [
"src"
]
}
11 changes: 11 additions & 0 deletions examples/package-esmodule/tsconfig.build.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.esm.json",
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "DOM"],
"outDir": "esm"
},
"include": [
"src"
]
}
7 changes: 7 additions & 0 deletions examples/package-esmodule/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "DOM"],
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"typedoc-plugin-markdown": "^3.12.1",
"typedoc-plugin-missing-exports": "^0.23.0",
"turbo": "1.3.4",
"typescript": "4.8.4"
"typescript": "5.5.2"
},
"pnpm": {
"peerDependencyRules": {
Expand Down
3 changes: 1 addition & 2 deletions packages/compiler/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"outDir": "lib",
"module": "commonjs"
},
"include": ["src"]
}
4 changes: 2 additions & 2 deletions packages/error-overlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"scripts": {
"dev": "run-p watch:*",
"watch:iframe": "cross-env NODE_ENV=development rollup -c rollup.config.iframe.js -w",
"watch:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib -w",
"watch:cjs": "tsc -p tsconfig.build.cjs.json -w",
"watch:umd": "cross-env NODE_ENV=development rollup -c rollup.config.js -w",
"prebuild": "rimraf lib",
"build": "pnpm run iframe-prod && run-p build:*",
"iframe-prod": "cross-env NODE_ENV=production rollup -c rollup.config.iframe.js",
"build:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib",
"build:cjs": "tsc -p tsconfig.build.cjs.json",
"build:umd": "cross-env NODE_ENV=production rollup -c rollup.config.js"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/error-overlay/rollup.config.iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
}),
resolve({ extensions }),
ts({
tsconfig: path.join('./tsconfig.build.json'),
tsconfig: path.join('./tsconfig.build.rollup.json'),
declaration: false
}),
commonjs({ extensions })
Expand Down
2 changes: 1 addition & 1 deletion packages/error-overlay/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
}),
resolve({ extensions }),
ts({
tsconfig: path.join('./tsconfig.build.json'),
tsconfig: path.join('./tsconfig.build.rollup.json'),
declaration: false
}),
commonjs({ extensions })
Expand Down
10 changes: 10 additions & 0 deletions packages/error-overlay/tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"lib": ["esnext", "dom"],
"esModuleInterop": true,
"jsx": "react-jsx",
"outDir": "lib",
},
"include": ["src"]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"lib": ["esnext", "dom"],
"module": "node16",
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-plugin-shuvi/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"outDir": "lib",
"module": "commonjs",
"esModuleInterop": true
},
"include": ["src"]
Expand Down
8 changes: 4 additions & 4 deletions packages/hook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
},
"scripts": {
"dev": "run-p watch:*",
"watch:esm": "tsc -p tsconfig.build.json -m esnext --outDir esm -w",
"watch:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib -w",
"watch:esm": "tsc -p tsconfig.build.esm.json -w",
"watch:cjs": "tsc -p tsconfig.build.cjs.json -w",
"prebuild": "rimraf lib esm",
"build": "run-p build:*",
"build:esm": "tsc -p tsconfig.build.json -m esnext --outDir esm",
"build:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib"
"build:esm": "tsc -p tsconfig.build.esm.json",
"build:cjs": "tsc -p tsconfig.build.cjs.json"
},
"author": "Zheng Yu Tay"
}
2 changes: 1 addition & 1 deletion packages/hook/src/hookGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const createHookManager = <HM extends HookMap, C = void>(
if (!_hookHandlers[hookName]) {
_hookHandlers[hookName] = [];
}
_hookHandlers[hookName].push(handlers[hookName]);
_hookHandlers[hookName]!.push(handlers[hookName]);
}
});
};
Expand Down
11 changes: 11 additions & 0 deletions packages/hook/tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "DOM"],
"outDir": "lib"
},
"include": [
"src"
]
}
11 changes: 11 additions & 0 deletions packages/hook/tsconfig.build.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.esm.json",
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "DOM"],
"outDir": "esm"
},
"include": [
"src"
]
}
10 changes: 0 additions & 10 deletions packages/hook/tsconfig.build.json

This file was deleted.

3 changes: 1 addition & 2 deletions packages/platform-shared/tsconfig.build.browser.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.esm.json",
"compilerOptions": {
"lib": ["esnext", "DOM"],
"esModuleInterop": false,
"module": "esnext",
"rootDir": "./src",
"outDir": "esm"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/platform-shared/tsconfig.build.node.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"lib": ["esnext", "DOM"],
"esModuleInterop": true,
"module": "commonjs",
"rootDir": "./src",
"outDir": "lib"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/platform-web/tsconfig.build.browser.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.esm.json",
"compilerOptions": {
"lib": ["esnext", "DOM"],
"esModuleInterop": false,
"jsx": "preserve",
"module": "esnext",
"rootDir": "./src",
"outDir": "esm"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/platform-web/tsconfig.build.node.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"lib": ["esnext", "DOM"],
"esModuleInterop": true,
"outDir": "lib",
"module": "commonjs",
"rootDir": "./src"
},
"include": ["src/node"]
Expand Down
4 changes: 2 additions & 2 deletions packages/reporters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"lib"
],
"scripts": {
"dev": "tsc -p tsconfig.build.json -m commonjs -w",
"dev": "tsc -p tsconfig.build.json -w",
"prebuild": "rimraf lib",
"build": "tsc -p tsconfig.build.json -m commonjs"
"build": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@shuvi/shared": "1.0.55",
Expand Down
3 changes: 1 addition & 2 deletions packages/reporters/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"extends": "../../tsconfig.build.json",
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "lib",
"lib": ["esnext", "DOM"],
"module": "commonjs",
"esModuleInterop": true
},
"include": ["src"]
Expand Down
8 changes: 4 additions & 4 deletions packages/router-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
],
"scripts": {
"dev": "run-p watch:*",
"watch:esm": "tsc -p tsconfig.build.json -m esnext --outDir esm -w",
"watch:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib -w",
"watch:esm": "tsc -p tsconfig.build.esm.json -w",
"watch:cjs": "tsc -p tsconfig.build.cjs.json -w",
"prebuild": "rimraf lib esm",
"build": "run-p build:*",
"build:esm": "tsc -p tsconfig.build.json -m esnext --outDir esm",
"build:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir lib"
"build:esm": "tsc -p tsconfig.build.esm.json",
"build:cjs": "tsc -p tsconfig.build.cjs.json"
},
"engines": {
"node": ">= 16.0.0"
Expand Down
11 changes: 11 additions & 0 deletions packages/router-react/tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.cjs.json",
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "DOM"],
"outDir": "lib"
},
"include": [
"src"
]
}
11 changes: 11 additions & 0 deletions packages/router-react/tsconfig.build.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.esm.json",
"compilerOptions": {
"rootDir": "./src",
"lib": ["esnext", "DOM"],
"outDir": "esm"
},
"include": [
"src"
]
}
Loading

0 comments on commit 64ef9e7

Please sign in to comment.