-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): do not copy polyfills to the
dist
OT unless building es5
Prior to this change Stencil will copy polyfills to the `dist` output target whether or not the user has indicated they'll be necessary. The polyfills comprise two things: copying the polyfills themselves into the 'loader' path, and adding code to the lazy-loader entry points which loads those polyfills. Instead of just assuming that the user wants this, we now gate this behavior on whether `buildEs5` is set on the Stencil configuration. fixes #5416 STENCIL-1288
- Loading branch information
1 parent
43454bb
commit 47c8efc
Showing
3 changed files
with
122 additions
and
16 deletions.
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
73 changes: 73 additions & 0 deletions
73
src/compiler/output-targets/test/output-lazy-loader.spec.ts
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,73 @@ | ||
import type * as d from '@stencil/core/declarations'; | ||
import { mockBuildCtx, mockCompilerCtx, mockCompilerSystem, mockValidatedConfig } from '@stencil/core/testing'; | ||
import { DIST, resolve } from '@utils'; | ||
|
||
import { validateDist } from '../../config/outputs/validate-dist'; | ||
import { outputLazyLoader } from '../output-lazy-loader'; | ||
|
||
function setup(configOverrides: Partial<d.ValidatedConfig> = {}) { | ||
const sys = mockCompilerSystem(); | ||
const config: d.ValidatedConfig = mockValidatedConfig({ | ||
...configOverrides, | ||
configPath: '/testing-path', | ||
buildAppCore: true, | ||
namespace: 'TestApp', | ||
outputTargets: [ | ||
{ | ||
type: DIST, | ||
dir: 'my-test-dir', | ||
}, | ||
], | ||
srcDir: '/src', | ||
sys, | ||
}); | ||
|
||
config.outputTargets = validateDist(config, config.outputTargets); | ||
|
||
const compilerCtx = mockCompilerCtx(config); | ||
const writeFileSpy = jest.spyOn(compilerCtx.fs, 'writeFile'); | ||
const buildCtx = mockBuildCtx(config, compilerCtx); | ||
|
||
return { config, compilerCtx, buildCtx, writeFileSpy }; | ||
} | ||
|
||
describe('Lazy Loader Output Target', () => { | ||
let config: d.ValidatedConfig; | ||
let compilerCtx: d.CompilerCtx; | ||
let writeFileSpy: jest.SpyInstance; | ||
|
||
afterEach(() => { | ||
writeFileSpy.mockRestore(); | ||
}); | ||
|
||
it('should write code for initializing polyfills when buildEs5=true', async () => { | ||
({ config, compilerCtx, writeFileSpy } = setup({ buildEs5: true })); | ||
await outputLazyLoader(config, compilerCtx); | ||
|
||
const expectedIndexOutput = `export * from '../esm/polyfills/index.js'; | ||
export * from '../esm-es5/loader.js';`; | ||
expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.js'), expectedIndexOutput); | ||
|
||
const expectedCjsIndexOutput = `module.exports = require('../cjs/loader.cjs.js'); | ||
module.exports.applyPolyfills = function() { return Promise.resolve() };`; | ||
expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.cjs.js'), expectedCjsIndexOutput); | ||
|
||
const expectedES2017Output = `export * from '../esm/polyfills/index.js'; | ||
export * from '../esm/loader.js';`; | ||
expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.es2017.js'), expectedES2017Output); | ||
}); | ||
|
||
it('should exclude polyfill code when buildEs5=false', async () => { | ||
({ config, compilerCtx, writeFileSpy } = setup({ buildEs5: false })); | ||
await outputLazyLoader(config, compilerCtx); | ||
|
||
const expectedIndexOutput = `export * from '../esm/loader.js';`; | ||
expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.js'), expectedIndexOutput); | ||
|
||
const expectedCjsIndexOutput = `module.exports = require('../cjs/loader.cjs.js');`; | ||
expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.cjs.js'), expectedCjsIndexOutput); | ||
|
||
const expectedES2017Output = `export * from '../esm/loader.js';`; | ||
expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.es2017.js'), expectedES2017Output); | ||
}); | ||
}); |