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

fix: ignore sideEffects for scripts imported from html #12786

Merged
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
19 changes: 12 additions & 7 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,24 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

if ((res = tryFsResolve(fsPath, options))) {
const resPkg = findNearestPackageData(
path.dirname(res),
options.packageCache,
)
res = ensureVersionQuery(res, id, options, depsOptimizer)
debug?.(`[relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)

return resPkg
? {
// If this isn't a script imported from a .html file, include side effects
// hints so the non-used code is properly tree-shaken during build time.
if (!importer?.endsWith('.html')) {
const resPkg = findNearestPackageData(
path.dirname(res),
options.packageCache,
)
if (resPkg) {
return {
id: res,
moduleSideEffects: resPkg.hasSideEffects(res),
}
: res
}
}
return res
}
}

Expand Down
10 changes: 10 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,13 @@ describe('importmap', () => {
)
})
})

describe('side-effects', () => {
beforeAll(async () => {
await page.goto(viteTestUrl + '/side-effects/')
})

test('console.log is not tree-shaken', async () => {
expect(browserLogs).toContain('message from sideEffects script')
})
})
2 changes: 2 additions & 0 deletions playground/html/side-effects/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>sideEffects false</h1>
<script type="module" src="./sideEffects.js"></script>
6 changes: 6 additions & 0 deletions playground/html/side-effects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-html-side-effects",
"private": true,
"version": "0.0.0",
"sideEffects": false
}
1 change: 1 addition & 0 deletions playground/html/side-effects/sideEffects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('message from sideEffects script')
1 change: 1 addition & 0 deletions playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default defineConfig({
valid: resolve(__dirname, 'valid.html'),
importmapOrder: resolve(__dirname, 'importmapOrder.html'),
env: resolve(__dirname, 'env.html'),
sideEffects: resolve(__dirname, 'side-effects/index.html'),
},
},
},
Expand Down