【发布时间】:2016-12-01 08:06:50
【问题描述】:
我在我的项目中使用了一些 npm 包。其中两个有错误的main-field。是否可以覆盖它们?
我使用 webpack。我找到了解决方案here。
这适用于主要领域,但我还需要来自同一个包的 css 文件。我在我的index.scss 文件中使用~package/css/style.css 引用它。使用上面的解决方案,它使用path/to/main.js/css/style.css(带main.js)而不是path/to/css/style.css(不带main.js)解析路径。
我可以直接引用它../node_modules/path/to/css/style.css 但我认为那很难看。
那么有没有其他解决方案使用 webpack 或 npm 来覆盖这个主要字段?
-- 编辑--
我使用bootstrap-treeview 作为包。我像这样在index.scss 中提及它
@import '~bootstrap-treeview/src/css/bootstrap-treeview.css';。这行得通。
当我在 webpack 中添加 'bootstrap-treeview': path.join(_path, 'node_modules', 'bootstrap-treeview', 'src', 'js', 'bootstrap-treeview.js') 作为别名时,import 'bootstrap-treeview'; 有效,但 css 无效(如上所述)。
-- 编辑 2--
webpack.conf.js:
resolve: {
extensions: ['', '.js'],
modulesDirectories: ['node_modules'],
alias: {
// bootstrap-treeview alias
'bootstrap-treeview': path.join(_path, 'node_modules', 'bootstrap-treeview', 'src', 'js', 'bootstrap-treeview.js')
}
},
module: {
loaders: [
{
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?sourceMap',
'postcss-loader'
]
},
{
test: /\.(scss|sass)$/,
loader: 'style-loader!css-loader?sourceMap!postcss-loader!sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true
}
]
}
index.scss 见上文。
bootstrap-treeview 别名错误:
Module not found: Error: Cannot resolve 'file' or 'directory' /home/ekf/develop/generator-angular-webpack/node_modules/bootstrap-treeview/src/js/bootstrap-treeview.js/src/css/bootstrap-treeview.css in ...
没有别名的错误:
Module not found: Error: Cannot resolve module 'bootstrap-treeview' in ...
【问题讨论】:
-
你确定你正在使用 Webpack 加载
.scss文件吗?你能把代码贴在你引用index.scss和style.css的地方吗? -
使用
~package/css/style.css它可以工作。一旦我将“别名”添加到 webpack 解析字段,它就会按照描述解析它。 -
在
bower.json中有一个overrides-字段正是为了这个目的。package.json有类似的吗?
标签: npm webpack package.json