4

I'm trying to get svelte material UI working with snowpack. I have installed Snowpack and Snowpacks svelte template like so:

npm install --save-dev snowpack@next
npx create-snowpack-app xpapp --template @snowpack/app-template-svelte

This works, the sample svelte page shows up. Next I followed the Svelte Material UI instructions to "bundle this in your own code" as cited on the Usage chapter in the instructions here: https://github.com/hperrin/svelte-material-ui#usage

So I installed Sass and configured it in my snowpack.config.json file like this:

{
  "extends": "@snowpack/app-scripts-svelte",
  "scripts": { 
    "build:scss": "sass"
  },
  "devOptions": {},
  "installOptions": {}
}

I followed the (very concise) instructions here: https://www.snowpack.dev/#sass

I've also added an empty src/theme/_smui-theme.scss file to my source files as the instructions say, and I installed the nessecary @smui components.

The problem is that I'm currently getting this error when starting the snowpack dev server:

> snowpack dev

Snowpack Dev Server (Beta)
NOTE: Still experimental, default behavior may change.
Starting up...

⠙ snowpack installing... @smui/icon-button, @smui/top-app-bar, svelte/internal
✘ /home/erik/Projects/svelte-xpapp/xpapp/node_modules/@smui/icon-button/_index.scss

Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript)
    at error (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:161:30)
    at Module.error (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:15120:16)
    at tryParse (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:15009:23)
    at Module.setSource (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:15410:30)
    at ModuleLoader.addModuleSource (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:17460:20)
    at async ModuleLoader.fetchModule (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:17521:9)
    at async /home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:17491:36
    at async Promise.all (index 0)
    at async ModuleLoader.fetchModule (/home/erik/Projects/svelte-xpapp/xpapp/node_modules/snowpack/node_modules/rollup/dist/shared/rollup.js:17522:9)
    at async Promise.all (index 0)

It seems that the @import statements in Material UI's _index.scss aren't recognized. I figure Snowpack should interpret/transpile .scss files, but it doesn't seem to be doing that.

Erik Oosterwaal
  • 4,272
  • 3
  • 44
  • 64
  • seems you forgot to install sass on development dependencies `npm i sass -D` – ashmna May 24 '20 at 09:37
  • Nope, Sass is installed. If I had to guess it seems like snowpack is getting the order wrong, like it's first trying to load stuff, before it is compiled with sass. I tried the rollup.js example in the svelte-material-ui repo, and that works. However, I would relly like to use snowpack. – Erik Oosterwaal May 24 '20 at 21:01
  • I'm going through the exact same thing. I was able to get past that error by adding `postcss` to the `installOptions.rollup.plugins` config in `snowpack.conf.js`. However, I'm still running into problems after that. If you figure it out, let me know. – willsters May 28 '20 at 22:22
  • @willsters: I have posted this question also on the snowpack issues page. – Erik Oosterwaal May 31 '20 at 12:46

2 Answers2

5

So I came across the same problem using Svite as well as Snowpack. I was able to use the bare implementation:

// component.svelte <script>
import Button, { Label } from '@smui/button/bare'
import '@smui/button/bare.css'

That's all that's required with Svite.

With Snowpack, I needed to add rollup-plugin-svelte and update snowpack.config.js

// snowpack.config.js
module.exports = {
  // ...
  installOptions: {
    rollup: { plugins: [require('rollup-plugin-svelte')()] }
  },
  // ...
}
ackushiw
  • 1,726
  • 2
  • 14
  • 15
2

I got it working with these install options:

  installOptions: {
    rollup: {
      plugins: [
        require("rollup-plugin-svelte")({
          include: ["./node_modules"],
        }),
        require("rollup-plugin-postcss")({
          use: [
            [
              "sass",
              {
                includePaths: ["./src/theme", "./node_modules"],
              },
            ],
          ],
        }),
      ],
    },
  },

Unfortunately, you'll have to run npx snowpack dev --reload for changes to the theme to take effect. This won't extract css into .css files. I also got an error message with the Dialog component during a production build.

Here is a full example: https://github.com/LeanderG/svelte-smui

Leander Gilles
  • 223
  • 1
  • 2
  • 7
  • 2
    This works for me, except for one little change: since snowpack 3.0 `installOptions` should be `packageOptions`. – Hagen Jan 22 '21 at 18:36