模块已外部化,以解决在 Vite 构建中的浏览器兼容性错误。

huangapple go评论133阅读模式
英文:

Module has been externalized for browser compatibility error in Vite build

问题

I am currently working on a project that utilizes Vite version 4.3.2 and is integrated with Adobe Experience Manager (AEM) via the aem-vite plugin. The project also includes '@aem-vite/vite-aem-plugin' and '@aem-vite/import-rewriter'.

The issue I'm facing is that the project works fine on the dev server but throws an error when I build for production. The error message I get in the browser console is:

> Module "" has been externalized for browser compatibility. Cannot
> access ".custom" in client code.

The Module "" part doesn't provide clarity on what actual module might be causing this issue.

Here is my entire vite.config.ts file:

// Your TypeScript configuration code

Here is the section of my vite.config.js where I've set up aliases in an attempt to fix this:

// Your JavaScript configuration code with aliases

Despite this, I'm still receiving the same error.

I've already tried upgrading and downgrading the Vite version, but the issue persists. Is there something I'm missing or not understanding here? Any help would be greatly appreciated. Thank you.

英文:

I am currently working on a project that utilizes Vite version 4.3.2 and is integrated with Adobe Experience Manager (AEM) via the aem-vite plugin. The project also includes '@aem-vite/vite-aem-plugin' and '@aem-vite/import-rewriter'.

The issue I'm facing is that the project works fine on the dev server but throws an error when I build for production. The error message I get in the browser console is:

> Module "" has been externalized for browser compatibility. Cannot
> access ".custom" in client code.

The Module "" part doesn't provide clarity on what actual module might be causing this issue.

Here is my entire vite.config.ts file:


    export default defineConfig(({ command, mode }) => ({
      plugins: [
        vue(),
        vueJsx(),
        tsconfigPaths(),
        viteForAem({
          contentPaths: [designsName, 'content'],
          publicPath: clientLibsPath,
        }),
        bundlesImportRewriter({
          publicPath: clientLibsPath,
          resourcesPath: 'resources/js',
        }),
        commonjs({
          include: '/node_modules/',
          requireReturnsDefault: 'auto',
          defaultIsModuleExports: 'auto',
        }),
      ],
      optimizeDeps: {
        include: ['qs', 'dayjs'],
      },
      resolve: {
        alias: {
          '@': fileURLToPath(new URL(clientScriptsPath, import.meta.url)),
          'aem-base': aemBaseClientPath(),
          ...createLibMock('lib/proxyImport', 'proxyImport'),
          ...createLibMock('components/mixins/isMobile', 'isMobile'),
          components: aemBaseClientPath('scripts/components'),
          constants: aemBaseClientPath('scripts/constants'),
          lib: aemBaseClientPath('scripts/lib'),
        },
        dedupe: ['vue'],
      },
      base: command === 'build' ? clientlibsFolderPath : '/',
      root: './',
      build: {
        brotliSize: false,
        manifest: false,
        minify: mode === 'development' ? false : 'esbuild',
        outDir: 'dist',
        sourcemap: command === 'serve' ? 'inline' : false,
        rollupOptions: {
          output: {
            assetFileNames: `${clientlibResourcesPath}/[ext]/[name][extname]`,
            chunkFileNames: `${clientlibResourcesPath}/chunks/[name].[hash].js`,
            entryFileNames: `${clientlibResourcesPath}/js/[name].js`,
          },
          input: {
            bundle: `${clientPath}/scripts/main.ts`,
            styles: `${clientPath}/assets/styles/main.scss`,
          },
        },
      },
      css: {
        preprocessorOptions: {
          scss: {
            additionalData: `$s-base-resource-path: "${aemBaseResourcePath}";$s-selectiontools-resource-path: "${clientLibsPath}";`,
            quietDeps: true,
          },
        },
        loaderOptions: {
          sass: {
            quietDeps: true,
          },
        },
      },
      test: {
        globals: true,
        environment: 'jsdom',
        exclude: [...configDefaults.exclude],
        root: `${clientScriptsPath}/tests`,
        coverage: {
          reporter: ['text', 'json', 'html', 'lcov'],
          reportsDirectory: `${testReportsPath}`,
        },
      },
    }));

Here is the section of my vite.config.js where I've set up aliases in an attempt to fix this:

resolve: {
alias: {
process: "process/browser",
buffer: "buffer",
crypto: "crypto-browserify",
stream: "stream-browserify",
assert: "assert",
http: "stream-http",
https: "https-browserify",
os: "os-browserify",
url: "url",
util: "util",
},

Despite this, I'm still receiving the same error.

I've already tried upgrading and downgrading the Vite version, but the issue persists. Is there something I'm missing or not understanding here? Any help would be greatly appreciated. Thank you.

答案1

得分: 1

你可以通过使用类似于 rollup-plugin-node-polyfills 的 polyfill Node 包,并在别名设置中使用它来避免这些警告:

    resolve: {
        alias: {
            util: "rollup-plugin-node-polyfills/polyfills/util",
            assert: "rollup-plugin-node-polyfills/polyfills/assert",
            os: "rollup-plugin-node-polyfills/polyfills/os",
            buffer: "rollup-plugin-node-polyfills/polyfills/buffer-es6",
            process: "rollup-plugin-node-polyfills/polyfills/process-es6",
            fs: "rollup-plugin-node-polyfills/polyfills/empty",
            net: "rollup-plugin-node-polyfills/polyfills/empty",
            perf_hooks: "rollup-plugin-node-polyfills/polyfills/empty",
            path: "rollup-plugin-node-polyfills/polyfills/path",
            child_process: "rollup-plugin-node-polyfills/polyfills/empty",
        },
    },

对于不存在的 polyfills,或者当你确定导入特定 Node.js 模块的代码在浏览器中永远不会执行时,请使用 "empty" "polyfill"。

英文:

You can avoid these warnings by using a polyfill node package like rollup-plugin-node-polyfills and use it in your alias settings:

    resolve: {
        alias: {
            util: "rollup-plugin-node-polyfills/polyfills/util",
            assert: "rollup-plugin-node-polyfills/polyfills/assert",
            os: "rollup-plugin-node-polyfills/polyfills/os",
            buffer: "rollup-plugin-node-polyfills/polyfills/buffer-es6",
            process: "rollup-plugin-node-polyfills/polyfills/process-es6",
            fs: "rollup-plugin-node-polyfills/polyfills/empty",
            net: "rollup-plugin-node-polyfills/polyfills/empty",
            perf_hooks: "rollup-plugin-node-polyfills/polyfills/empty",
            path: "rollup-plugin-node-polyfills/polyfills/path",
            child_process: "rollup-plugin-node-polyfills/polyfills/empty",
        },
    },

For non-existing polyfills or when you are sure the code that imports a specific Node.js module is never executed in the browser, use the empty "polyfill".

huangapple
  • 本文由 发表于 2023年6月8日 20:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定