简体   繁体   English

Storybook 6,使用模块路径?

[英]Storybook 6, use module paths?

Is it possible to configure storybook 6 to use the module paths in my tsconfig.json file to work with sass-loader (or just to replicate the same pattern if that's not possible).是否可以将故事书 6 配置为使用我的 tsconfig.json 文件中的模块路径来与sass-loader一起使用(或者如果不可能,则只是复制相同的模式)。

Ideally, I'd like to be able to add a sass loader with this option:理想情况下,我希望能够使用此选项添加一个 sass 加载程序:

additionalData: `
  @use '@themes' as vars;
  @use '@themes/breakpoints' as bp;
`,

instead of代替

additionalData: `
  @use '../themes' as vars;
  @use '../themes/breakpoints' as bp;
`,

My tsconfig.json file has this section in it which works well inside.ts files but obviously doesn't work in sass files:我的tsconfig.json文件中包含此部分,它在 inside.ts 文件中运行良好,但显然在 sass 文件中不起作用:

"paths": {
      "@components/*": ["./components/*"]
    }

If I could replicate that for themes, that'd be amazing.如果我可以为主题复制它,那就太棒了。

Turns out it was pretty easy:事实证明这很容易:

in main.js .main.js Add the following to your module.exports:将以下内容添加到您的 module.exports 中:

webpackFinal: async (config, { configType }) => {
 config.resolve.alias = {
    ...config.resolve.alias,
    "@themes": path.resolve(__dirname, "../themes/default")
  }
  return config;
}

Actually, past Alex, there is a better solution now using the ts-config-paths-webpack-plugin that is automatic and doesn't require repeating config code:实际上,除了 Alex,现在有一个更好的解决方案,使用ts-config-paths-webpack-plugin ,它是自动的,不需要重复配置代码:

// main.js

const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');

const pathsPlugin = new TsconfigPathsPlugin({
  configFile: path.resolve(__dirname, '../tsconfig.json')
})

webpackFinal: async (config) => {
    if (config.resolve.plugins) {
      config.resolve.plugins.push(pathsPlugin);
    } else {
      config.resolve.plugins = [pathsPlugin];
    }
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM