1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-01-17 20:38:15 +01:00
php-web-maker/gulpfile.js

320 lines
8.1 KiB
JavaScript
Raw Normal View History

2018-01-13 14:54:49 +05:30
/*eslint-env node*/
2018-01-13 23:09:35 +05:30
const fs = require('fs');
const gulp = require('gulp');
2022-09-08 09:39:48 +05:30
const { parallel, series, watch } = require('gulp');
2018-01-13 23:09:35 +05:30
const useref = require('gulp-useref');
const cleanCSS = require('gulp-clean-css');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
2018-01-13 23:09:35 +05:30
const babelMinify = require('babel-minify');
2018-06-16 19:18:24 +05:30
const child_process = require('child_process');
const merge = require('merge-stream');
2022-09-08 09:39:48 +05:30
// const zip = require('gulp-zip');
2018-06-23 09:42:42 +05:30
var packageJson = JSON.parse(fs.readFileSync('./package.json'));
const connect = require('gulp-connect');
2024-02-29 18:02:21 +05:30
const APP_FOLDER = 'create';
2017-11-08 01:04:59 +05:30
2018-01-13 23:09:35 +05:30
function minifyJs(fileName) {
2018-01-22 12:03:06 +05:30
const content = fs.readFileSync(fileName, 'utf8');
const minifiedContent = babelMinify(
content,
{ mangle: content.length < 700000 },
{ sourceMaps: false }
).code;
2018-01-13 23:09:35 +05:30
fs.writeFileSync(fileName, minifiedContent);
2018-01-22 12:03:06 +05:30
console.log(
2021-01-18 16:32:06 +05:30
`[${fileName}]: ${content.length / 1024}K -> ${
minifiedContent.length / 1024
}K`
2018-01-22 12:03:06 +05:30
);
2018-01-13 23:09:35 +05:30
}
2022-09-08 09:39:48 +05:30
function runWebpack() {
return child_process.exec('npm run build', (error, stdout, stderr) => {
console.log('runWebpack', error, stdout, stderr);
});
}
2021-01-18 16:32:06 +05:30
gulp.task('runWebpack', function () {
2021-03-31 03:13:23 +05:30
return child_process.exec('npm run build', (error, stdout, stderr) => {
console.log('runWebpack', error, stdout, stderr);
});
2018-06-16 19:18:24 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('copyFiles', function () {
2018-06-16 19:18:24 +05:30
return merge(
gulp
.src('src/lib/codemirror/theme/*')
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`${APP_FOLDER}/lib/codemirror/theme`)),
2018-06-16 19:18:24 +05:30
gulp
.src('src/lib/codemirror/mode/**/*')
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`${APP_FOLDER}/lib/codemirror/mode`)),
gulp
.src('src/lib/transpilers/*')
.pipe(gulp.dest(`${APP_FOLDER}/lib/transpilers`)),
gulp
.src('src/lib/prettier-worker.js')
.pipe(gulp.dest(`${APP_FOLDER}/lib/`)),
gulp
.src('src/lib/prettier/*')
.pipe(gulp.dest(`${APP_FOLDER}/lib/prettier`)),
gulp
.src(['!src/lib/monaco/monaco.bundle.js', 'src/lib/monaco/**/*'])
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`${APP_FOLDER}/lib/monaco`)),
gulp.src('src/lib/screenlog.js').pipe(gulp.dest(`${APP_FOLDER}/lib`)),
gulp.src('icons/*').pipe(gulp.dest(`${APP_FOLDER}/icons`)),
gulp.src('src/assets/*').pipe(gulp.dest(`${APP_FOLDER}/assets`)),
gulp.src('src/templates/*').pipe(gulp.dest(`${APP_FOLDER}/templates`)),
gulp.src('preview/*').pipe(gulp.dest(`${APP_FOLDER}/preview`)),
gulp
.src([
'src/preview.html',
2024-04-10 13:58:08 +05:30
'src/indexpm.html',
'src/detached-window.js',
'src/icon-48.png',
'src/icon-128.png',
2021-04-01 14:45:32 +05:30
'src/manifest.json'
])
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(APP_FOLDER)),
2024-02-29 18:02:21 +05:30
gulp.src('build/*').pipe(gulp.dest(APP_FOLDER)),
// Following CSS are copied to build/ folder where they'll be referenced by
// useRef plugin to concat into one.
gulp
.src('src/lib/codemirror/lib/codemirror.css')
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`build/lib/codemirror/lib`)),
gulp
.src('src/lib/codemirror/addon/hint/show-hint.css')
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`build/lib/codemirror/addon/hint`)),
gulp
.src('src/lib/codemirror/addon/fold/foldgutter.css')
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`build/lib/codemirror/addon/fold`)),
gulp
.src('src/lib/codemirror/addon/dialog/dialog.css')
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(`build/lib/codemirror/addon/dialog`)),
2018-06-16 19:18:24 +05:30
gulp.src('src/lib/hint.min.css').pipe(gulp.dest('build/lib')),
gulp.src('src/lib/inlet.css').pipe(gulp.dest('build/lib')),
// gulp.src('src/style.css').pipe(gulp.dest('build')),
2018-06-16 19:18:24 +05:30
gulp
.src([
'src/FiraCode.ttf',
'src/FixedSys.ttf',
'src/Inconsolata.ttf',
2021-04-01 14:45:32 +05:30
'src/Monoid.ttf'
])
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(APP_FOLDER))
2018-06-16 19:18:24 +05:30
);
2017-11-08 01:04:59 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('useRef', function () {
2024-02-29 18:02:21 +05:30
return gulp
.src('build/index.html')
.pipe(useref())
.pipe(gulp.dest(APP_FOLDER));
2017-11-08 01:04:59 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('concatSwRegistration', function () {
2022-04-06 20:29:39 +05:30
const bundleFile = fs
2024-02-29 18:02:21 +05:30
.readdirSync(APP_FOLDER)
2022-04-06 20:29:39 +05:30
.filter(allFilesPaths => allFilesPaths.match(/bundle.*\.js$/) !== null)[0];
console.log('matched', bundleFile);
return gulp
2024-02-29 18:02:21 +05:30
.src(['src/service-worker-registration.js', `${APP_FOLDER}/${bundleFile}`])
2022-04-06 20:29:39 +05:30
.pipe(concat(bundleFile))
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(APP_FOLDER));
});
2021-01-18 16:32:06 +05:30
gulp.task('minify', function () {
2022-04-06 20:29:39 +05:30
// minifyJs('app/script.js');
// minifyJs('app/vendor.js');
2024-02-29 18:02:21 +05:30
minifyJs(`${APP_FOLDER}/lib/screenlog.js`);
2018-01-13 23:09:35 +05:30
return gulp
2024-02-29 18:02:21 +05:30
.src(`${APP_FOLDER}/*.css`)
2018-01-22 12:03:06 +05:30
.pipe(
cleanCSS(
{
2021-04-01 14:45:32 +05:30
debug: true
},
2021-04-01 14:45:32 +05:30
details => {
console.log(
2021-01-18 16:32:06 +05:30
`${details.name}: ${details.stats.originalSize} 👉🏼 ${details.stats.minifiedSize}`
);
}
)
2018-01-22 12:03:06 +05:30
)
2024-02-29 18:02:21 +05:30
.pipe(gulp.dest(APP_FOLDER));
2018-01-13 23:09:35 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('fixIndex', function (cb) {
2018-06-16 19:18:24 +05:30
var contents = fs.readFileSync('build/index.html', 'utf8');
// Replace hashed-filename script tags with unhashed ones
contents = contents.replace(
/\<\!\-\- SCRIPT-TAGS \-\-\>[\S\s]*?\<\!\-\- END-SCRIPT-TAGS \-\-\>/,
'<script defer src="vendor.js"></script><script defer src="script.js"></script>'
);
// vendor.hash.js gets created outside our markers, so remove it
contents = contents.replace(
/\<script src="[\S\s]*?vendor\.[\S\s]*?\<\/script\>/,
''
);
2021-04-01 14:45:32 +05:30
// fs.writeFileSync('build/index.html', contents, 'utf8');
2021-01-18 16:32:06 +05:30
cb();
2018-06-16 19:18:24 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('generate-service-worker', function (callback) {
2017-11-19 23:47:02 +05:30
var swPrecache = require('sw-precache');
2024-02-29 18:02:21 +05:30
var rootDir = APP_FOLDER;
2017-11-08 01:04:59 +05:30
2017-11-19 23:47:02 +05:30
swPrecache.write(
`${rootDir}/service-worker.js`,
{
2017-11-19 23:47:02 +05:30
staticFileGlobs: [
2021-04-01 14:45:32 +05:30
rootDir + '/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}'
2017-11-19 23:47:02 +05:30
],
2018-01-14 00:39:03 +05:30
stripPrefix: `${rootDir}/`,
// has to be increased to around 2.8mb for sass.worker.js
2021-04-01 14:45:32 +05:30
maximumFileSizeToCacheInBytes: 2900000
2017-11-19 23:47:02 +05:30
},
callback
);
2017-11-08 01:04:59 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('packageExtension', function () {
2019-03-15 17:21:30 +05:30
child_process.execSync('rm -rf extension');
2024-02-29 18:02:21 +05:30
child_process.execSync(`cp -R ${APP_FOLDER} extension`);
2018-06-21 01:01:48 +05:30
child_process.execSync('cp src/manifest.json extension');
child_process.execSync('cp src/options.js extension');
child_process.execSync('cp src/options.html extension');
child_process.execSync('cp src/eventPage.js extension');
child_process.execSync('cp src/icon-16.png extension');
child_process.execSync('cp offscreen.html extension');
child_process.execSync('cp offscreen.js extension');
child_process.execSync('rm -rf extension/service-worker.js');
2018-06-21 01:01:48 +05:30
return merge(
gulp
.src('build/bundle.*.js')
.pipe(rename('script.js'))
2021-04-01 14:45:32 +05:30
.pipe(gulp.dest('extension'))
2018-06-21 01:01:48 +05:30
2021-04-01 14:45:32 +05:30
// gulp
// .src('extension/**/*')
// .pipe(zip(`extension-${packageJson.version}.zip`))
// .pipe(gulp.dest('./'))
2018-06-21 01:01:48 +05:30
);
});
2021-01-18 16:32:06 +05:30
gulp.task('buildWebsite', function () {
2021-03-31 03:13:23 +05:30
return child_process.exec(
'npm run build-website',
(error, stdout, stderr) => {
console.log('buildWebsite', error, stdout, stderr);
}
);
});
2021-01-18 16:32:06 +05:30
gulp.task('buildDistFolder', function (cb) {
child_process.execSync('rm -rf dist');
child_process.execSync('mv packages/website/_site dist');
2024-02-29 18:02:21 +05:30
child_process.execSync(`mv ${APP_FOLDER} dist/`);
2021-01-18 16:32:06 +05:30
cb();
});
2021-01-18 16:32:06 +05:30
gulp.task('cleanup', function () {
2024-05-06 12:49:01 +05:30
return child_process.exec('rm -rf build create');
2018-06-16 19:18:24 +05:30
});
2021-01-18 16:32:06 +05:30
gulp.task('start-preview-server', function () {
connect.server({
root: 'preview',
port: 7888,
2021-04-01 14:45:32 +05:30
https: false
});
});
2024-05-04 11:54:45 +05:30
// TODO: fix tasks. eg. buildWebsite isn't needed anymore
2021-01-18 16:32:06 +05:30
exports.release = series(
parallel('runWebpack', 'buildWebsite'),
'copyFiles',
'fixIndex',
'useRef',
'concatSwRegistration',
'minify',
'generate-service-worker',
'packageExtension',
'buildDistFolder',
'cleanup',
function (callback, error) {
2021-01-18 16:32:06 +05:30
if (error) {
console.log(error.message);
} else {
console.log('RELEASE FINISHED SUCCESSFULLY');
2019-01-28 18:10:39 +05:30
}
2018-06-16 19:18:24 +05:30
2021-01-18 16:32:06 +05:30
callback(error);
}
);
exports.devRelease = gulp.series(
parallel('runWebpack', 'buildWebsite'),
'copyFiles',
'fixIndex',
'useRef',
2021-04-01 14:45:32 +05:30
// 'concatSwRegistration',
// 'generate-service-worker',
'buildDistFolder',
'cleanup',
function (callback, error) {
if (error) {
console.log(error.message);
} else {
console.log('DEV-RELEASE FINISHED SUCCESSFULLY');
}
callback(error);
}
2021-01-18 16:32:06 +05:30
);
2022-09-08 09:39:48 +05:30
const buildExtension = series(
'runWebpack',
'copyFiles',
'fixIndex',
'useRef',
2024-05-04 11:54:45 +05:30
'packageExtension',
'cleanup'
2022-09-08 09:39:48 +05:30
);
function runWatcher(cb) {
2024-02-20 10:59:07 +05:30
return watch(
['src/**/*.js', 'src/**/*.jsx', 'src/**/*.json'],
function (cbb) {
buildExtension();
cbb();
}
);
2022-09-08 09:39:48 +05:30
cb();
}
exports.setupExtension = series(
buildExtension,
runWatcher,
function (callback, error) {
if (error) {
console.log(error.message);
} else {
console.log('RELEASE FINISHED SUCCESSFULLY');
}
callback(error);
}
);
2023-05-29 01:28:04 +05:30
exports.buildExtension = buildExtension;