Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat(cssModules): namedExport support #1909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-loader",
"version": "17.4.2",
"version": "17.4.3",
"license": "MIT",
"author": "Evan You",
"repository": "vuejs/vue-loader",
Expand Down
8 changes: 5 additions & 3 deletions 8 src/cssModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ export function genCSSModulesCode(
needsHotReload: boolean
): string {
const styleVar = `style${index}`
let code = `\nimport ${styleVar} from ${request}`
let code = `\nimport * as ${styleVar} from ${request}`

// inject variable
const name = typeof moduleName === 'string' ? moduleName : '$style'
code += `\ncssModules["${name}"] = ${styleVar}`

// omit no default export error
code += `\ncssModules["${name}"] = {...${styleVar}}.default || ${styleVar}`

if (needsHotReload) {
code += `
if (module.hot) {
module.hot.accept(${request}, () => {
cssModules["${name}"] = ${styleVar}
cssModules["${name}"] = {...${styleVar}}.default || ${styleVar}
__VUE_HMR_RUNTIME__.rerender("${id}")
})
}`
Expand Down
15 changes: 11 additions & 4 deletions 15 src/pitcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,16 @@ export const pitch = function () {
? [styleInlineLoaderPath]
: loaders.slice(0, cssLoaderIndex + 1)
const beforeLoaders = loaders.slice(cssLoaderIndex + 1)

const { namedExport = false } = // @ts-ignore
loaders[cssLoaderIndex]?.options?.modules || {}

return genProxyModule(
[...afterLoaders, stylePostLoaderPath, ...beforeLoaders],
context,
!!query.module || query.inline != null,
(query.lang as string) || 'css'
(query.lang as string) || 'css',
namedExport
)
}
}
Expand All @@ -134,15 +139,17 @@ function genProxyModule(
loaders: (Loader | string)[],
context: LoaderContext<VueLoaderOptions>,
exportDefault = true,
lang = 'js'
lang = 'js',
cssNamedExport = false
) {
const request = genRequest(loaders, lang, context)
// return a proxy module which simply re-exports everything from the
// actual request. Note for template blocks the compiled module has no
// default export.
return (
(exportDefault ? `export { default } from ${request}; ` : ``) +
`export * from ${request}`
(exportDefault && !cssNamedExport
? `export { default } from ${request}; `
: ``) + `export * from ${request}`
)
}

Expand Down
5 changes: 2 additions & 3 deletions 5 src/pluginWebpack5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const NormalModule = require('webpack/lib/NormalModule')
const BasicEffectRulePlugin = require('webpack/lib/rules/BasicEffectRulePlugin')
const BasicMatcherRulePlugin = require('webpack/lib/rules/BasicMatcherRulePlugin')
const UseEffectRulePlugin = require('webpack/lib/rules/UseEffectRulePlugin')
const RuleSetCompiler =
require('webpack/lib/rules/RuleSetCompiler') as RuleSetCompiler
const RuleSetCompiler = require('webpack/lib/rules/RuleSetCompiler') as RuleSetCompiler

let objectMatcherRulePlugins = []
try {
Expand Down Expand Up @@ -155,7 +154,7 @@ class VueLoaderPlugin {
// get vue-loader options
const vueLoaderUseIndex = vueUse.findIndex((u) => {
// FIXME: this code logic is incorrect when project paths starts with `vue-loader-something`
return /^vue-loader|(\/|\\|@)vue-loader/.test(u.loader)
return /^vue-loader|^@\S+[\/\\]vue-loader/.test(u.loader)
})

if (vueLoaderUseIndex < 0) {
Expand Down
83 changes: 83 additions & 0 deletions 83 test/style.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,89 @@ test('CSS Modules', async () => {
)
})

test('CSS Modules namedExport', async () => {
const testWithIdent = async (
localIdentName: string | undefined,
regexToMatch: RegExp
) => {
const baseLoaders = [
{
loader: 'style-loader',
options: {
modules: {
namedExport: true,
},
},
},
{
loader: 'css-loader',
options: {
modules: {
localIdentName,
namedExport: true,
},
},
},
]

const { window, instance } = await mockBundleAndRun({
entry: 'css-modules.vue',
modify: (config: any) => {
config!.module!.rules = [
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.css$/,
use: baseLoaders,
},
{
test: /\.stylus$/,
use: [...baseLoaders, 'stylus-loader'],
},
]
},
})

// get local class name
const className = instance.$style.red
expect(className).toMatch(regexToMatch)

// class name in style
let style = [].slice
.call(window.document.querySelectorAll('style'))
.map((style: any) => {
return style!.textContent
})
.join('\n')
style = normalizeNewline(style)
expect(style).toContain('.' + className + ' {\n color: red;\n}')

// animation name
const match = style.match(/@keyframes\s+(\S+)\s+{/)
expect(match).toHaveLength(2)
const animationName = match[1]
expect(animationName).not.toBe('fade')
expect(style).toContain('animation: ' + animationName + ' 1s;')

// default module + pre-processor + scoped
const anotherClassName = instance.$style.red
expect(anotherClassName).toMatch(regexToMatch)
const id = 'data-v-' + genId('css-modules.vue')
expect(style).toContain('.' + anotherClassName + '[' + id + ']')
}

// default ident
await testWithIdent(undefined, /^\w{21,}/)

// custom ident
await testWithIdent(
'[path][name]---[local]---[hash:base64:5]',
/css-modules---red---\w{5}/
)
})

test('CSS Modules Extend', async () => {
const baseLoaders = [
'style-loader',
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.