protibimbok / django-vite-plugin

This plugin configures Vite for use with Django backend.
112 stars 14 forks source link

Allow to have a separate ESM compatible build #62

Closed cpontvieux-systra closed 3 months ago

cpontvieux-systra commented 3 months ago

Based on a blog post of Charles Loder: https://dev.to/charlesloder/publishing-esm-and-commonjs-packages-with-typescript-4e20

This will offer two different folders for a ESM project (type: module) or a legacy CommonJs project..


Tested with the following CommonJS package.json:

{
  "name": "test-cjs",
  "version": "1.0.0",
  "main": "index.js",
  "dependencies": {
    "django-vite-plugin": "./django-vite-plugin-v4.0.1.tgz"
  }
}

and index.js:

const django_vite_plugin = require('django-vite-plugin');
const djangoVitePlugin = django_vite_plugin.djangoVitePlugin;

console.log(djangoVitePlugin)

This produces the correct output with node:

$ node index.js
The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
[AsyncFunction: djangoVitePlugin]

And here with the following ESM package.json:

{
  "name": "test-esm",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "django-vite-plugin": "./django-vite-plugin-v4.0.1.tgz"
  }
}

and index.js:

import { djangoVitePlugin } from 'django-vite-plugin';

console.log(djangoVitePlugin)

This produces the correct output with node:

$ node index.js
[AsyncFunction: djangoVitePlugin]