js代码输出文件下的资源目录

本文介绍了如何使用Node.js的`fs`和`path`模块创建一个函数,用于递归地遍历指定目录树,并忽略预定义的几个子目录。通过`createDirTree`函数,可以查看如`.git`和`node_modules`等常见隐藏目录的结构。
摘要由CSDN通过智能技术生成

一、代码:

// 该文件名是:outFileUrl.js
const fs = require("fs");
const path = require("path");
const rootDir = path.resolve(__dirname);

const createDirTree = (dir, skip) => {
  const recur = (curDir) => {
    fs.readdirSync(curDir, { withFileTypes: true }).forEach((value) => {
      if (skip.includes(value.name)) return;
      const childPath = path.join(curDir, value.name);
      const depths = childPath.replace(rootDir, '').split(path.sep).filter(Boolean).length;
      const isDirectory = value.isDirectory();
      const prefix = `${'│  '.repeat(depths - 1)}${isDirectory ? '├──' : '├──'}── `;
      console.log(`${prefix}${value.name}`);
      if (isDirectory) {
        recur(path.join(curDir, value.name));
      }
    });
  }
  recur(dir);
}
createDirTree(rootDir, ["node_modules", ".vscode", ".git", ".next", "_next"])

二、文件存放:
在这里插入图片描述

三、运行命令:

node ./outFileUrl

四、运行结果:
在这里插入图片描述

五、git源码仓库:
https://github.com/linjyuan/webpack_code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 打包输出公共 JS 加载文件可以通过配置 `webpack` 实现。 首先,在 `webpack.base.conf.js` 中添加 `CommonsChunkPlugin` 插件,将公共的代码提取到一个 `vendor` chunk 中: ```javascript const webpack = require('webpack') module.exports = { // ...其他配置 plugins: [ // 提取公共代码 new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', minChunks: function (module) { // 任何引用 node_modules 中的模块都被视为外部模块 return ( module.resource && /\.js$/.test(module.resource) && module.resource.indexOf( path.join(__dirname, '../node_modules') ) === 0 ) } }) ] } ``` 然后,在 `webpack.prod.conf.js` 中,使用 `HtmlWebpackPlugin` 插件将 `vendor` chunk 添加到生成的 HTML 文件中: ```javascript const HtmlWebpackPlugin = require('html-webpack-plugin') module.exports = { // ...其他配置 plugins: [ // 生成 HTML 文件,并注入静态资源 new HtmlWebpackPlugin({ filename: config.build.index, template: 'index.html', inject: true, chunksSortMode: 'dependency' }) ] } ``` 其中 `chunksSortMode` 选项指定 chunk 的排序方式,`'dependency'` 表示按依赖关系排序,这样可以确保 `vendor` chunk 在其他 chunk 之前加载。 最后,打包生成的静态资源文件中,将会有一个名为 `vendor.js` 的文件,它包含了所有来自 `node_modules` 目录下的第三方依赖库。在 HTML 文件中添加: ```html <script src="static/js/vendor.js"></script> ``` 即可加载这个公共的 JS 加载文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值