Skip to content
This repository has been archived by the owner on May 1, 2022. It is now read-only.

Commit

Permalink
Add initial support for minifying in production (#19)
Browse files Browse the repository at this point in the history
* Add initial support for minifying in production

* Prettier

* Don't minify when passed --no-minify

Co-Authored-By: Jake Deichert <git@jakedeichert.com>

* set module: true to terser for better minification

* format

* add minifed test results

* add yarn.lock to .gitignore

Co-authored-by: Jake Deichert <git@jakedeichert.com>
  • Loading branch information
v0idifier and jacobdeichert committed Jan 22, 2020
1 parent ef15a8d commit 1b1f856
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 242 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ lib
# node
#######################################
package-lock.json
yarn.lock

# Logs
logs
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"chokidar": "^3.3.1",
"glob": "^7.1.6",
"p-limit": "^2.2.2",
"snowpack": "^1.0.5"
"snowpack": "^1.0.5",
"terser": "^4.6.3"
},
"devDependencies": {
"@jakedeichert/eslint-config": "^4.0.5",
Expand Down
32 changes: 32 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as svelte from 'svelte/compiler';
import * as chokidar from 'chokidar';
import * as babel from '@babel/core';
import * as glob from 'glob';
import * as Terser from 'terser';
import pLimit from 'p-limit';

const exec = util.promisify(execSync);
Expand Down Expand Up @@ -82,6 +83,25 @@ async function transform(destPath: string): Promise<void> {
}
}

// Minify file with terser.
async function minify(destPath: string): Promise<void> {
try {
const source = await fs.readFile(destPath, 'utf8');

const result = Terser.minify(source, {
module: true,
});

await fs.writeFile(destPath, result.code);
console.info(`Terser minified ${destPath}`);
} catch (err) {
console.log('');
console.error(`Failed to minify with terser: ${destPath}`);
console.error(err);
console.log('');
}
}

// Only needs to run during the initial compile cycle. If a developer adds a new package dependency,
// they should restart svelvet.
const snowpack = async (): Promise<void> => {
Expand Down Expand Up @@ -149,6 +169,18 @@ async function initialBuild(): Promise<void> {
})
)
);

// Minify js files with terser if in production.
if (IS_PRODUCTION_MODE && !process.argv.includes('--no-minify')) {
await Promise.all(
destFiles.map(destPath =>
concurrencyLimit(async () => {
if (!destPath) return;
await minify(destPath);
})
)
);
}
}

function startWatchMode(): void {
Expand Down
163 changes: 62 additions & 101 deletions tests/snapshot/dist/App.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 20 additions & 30 deletions tests/snapshot/dist/components/Footer/Links.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
/* generated by Svelte v3.17.2 */
import {
SvelteComponent,
detach,
element,
init,
insert,
noop,
safe_not_equal,
SvelteComponent as e,
detach as t,
element as n,
init as r,
insert as o,
noop as s,
safe_not_equal as l,
} from '/web_modules/svelte/internal.js';

function create_fragment(ctx) {
let nav;
function u(e) {
let r;
return {
c() {
nav = element('nav');
nav.innerHTML = `<a href="/">Home</a>`;
(r = n('nav')), (r.innerHTML = '<a href="/">Home</a>');
},

m(target, anchor) {
insert(target, nav, anchor);
m(e, t) {
o(e, r, t);
},

p: noop,
i: noop,
o: noop,

d(detaching) {
if (detaching) detach(nav);
p: s,
i: s,
o: s,
d(e) {
e && t(r);
},
};
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, null, create_fragment, safe_not_equal, {});
export default class extends e {
constructor(e) {
super(), r(this, e, null, u, l, {});
}
}

export default Component;
Loading

0 comments on commit 1b1f856

Please sign in to comment.