Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么生成html文件时出错?_Node.js_Webpack_Pug - Fatal编程技术网

Node.js 为什么生成html文件时出错?

Node.js 为什么生成html文件时出错?,node.js,webpack,pug,Node.js,Webpack,Pug,我试图读取“project/lib/viewer\u lib/templates”目录中的所有子文件夹和文件,但出现错误。如果我试图读取此目录文件夹中具有特定名称的文件,那么我可以这样做。下面是网页代码。我将非常感谢你的帮助。 下面的代码创建了html文件,但是生成的文件不正确,也就是说,它们没有正确地收集在html中,我需要修复它。 项目文件夹结构: /计划 /资产 webpack.config.js B.法律改革委员会 包装 /js app.js /浏览器 /模板 /登录 l

我试图读取“project/lib/viewer\u lib/templates”目录中的所有子文件夹和文件,但出现错误。如果我试图读取此目录文件夹中具有特定名称的文件,那么我可以这样做。下面是网页代码。我将非常感谢你的帮助。 下面的代码创建了html文件,但是生成的文件不正确,也就是说,它们没有正确地收集在html中,我需要修复它。 项目文件夹结构:

  • /计划
    • /资产
      • webpack.config.js
      • B.法律改革委员会
      • 包装
      • /js
        • app.js
    • /浏览器
      • /模板
      • /登录
        • login.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)
});

使用FLASH库,它会将错误传递给视图

我无法用这种方法解决它,但下面的代码解决了我的问题。 我只是想把所有的东西分成不同的函数,但我对此感到困惑

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)));

你能更详细地谈谈吗?我找不到那个图书馆。