-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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(minify): set the default value of esbuild's legalComments option to external #18104
Open
sunnylost
wants to merge
4
commits into
vitejs:main
Choose a base branch
from
sunnylost:feat/extract-legal-comments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+208
−10
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b741af
fix(minify): set the default value of esbuild's legalComments option …
sunnylost 874277f
fix(minify): improve legalComments:linked option
sunnylost 3b943bd
fix(minify): update output filename, update legalComments' default va…
sunnylost 33e81dc
fix(minify): ensure the css files contains legal comments link.
sunnylost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,78 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
import { expect, test } from 'vitest' | ||
import { describe, expect, test } from 'vitest' | ||
import { isBuild, readFile, testDir } from '~utils' | ||
|
||
test.runIf(isBuild)('no minifySyntax', () => { | ||
const assetsDir = path.resolve(testDir, 'dist/assets') | ||
function getJsAndCssContent(assetsDir: string) { | ||
const files = fs.readdirSync(assetsDir) | ||
|
||
const jsFile = files.find((f) => f.endsWith('.js')) | ||
const jsContent = readFile(path.resolve(assetsDir, jsFile)) | ||
const jsContent = readFile(path.resolve(assetsDir, jsFile)).trim() | ||
|
||
const cssFile = files.find((f) => f.endsWith('.css')) | ||
const cssContent = readFile(path.resolve(assetsDir, cssFile)) | ||
const cssContent = readFile(path.resolve(assetsDir, cssFile)).trim() | ||
|
||
expect(jsContent).toContain('{console.log("hello world")}') | ||
expect(jsContent).not.toContain('/*! explicit comment */') | ||
return { jsContent, cssContent } | ||
} | ||
|
||
expect(cssContent).toContain('color:#ff0000') | ||
expect(cssContent).not.toContain('/*! explicit comment */') | ||
function getLegalFileContent(assetsDir: string) { | ||
return readFile(path.resolve(assetsDir, 'LEGAL.txt')) | ||
} | ||
|
||
describe.runIf(isBuild)('minify', () => { | ||
test('Do not preserve any legal comments', () => { | ||
const { jsContent, cssContent } = getJsAndCssContent( | ||
path.resolve(testDir, 'dist/none/assets'), | ||
) | ||
|
||
expect(jsContent).toContain('{console.log("hello world")}') | ||
expect(jsContent).not.toContain('/*! explicit comment */') | ||
|
||
expect(cssContent).toContain('color:#ff0000') | ||
expect(cssContent).not.toContain('/*! explicit comment */') | ||
}) | ||
|
||
test('Preserve legal comments', () => { | ||
const { jsContent, cssContent } = getJsAndCssContent( | ||
path.resolve(testDir, 'dist/inline/assets'), | ||
) | ||
|
||
expect(jsContent).toContain('/*! explicit comment */') | ||
expect(cssContent).toContain('/*! explicit comment */') | ||
}) | ||
|
||
test('Move all legal comments to the end of the file.', () => { | ||
const { jsContent, cssContent } = getJsAndCssContent( | ||
path.resolve(testDir, 'dist/eof/assets'), | ||
) | ||
expect(jsContent.endsWith('/*! explicit comment */')).toBeTruthy() | ||
expect(cssContent.endsWith('/*! explicit comment */')).toBeTruthy() | ||
}) | ||
|
||
test('Move all legal comments to a LEGAL.txt file but to not link to them.', () => { | ||
const { jsContent, cssContent } = getJsAndCssContent( | ||
path.resolve(testDir, 'dist/external/assets'), | ||
) | ||
const legaContent = getLegalFileContent( | ||
path.resolve(testDir, 'dist/external'), | ||
) | ||
expect(jsContent).not.toContain('/*! explicit comment */') | ||
expect(cssContent).not.toContain('/*! explicit comment */') | ||
expect(legaContent).toContain('/*! explicit comment */') | ||
}) | ||
|
||
test('Move all legal comments to a LEGAL.txt file and link to them with a comment.', () => { | ||
const { jsContent, cssContent } = getJsAndCssContent( | ||
path.resolve(testDir, 'dist/linked/assets'), | ||
) | ||
const legaContent = getLegalFileContent( | ||
path.resolve(testDir, 'dist/linked'), | ||
) | ||
expect(jsContent).not.toContain('/*! explicit comment */') | ||
expect(jsContent).toContain( | ||
'/*! For license information please see LEGAL.txt */', | ||
) | ||
expect(cssContent).not.toContain('/*! explicit comment */') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable. |
||
expect(legaContent).toContain('/*! explicit comment */') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// this is automatically detected by playground/vitestSetup.ts and will replace | ||
// the default e2e test serve behavior | ||
|
||
import path from 'node:path' | ||
import { ports, rootDir } from '~utils' | ||
|
||
export const port = ports.lib | ||
|
||
export async function serve() { | ||
const { build } = await import('vite') | ||
await build({ | ||
root: rootDir, | ||
configFile: path.resolve(__dirname, '../vite.legal-comments-eof.config.js'), | ||
}) | ||
|
||
await build({ | ||
root: rootDir, | ||
configFile: path.resolve( | ||
__dirname, | ||
'../vite.legal-comments-external.config.js', | ||
), | ||
}) | ||
|
||
await build({ | ||
root: rootDir, | ||
configFile: path.resolve( | ||
__dirname, | ||
'../vite.legal-comments-inline.config.js', | ||
), | ||
}) | ||
|
||
await build({ | ||
root: rootDir, | ||
configFile: path.resolve( | ||
__dirname, | ||
'../vite.legal-comments-none.config.js', | ||
), | ||
}) | ||
|
||
await build({ | ||
root: rootDir, | ||
configFile: path.resolve( | ||
__dirname, | ||
'../vite.legal-comments-linked.config.js', | ||
), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
esbuild: { | ||
legalComments: 'eof', | ||
minifySyntax: false, | ||
}, | ||
build: { | ||
outDir: 'dist/eof', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
esbuild: { | ||
legalComments: 'external', | ||
minifySyntax: false, | ||
}, | ||
build: { | ||
outDir: 'dist/external', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
esbuild: { | ||
legalComments: 'inline', | ||
minifySyntax: false, | ||
}, | ||
build: { | ||
outDir: 'dist/inline', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
esbuild: { | ||
legalComments: 'linked', | ||
minifySyntax: false, | ||
}, | ||
build: { | ||
outDir: 'dist/linked', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,7 @@ export default defineConfig({ | |
legalComments: 'none', | ||
minifySyntax: false, | ||
}, | ||
build: { | ||
outDir: 'dist/none', | ||
}, | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to use
name: 'LEGAL.txt'
instead so that the file name includes a hash. That requires theres.code += '\n/*! For license information please see LEGAL.txt */'
to be moved intogenerateBundle
hook.