Skip to content

Commit

Permalink
support for es6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenschobert committed Sep 6, 2020
1 parent f12d70d commit bf45b7a
Show file tree
Hide file tree
Showing 6 changed files with 603 additions and 482 deletions.
37 changes: 32 additions & 5 deletions package-lock.json

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

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@
"type": "git",
"url": "https://github.com/stevenschobert/instafeed.js.git"
},
"main": "./src/instafeed.js",
"main": "./dist/instafeed.js",
"module": "./dist/instafeed.es.js",
"exports": {
"import": "./dist/instafeed.es.js",
"require": "./dist/instafeed.js"
},
"scripts": {
"lint-src": "jshint --reporter ./node_modules/jshint-stylish src/**.js",
"lint-test": "jshint --reporter ./node_modules/jshint-stylish test/**.js",
"unit-test": "mocha -R spec test/",
"build-browser-test": "cp node_modules/mocha/mocha.{css,js} browser-test/ && browserify test/test.js > browser-test/test-bundle.js",
"build-dist": "node scripts/build-min.js src/instafeed.js dist/ && node scripts/print-file-size.js src/instafeed.js dist/instafeed.js dist/instafeed.min.js dist/instafeed.min.map",
"build-dist": "node scripts/build-min.js src/instafeed.js dist/ && node scripts/print-file-size.js src/instafeed.js dist/instafeed.es.js dist/instafeed.es.min.js dist/instafeed.es.min.map dist/instafeed.js dist/instafeed.min.js dist/instafeed.min.map",
"test": "npm run lint-src && npm run lint-test && npm run unit-test"
},
"devDependencies": {
"browserify": "^16.5.2",
"jshint": "^2.12.0",
"jshint-stylish": "^2.2.1",
"mocha": "^8.1.3",
"uglify-js": "^3.10.3"
"rollup": "^2.26.10",
"uglify-es": "^3.3.9"
}
}
194 changes: 147 additions & 47 deletions scripts/build-min.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,173 @@
const uglify = require('uglify-js');
const fs = require('fs');
const uglifyEs = require('uglify-es');
const fs = require('fs').promises;
const path = require('path');
const rollup = require('rollup');

const pkg = require('../package.json');

const args = process.argv.slice(2);
const [srcPath, destFolder] = args;

const srcName = path.basename(srcPath);
const mapName = srcName.replace('.js', '.min.map');
const minName = srcName.replace('.js', '.min.js');

const unMinPath = path.join(destFolder, srcName);
const mapPath = path.join(destFolder, mapName);
const minPath = path.join(destFolder, minName);
const esUnminName = srcName.replace('.js', '.es.js');
const esMinName = srcName.replace('.js', '.es.min.js');
const esMapName = srcName.replace('.js', '.es.min.map');

const preamble = `/* ${pkg.name} | v${pkg.version} | ${pkg.homepage} | License: ${pkg.license} */`;
const umdExportName = 'Instafeed';
const umdUnminName = srcName.replace('.js', '.js');
const umdMinName = srcName.replace('.js', '.min.js');
const umdMapName = srcName.replace('.js', '.min.map');

const srcContent = fs.readFileSync(srcPath, { encoding: 'utf-8' });

const unMinResult = minify({
[srcName]: srcContent
}, {
warnings: 'verbose',
ie8: true,
compress: false,
output: {
preamble: preamble,
beautify: true,
comments: false,
indent_level: 2
},
mangle: false,
sourceMap: false
});

const minResult = minify({
[srcName]: unMinResult.code
}, {
warnings: 'verbose',
ie8: true,
compress: true,
output: {
preamble: preamble
},
mangle: true,
sourceMap: {
filename: minName,
url: mapName
}
});
const esUnminPath = path.join(destFolder, esUnminName);
const esMinPath = path.join(destFolder, esMinName);
const esMapPath = path.join(destFolder, esMapName);

fs.writeFileSync(unMinPath, unMinResult.code, { encoding: 'utf-8' });
fs.writeFileSync(minPath, minResult.code, { encoding: 'utf-8' });
fs.writeFileSync(mapPath, minResult.map, { encoding: 'utf-8' });
const umdUnminPath = path.join(destFolder, umdUnminName);
const umdMinPath = path.join(destFolder, umdMinName);
const umdMapPath = path.join(destFolder, umdMapName);

process.exit(0);
const preamble = `/* ${pkg.name} | v${pkg.version} | ${pkg.homepage} | License: ${pkg.license} */`;

function minify(srcMap, opts) {
const result = uglify.minify(srcMap, opts);
const result = uglifyEs.minify(srcMap, opts);

if (result.error) {
console.error(`${result.error.filename}:${result.error.line},${result.error.col}: ${result.error.message}`);
process.exit(1);
throw new Error(`error minifying`);
}

if (Array.isArray(result.warnings)) {
for (const warn of result.warnings) {
console.warn(warn);
}
}

return result;
}

async function build() {
console.log('------------------------\nBuilding ES version\n------------------------');

const esModule = await getModuleBundle(srcPath, {
format: 'es'
});

const esUnminResult = minify({
[srcName]: esModule.code
}, {
warnings: 'verbose',
ecma: 5,
ie8: true,
compress: false,
output: {
preamble: preamble,
beautify: true,
comments: false,
indent_level: 2
},
mangle: false,
sourceMap: false
});

const esMinResult = minify({
[srcName]: esUnminResult.code
}, {
warnings: 'verbose',
ecma: 5,
ie8: true,
compress: true,
output: {
preamble: preamble
},
mangle: true,
sourceMap: {
filename: esMinName,
url: esMapName
}
});

console.log('------------------------\nBuilding UMD version\n------------------------');

const umdModule = await getModuleBundle(srcPath, {
format: 'umd',
name: umdExportName,
exports: 'default'
});

const umdUnminResult = minify({
[srcName]: umdModule.code
}, {
warnings: 'verbose',
ecma: 5,
ie8: true,
compress: false,
output: {
preamble: preamble,
beautify: true,
comments: false,
indent_level: 2
},
mangle: false,
sourceMap: false
});

const umdMinResult = minify({
[srcName]: umdUnminResult.code
}, {
warnings: 'verbose',
ecma: 5,
ie8: true,
compress: true,
output: {
preamble: preamble
},
mangle: true,
sourceMap: {
filename: umdMinName,
url: umdMapName
}
});

await Promise.all([
fs.writeFile(esUnminPath, esUnminResult.code, { encoding: 'utf-8' }),
fs.writeFile(esMinPath, esMinResult.code, { encoding: 'utf-8' }),
fs.writeFile(esMapPath, esMinResult.map, { encoding: 'utf-8' }),

fs.writeFile(umdUnminPath, umdUnminResult.code, { encoding: 'utf-8' }),
fs.writeFile(umdMinPath, umdMinResult.code, { encoding: 'utf-8' }),
fs.writeFile(umdMapPath, umdMinResult.map, { encoding: 'utf-8' })
]);
}

async function getModuleBundle(filePath, outputOptions) {
const bundle = await rollup.rollup({
input: filePath
});

const { output } = await bundle.generate(outputOptions);

const chunk = output.find((chunk) => {
return chunk.fileName === path.basename(filePath);
});

if (!chunk) {
throw new Error(`no rollup output chunk found for ${filePath}`);
}

return chunk;
}

const start = process.hrtime();
build().then(() => {
const duration = process.hrtime(start);
console.log(
'------------------------\nBuild complete (%ds %dms)\n------------------------',
duration[0],
Math.round(duration[1] / 1000000)
);
process.exit(0);
}).catch((err) => {
console.error(err);
process.exit(1);
})
4 changes: 2 additions & 2 deletions src/.jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"esversion": 5,
"strict": true,
"esversion": 6,
"strict": "global",
"undef": true,
"unused": true,
"eqeqeq": true,
Expand Down
Loading

0 comments on commit bf45b7a

Please sign in to comment.