Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scss module not in bundle when importing via ?inline #7552

Closed
7 tasks done
johannes-z opened this issue Mar 31, 2022 · 3 comments · Fixed by #7591
Closed
7 tasks done

Scss module not in bundle when importing via ?inline #7552

johannes-z opened this issue Mar 31, 2022 · 3 comments · Fixed by #7591

Comments

@johannes-z
Copy link

johannes-z commented Mar 31, 2022

Describe the bug

When I import a <file>.module.scss?inline in a file to use its content, the file content is just gone when I run vite build. It#'s there in vite dev however, and I can console.log it.

(for some reason, modules don't work with vite.new - I couldn't get it to work there at all).

source code:

// Label.module.scss
.root {
  color: red;
}
// index.ts
import styles from './Label.module.scss?inline'

console.log(styles)

compiled code:

const root = "ms-Label";
var styles = {
  root
};
console.log(styles);

expected output:

var styles = `.ms-Label {
  color: red;
}`;
console.log(styles);

Reproduction

https://stackblitz.com/edit/vitejs-vite-1vjpzw?file=package.json

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (16) x64 AMD Ryzen 7 PRO 5850U with Radeon Graphics
    Memory: 11.16 GB / 18.62 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
  npmPackages:
    vite: ^2.9.1 => 2.9.1

Used Package Manager

pnpm

Logs

vite:config bundled config file loaded in 142.56ms +0ms
  vite:config using resolved config: {
  vite:config   test: { environment: 'happy-dom' },
  vite:config   optimizeDeps: {
  vite:config     entries: 'src/dev.ts',
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   css: { postcss: { plugins: [] } },
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite-plugin-vue2',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   resolve: {
  vite:config     dedupe: undefined,
  vite:config     alias: [ [Object], [Object], [Object], [Object] ]
  vite:config   },
  vite:config   define: { _APP_VERSION: '"1.0.0"' },
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     port: 3000,
  vite:config     hmr: { protocol: 'ws', host: 'localhost', port: 3000 },
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: false,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { external: [Array], plugins: [] },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: {
  vite:config       entry: '/home/johannes/github/office-fabric/packages/next/src/index.ts',
  vite:config       name: '@fluent-vue/components',
  vite:config       formats: [Array]
  vite:config     },
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   esbuild: { include: /\.ts$/, exclude: /\.(tsx|jsx)$/ },
  vite:config   configFile: '/home/johannes/github/office-fabric/packages/next/vite.config.ts',
  vite:config   configFileDependencies: [
  vite:config     '/home/johannes/github/office-fabric/packages/next/package.json',
  vite:config     '/home/johannes/github/office-fabric/packages/next/vite.config.ts'
  vite:config   ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/home/johannes/github/office-fabric/packages/next',
  vite:config   base: '/',
  vite:config   publicDir: '/home/johannes/github/office-fabric/packages/next/public',
  vite:config   cacheDir: '/home/johannes/github/office-fabric/packages/next/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isWorker: false,
  vite:config   isProduction: true,
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config }

Validations

@johannes-z johannes-z changed the title Scss module disappears after building when importing via ?inline Scss module not in bundle when importing via ?inline Mar 31, 2022
@sapphi-red
Copy link
Member

sapphi-red commented Mar 31, 2022

Reproduction worked by applying the following changes.

  • rename style.scss to style.module.scss
  • comment out css.module.generateScopedName in vite.config.js

https://stackblitz.com/edit/vitejs-vite-6l3rpv?file=src/main.js

Since styles is like below during dev, I think the expected behavior is like this.

._root_115gd_1 {
  color: red;
}
var styles = `._root_115gd_1 {
  color: red;
}`;
console.log(styles);

Also I feel a bit strange to inline css module files. How do you deal with random string suffixed class names without normally importing them?

@johannes-z
Copy link
Author

johannes-z commented Mar 31, 2022

Thanks for fixing it, forgot about renaming the file...

So the reason I want to do inline imports is to keep CSS with the actual component. If I don't do that, all the CSS ends up in a single CSS. When building a component library, that is not desirable, as you would have to import all the styles even if you only use a single component from it. Anyways...

Even if it's a weird use case, this is not expected behavior. The build chain clearly eliminates that chunk, even if it's used.

Regarding random class names: I use generateScopedName so I know what my classes will end up as; alternatively you could have two import: style.module.scss?inline and style.module.scss where the latter gives me all the class names generated. With a plugin you can then remove all the CSS generated for the normal module import (not very clean, but the only way to make this work dynamically).

@sapphi-red
Copy link
Member

Thank you for the explanation.

Even if it's a weird use case, this is not expected behavior.

I was trying say that generateScopedName needs to be configured to get the expected output you shown. I think your expected output is correct.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants