path.resolve(_dirname,filename)__dirname变量

本文介绍如何在Node.js中使用__dirname变量获取当前模块文件的完整绝对路径,并通过示例展示了如何在应用程序中引用和打印模块文件的路径。

在任何模块文件内部,可以使用__dirname变量获取当前模块文件所在目录的完整绝对路径。

在应用程序根目录下新建app.js文件,其中代码如下所示。
var testModule1=require('./test/testModule.js');

在应用程序根目录下新建一个test子目录,在该目录下新建一个testModule.js文件,其中代码如下所示。
console.log(__dirname);

在命令行窗口中输入“node app.js”命令并按下回车键,REPL运行环境中将显示testModule.js文件所在目录的完整绝对路径,执行结果如图3-22所示。

\
未启用CSS Modules的情况下在// 引入 path 模块 const path = require('path') // 判断当前环境是否是生产环境 const isProduction = process.env.NODE_ENV === 'production' // 样式单独分离到一个文件中 const MiniCssExtractPlugin = require('mini-css-extract-plugin') // 官方文档:Vue Loader v15 现在需要配合一个 webpack 插件才能正确使用 const VueLoaderPlugin = require("vue-loader/lib/plugin"); // 引入静态资源复制插件 const CopyWebpackPlugin = require('copy-webpack-plugin') // nodejs核心模块,直接使用 const os = require("os"); // cpu核数 const threads = os.cpus().length; // 引入入口文件 const { entry } = require('./setting.js') const webpack = require("webpack") module.exports = { // 打包入口地址 entry: entry, // 模块resolve的规则 resolve: { //自动的扩展后缀,比如一个js文件,则引用时书写可不要写.js extensions: ['.js', '.json', '.css', '.less', '.vue','.jsx'], // 路径别名 alias: { '@': path.resolve(__dirname, '../src'), 'vue$': 'vue/dist/vue.esm.js', "@ht-form-create/element-ui": path.resolve(__dirname, "../src/libs/form-create/packages/element-ui/src"), "@ht-form-create/utils": path.resolve(__dirname, "../src/libs/form-create/packages/utils"), "@ht-form-create/designer": path.resolve(__dirname, "../src/libs/form-designer/src") }, fallback: { process: require.resolve("process/browser") } }, // context 是 webpack entry 的上下文,是入口文件所处的目录的绝对路径,默认情况下是当前根目录。 // 由于我们 webpack 配置文件放于 build 目录下,所以需要重新设置下 context ,使其指向根目录。 context: path.resolve(__dirname, '../'), // 构建目标 target: ['web', 'es5'], // 不同类型模块的处理规则 module: { rules: [ { oneOf: [ // 处理 css、less 文件 { test: /\.css$/i, use: [ isProduction ? MiniCssExtractPlugin.loader : 'style-loader', { loader: 'css-loader', options: { // 是否使用source-map sourceMap: !isProduction, esModule: false } }, { loader: 'postcss-loader', options: { // 是否使用source-map sourceMap: !isProduction } } ] }, { test: /\.less$/i, use: [ isProduction ? MiniCssExtractPlugin.loader : 'style-loader', { loader: 'css-loader', options: { // 是否使用source-map sourceMap: !isProduction, esModule: false } }, { loader: 'postcss-loader', options: { // 是否使用source-map sourceMap: !isProduction } }, 'less-loader' ] }, // 处理sass资源 { test: /\.s[ca]ss$/, use: [ 'style-loader', 'css-loader', 'sass-loader', ] }, // 解析 html 中的 src 路径 // { // test: /\.html$/, // use: 'html-loader' // }, // 对图片资源文件进行处理,webpack5已经废弃了url-loader,改为type { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, type: 'asset', exclude: [path.resolve(__dirname, 'src/assets/imgs')], generator: { filename: 'imgs/[name].[contenthash][ext]' } }, // 对字体资源文件进行处理,webpack5已经废弃了url-loader,改为type { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, type: 'asset', generator: { filename: 'fonts/[name].[contenthash][ext]' } }, // 对音频资源文件进行处理,webpack5已经废弃了url-loader,改为type { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, type: 'asset', exclude: [path.resolve(__dirname, 'src/assets/medias')], generator: { filename: 'medias/[name].[contenthash][ext]' } }, { test: /\.(m|j)s$/, exclude: /node_modules/, use: [ { loader: 'babel-loader', options: { cacheDirectory: true } }, { loader: "thread-loader", // 开启多进程 options: { workers: threads, // 数量 }, } ] }, { test: /\.(js|jsx)$/, include: [path.resolve(__dirname, "../src"), path.resolve(__dirname, "../src/libs/form-create"), path.resolve(__dirname, "../src/libs/form-designer")], use: [ { loader: "babel-loader", options: { cacheDirectory: true, // 确保包含处理JSX的预设 presets: ["@babel/preset-env", "@vue/babel-preset-jsx"] } }, { loader: "thread-loader", options: { workers: threads } } ] } ] }, // 处理.vue文件 { test: /\.vue$/, use: 'vue-loader' } ] }, watchOptions: { ignored: /node_modules/, }, // 插件 plugins: [ new VueLoaderPlugin(), // 把public的一些静态文件复制到指定位置,排除 html 文件 new CopyWebpackPlugin({ patterns: [ { from: path.resolve(__dirname, '../public'), globOptions: { dot: true, gitignore: true } } ] }), new webpack.DefinePlugin({ "process.env": { NODE_ENV: JSON.stringify(process.env.NODE_ENV || "development") // 可根据需要添加其他环境变量 // API_URL: JSON.stringify('https://api.example.com') } }) ] } 怎么添加
最新发布
08-01
let vueConfigs = { publicPath: process.env.VUE_APP_PUBLIC_PATH ? `/${process.env.VUE_APP_PUBLIC_PATH}/` : '/', transpileDependencies: [ 'resize-detector', 'v-contextmenu', 'vue-runtime-helpers', 'vuex', '@adam/slink', '@adam/base', '@adam/flow', '@adam/micro' ], chainWebpack: webpack => { let cfgs = configs webpack.performance.set('hints', false) if (isSinglePage) { cfgs = { [process.env.VUE_APP_PROJECT]: configs[process.env.VUE_APP_PROJECT] } } if (process.env.use_analyzer) { // 分析 webpack .plugin('webpack-bundle-analyzer') .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin) } webpackConfig(webpack, cfgs) }, configureWebpack: config => { config.resolve.symlinks = false if (process.env.NODE_ENV === 'production') { // 配置webpack 压缩 config.plugins.push( new CompressionWebpackPlugin({ test: /\.js$|\.html$|\.css$/, threshold: 4096 // 超过4kb压缩 }) ) } else { // 设置 source map 的类型 config.devtool = 'cheap-module-source-map' } // 将本地配置文件的环境变量提取出来,供外部修改 config.plugins.push( new ExtraSystemConfigWebpackPlugin({ VUE_APP_AUTHORITY_MODEL: `${process.env.VUE_APP_AUTHORITY_MODEL}`, VUE_APP_CLAZ_JHCC: `${process.env.VUE_APP_CLAZ_JHCC}` }) ) // 乾坤微服务子应用改造设置 config.output.library = `${packageName}-[name]` // 将你的 library 暴露为所有的模块定义下都可运行的方式 config.output.libraryTarget = 'umd' config.output.jsonpFunction = `webpackJsonp_${packageName}` }, // 不需要生产环境的 source map productionSourceMap: false } // 开发环境添加devServer if (process.env.NODE_ENV === 'development') { vueConfigs.publicPath = '/' vueConfigs.devServer = { host: process.env.VUE_APP_HOST, port: process.env.VUE_APP_PORT, index: projectConfig.indexHtml, disableHostCheck: true, hot: true, inline: true, proxy: proxy, // 配置跨域请求头,解决开发环境的跨域问题 headers: { 'Access-Control-Allow-Origin': '*' } } if (process.env.VUE_APP_PORT == '443') { vueConfigs.devServer.https = { key: fs.readFileSync( path.resolve(__dirname, './config/https/' + projectConfig.certFiles['key']) ), cert: fs.readFileSync( path.resolve(__dirname, './config/https/' + projectConfig.certFiles['cert']) ) } } } vueConfigs.pages = pages module.exports = vueConfigs
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值