webpack 使用Rollup构建Nx库不会绑定所需的依赖项

pgvzfuti  于 2023-10-19  发布在  Webpack
关注(0)|答案(1)|浏览(168)

提问

我正在使用default Rollup executor构建一个Nx库,稍后将在类似浏览器的环境中运行该库。结果束不能包含import % s或require % s。运行nx run ssr-bundle:build应该会创建一个包含应用程序代码和依赖项的bundle。
如何将所有代码捆绑在一起,使import艾德代码位于同一个文件中?

示例

源文件index.ts

  1. import { isBoolean } from 'lodash';
  2. async function handler() {
  3. return new Promise((resolve) => {
  4. setTimeout(() => {
  5. resolve(isBoolean);
  6. }, 1000);
  7. });
  8. }
  9. export default handler;

输出文件index.cjs.js

  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var lodash = require('lodash'); <--------- this should be the lodash source code
  4. async function handler() {
  5. return new Promise((resolve) => {
  6. setTimeout(() => {
  7. resolve(lodash.isBoolean);
  8. }, 1000);
  9. });
  10. }
  11. exports["default"] = handler;
eivgtgni

eivgtgni1#

汇总:
multi-entry plugin
rollup.config.js

  1. import multi from '@rollup/plugin-multi-entry';
  2. export default {
  3. input: ['batman.js', 'robin.js', 'joker.js'],
  4. output: {
  5. dir: 'output'
  6. },
  7. plugins: [multi()]
  8. };

Webpack:使用webpack并指定单个输出文件。
Webpack.js.js简单示例:

  1. const path = require("path");
  2. module.exports = {
  3. // Specify the entry point of your application
  4. entry: path.resolve(__dirname, "./src/index.tsx"),
  5. // Specify the output path and name
  6. output: {
  7. path: path.resolve(__dirname, "../build/"),
  8. filename: 'bundle.js',
  9. },
  10. };

如果您有多个文件没有导入到入口点内,并且需要多个文件(或块),

  1. module.exports = {
  2. entry: {
  3. app: './src/app.js',
  4. search: './src/search.js',
  5. },
  6. output: {
  7. filename: '[name].js',
  8. path: __dirname + '/dist',
  9. },
  10. };

如果你想要一个文件:

  1. module.exports = {
  2. entry: {
  3. app: './src/app.js',
  4. search: './src/search.js',
  5. },
  6. output: {
  7. filename: 'bundle.js',
  8. path: __dirname + '/dist',
  9. },
  10. };

阅读更多here

展开查看全部

相关问题