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: support preloading also on legacy bundle #9920

Open
wants to merge 53 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d4db5c9
feat: support preloading also in legacy bundle
Tal500 Aug 30, 2022
03d282e
chore: describe the `parseImportsSystemJS` function better
Tal500 Aug 31, 2022
06e69a8
test: ignoring code file for testing in prettier
Tal500 Aug 31, 2022
eced9b1
chore: adapt the code to work also in Node 12
Tal500 Aug 31, 2022
a413bc0
chore: test a change in the code
Tal500 Sep 1, 2022
2dc09b4
revert: chore: test a change in the code
Tal500 Sep 1, 2022
59fd103
chore: merge branch 'preload-on-system-format' of github.com:Tal500/v…
Tal500 Sep 1, 2022
96780a1
chore: fix revert issues
Tal500 Sep 1, 2022
6c38aa1
fix: fix the css regexp
Tal500 Sep 2, 2022
02995a1
fix: return the format to `module.import` in regex
Tal500 Sep 4, 2022
4b2b1d1
fix: and simplify the RE in `parseImportsSystemJS`
Tal500 Sep 4, 2022
900960b
test: more test snippets for `parseImportsSystemJS`
Tal500 Sep 4, 2022
d579efa
fix: safer detection of import regexp for empty css's
Tal500 Sep 4, 2022
9e04a5c
chore: omit the obsolete css inlining on legacy
Tal500 Sep 4, 2022
e6361ff
test: fix line ending issues while reading, and update the snapshot
Tal500 Sep 4, 2022
5e1ba19
test: (hopefully) fix line ending issues
Tal500 Sep 4, 2022
50627d2
test: and code files for testing to lint ignore
Tal500 Sep 4, 2022
e075dac
feat: better check on seeing whether the asset path was already loaded
Tal500 Sep 4, 2022
e567b21
fix: a much safer way to load CSS on legacy, while try to save perfor…
Tal500 Sep 4, 2022
0957a50
chore: fixing the `@ts-ignore` location (fighting with lint)
Tal500 Sep 4, 2022
6fbeb25
fix: linting issues
Tal500 Sep 4, 2022
a7501c2
feat: conditional waiting in preloading
Tal500 Sep 4, 2022
6bdb4e4
fix: make sure to have the full dep path
Tal500 Sep 4, 2022
40e4da0
fix: try to break also when paths are simply equal
Tal500 Sep 4, 2022
e72c090
chore: remove full dep equlity optimization for checking tests
Tal500 Sep 4, 2022
8298666
chore: simplify the code by removing the xhr request and thanks to th…
Tal500 Sep 5, 2022
6dbe676
chore: minor style improvement
Tal500 Sep 5, 2022
e578475
fix: add and use `analyzeSystemRegisteration` to handle parameter nam…
Tal500 Sep 5, 2022
2fd8eaf
build: change the dir to ignore on eslint
Tal500 Sep 5, 2022
8f68cdf
chore: simplify the code
Tal500 Sep 5, 2022
6923be3
fix: copy the suggested code from #9970
Tal500 Sep 10, 2022
94908e1
chore: fix typo
Tal500 Sep 10, 2022
1f05b29
chore: omit the fix for double stylesheets loading
Tal500 Sep 18, 2022
3c6240b
Merge branch 'main' into preload-on-system-format
Tal500 Sep 23, 2022
22dd608
Merge branch 'vitejs:main' into preload-on-system-format
Tal500 Sep 23, 2022
69d62a7
Merge branch 'main' into preload-on-system-format
Tal500 Sep 24, 2022
d5c9469
chore: Merge branch 'main' into preload-on-system-format
Tal500 Sep 28, 2022
f153ff2
fix: return css inlining only for library mode
Tal500 Sep 28, 2022
4b8a587
Merge branch 'main' into preload-on-system-format
Tal500 Oct 1, 2022
a722090
chore: Merge branch 'main' into preload-on-system-format
Tal500 Oct 19, 2022
6d4d82e
fix: give empty list of systemJS dynamic import, if the `moduleParam`…
Tal500 Oct 19, 2022
37e43a7
chore: merge branch 'main' into preload-on-system-format
Tal500 Nov 9, 2022
3d93a9f
refactor: cancel many regexp analysis, and use SystemJS `instantiate`…
Tal500 Nov 14, 2022
998ed72
chore: merge branch 'main' into preload-on-system-format
Tal500 Dec 11, 2022
ee90f4a
Merge branch 'main' into preload-on-system-format
Tal500 Jan 4, 2023
06f4657
chore: fix the `@ts-ignore` issue on CI
Tal500 Jan 4, 2023
44a4515
Merge branch 'main' into preload-on-system-format
Tal500 Feb 13, 2023
df949be
chore: add the non-nullish ! to "chunk.viteMetadata"
Tal500 Feb 13, 2023
1310ac0
Merge branch 'main' into pr/9920
sapphi-red Mar 13, 2023
3a3b6bc
Merge branch 'main' into pr/9920
sapphi-red Jun 22, 2023
896822a
fix: inline CSS in non-lib mode if css.codeSplit is false
sapphi-red Jun 22, 2023
1509a97
refactor: revert rename preloadMethod
sapphi-red Jun 22, 2023
211f4a7
test: test nested dynamic import
sapphi-red Jun 22, 2023
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
68 changes: 44 additions & 24 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import glob from 'fast-glob'
import postcssrc from 'postcss-load-config'
import type {
ExistingRawSourceMap,
InternalModuleFormat,
NormalizedOutputOptions,
OutputChunk,
RenderedChunk,
Expand Down Expand Up @@ -218,6 +219,30 @@ function encodePublicUrlsInCSS(config: ResolvedConfig) {
return config.command === 'build'
}

export function calcEmptyChunkRE(
pureCssChunkNames: string[],
format: InternalModuleFormat,
): RegExp {
const emptyChunkFiles = pureCssChunkNames
.map((file) => path.basename(file))
.join('|')
.replace(/\./g, '\\.')
let regexStr: string
switch (format) {
case 'es':
regexStr = `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];\n?`
break
// notice we allow regex group capturing only on the system format case
case 'system':
regexStr = `\\w.import\\(\\s*["'][^"']*(${emptyChunkFiles})["']\\);?\n?`
break
default:
regexStr = `\\brequire\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\);\n?`
break
}
return new RegExp(regexStr, 'g')
}

/**
* Plugin applied before user plugins
*/
Expand Down Expand Up @@ -615,7 +640,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// this is a shared CSS-only chunk that is empty.
pureCssChunks.add(chunk)
}
if (opts.format === 'es' || opts.format === 'cjs') {
if (
opts.format === 'es' ||
opts.format === 'cjs' ||
opts.format === 'system'
) {
const cssAssetName = chunk.facadeModuleId
? normalizePath(path.relative(config.root, chunk.facadeModuleId))
: chunk.name
Expand Down Expand Up @@ -659,14 +688,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
emitTasks = []
}
} else if (!config.build.ssr) {
// legacy build and inline css

// Entry chunk CSS will be collected into `chunk.viteMetadata.importedCss`
// and injected later by the `'vite:build-html'` plugin into the `index.html`
// so it will be duplicated. (https://github.com/vitejs/vite/issues/2062#issuecomment-782388010)
// But because entry chunk can be imported by dynamic import,
// we shouldn't remove the inlined CSS. (#10285)

// inline css
chunkCSS = await finalizeCss(chunkCSS, true, config)
let cssString = JSON.stringify(chunkCSS)
cssString =
Expand Down Expand Up @@ -744,16 +766,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
}

const emptyChunkFiles = pureCssChunkNames
.map((file) => path.basename(file))
.join('|')
.replace(/\./g, '\\.')
const emptyChunkRE = new RegExp(
opts.format === 'es'
? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];\n?`
: `\\brequire\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\);\n?`,
'g',
)
// if the format is `system`, the removing process of the empty chunk is done in the
// `importAnalysisBuild` plugin on `generateBundle()`.
const emptyChunkRE =
opts.format !== 'system'
? calcEmptyChunkRE(pureCssChunkNames, opts.format)
: undefined
for (const file in bundle) {
const chunk = bundle[file]
if (chunk.type === 'chunk') {
Expand All @@ -771,11 +789,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}
return true
})
chunk.code = chunk.code.replace(
emptyChunkRE,
// remove css import while preserving source map location
(m) => `/* empty css ${''.padEnd(m.length - 15)}*/`,
)
if (emptyChunkRE !== undefined) {
chunk.code = chunk.code.replace(
emptyChunkRE,
// remove css import while preserving source map location
(m) => `/* empty css ${''.padEnd(m.length - 15)}*/`,
)
}
}
}
const removedPureCssFiles = removedPureCssFilesCache.get(config)!
Expand Down