-
Notifications
You must be signed in to change notification settings - Fork 798
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(compiler): don't break HMR by mangling CSS #3517
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea909ef
fix(compiler): don't break livereload by mangling CSS
alicewriteswrongs cb5591f
propagate ValidatedConfig to fix some strictNullChecks errors
alicewriteswrongs 0209d12
prettier
alicewriteswrongs 42d65ec
trying to make this test work cross platform
alicewriteswrongs b92e451
feedback, and change the fix for HMR issue
alicewriteswrongs 8aecab4
expand tests a bit to cover the changes
alicewriteswrongs 964efa9
prettier
alicewriteswrongs cc663cd
fmt
alicewriteswrongs 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
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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { mockBuildCtx, mockCompilerCtx, mockModule, mockValidatedConfig } from '@stencil/core/testing'; | ||
import { stubComponentCompilerMeta } from '../../types/tests/ComponentCompilerMeta.stub'; | ||
import { BundleOptions } from '../bundle-interface'; | ||
import { extTransformsPlugin } from '../ext-transforms-plugin'; | ||
import * as importPathLib from '../../transformers/stencil-import-path'; | ||
import { normalizePath } from '@utils'; | ||
|
||
describe('extTransformsPlugin', () => { | ||
function setup(bundleOptsOverrides: Partial<BundleOptions> = {}) { | ||
const config = mockValidatedConfig({ | ||
plugins: [], | ||
outputTargets: [ | ||
{ | ||
type: 'dist-collection', | ||
dir: 'dist/', | ||
collectionDir: 'dist/collectionDir', | ||
}, | ||
], | ||
srcDir: '/some/stubbed/path', | ||
}); | ||
const compilerCtx = mockCompilerCtx(config); | ||
const buildCtx = mockBuildCtx(config, compilerCtx); | ||
|
||
const compilerComponentMeta = stubComponentCompilerMeta({ | ||
tagName: 'my-component', | ||
componentClassName: 'MyComponent', | ||
}); | ||
|
||
buildCtx.components = [compilerComponentMeta]; | ||
|
||
compilerCtx.moduleMap.set( | ||
compilerComponentMeta.sourceFilePath, | ||
mockModule({ | ||
cmps: [compilerComponentMeta], | ||
}) | ||
); | ||
|
||
const bundleOpts: BundleOptions = { | ||
id: 'test-bundle', | ||
platform: 'client', | ||
inputs: {}, | ||
...bundleOptsOverrides, | ||
}; | ||
|
||
const cssText = ':host { text: pink; }'; | ||
|
||
// mock out the read for our CSS | ||
jest.spyOn(compilerCtx.fs, 'readFile').mockResolvedValue(cssText); | ||
|
||
// mock out compilerCtx.worker.transformCssToEsm because 1) we want to | ||
// test what arguments are passed to it and 2) calling it un-mocked causes | ||
// the infamous autoprefixer-spew-issue :( | ||
const transformCssToEsmSpy = jest.spyOn(compilerCtx.worker, 'transformCssToEsm').mockResolvedValue({ | ||
styleText: cssText, | ||
output: cssText, | ||
map: null, | ||
diagnostics: [], | ||
imports: [], | ||
defaultVarName: 'foo', | ||
styleDocs: [], | ||
}); | ||
|
||
const writeFileSpy = jest.spyOn(compilerCtx.fs, 'writeFile'); | ||
return { | ||
plugin: extTransformsPlugin(config, compilerCtx, buildCtx, bundleOpts), | ||
config, | ||
compilerCtx, | ||
buildCtx, | ||
bundleOpts, | ||
writeFileSpy, | ||
transformCssToEsmSpy, | ||
cssText, | ||
}; | ||
} | ||
|
||
describe('transform function', () => { | ||
it('should set name', () => { | ||
expect(setup().plugin.name).toBe('extTransformsPlugin'); | ||
}); | ||
|
||
it('should return early if no data can be gleaned from the id', async () => { | ||
const { plugin } = setup(); | ||
// @ts-ignore we're testing something which shouldn't normally happen, | ||
// but might if an argument of the wrong type were passed as `id` | ||
const parseSpy = jest.spyOn(importPathLib, 'parseImportPath').mockReturnValue({ data: null }); | ||
// @ts-ignore the Rollup plugins expect to be called in a Rollup context | ||
expect(await plugin.transform('asdf', 'foo.css')).toBe(null); | ||
parseSpy.mockRestore(); | ||
}); | ||
|
||
it('should write CSS files if associated with a tag', async () => { | ||
const { plugin, writeFileSpy } = setup(); | ||
|
||
// @ts-ignore the Rollup plugins expect to be called in a Rollup context | ||
await plugin.transform('asdf', '/some/stubbed/path/foo.css?tag=my-component'); | ||
|
||
const [path, css] = writeFileSpy.mock.calls[0]; | ||
|
||
expect(normalizePath(path)).toBe('./dist/collectionDir/foo.css'); | ||
|
||
expect(css).toBe(':host { text: pink; }'); | ||
}); | ||
|
||
describe('passing `commentOriginalSelector` to `transformCssToEsm`', () => { | ||
it.each([ | ||
[false, 'tag=my-component&encapsulation=scoped'], | ||
[true, 'tag=my-component&encapsulation=shadow'], | ||
[false, 'tag=my-component'], | ||
])('should pass true if %p and hydrate', async (expectation, queryParams) => { | ||
const { plugin, transformCssToEsmSpy } = setup({ platform: 'hydrate' }); | ||
// @ts-ignore the Rollup plugins expect to be called in a Rollup context | ||
await plugin.transform('asdf', `/some/stubbed/path/foo.css?${queryParams}`); | ||
expect(transformCssToEsmSpy.mock.calls[0][0].commentOriginalSelector).toBe(expectation); | ||
}); | ||
|
||
it('should pass false if shadow, hydrate, but using HMR in dev watch mode', async () => { | ||
const { plugin, transformCssToEsmSpy, config } = setup({ platform: 'hydrate' }); | ||
|
||
config.flags.watch = true; | ||
config.flags.dev = true; | ||
config.flags.serve = true; | ||
config.devServer = { reloadStrategy: 'hmr' }; | ||
|
||
// @ts-ignore the Rollup plugins expect to be called in a Rollup context | ||
await plugin.transform('asdf', '/some/stubbed/path/foo.css?tag=my-component&encapsulation=shadow'); | ||
expect(transformCssToEsmSpy.mock.calls[0][0].commentOriginalSelector).toBe(false); | ||
}); | ||
|
||
it.each(['tag=my-component&encapsulation=scoped', 'tag=my-component&encapsulation=shadow', 'tag=my-component'])( | ||
'should pass false if %p without hydrate', | ||
async (queryParams) => { | ||
const { plugin, transformCssToEsmSpy } = setup(); | ||
// @ts-ignore the Rollup plugins expect to be called in a Rollup context | ||
await plugin.transform('asdf', `/some/stubbed/path/foo.css?${queryParams}`); | ||
expect(transformCssToEsmSpy.mock.calls[0][0].commentOriginalSelector).toBe(false); | ||
} | ||
); | ||
}); | ||
}); | ||
}); |
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
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 don't fully understand this comment (yet) - is this something we wanted to include in the final code, or was this a debugging note?
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 was thinking it might be a helpful note to leave in - when I found
serializeImportPath
andparseImportPath
and read them I thought it was an odd and sort of surprising thing so thought it might be helpful to leave a little signpost behindThere 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 the sort of strange bit (relating to the thread below about which files are actually written) is that the
id
for a module which has a Stencil component in it will be rewritten using thatserializeImportPath
function to include the information set in the@Component
decorator as query-params like?scoped=true&tag=my-component
and so on. This seems like a very indirect and confusing pattern to me since then this information is crucial here for figuring out when we need to write the transformed CSS corresponding to that module...anywhoo just a weird little corner here where I don't quite understand why it's like this