Implement @pegjs/bundler

master
Futago-za Ryuu 6 years ago
parent b2e6958698
commit da3ea8e47d

@ -5,4 +5,5 @@
node_modules
/website/*/**
/docs/.vuepress/dist
/docs/.vuepress/public
/dist

1
.gitignore vendored

@ -1,6 +1,7 @@
/.idea
/packages/pegjs/dist
/docs/.vuepress/dist
/docs/.vuepress/public
/dist
/examples/*.js
node_modules

@ -2,7 +2,6 @@
"name": "pegjs-dev",
"private": true,
"workspaces": [
"editor",
"packages/*",
"tools/*",
"test"
@ -14,7 +13,8 @@
"build-docs": "vuepress build docs",
"watch-docs": "vuepress dev docs",
"build-parser": "pegjs -c src/pegjs.config.js",
"generate-dist": "node tools/bundle/create-pegjs-dist",
"build-bundles": "bundle",
"watch-bundles": "bundle --watch",
"clean": "rimraf packages/pegjs/dist examples/*.js",
"impact": "node tools/impact master",
"now-build": "npm run build-docs",

@ -1 +0,0 @@
This directory contains a package bundler for PEG.js that is actually a thin layer around [Rollup](https://rollupjs.org/).

@ -1,42 +0,0 @@
"use strict";
const bundle = require( "./index" );
const dedent = require( "dedent" );
const peg = require( "pegjs" );
const HEADER = dedent`
/**
* PEG.js v${ peg.VERSION }
* https://pegjs.org/
*
* Copyright (c) 2010-2016 David Majda
* Copyright (c) 2017+ Futago-za Ryuu
*
* Released under the MIT License.
*/
/* eslint-disable */
`;
function generate( target ) {
bundle( {
banner: HEADER,
format: "umd",
name: "peg",
source: "packages/pegjs/lib/peg.js",
target: `packages/pegjs/dist/${ target }`,
silent: process.argv.includes( "-s" ),
} );
}
// es5 release
generate( "peg.js" );
// es5 release (minified)
generate( "peg.min.js" );

@ -1,137 +0,0 @@
"use strict";
const babelOptions = require( "./babelOptions" );
const ms = require( "pretty-ms" );
const rollup = require( "rollup" );
const babel = require( "rollup-plugin-babel" );
const commonjs = require( "rollup-plugin-commonjs" );
const json = require( "rollup-plugin-json" );
const multiEntry = require( "rollup-plugin-multi-entry" );
const resolve = require( "rollup-plugin-node-resolve" );
const { terser } = require( "rollup-plugin-terser" );
// pretty-path
function pp( p ) {
return ( Array.isArray( p ) ? p.join( ", " ) : p )
.replace( process.cwd(), "" )
.replace( /\\/g, "/" )
.replace( /^\//, "" );
}
// based on https://github.com/rollup/rollup/blob/master/bin/src/logging.ts
function handleError( err ) {
let description = err.message || err;
if ( err.name ) description = `${ err.name }: ${ description }`;
const message = err.plugin ? `(${ err.plugin } plugin) ${ description }` : description;
console.error( message.toString() );
if ( err.url ) console.error( err.url );
if ( err.loc )
console.error( `${ err.loc.file || err.id } (${ err.loc.line }:${ err.loc.column })` );
else if ( err.id )
console.error( err.id );
if ( err.frame ) console.error( err.frame );
if ( err.stack ) console.error( err.stack );
}
module.exports = bundle => {
const log = bundle.silent === true ? () => void 0 : console.info;
const plugins = [
resolve(),
commonjs(),
json( { namedExports: false } ),
babel( babelOptions ),
];
if ( bundle.source.includes( "*" ) ) plugins.unshift( multiEntry() );
if ( bundle.target.endsWith( ".min.js" ) )
plugins.push( terser( { output: { comments: /MIT License/ } } ) );
const config = {
input: bundle.source,
output: {
banner: bundle.banner,
file: bundle.target,
format: bundle.format || "iife",
interop: false,
name: bundle.name,
},
plugins,
onwarn( warning, warn ) {
if ( warning.code === "CIRCULAR_DEPENDENCY" ) return void 0;
if ( warning.code === "NAMESPACE_CONFLICT" ) return void 0;
if ( warning.code === "EVAL" ) return void 0;
if ( bundle.silent !== true ) warn( warning );
},
treeshake: {
propertyReadSideEffects: true,
},
watch: {
exclude: "node_modules/**",
},
};
if ( bundle.watch !== true ) {
return rollup.rollup( config )
.then( bundle => bundle.write( config.output ) )
.then( () => {
log( `@pegjs/bundle-generator > created ${ pp( bundle.target ) }` );
} )
.catch( handleError );
}
const watcher = rollup.watch( config );
// https://rollupjs.org/guide/en#rollup-watch
watcher.on( "event", event => {
switch ( event.code ) {
case "BUNDLE_START":
log( `@pegjs/bundle-generator > watching ${ pp( event.input ) }` );
break;
case "BUNDLE_END":
log( `@pegjs/bundle-generator > created ${ pp( event.output ) } in ${ ms( event.duration ) }` );
break;
case "ERROR":
handleError( event.error );
break;
case "FATAL":
console.error( "@pegjs/bundle-generator > Fatel Error!" );
handleError( event.error );
break;
}
} );
process.on( "exit", () => watcher.close() );
return watcher;
};

@ -1,19 +0,0 @@
{
"name": "@pegjs/bundle-generator",
"version": "2.1.0",
"private": true,
"main": "index.js",
"dependencies": {
"@babel/core": "7.2.2",
"@babel/preset-env": "7.3.1",
"dedent": "0.7.0",
"pretty-ms": "4.0.0",
"rollup": "1.1.2",
"rollup-plugin-babel": "4.3.2",
"rollup-plugin-commonjs": "9.2.0",
"rollup-plugin-json": "3.1.0",
"rollup-plugin-multi-entry": "2.1.0",
"rollup-plugin-node-resolve": "4.0.0",
"rollup-plugin-terser": "4.0.4"
}
}

@ -1,12 +1,12 @@
"use strict";
// @babel/core
const babelOptions = {
module.exports = {
"comments": false,
"compact": false,
"presets": [
[
"@babel/preset-env",
require.resolve( "@babel/preset-env" ),
{
"modules": false,
"targets": {
@ -15,11 +15,5 @@ const babelOptions = {
}
]
]
};
// rollup-plugin-babel
babelOptions.babelrc = false;
babelOptions.exclude = "node_modules/**";
babelOptions.runtimeHelpers = true;
module.exports = babelOptions;
};

@ -0,0 +1,5 @@
#!/usr/bin/env node
"use strict";
require( "webpack-cli" );

@ -0,0 +1,21 @@
{
"name": "@pegjs/bundler",
"version": "3.0.0",
"private": true,
"bin": {
"bundle": "bundler.js"
},
"devDependencies": {
"@babel/core": "7.2.2",
"@babel/preset-env": "7.3.1",
"@futagoza/child-process": "1.0.0",
"babel-loader": "8.0.5",
"dedent": "0.7.0",
"glob": "7.1.3",
"webpack": "4.29.3",
"webpack-cli": "3.2.3"
},
"engines": {
"node": ">= 6"
}
}

@ -0,0 +1,91 @@
"use strict";
const dedent = require( "dedent" );
const glob = require( "glob" );
const path = require( "path" );
const peg = require( "pegjs" );
const webpack = require( "webpack" );
const HEADER = dedent`
/**
* PEG.js v${ peg.VERSION }, [hash]
* https://pegjs.org/
*
* Copyright (c) 2010-2016 David Majda
* Copyright (c) 2017+ Futago-za Ryuu
*
* Released under the MIT License.
*/
/* eslint-disable */
`;
/**
*
* @param {{ entry: string|{}, output: string }} param0
*/
function target( { entry, output } ) {
if ( typeof entry !== "object" ) entry = [ entry ];
const cwd = process.cwd();
Object
.keys( entry )
.forEach( name => {
const value = entry[ name ];
entry[ name ] = value.includes( "*" )
? glob
.sync( value )
.map( p => path.join( cwd, p ) )
: value;
} );
return {
mode: process.argv.includes( "--mode=development" ) ? "development" : "production",
entry: entry,
output: {
path: path.dirname( path.resolve( cwd, output ) ),
filename: path.basename( output ),
libraryTarget: "umd",
umdNamedDefine: true,
sourcePrefix: " ",
globalObject: "typeof self !== 'undefined' ? self : window",
},
optimization: {
minimize: output.endsWith( ".min.js" ),
},
performance: {
hints: false,
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: require.resolve( "babel-loader" ),
options: require( "./babel.config.js" ),
},
},
],
},
plugins: [
new webpack.BannerPlugin( {
banner: HEADER,
raw: true,
} )
],
};
}
module.exports = target;

@ -0,0 +1,34 @@
"use strict";
const target = require( "@pegjs/bundler/target" );
module.exports = [
/* packages/pegjs/dist/peg.js */
target( {
entry: require.resolve( "pegjs" ),
output: "packages/pegjs/dist/peg.js",
} ),
/* packages/pegjs/dist/peg.min.js */
target( {
entry: require.resolve( "pegjs" ),
output: "packages/pegjs/dist/peg.min.js",
} ),
/* https://pegjs.org/*-bundle.min.js */
target( {
entry: {
"benchmark": require.resolve( "@pegjs/benchmark-suite/browser.js" ),
"test": "test/**/*.+(spec|test).js",
},
output: "docs/.vuepress/public/[name]-bundle.min.js",
} ),
];

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save