From 391c5f00ff92a3c52058476aaa19e71dab53b4cd Mon Sep 17 00:00:00 2001 From: Michael Petrinolis Date: Wed, 17 Jul 2019 04:39:59 +0300 Subject: [PATCH] feat(build-plugin): add sourcemaps and watch support for plugin development --- package-lock.json | 41 +++++-------------- .../tasks__if_babel/transpile.js | 34 +++++++++++---- .../tasks__if_typescript/transpile.ts | 8 +++- .../aurelia_project/tasks/build-plugin.ext | 36 ++++++++++++---- skeleton/plugin/package.json | 9 ++-- 5 files changed, 78 insertions(+), 50 deletions(-) diff --git a/package-lock.json b/package-lock.json index 64b7c842e..d075f14a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3470,8 +3470,7 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true, - "optional": true + "bundled": true }, "aproba": { "version": "1.2.0", @@ -3489,13 +3488,11 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true, - "optional": true + "bundled": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3508,18 +3505,15 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "concat-map": { "version": "0.0.1", - "bundled": true, - "optional": true + "bundled": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true, - "optional": true + "bundled": true }, "core-util-is": { "version": "1.0.2", @@ -3622,8 +3616,7 @@ }, "inherits": { "version": "2.0.3", - "bundled": true, - "optional": true + "bundled": true }, "ini": { "version": "1.3.5", @@ -3633,7 +3626,6 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3646,20 +3638,17 @@ "minimatch": { "version": "3.0.4", "bundled": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true, - "optional": true + "bundled": true }, "minipass": { "version": "2.3.5", "bundled": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -3676,7 +3665,6 @@ "mkdirp": { "version": "0.5.1", "bundled": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -3749,8 +3737,7 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true, - "optional": true + "bundled": true }, "object-assign": { "version": "4.1.1", @@ -3760,7 +3747,6 @@ "once": { "version": "1.4.0", "bundled": true, - "optional": true, "requires": { "wrappy": "1" } @@ -3836,8 +3822,7 @@ }, "safe-buffer": { "version": "5.1.2", - "bundled": true, - "optional": true + "bundled": true }, "safer-buffer": { "version": "2.1.2", @@ -3867,7 +3852,6 @@ "string-width": { "version": "1.0.2", "bundled": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3885,7 +3869,6 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3924,13 +3907,11 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true, - "optional": true + "bundled": true }, "yallist": { "version": "3.0.3", - "bundled": true, - "optional": true + "bundled": true } } }, diff --git a/skeleton/cli-bundler/aurelia_project/tasks__if_babel/transpile.js b/skeleton/cli-bundler/aurelia_project/tasks__if_babel/transpile.js index 67c7601ed..aef05eeae 100644 --- a/skeleton/cli-bundler/aurelia_project/tasks__if_babel/transpile.js +++ b/skeleton/cli-bundler/aurelia_project/tasks__if_babel/transpile.js @@ -7,18 +7,23 @@ import cache from 'gulp-cache'; import project from '../aurelia.json'; import fs from 'fs'; import through from 'through2'; -import {CLIOptions, build, Configuration} from 'aurelia-cli'; +import { CLIOptions, build, Configuration } from 'aurelia-cli'; +import gulpSourcemaps from 'gulp-sourcemaps'; let env = CLIOptions.getEnvironment(); const buildOptions = new Configuration(project.build.options); const useCache = buildOptions.isApplicable('cache'); function configureEnvironment() { - return gulp.src(`aurelia_project/environments/${env}.js`, {since: gulp.lastRun(configureEnvironment)}) + return gulp.src(`aurelia_project/environments/${env}.js`, { + since: gulp.lastRun(configureEnvironment) + }) .pipe(rename('environment.js')) .pipe(through.obj(function (file, _, cb) { // https://github.com/aurelia/cli/issues/1031 - fs.unlink(`${project.paths.root}/${file.relative}`, function () { cb(null, file); }); + fs.unlink(`${project.paths.root}/${file.relative}`, function () { + cb(null, file); + }); })) .pipe(gulp.dest(project.paths.root)); } @@ -28,11 +33,18 @@ function buildJavaScript() { if (useCache) { // the cache directory is "gulp-cache/projName-env" inside folder require('os').tmpdir() // use command 'au clear-cache' to purge all caches - transpile = cache(transpile, {name: project.name + '-' + env}); + transpile = cache(transpile, { + name: project.name + '-' + env + }); } - return gulp.src(project.transpiler.source, {sourcemaps: true, since: gulp.lastRun(buildJavaScript)}) - .pipe(plumber({errorHandler: notify.onError('Error: <%= error.message %>')})) + return gulp.src(project.transpiler.source, { + sourcemaps: true, + since: gulp.lastRun(buildJavaScript) + }) + .pipe(plumber({ + errorHandler: notify.onError('Error: <%= error.message %>') + })) .pipe(transpile) .pipe(build.bundle()); } @@ -49,13 +61,21 @@ export function buildPluginJavaScript(dest, format) { if (format === 'commonjs') { opts = { plugins: [ - ['@babel/plugin-transform-modules-commonjs', {loose: true}] + ['@babel/plugin-transform-modules-commonjs', { + loose: true + }] ] }; } const transpile = babel(opts); + return gulp.src(project.plugin.source.js) + .pipe(gulpSourcemaps.init()) .pipe(transpile) + .pipe(gulpSourcemaps.write('.', { + includeContent: false, + sourceRoot: '../../src/' + })) .pipe(gulp.dest(dest)); }; } diff --git a/skeleton/cli-bundler/aurelia_project/tasks__if_typescript/transpile.ts b/skeleton/cli-bundler/aurelia_project/tasks__if_typescript/transpile.ts index 0cf31fd3c..016e330ef 100644 --- a/skeleton/cli-bundler/aurelia_project/tasks__if_typescript/transpile.ts +++ b/skeleton/cli-bundler/aurelia_project/tasks__if_typescript/transpile.ts @@ -6,12 +6,13 @@ import * as ts from 'gulp-typescript'; import * as project from '../aurelia.json'; import * as fs from 'fs'; import * as through from 'through2'; -import {CLIOptions, build} from 'aurelia-cli'; +import { CLIOptions, build, Configuration } from 'aurelia-cli'; +import * as gulpSourcemaps from 'gulp-sourcemaps'; function configureEnvironment() { let env = CLIOptions.getEnvironment(); - return gulp.src(`aurelia_project/environments/${env}.ts`, {since: gulp.lastRun(configureEnvironment)}) + return gulp.src(`aurelia_project/environments/${env}.ts`, { since: gulp.lastRun(configureEnvironment) }) .pipe(rename('environment.ts')) .pipe(through.obj(function (file, _, cb) { // https://github.com/aurelia/cli/issues/1031 @@ -50,9 +51,12 @@ export function buildPluginJavaScript(dest, format) { typescript: require('typescript'), module: format }); + return gulp.src(project.transpiler.dtsSource) .pipe(gulp.src(project.plugin.source.js)) + .pipe(gulpSourcemaps.init()) .pipe(typescriptCompiler()) + .pipe(gulpSourcemaps.write('.', { includeContent: false, sourceRoot: '../../src/' })) .pipe(gulp.dest(dest)); }; } diff --git a/skeleton/plugin/aurelia_project/tasks/build-plugin.ext b/skeleton/plugin/aurelia_project/tasks/build-plugin.ext index 6a57ad3ba..7e1193153 100644 --- a/skeleton/plugin/aurelia_project/tasks/build-plugin.ext +++ b/skeleton/plugin/aurelia_project/tasks/build-plugin.ext @@ -6,17 +6,17 @@ import del from 'del'; import * as gulp from 'gulp'; import * as del from 'del'; // @endif -import {pluginMarkup} from './process-markup'; -import {pluginCSS} from './process-css'; -import {pluginJson} from './process-json'; -import {buildPluginJavaScript} from './transpile'; +import { pluginMarkup } from './process-markup'; +import { pluginCSS } from './process-css'; +import { pluginJson } from './process-json'; +import { buildPluginJavaScript } from './transpile'; +import { CLIOptions } from 'aurelia-cli'; function clean() { return del('dist'); } -export default gulp.series( - clean, +let build = gulp.series( gulp.parallel( // package.json "module" field pointing to dist/native-modules/index.js pluginMarkup('dist/native-modules'), @@ -29,6 +29,26 @@ export default gulp.series( pluginCSS('dist/commonjs'), pluginJson('dist/commonjs'), buildPluginJavaScript('dist/commonjs', 'commonjs'), - ), - () => console.log('Finish building Aurelia plugin to dist/commonjs and dist/native-modules') + ), (done) => { + console.log('Finish building Aurelia plugin to dist/commonjs and dist/native-modules.'); + done(); + } ); + +let main; +if (CLIOptions.hasFlag('watch')) { + main = gulp.series( + clean, + () => { + console.log('Watching plugin sources for changes ...'); + return gulp.watch('src/**/*', { ignoreInitial: false }, build); + } + ); +} else { + main = gulp.series( + clean, + build + ); +} + +export { main as default }; diff --git a/skeleton/plugin/package.json b/skeleton/plugin/package.json index ab876e7fb..45710f655 100644 --- a/skeleton/plugin/package.json +++ b/skeleton/plugin/package.json @@ -6,6 +6,7 @@ "license": "UNLICENSED", "scripts": { "build": "au build-plugin", + "watch": "au build-plugin --watch", "prepare": "npm run build", // @if feat.karma || feat.jest "pretest": "au lint", @@ -14,9 +15,11 @@ }, "files": [ - "dist" + "dist", + "src" ], "devDependencies": { - "del": "" + "del": "", + "gulp-sourcemaps": "" } -} \ No newline at end of file +}