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

WIP: Per-source minification (DO NOT MERGE) #115

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ async function main() {
__dirname + "/../src/loaders/shebang-loader"
);

const { code: terserLoader, assets: terserLoaderAssets } = await ncc(
__dirname + "/../src/loaders/terser-loader"
);

const { code: tsLoader, assets: tsLoaderAssets } = await ncc(
__dirname + "/../src/loaders/ts-loader"
);
Expand All @@ -51,6 +55,7 @@ async function main() {
Object.keys(nodeLoaderAssets).length ||
Object.keys(relocateLoaderAssets).length ||
Object.keys(shebangLoaderAssets).length ||
Object.keys(terserLoaderAssets).length ||
Object.keys(tsLoaderAssets).some(asset => !asset.startsWith('lib/')) ||
Object.keys(sourcemapAssets).length
) {
Expand All @@ -63,6 +68,7 @@ async function main() {
writeFileSync(__dirname + "/../dist/ncc/loaders/node-loader.js", nodeLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js", relocateLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js", shebangLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/terser-loader.js", terserLoader);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js", tsLoader);

// copy typescript types
Expand Down
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,14 @@ module.exports = async (
}
}
}]
}
},
...minify ? [{
test: /\.(js|mjs|tsx?)$/,
use: [{
loader: __dirname + "/loaders/terser-loader.js",
options: { sourceMap }
}]
}] : []
]
},
plugins: [
Expand Down Expand Up @@ -263,7 +270,7 @@ module.exports = async (
if (map)
map.mappings = ";" + map.mappings;
return { code, map, assets };
})
});
};

// this could be rewritten with actual FS apis / globs, but this is simpler
Expand Down
21 changes: 21 additions & 0 deletions src/loaders/terser-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const terser = require("terser");
const { getOptions } = require('loader-utils');

module.exports = function (code) {
const options = getOptions(this);
const result = terser.minify(code, {
compress: {
keep_classnames: true,
keep_fnames: true
},
mangle: {
keep_classnames: true,
keep_fnames: true
},
sourceMap: options.sourceMap
});
// For some reason, auth0 returns "undefined"!
if (result.code === undefined)
return this.callback(null, code);
return this.callback(null, result.code, result.map);
};