使用相对路径为长路径配置 npm&webpack Win10

configure npm&webpack Win10 for long paths using relative paths

当前webpack bundling项目文件夹结构(win10):
root_folder\
|--node_modules
|--src
|--index.js
|--template.html
|--package.json
|--webpack.config.js

index.js的内容:
console.log("Hello webpack");

template.html的内容:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title><%= htmlWebpackPlugin.options.title %></title>
    </head>
    <body>
        <div id="root"></div>
    </body>
    </html>

package.json 的内容:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "dependencies": {},
  "devDependencies": {
    "html-webpack-plugin": "^4.5.0",
    "webpack": "^5.4.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

webpack.config.js的内容:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    entry: {
        main: path.resolve(__dirname, './src/index.js'),
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].bundle.js',
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'webpack Boilerplate',
            template: path.resolve(__dirname, './src/template.html'), // template file
            filename: 'index.html', // output file
        }),
    ]
};

如何使此文件夹完全可移植,即当 运行ning npx webpacknpm run build 时,无论是否使用 C:\root_folder\C:\very\longpath\root_folder
C:\root_folder\ 中成功地 运行 npx webpack 这个例子,然后我复制了 ** root_folder ** 就像它进入 D:\testing\root_folder\ 并且当 运行 ning npx webpack from D:\testing\root_folder\ 它起作用了,这显然表明它是可移植的。
总结:将属于其他项目的 webpack 打包项目的根文件夹存储在自己的项目子文件夹中是有帮助的,因此有时在嵌套文件夹中可以有 root_folder 是很有用的。
问题:是否有一种方法可以使用简单的 npm 脚本甚至 npx 命令来解析 windows 中所有具有本地路径的 root_folder/ 脚本,这样长路径就不会出现 return 错误?
当前答案:很好地找到了将嵌套 root_folder 复制到临时 C:\temp\root_folder 并从那里进行所有 npm webpack 处理和模块捆绑的工作。

所以这里有效的答案是挂载项目目录,然后从那里 运行 构建。

所有必要的是拥有以下 npm 脚本(在 package.json 中):

"scripts": {
    "test": "ECHO \"Error: no test specified\" && EXIT 1",
    "build": "(IF EXIST \"%CD%/dist\" RMDIR dist /S /Q) && webpack",
    "nestingcompliance:build": "SUBST Z: \"%CD%\" && PUSHD Z: && npm run build && POPD && SUBST Z: /D"
  }

然后在 cmd 行中 运行:

npm run nestingcompliance:build