[英]Add a custom webpack rule in nuxt.config.js
I am trying to configure nuxt so that webpack loads all *.quirk files using specific loaders but it's like if it ignores the new created rule about *.quirk files.我正在尝试配置 nuxt,以便 webpack 使用特定的加载器加载所有 *.quirk 文件,但就像它忽略了关于 *.quirk 文件的新创建的规则一样。
To simplify the problem I tread .quirk files as CSS files.为了简化问题,我将 .quirk 文件视为 CSS 文件。 So I have a definition of CSS inside this file.
所以我在这个文件中有一个 CSS 定义。
Also because of that simplification I use style-loader and css-loader.也因为这种简化,我使用了 style-loader 和 css-loader。
The quirk file I am trying to load is in ~/assets
folder.我试图加载的怪癖文件在
~/assets
文件夹中。 To make sure webpack includes files from this folder I added it via include:
property of a rule.为了确保 webpack 包含该文件夹中的文件,我通过规则的
include:
属性添加了它。
Please find source codes below.请在下面找到源代码。
// nuxt.config.js
import path from 'path';
export default {
mode: 'universal',
// ...
/*
** Build configuration
*/
build: {
/*
** You can extend webpack config here
*/
extend(config, ctx) {
config.module.rules.push({
test: /\.quirk$/,
include: [path.resolve(__dirname, "./assets")],
use: [
'style-loader',
'css-loader'
]
})
}
}
}
/* test.quirk */
/* I am trying to import this file as a CSS file */
.testouille.other.mine.bonjour.baguette {
color: red !important;
}
<!-- index.vue -->
<!-- This is the default view generated by nuxt -->
<!-- I just added 'testouille' class below to -->
<!-- see a direct result of loaded test.quirk file -->
<template>
<div class="container">
<div>
<logo />
<h1 class="title">
webpackquirk
</h1>
<h2 class="subtitle testouille">
My striking Nuxt.js project
</h2>
<div class="links">
<a
href="https://nuxtjs.org/"
target="_blank"
class="button--green"
>
Documentation
</a>
<a
href="https://github.com/nuxt/nuxt.js"
target="_blank"
class="button--grey"
>
GitHub
</a>
</div>
</div>
</div>
</template>
<!-- ... rest of the file ...-->
When executing npm run dev
I expect the text My striking Nuxt.js project
to turn red since my test.quirk should be loaded as a CSS file but it doesn't work.执行
npm run dev
我希望文本My striking Nuxt.js project
变成红色,因为我的 test.quirk 应该作为 CSS 文件加载,但它不起作用。
If I add the definition of the CSS class .testouille
in the style of the .vue file it works.如果我在 .vue 文件的样式中添加 CSS 类
.testouille
的定义,它会起作用。
I also tried to create a custom loader in which I call a console.log('...')
which would appear in the console and would show that a .quirk file has been found and loaded, but I don't see any message in the console, which means it didn't find any .quirk file.我还尝试创建一个自定义加载器,在其中调用
console.log('...')
,它会出现在控制台中,并显示已找到并加载了 .quirk 文件,但我没有看到任何控制台中的消息,这意味着它没有找到任何 .quirk 文件。
So it's like it ignores my rule.所以就好像它忽略了我的规则。
What am I missing?我错过了什么?
For whoever finds this question now:对于现在发现这个问题的人:
seems with:似乎与:
// Build Configuration: https://go.nuxtjs.dev/config-build
build: {
extend(config) {
config.module.rules.push({
test: /\.weird$/,
loader: "raw-loader"
});
}
}
in nuxt.config.js
& the loader installed you can do:在
nuxt.config.js
和已安装的加载程序中,您可以执行以下操作:
import info from "./raw.weird";
and it works as expected.它按预期工作。 But there are some weird things happening in the config, but I couldn't pinpoint why exactly.
但是配置中发生了一些奇怪的事情,但我无法确定确切原因。 After going in circles, it just start working.
转了一圈后,它才开始工作。 Repository for the reference: https://github.com/marcin-wosinek/webpack-nuxt
参考资料库: https : //github.com/marcin-wosinek/webpack-nuxt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.