developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.06k stars 361 forks source link

.cjs extension in `main` package.json field causes imported CSS file output to be overwritten #883

Closed lokimckay closed 2 years ago

lokimckay commented 3 years ago

Using a .cjs extension in the main field of package.json (e.g. "main": "./dist/foo.cjs") as per README causes microbundle to swallow any .css or .scss files imported

Reproduction steps

Repo: https://github.com/lokimckay-references/microbundle-883

  1. npm init -y
  2. npm install microbundle --save-dev
  3. add styles.css and index.js files
  4. import "./styles.css" within index.js
  5. copy source/exports/main/module/unpkg fields into package.json as per README
    {
      "name": "foo",                     // your package name
      "type": "module",
      "source": "src/foo.js",            // your source code
      "exports": "./dist/foo.modern.js", // where to generate the modern bundle (see below)
      "main": "./dist/foo.cjs",          // where to generate the CommonJS bundle
      "module": "./dist/foo.module.js",  // where to generate the ESM bundle
      "unpkg": "./dist/foo.umd.js",      // where to generate the UMD bundle (also aliased as "umd:main")
      "scripts": {
        "build": "microbundle",          // compiles "source" to "main"/"module"/"unpkg"
        "dev": "microbundle watch"       // re-build when source files change
      }
    }
  6. update source field to point to index.js
  7. npm run build

Actual output

The emitted file "foo.cjs" overwrites a previously emitted file of the same name.

image

Expected output

Workaround

Change the file extension of the main field from ./dist/foo.cjs -> ./dist/foo.js = achieves expected output

bluelovers commented 2 years ago

image

rschristian commented 2 years ago

Sorry about that, was missing .cjs from the regex. Should be fixed when #919 is merged and released.

leohxj commented 2 years ago

microbundle: 0.14.2 see the same error, does it published ??

rschristian commented 2 years ago

No, that has not yet been published.

leohxj commented 2 years ago

thanks @rschristian , when will publish it? is there a estimate time?

rschristian commented 2 years ago

There's no ETA, no. You can use something like patch-package to copy over the diff from #919 in the meantime.

rschristian commented 2 years ago

@leohxj Just wanted to update you that v0.15 was just published containing this fix.

leohxj commented 2 years ago

@rschristian thanks bro