-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7de154a
commit 71b62f9
Showing
7 changed files
with
80 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { promisify } = require('util') | ||
const zipDir = promisify(require('zip-dir')) | ||
const path = require('path') | ||
const fs = require('parcel-bundler/src/utils/fs') | ||
const Packager = require('parcel-bundler/src/packagers/Packager') | ||
|
||
class WebExtensionPackager extends Packager { | ||
async addAsset(asset) { | ||
const artifactsDir = path.resolve( | ||
asset.options.outDir, | ||
'..', | ||
'artifacts' | ||
) | ||
await fs.mkdirp(artifactsDir) | ||
|
||
// Write the manifest.json | ||
const bundlePath = path.parse(this.bundle.name) | ||
const contents = await this.resolveAssetContents(asset) | ||
const manifestJsonFilename = path.resolve( | ||
bundlePath.dir, | ||
bundlePath.name + '.json' | ||
) | ||
await fs.writeFile(manifestJsonFilename, contents, { encoding: 'utf8' }) | ||
|
||
// Package the extension (XPI/CRX) | ||
const zipped = await zipDir(this.options.outDir) | ||
const zippedFilename = path.resolve( | ||
artifactsDir, | ||
bundlePath.name + '.zip' | ||
) | ||
this.size = zipped.length | ||
await fs.writeFile(zippedFilename, zipped) | ||
} | ||
|
||
async resolveAssetContents(asset) { | ||
let contents = asset.generated[this.bundle.type] | ||
if (!contents || (contents && contents.path)) { | ||
contents = await fs.readFile(contents ? contents.path : asset.name) | ||
} | ||
// Create sub-directories if needed | ||
if (this.bundle.name.includes(path.sep)) { | ||
await fs.mkdirp(path.dirname(this.bundle.name)) | ||
} | ||
return contents | ||
} | ||
|
||
setup() {} | ||
|
||
end() {} | ||
|
||
getSize() { | ||
return this.size || 0 | ||
} | ||
} | ||
|
||
module.exports = WebExtensionPackager |
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,5 +1,6 @@ | ||
function WebExtensionPlugin(bundler) { | ||
bundler.addAssetType('json', require.resolve('./ManifestAsset')) | ||
bundler.addPackager('@@webext', require.resolve('./WebExtensionPackager')) | ||
} | ||
|
||
module.exports = WebExtensionPlugin |
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 +1,2 @@ | ||
dist/ | ||
artifacts/ |
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