snowpack: [BUG] `rimrafSafe(): …/node_modules/object-assign/index.js` outside of `buildOptions.out` when `optimize.bundle` is on

Bug Report Quick Checklist

  • I am on the latest version of Snowpack & all plugins.
  • I use package manager npm (Fill in: npm, yarn, pnpm, etc).
  • I run Snowpack on OS Mac (Fill in: Windows, Mac, Linux, etc).
  • I run Snowpack on Node.js v14 & v16

Describe the bug

My project depends on single-spa-react. When optimize.bundle in the Snowpack config is turned on, importing this module produces the following during the build:

[17:06:41] [snowpack] rimrafSafe(): /Users/emccarthy/Developer/snowpack-repro/react-snowpack/node_modules/object-assign/index.js outside of buildOptions.out /Users/emccarthy/Developer/snowpack-repro/react-snowpack/build

To Reproduce

  1. npm init snowpack-app --template @snowpack/app-template-react
  2. npm install
  3. npm install single-spa-react
  4. Edit snowpack.config.js to set optimize.bundle to true
  5. Edit index.jsx to add import 'single-spa-react'
  6. npm run build
  7. See error!

Expected behavior

No error. 😄

Anything else?

The single-spa-react package does a require("react") in a top-level try/catch block. I think this might be triggering esbuild’s importing of the module and bypassing Snowpack’s import, evidenced by the fact that the import is pulling from node_modules and not _snowpack/pkg.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 8
  • Comments: 18

Most upvoted comments

I have the same-looking issue when using the framer-motion library (or, more specifically its @emotion/memoize dependency). Node v14.2.0.

Marcos-MBP:new-dir marco$ npm run build

> @ build /Users/marco/src/temp/framer-snowpack-repro/new-dir
> snowpack build

[16:53:07] [snowpack] ! building files...
[16:53:07] [snowpack] ✔ files built. [0.07s]
[16:53:07] [snowpack] ! building dependencies...
[16:53:10] [@snowpack/plugin-typescript] Command completed.
[16:53:10] [snowpack] ✔ dependencies built. [3.09s]
[16:53:10] [snowpack] ! writing to disk...
[16:53:10] [snowpack] ✔ write complete. [0.02s]
[16:53:10] [snowpack] ! optimizing build...
[16:53:10] [snowpack] rimrafSafe(): /Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/@emotion/memoize/dist/memoize.browser.cjs.js outside of buildOptions.out /Users/marco/src/temp/framer-snowpack-repro/new-dir/build
[16:53:10] [snowpack] Error: rimrafSafe(): /Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/@emotion/memoize/dist/memoize.browser.cjs.js outside of buildOptions.out /Users/marco/src/temp/framer-snowpack-repro/new-dir/build
    at Object.deleteFromBuildSafe (/Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/snowpack/lib/index.js:34081:15)
    at Object.runBuiltInOptimize (/Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/snowpack/lib/index.js:152260:24)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Object.optimize$1 [as optimize] (/Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/snowpack/lib/index.js:183387:9)
    at async build (/Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/snowpack/lib/index.js:183419:5)
    at async Object.command (/Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/snowpack/lib/index.js:183436:9)
    at async cli (/Users/marco/src/temp/framer-snowpack-repro/new-dir/node_modules/snowpack/lib/index.js:183748:9)

To Reproduce

  1. npx create-snowpack-app new-dir --template @snowpack/app-template-react-typescript
  2. cd new-dir && npm install framer-motion
  3. Edit snowpack.config.js to set optimize.bundle to true
  4. Edit src/App.tsx and add import { motion } from 'framer-motion'
  5. Edit src/App.tsx and add <motion.div /> on line 19ish to the React Component
  6. npm run build
  7. See error!

Same issue as @adieuadieu with @emotion/memoize - any insights on this?

Any workaround? or potential fix due date? Thank you.

As others have mentioned, I am also using framer-motion and am running into the same error when building for production. The optimize settings in my config file are:

optimize: {
        bundle: true,
        minify: true,
        splitting: true,
        treeshake: true,
        target: "es2018",
    },

@calvinwoo What would the Plugin Options be for @snowpack/plugin-webpack?

You can find the all the options in the plugin docs: https://github.com/withastro/snowpack/tree/main/plugins/plugin-webpack#readme

As an example, I have something simple like this in the Snowpack config:

  plugins: [
    ['@snowpack/plugin-webpack', { sourceMap: true }],
  ]