UNPKG

turbo-gulp

Version:

Gulp tasks to boost high-quality projects.

100 lines (99 loc) 3.71 kB
/** * This module defines the tasks to create webpack bundles. * * @module task-generators/build-webpack */ /** (Placeholder comment, see christopherthielen/typedoc-plugin-external-module-name#6) */ import fancyLog from "fancy-log"; import { Minimatch } from "minimatch"; import { posix as path, resolve as sysResolvePath } from "path"; import PluginError from "plugin-error"; import webpack from "webpack"; import webpackMerge from "webpack-merge"; import webpackStream from "webpack-stream"; import * as matcher from "../utils/matcher"; /** * Return the canonical name of the build-webpack task according to the target name. * * @param targetName Current target name * @returns {string} The canonical of the build-webpack task for the provided target */ export function getTaskName(targetName) { return `${targetName}:build:webpack`; } export function generateTask(gulp, options) { const entryFile = options.entry + ".js"; let curWebpack = webpack; let userConfiguration = {}; if (options.webpackOptions !== undefined) { if (options.webpackOptions.webpack !== undefined) { curWebpack = options.webpackOptions.webpack; } if (options.webpackOptions.configuration !== undefined) { userConfiguration = options.webpackOptions.configuration; } } const angularWebpackConfig = { context: sysResolvePath(options.projectRoot), target: "web", resolve: { extensions: [".js", ".json"], }, module: { loaders: [ { test: /\.component\.js$/, loaders: ["angular2-template-loader"], include: [sysResolvePath(options.srcDir)], }, { test: /\.json$/, loaders: ["json-loader"], }, { test: /\.(html|css)$/, loaders: ["raw-loader"], }, ], }, plugins: [ new curWebpack.ContextReplacementPlugin( // The (\\|\/) piece accounts for path separators in posix and Windows /angular(\\|\/)core(\\|\/)src(\\|\/)linker/, sysResolvePath(options.srcDir), {}), ], node: { global: true, __dirname: true, __filename: true, process: true, Buffer: true, }, devtool: "inline-source-map", output: { filename: "[name].js", }, }; const task = function () { return gulp .src([path.join(options.srcDir, entryFile)], { base: options.srcDir }) .pipe(webpackStream( // TODO: Remove `as any` once webpackMerge's typing support the latest version of Webpack (with `EntryFunc`) webpackMerge(angularWebpackConfig, userConfiguration), // TODO: Remove `as any` once gulpWebpack's typing support the latest version of Webpack (with `EntryFunc`) curWebpack, (err, stats) => { // TODO: Check if err is `null` or `undefined` (success) and type properly if (err) { throw new PluginError("_build:webpack", err); } fancyLog("[_build:webpack]", stats.toString({ colors: true })); })) .pipe(gulp.dest(options.buildDir)); }; task.displayName = "_build:webpack"; return task; } export function watch(gulp, options) { const buildTask = generateTask(gulp, options); const sources = matcher.asString(matcher.join(options.srcDir, new Minimatch("**/*"))); return gulp.watch(sources, { cwd: options.srcDir }, buildTask); }