【问题标题】:How to Import Specific Files Inside Node_Modules如何在 Node_Modules 中导入特定文件
【发布时间】:2018-02-18 16:35:32
【问题描述】:

好的,所以我正在为 VueJS 使用 webpack-simple。我安装了一个名为 AdminLTE 的主题。我尝试通过下面的代码导入其中的引导文件。当我运行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 下没有@符号的任何可用文件夹,您可以通过波浪号示例js @import '~admin-lte/dist/css/adminlte.min.css'; 访问它如果在文件夹之前有@符号,那么您可以通过@符号访问它例如js @import '@admin-lte/dist/css/adminlte.min.css';

标签: npm webpack vue.js


【解决方案1】:

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

如果您的 main.js 在 /src 中,则使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-16
    • 2016-08-22
    • 2017-04-29
    • 2019-10-21
    • 2018-05-02
    • 2020-09-19
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多