如何在Node_Modules中导入特定文件

6

好的,所以我正在使用webpack-simple来构建VueJS项目。我安装了一个名为AdminLTE的主题。我尝试通过以下代码导入其中的bootstrap文件。当我运行npm run build时,应用程序在src文件夹中搜索,但AdminLTE位于node_modules文件夹中。

我应该只导入我需要的文件,还是应该导入整个文件夹?如何正确地导入这些文件?

我的main.js文件

import Vue from 'vue'
import App from './App.vue'

// import BootstrapCSS from 'admin-lte/bootstrap/bootstrap.min.css'
// import BootstrapCSSTheme from 'admin-lte/bootstrap/bootstrap-theme.min.css'
import 'admin-lte/bootstrap/bootstrap.min.css'
import 'admin-lte/bootstrap/bootstrap-theme.min.css'


new Vue({
  el: '#app',
  render: h => h(App)
})

我的Webpack配置

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: './dist/',
    filename: 'build.js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
         test: /\.css$/,
         use: ['style-loader','css-loader']
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

只需在admin lte之前使用波浪号符号,因为在node_modules下有任何文件夹没有@符号,您可以通过波浪号符号访问它,例如@import '~admin-lte/dist/css/adminlte.min.css';如果文件夹之前有@符号,则可以通过@符号访问它,例如@import '@admin-lte/dist/css/adminlte.min.css'; - Sumon Sarker
1个回答

10

您可能需要使用相对路径引用目录。

如果您的 main.js 文件位于 /src 目录中,则应使用以下代码:

import '../node_modules/admin-lte/bootstrap/bootstrap.min.css'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接