microenvi icon indicating copy to clipboard operation
microenvi copied to clipboard

Alias does not work for imported module.

Open jakehamilton opened this issue 4 years ago • 2 comments

Hi there! 👋

I'm running into an issue where importing a module, that then imports an aliased dependency, throws an error. I'm not sure if this is an issue with this project or the upstream Microbundle.

Logs

npm run dev
npm run dev     

> [email protected] dev /Users/JHAMILTON/work/micro
> microenvi --alias react=preact-compat,react-dom=preact-compat

microenvi running on: 3000
node-resolve: setting options.module is deprecated, please override options.mainFields instead
node-resolve: setting options.jsnext is deprecated, please override options.mainFields instead
Watching source, compiling to public/scripts:
Error: Could not load preact-compat (imported by /Users/JHAMILTON/work/micro/node_modules/@material-ui/core/esm/styles/useTheme.js): ENOENT: no such file or directory, open 'preact-compat'
    at /Users/JHAMILTON/work/micro/node_modules/rollup/dist/rollup.js:21723:19
    at async Promise.all (index 1)
    at async Promise.all (index 8)
    at async Promise.all (index 0)
    at async Promise.all (index 0)

Reproduction

  1. Install dependencies
npm init --yes

npm install --save-dev microbundle microenvi preact preact-compat @material-ui/core
  1. Update the package.json with a new main and a script to run microenvi.
{
  "main": "public/scripts/bundle.mjs",
  "scripts": {
    "dev": "microenvi --alias react=preact-compat,react-dom=preact-compat"
  }
}
  1. Create html file public/index.html.
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Hello World</title>
        <link
            rel="stylesheet"
            href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
        />
    </head>
    <body>
        <div id="root"></div>
    </body>
</html>

  1. Create js entry file src/index.js.
import { createMuiTheme } from "@material-ui/core/styles";

const theme = createMuiTheme({});
  1. Run Microenvi.
npm run dev

jakehamilton avatar Oct 03 '20 02:10 jakehamilton

Addendum, running npx microbundle build -i src/index.js --alias react=preact-compat,react-dom=preact-compat --no-compress --define process.env.NODE_ENV=production doesn't seem to throw an error. It'd seem the problem lies somewhere on the Microenvi side.

jakehamilton avatar Oct 03 '20 02:10 jakehamilton

Did a little more digging and it looks like this may be an issue with how dependencies are structured. After install, running rm -rf node_modules/microenvi/node_modules makes the build work 🤔

jakehamilton avatar Oct 03 '20 02:10 jakehamilton