为什么生成html文件时出现错误?

时间:2019-06-26 18:46:41

标签: node.js webpack pug

我尝试读取“ project / lib / viewer_lib / templates”目录中的所有子文件夹和文件,但出现错误。如果我尝试在此目录的文件夹中读取一个具有特定名称的文件,则可以这样做。以下是webpack代码。我将非常感谢您的帮助。 以下代码创建了html文件,但生成的文件不正确,即它们未正确收集在html中,我需要对其进行修复。 项目文件夹结构:

  • /项目
    • /资产
      • webpack.config.js
      • .babelrc
      • 包裹
      • / js
        • app.js
    • / viewer_web
      • /模板
      • /登录
        • login.pug
        • connect.pug
      • /主要
        • index.pug
        • users.pug
      • /页
        • page.pug
    • /私人
      • /静态
      • / js
        • app.js
        • app.css
        • index.html

项目

const path = require("path");
const glob = require("glob");
const fs = require("fs");
const async = require("async");

const devMode = process.env.NODE_ENV !== "production";

// styles
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

//templates
const HtmlWebpackPlugin = require("html-webpack-plugin");

const dirpath = "../lib/viewer_web/templates";

const folder = dirpath => {
  const folders = fs.readdirSync(path.resolve(__dirname, dirpath));
  return folders.map(name => `${dirpath}/${name}`);
};

const template = foldpath => {
  return foldpath.map(path_fold => {
    const files = fs.readdirSync(path.resolve(__dirname, path_fold));
    return files;
  });
};

const parse = template => {
  const handel = template.map(array => array.map(item => {
    const parts = item.split(".");
    const name = parts[0];
    const extension = parts[1];
    return {name, extension};
  }));
  return handel;
};

const directories = folder(dirpath);
const files = template(directories);

const rendering = handel => {
  const path_file = directories.map(item => {
    const files = fs.readdirSync(path.resolve(__dirname, item));
    return files.map(name => {
      const templateDir = `${item}`;
      return templateDir;
    })
  })
  return handel.map(arr => arr.map(obj => {
    return path_file.map(arr => arr.map(x => {
      const name = obj.name;
      const ext = obj.extension;
      return new HtmlWebpackPlugin({
        filename: `${name}.html`,
        template: path.resolve(__dirname, `${x}/${name}.${ext}`),
      })
    }));
  }));
}

const handel = parse(files);
const htmlPlugin = rendering(handel);
let a = [];
const f = htmlPlugin.map(x => x.map(y => y.map(z => z.map(t => a.push(t)))));

module.exports = (env, options) => ({
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  entry: {
    "./js/app.js": ["./js/app.js"].concat(glob.sync("./vendor/**/*.js"))
  },
  output: {
    filename: "app.js",
    path: path.resolve(__dirname, "../priv/static/js")
  },
  devServer: {
    port: 3000
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: "file-loader",
            options: { name: "img/[name].[ext]" }
          }
        ]
      },
      {
        test: /\.(sass|scss)$/,
        exclude: /node_modules/,
        include: path.resolve(__dirname, "./scss"),
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: "css-loader",
            options: {
              sourceMap: true
            }
          },
          {
            loader: "resolve-url-loader"
          },
          {
            loader: "sass-loader",
            options: {
              sourceMap: true
            }
          }
        ]
      },
      {
        test: /\.pug$/,
        include: path.resolve(__dirname, '../lib/viewer_web/templates/main'),
        use: ["pug-loader"]
      }
    ]
  },
  plugins: [
    new CopyWebpackPlugin([{ from: "static/", to: "../" }]),
    new MiniCssExtractPlugin({
      filename: "app.css",
      allChunks: true
    })
    ].concat(a)
});

2 个答案:

答案 0 :(得分:0)

我无法以这种方式解决它,但是以下代码解决了我的问题。 我只是想将所有内容都划分为单独的功能,但对此感到困惑。

const dirpath = "../lib/viewer_web/templates";

function generateHtmlPlugins(templateDir) {
  const templateFolds = fs.readdirSync(path.resolve(__dirname, templateDir));
  return templateFolds.map(name_fold => {
    const templates = fs.readdirSync(path.resolve(__dirname, `${templateDir}/${name_fold}`));
    return templates.map(files => {
      const parts = files.split(".");
      const name = parts[0];
      const extension = parts[1];
      return new HtmlWebpackPlugin({
        filename: `${name}.html`,
        template: path.resolve(__dirname, `${templateDir}/${name_fold}/${name}.${extension}`)
      });
    });
  });
}

const htmlPlugin = generateHtmlPlugins(dirpath);
let rendering = [];
htmlPlugin.map(x => x.map(y => rendering.push(y)));

答案 1 :(得分:-1)

使用FLASH库,它将传递错误以进行查看。