Skip to content
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

feat: css sourcemap support during dev #7173

Merged
merged 29 commits into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
732c3bc
feat(dev): css sourcemap
sapphi-red Feb 26, 2022
38c0052
feat(dev): sass sourcemap
sapphi-red Feb 27, 2022
32090d9
feat(dev): less sourcemap
sapphi-red Feb 27, 2022
55077e3
feat(dev): stylus sourcemap
sapphi-red Feb 27, 2022
64742be
dx: add css sourcemap debug output
sapphi-red Feb 27, 2022
34f138d
fix(dev): direct css request source map
sapphi-red Feb 28, 2022
31730b2
feat: vue sfc css sourcemap
sapphi-red Mar 2, 2022
91d8bf5
fix: css additional data sourcemap
sapphi-red Mar 2, 2022
13c0bb6
refactor: move genSourceMapUrl function
sapphi-red Mar 4, 2022
3e54315
fix: better approach for windows hack
sapphi-red Mar 4, 2022
215c9e9
test: move sourcemap test
sapphi-red Mar 6, 2022
287cceb
test: css sourcemap
sapphi-red Mar 6, 2022
e041930
test: sass sourcemap
sapphi-red Mar 6, 2022
5201c1f
test: less sourcemap
sapphi-red Mar 6, 2022
6f7569b
test: stylus sourcemap
sapphi-red Mar 6, 2022
f3d4461
test: css additional data sourcemap
sapphi-red Mar 6, 2022
da08d19
test: vue sfc style sourcemap
sapphi-red Mar 6, 2022
15c7f35
chore: merge main branch
sapphi-red Mar 6, 2022
3311d00
fix: rewrite .at(-1)
sapphi-red Mar 6, 2022
add2cd6
fix: linux sourcemap path escape
sapphi-red Mar 6, 2022
5607a24
test: ignore no test for serve only test file
sapphi-red Mar 6, 2022
4ca19b0
test: change toMatchSnapshot to toMatchInlineSnapshot
sapphi-red Mar 6, 2022
0f56319
fix: sourcemap warning with plain css
sapphi-red Mar 6, 2022
7e52db0
test: add sourcemap warning test
sapphi-red Mar 6, 2022
9b02811
chore: merge main branch
sapphi-red Mar 12, 2022
59dff9e
refactor: use type assertions if it is legal
sapphi-red Mar 12, 2022
5b0d47b
refactor: rewrite RawSourceMap type
sapphi-red Mar 13, 2022
8e479d3
refactor: remove unused imports
sapphi-red Mar 13, 2022
a1e03f7
feat: return sourcemap with additionalData
sapphi-red Mar 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/playground/vue/PreProcessors.vue
Expand Up @@ -9,6 +9,12 @@ p.pug-less
p.pug-stylus
| This is rendered from <template lang="pug">
| and styled with <style lang="stylus">. It should be orange.
p.pug-sass-import
| This is rendered from <template lang="pug">
| and styled with <style lang="sass"> which has import. It should be blue.
p.pug-sass-non-import
| This is rendered from <template lang="pug">
| and styled with <style lang="sass"> which has import. It should be yellow.
SlotComponent
template(v-slot:test-slot)
div.pug-slot slot content
Expand Down Expand Up @@ -42,3 +48,10 @@ color = orange
.pug-stylus
color: color
</style>

<style lang="sass">
@import './import.sass'

.pug-sass-non-import
color: yellow
</style>
2 changes: 2 additions & 0 deletions packages/playground/vue/import.sass
@@ -0,0 +1,2 @@
.pug-sass-import
color: blue
3 changes: 2 additions & 1 deletion packages/plugin-vue/src/index.ts
Expand Up @@ -235,7 +235,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
descriptor,
Number(query.index),
options,
this
this,
filename
)
}
}
Expand Down
19 changes: 16 additions & 3 deletions packages/plugin-vue/src/style.ts
@@ -1,14 +1,16 @@
import type { SFCDescriptor } from 'vue/compiler-sfc'
import type { TransformPluginContext } from 'rollup'
import type { ResolvedOptions } from '.'
import { formatPostcssSourceMap } from 'vite'

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function transformStyle(
code: string,
descriptor: SFCDescriptor,
index: number,
options: ResolvedOptions,
pluginContext: TransformPluginContext
pluginContext: TransformPluginContext,
filename: string
) {
const block = descriptor.styles[index]
// vite already handles pre-processors and CSS module so this is only
Expand All @@ -19,7 +21,14 @@ export async function transformStyle(
id: `data-v-${descriptor.id}`,
isProd: options.isProduction,
source: code,
scoped: block.scoped
scoped: block.scoped,
postcssOptions: {
map: {
from: filename,
inline: false,
annotation: false
}
}
})

if (result.errors.length) {
Expand All @@ -36,8 +45,12 @@ export async function transformStyle(
return null
}

const map = result.map
? formatPostcssSourceMap(result.map, filename)
: ({ mappings: '' } as any)

return {
code: result.code,
map: result.map || ({ mappings: '' } as any)
map: map
}
}
128 changes: 128 additions & 0 deletions packages/vite/src/node/__tests__/plugins/css.spec.ts
Expand Up @@ -2,6 +2,7 @@ import { cssUrlRE, cssPlugin } from '../../plugins/css'
import { resolveConfig } from '../../config'
import fs from 'fs'
import path from 'path'
import { normalizePath } from '../../utils'
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

describe('search css url function', () => {
test('some spaces before it', () => {
Expand Down Expand Up @@ -114,3 +115,130 @@ describe('css path resolutions', () => {
mockFs.mockReset()
})
})

describe('css source maps', () => {
const root = normalizePath(process.cwd())
const testcases = [
{
name: 'cssmodule',
code: `
.foo {
color: red;
}
`,
id: `${root}/foo.module.css`,
expectedMap: {
file: `${root}/foo.module.css`,
mappings: ';IACI;MACE,UAAU;IACZ',
names: [],
sources: [`${root}/foo.module.css`],
version: 3
}
},
{
name: 'sass',
code: `
.foo
color: red
`,
id: `${root}/foo.sass`,
expectedMap: {
file: `${root}/foo.sass`,
mappings: 'AACI;EACE',
names: [],
sources: [`${root}/foo.sass`],
version: 3
}
},
{
name: 'scss',
code: `
.foo {
color: red;
}
`,
id: `${root}/foo.scss`,
expectedMap: {
file: `${root}/foo.scss`,
mappings: 'AACM;EACE',
names: [],
sources: [`${root}/foo.scss`],
version: 3
}
},
{
name: 'less',
code: `
.foo {
color: red;
}
`,
id: `${root}/foo.less`,
expectedMap: {
file: `${root}/foo.less`,
mappings: 'AACM;EACE',
names: [],
sources: [`${root}/foo.less`],
version: 3
}
},
{
name: 'stylus',
code: `
.foo
color: red
`,
id: `${root}/foo.styl`,
expectedMap: {
file: `${root}/foo.styl`,
mappings: 'AACM;EACE,WAAO',
names: [],
sources: [`${root}/foo.styl`],
version: 3
}
},
{
name: 'scss module',
code: `
.foo {
color: red;
}
`,
id: `${root}/foo.module.scss`,
expectedMap: {
file: `${root}/foo.module.scss`,
mappings: 'AACM;EACE',
names: [],
sources: [`${root}/foo.module.scss`],
version: 3
}
}
]

test.concurrent.each(testcases)(
'$name',
async ({ code, id, expectedMap }) => {
const config = await resolveConfig(
{
root
},
'serve'
)
const { transform, buildStart } = cssPlugin(config)

await buildStart.call({})

const { map } = await transform.call(
{
addWatchFile() {
return
}
},
code,
id
)

expect(map).toMatchObject(expectedMap)
}
)
})
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Expand Up @@ -5,6 +5,7 @@ export { build } from './build'
export { optimizeDeps } from './optimizer'
export { send } from './server/send'
export { createLogger, printHttpServerUrls } from './logger'
export { formatPostcssSourceMap } from './plugins/css'
export { transformWithEsbuild } from './plugins/esbuild'
export { resolvePackageEntry } from './plugins/resolve'
export { resolvePackageData } from './packages'
Expand Down