-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature flags to ParcelOptions (#9551)
Implements a Feature Flag system for Parcel, available to both core code and plugins. This allows for early merges of not-ready-for-general-use features that can be enabled selectively by Parcel users.
- Loading branch information
Showing
12 changed files
with
157 additions
and
0 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
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,23 @@ | ||
{ | ||
"name": "@parcel/feature-flags", | ||
"version": "2.12.0", | ||
"description": "Blazing fast, zero configuration web application bundler", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/parcel" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/parcel-bundler/parcel.git" | ||
}, | ||
"main": "lib/index.js", | ||
"source": "src/index.js", | ||
"types": "index.d.ts", | ||
"engines": { | ||
"node": ">= 16.0.0" | ||
} | ||
} |
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,10 @@ | ||
// @flow strict | ||
|
||
export type FeatureFlags = {| | ||
// This feature flag mostly exists to test the feature flag system, and doesn't have any build/runtime effect | ||
+exampleFeature: boolean, | ||
|}; | ||
|
||
export const DEFAULT_FEATURE_FLAGS: FeatureFlags = { | ||
exampleFeature: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import assert from 'assert'; | ||
import path from 'node:path'; | ||
import {rimraf} from 'rimraf'; | ||
import {bundle, run, overlayFS, fsFixture} from '@parcel/test-utils'; | ||
|
||
describe('feature flags', () => { | ||
let dir = path.join(__dirname, 'feature-flags-fixture'); | ||
beforeEach(async () => { | ||
await rimraf(dir); | ||
await overlayFS.mkdirp(dir); | ||
await fsFixture(overlayFS, dir)` | ||
yarn.lock: | ||
// required for .parcelrc to work | ||
package.json: | ||
{ | ||
"name": "feature-flags-fixture", | ||
"version": "1.0.0" | ||
} | ||
index.js: | ||
module.exports = "MARKER"; | ||
.parcelrc: | ||
{ | ||
extends: "@parcel/config-default", | ||
transformers: { | ||
'*.js': ['./transformer.js', '...'] | ||
}, | ||
} | ||
transformer.js: | ||
const {Transformer} = require('@parcel/plugin'); | ||
module.exports = new Transformer({ | ||
async transform({asset, options}) { | ||
const code = await asset.getCode(); | ||
if (code.includes('MARKER') && options.featureFlags.exampleFeature) { | ||
asset.setCode(code.replace('MARKER', 'REPLACED')); | ||
} | ||
return [asset]; | ||
} | ||
}); | ||
`; | ||
}); | ||
|
||
it('flag should be available in plugins and set from options', async () => { | ||
await overlayFS.mkdirp(dir); | ||
|
||
const b = await bundle(path.join(dir, 'index.js'), { | ||
inputFS: overlayFS, | ||
featureFlags: {exampleFeature: true}, | ||
}); | ||
const output = await run(b); | ||
|
||
assert( | ||
output.includes('REPLACED'), | ||
`Expected ${output} to contain 'REPLACED'`, | ||
); | ||
}); | ||
|
||
it('flag defaults should be available in plugins if not set from options', async () => { | ||
await overlayFS.mkdirp(dir); | ||
|
||
const b = await bundle(path.join(dir, 'index.js'), { | ||
inputFS: overlayFS, | ||
}); | ||
const output = await run(b); | ||
|
||
assert( | ||
!output.includes('REPLACED'), | ||
`Expected ${output} to NOT contain 'REPLACED'`, | ||
); | ||
}); | ||
|
||
it('cache should invalidate on flag switch', async () => { | ||
await overlayFS.mkdirp(dir); | ||
|
||
const b = await bundle(path.join(dir, 'index.js'), { | ||
inputFS: overlayFS, | ||
shouldDisableCache: false, | ||
featureFlags: {exampleFeature: true}, | ||
}); | ||
const output = await run(b); | ||
|
||
assert( | ||
output.includes('REPLACED'), | ||
`Expected ${output} to contain 'REPLACED'`, | ||
); | ||
|
||
const b2 = await bundle(path.join(dir, 'index.js'), { | ||
inputFS: overlayFS, | ||
shouldDisableCache: false, | ||
featureFlags: {exampleFeature: false}, | ||
}); | ||
const output2 = await run(b2); | ||
assert( | ||
!output2.includes('REPLACED'), | ||
`Expected ${output} to NOT contain 'REPLACED'`, | ||
); | ||
}); | ||
}); |
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