admin管理员组文章数量:1122832
I'm using Storybook and Vite to build the components. On prod I'm getting this error:
ReferenceError: require is not defined
So I build the project on local and started serving this using http-server
. Thus, I'm able to re-produce this on local as well.
I tried adding
import commonjs from 'vite-plugin-commonjs';
into the plugins
But it didn't resolve.
Here's my vite.config
import { UserConfig, defineConfig } from 'vite';
import { resolve } from 'path';
import react from '@vitejs/plugin-react';
import commonjs from 'vite-plugin-commonjs';
import { visualizer } from 'rollup-plugin-visualizer';
import svgr from 'vite-plugin-svgr';
import dts from 'vite-plugin-dts';
import { libInjectCss } from 'vite-plugin-lib-inject-css';
import tsconfigPaths from 'vite-tsconfig-paths';
import packageJson from './package.json';
import tsConfig from './tsconfig.json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
// /config/
export default defineConfig((): UserConfig => {
return {
plugins: [
react(),
commonjs(),
tsconfigPaths(),
libInjectCss(),
visualizer({ filename: 'stats/stats.html' }),
svgr(),
dts({
exclude: [
'**/*.stories.tsx',
'**/*.test.tsx',
'**/*.test.ts',
'**/*.stories.ts',
'src/test-utils/**',
'src/mock/**',
'src/setupTests.ts',
'**/*._test.tsx',
],
tsconfigPath: './tsconfig.json',
compilerOptions: {
baseUrl: tsConfigpilerOptions.baseUrl,
paths: tsConfigpilerOptions.paths,
},
}),
],
build: {
copyPublicDir: false,
sourcemap: true,
emptyOutDir: true,
lib: {
entry: resolve(__dirname, 'src/index.ts'),
formats: ['es'],
name: 'te-components',
},
rollupOptions: {
plugins: [
peerDepsExternal({ includeDependencies: true }),
nodeResolve({
mainFields: ['module', 'main', 'browser'],
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
}),
],
external: [
...Object.keys(packageJson.dependencies),
...Object.keys(packageJson.peerDependencies),
'react/jsx-runtime',
'@babel/runtime',
'prop-types',
'lodash',
'@mui/system',
'@mui/material',
'@mui/icons-material',
],
output: {
assetFileNames: 'assets/[name][extname]',
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
preserveModules: true,
preserveModulesRoot: 'src',
},
},
},
optimizeDeps: {
include: ['vite-plugin-commonjs'], // List the packages here
esbuildOptions: {
// Ensure that require statements are transformed for ES modules
loader: {
'.js': 'jsx',
},
},
},
};
});
As the source of error seems to be $i=require("./source.json")
line in one of the preview files generated by storybook, I can't modify it to import
for e.g.
本文标签: javascriptStorybook build using vite ReferenceError require is not definedStack Overflow
版权声明:本文标题:javascript - Storybook build using vite: ReferenceError: require is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283442a1926971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论