Skip to content
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

npm scripts to pack an extension #35

Merged
merged 14 commits into from
Mar 30, 2023
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Then follow these instructions to see your extension:
3. Click on the **Load unpacked extension** button
4. Select the folder **my-extension/build**

When you're ready to publish to Chrome Web Store, create a minified bundle with `npm run build` and then zip the `build` folder.
When you're ready to publish to Chrome Web Store, create a minified bundle with `npm run build` and pack it with `npm run pack`.
dutiyesh marked this conversation as resolved.
Show resolved Hide resolved
Or you can zip the `build` folder manually.

<div align="center">
<img width="600" height="413" src="assets/installation.png" alt="Chrome Extension CLI">
Expand Down Expand Up @@ -106,7 +107,18 @@ Then follow these instructions to see your app:
### `npm run build`

Builds the app for production to the build folder.<br>
Zip the build folder and your app is ready to be published on Chrome Web Store.
Run `npm run pack` to
zip the build folder and your app is ready to be published on Chrome Web Store.<br>
Or you can zip it manually.

### `npm run pack`

Packs the build folder into a zip file under release folder.

### `npm run repack`

Rebuilds and packs the app.
dutiyesh marked this conversation as resolved.
Show resolved Hide resolved
It is a shorthand for `npm run build && npm run pack`.

### `npm run format`

Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ function createExtension(name, { overridePage, devtools, language }) {
watch:
'webpack --mode=development --watch --config config/webpack.config.js',
build: 'webpack --mode=production --config config/webpack.config.js',
pack: 'node pack.js',
repack: 'npm run build && npm run pack',
dutiyesh marked this conversation as resolved.
Show resolved Hide resolved
format:
'prettier --write --ignore-unknown "{config,public,src}/**/*.{html,css,js,ts,json}"',
};
Expand Down Expand Up @@ -216,7 +218,8 @@ function createExtension(name, { overridePage, devtools, language }) {
'mini-css-extract-plugin@^2.6.0',
'css-loader@^6.7.1',
'file-loader@^6.2.0',
'prettier@^2.6.2'
'prettier@^2.6.2',
'adm-zip@^0.5.10'
);

if (languageName === 'typescript') {
Expand Down Expand Up @@ -288,6 +291,12 @@ function createExtension(name, { overridePage, devtools, language }) {
);
});

// Copy script to automatically generate zip file
fs.copyFileSync(
path.resolve(__dirname, 'templates', 'shared', 'pack.js'),
path.join(root, 'pack.js')
);

// Setup the manifest file
const manifestDetails = Object.assign(
{},
Expand Down
3 changes: 3 additions & 0 deletions templates/shared/dotfiles/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
.DS_Store

npm-debug.log*

# packed files
/release
20 changes: 20 additions & 0 deletions templates/shared/pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { readFileSync } = require('fs');
const { parse, resolve } = require('path');
const AdmZip = require('adm-zip');

const { base } = parse(__dirname);
const { version } = JSON.parse(
readFileSync(resolve(__dirname, 'build', 'manifest.json'), 'utf8')
);

const outdir = 'release';
const filename = `${base}-v${version}.zip`;
const zip = new AdmZip();
zip.addLocalFolder('build');
zip.writeZip(`${outdir}/${filename}`);
dutiyesh marked this conversation as resolved.
Show resolved Hide resolved

console.log(
`${chalk.green(
'Success!'
)} Created a ${filename} file under ${outdir} directory. You can upload this file to web store.`
);
dutiyesh marked this conversation as resolved.
Show resolved Hide resolved