Skip to content

Commit d446d2d

Browse files
authored
Build MJS and CJS versions (#189)
1 parent 9d86fd7 commit d446d2d

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

UPGRADE.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ Update your NPM scripts in `package.json`:
102102
}
103103
```
104104

105+
You should also add `"type": "module"` as a top-level key / value pair to your project's `package.json` by running the following command:
106+
107+
```shell
108+
npm pkg set type="module"
109+
```
110+
105111
### Vite compatible imports
106112

107113
Vite only supports ES modules, so if you are upgrading an existing application you will need to replace any `require()` statements with `import`. You may refer to [this pull request](https://github.com/laravel/laravel/pull/5895/files) for an example.
@@ -265,7 +271,37 @@ Then you will need to specify the base URL for assets in your application's entr
265271

266272
### Optional: Configure Tailwind
267273

268-
If you are using Tailwind, perhaps with one of Laravel's starter kits, you will need to create a `postcss.config.js` file. Tailwind can generate this for you automatically:
274+
If you are using Tailwind, perhaps with one of Laravel's starter kits, you will need migrate your `tailwind.config.js` to use [Vite compatible imports](#vite-compatible-imports) and exports.
275+
276+
```diff
277+
- const defaultTheme = require('tailwindcss/defaultTheme');
278+
+ import defaultTheme from 'tailwindcss/defaultTheme';
279+
+ import forms from '@tailwindcss/forms';
280+
281+
/** @type {import('tailwindcss').Config} */
282+
- module.exports = {
283+
+ export default {
284+
content: [
285+
'./vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php',
286+
'./storage/framework/views/*.php',
287+
'./resources/views/**/*.blade.php',
288+
'./resources/js/**/*.vue',
289+
],
290+
291+
theme: {
292+
extend: {
293+
fontFamily: {
294+
sans: ['Figtree', ...defaultTheme.fontFamily.sans],
295+
},
296+
},
297+
},
298+
299+
- plugins: [require('@tailwindcss/forms')],
300+
+ plugins: [forms],
301+
};
302+
```
303+
304+
You will also need to create a `postcss.config.cjs` file. Tailwind can generate this for you automatically:
269305

270306
```shell
271307
npx tailwindcss init -p
@@ -274,7 +310,7 @@ npx tailwindcss init -p
274310
Or, you can create it manually:
275311

276312
```js
277-
module.exports = {
313+
export default {
278314
plugins: {
279315
tailwindcss: {},
280316
autoprefixer: {},

package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,27 @@
1414
},
1515
"license": "MIT",
1616
"author": "Laravel",
17-
"main": "dist/index.js",
18-
"types": "dist/index.d.ts",
17+
"exports": {
18+
".": {
19+
"import": "./dist/index.mjs",
20+
"require": "./dist/index.cjs",
21+
"types": "./dist/index.d.ts"
22+
},
23+
"./inertia-helpers": {
24+
"import": "./inertia-helpers/index.js",
25+
"types": "./inertia-helpers/index.d.ts"
26+
}
27+
},
1928
"files": [
2029
"/dist",
2130
"/inertia-helpers"
2231
],
2332
"scripts": {
2433
"build": "npm run build-plugin && npm run build-inertia-helpers",
25-
"build-plugin": "rm -rf dist && tsc && cp src/dev-server-index.html dist/",
34+
"build-plugin": "rm -rf dist && npm run build-plugin-types && npm run build-plugin-esm && npm run build-plugin-cjs && cp src/dev-server-index.html dist/",
35+
"build-plugin-types": "tsc --emitDeclarationOnly",
36+
"build-plugin-cjs": "esbuild src/index.ts --platform=node --format=cjs --outfile=dist/index.cjs",
37+
"build-plugin-esm": "esbuild src/index.ts --platform=node --format=esm --outfile=dist/index.mjs",
2638
"build-inertia-helpers": "rm -rf inertia-helpers && tsc --project tsconfig.inertia-helpers.json",
2739
"lint": "eslint --ext .ts ./src ./tests",
2840
"test": "vitest run"
@@ -31,6 +43,7 @@
3143
"@types/node": "^18.11.9",
3244
"@typescript-eslint/eslint-plugin": "^5.21.0",
3345
"@typescript-eslint/parser": "^5.21.0",
46+
"esbuild": "0.16.10",
3447
"eslint": "^8.14.0",
3548
"typescript": "^4.6.4",
3649
"vite": "^4.0.0",

tsconfig.inertia-helpers.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "inertia-helpers",
5-
"target": "ES2020",
6-
"module": "ES2020",
4+
"outDir": "inertia-helpers"
75
},
86
"include": [
97
"./src/inertia-helpers/index.ts"

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"compilerOptions": {
33
"outDir": "./dist",
4-
"target": "ES2019",
5-
"module": "commonjs",
4+
"target": "ES2020",
5+
"module": "ES2020",
66
"moduleResolution": "node",
77
"resolveJsonModule": true,
88
"strict": true,

0 commit comments

Comments
 (0)