Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

perf(nuxt): transform #imports to improve tree-shaking #5763

Merged
merged 8 commits into from
Jul 21, 2022
Merged
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
3 changes: 2 additions & 1 deletion packages/nuxt/src/auto-imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
const ctx = createUnimport({
presets: options.presets,
imports: options.imports,
virtualImports: ['#imports'],
addons: {
vueTemplate: true
}
Expand All @@ -58,7 +59,7 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
// Support for importing from '#imports'
addTemplate({
filename: 'imports.mjs',
getContents: () => ctx.toExports()
getContents: () => ctx.toExports() + '\nif (process.dev) { console.warn("[nuxt] `#imports` should be transformed with real imports. There seems to be something wrong with the auto-imports plugin.") }'
})
nuxt.options.alias['#imports'] = join(nuxt.options.buildDir, 'imports')

Expand Down
34 changes: 21 additions & 13 deletions packages/nuxt/src/auto-imports/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createUnplugin } from 'unplugin'
import { parseQuery, parseURL } from 'ufo'
import { Unimport } from 'unimport'
import { AutoImportsOptions } from '@nuxt/schema'
import { normalize } from 'pathe'

export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx: Unimport, options: Partial<AutoImportsOptions>, sourcemap?: boolean }) => {
return {
Expand All @@ -12,35 +13,42 @@ export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: {ctx
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
const { type, macro } = parseQuery(search)

const exclude = options.transform?.exclude || [/[\\/]node_modules[\\/]/]
const include = options.transform?.include || []

// Custom includes - exclude node_modules by default
if (exclude.some(pattern => id.match(pattern)) && !include.some(pattern => id.match(pattern))) {
// Included
if (options.transform?.include?.some(pattern => id.match(pattern))) {
return true
}
// Excluded
if (options.transform?.exclude?.some(pattern => id.match(pattern))) {
return false
}

// vue files
// Vue files
if (
pathname.endsWith('.vue') &&
(type === 'template' || type === 'script' || macro || !search)
) {
return true
}

// js files
// JavaScript files
if (pathname.match(/\.((c|m)?j|t)sx?$/g)) {
return true
}
},
async transform (_code, id) {
const { code, s } = await ctx.injectImports(_code, id)
if (code === _code) {
async transform (code, id) {
id = normalize(id)
const isNodeModule = id.match(/[\\/]node_modules[\\/]/) && !options.transform?.include?.some(pattern => id.match(pattern))
// For modules in node_modules, we only transform `#imports` but not doing auto-imports
if (isNodeModule && !code.match(/(['"])#imports\1/)) {
return
}
return {
code,
map: sourcemap && s.generateMap({ source: id, includeContent: true })

const { s } = await ctx.injectImports(code, id, { autoImport: !isNodeModule })
if (s.hasChanged()) {
return {
code: s.toString(),
map: sourcemap && s.generateMap({ source: id, includeContent: true })
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/basic/composables/badSideEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function badSideEffect () {
// ...
}

throw new Error('composables/badSideEffect.ts should be tree-shaked')
2 changes: 2 additions & 0 deletions test/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
</template>

<script setup>
import { useRuntimeConfig } from '#imports'

const config = useRuntimeConfig()

const foo = useFoo()
Expand Down