Skip to content

Commit 8da690d

Browse files
committed
olo-gulp-helpers: allow for injecting a few webpack options
1 parent 2f615fa commit 8da690d

File tree

3 files changed

+72
-53
lines changed

3 files changed

+72
-53
lines changed

packages/olo-gulp-helpers/helpers/scripts.js

+65-48
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,53 @@ function createBundle(bundleName, bundleFiles, outputPath, currentDirectory, wat
5050
.pipe(gulp.dest(outputPath));
5151
}
5252

53-
function createWebpackBundle(bundleName, entryScriptPath, outputPath, watchMode) {
54-
console.log('Creating webpack script bundle: ' + bundleName);
55-
53+
function createWebpackManifestWriter(bundleName, watchMode) {
54+
return function(stats) {
55+
if (!watchMode && stats.compilation.errors && stats.compilation.errors.length) {
56+
console.error('Failed to generate webpack: ' + bundleName);
57+
58+
stats.compilation.errors.forEach(function(error) {
59+
console.error('in ' + error.file + ':');
60+
console.error(error.message);
61+
});
62+
63+
throw new Error('Error generating webpack');
64+
}
65+
66+
// TODO: introduce a file lock here to avoid any concurrency issues (for now we just process these serially)
67+
var manifestPath = path.join(process.cwd(), 'rev-manifest.json');
68+
69+
var hashedFileName = Object.keys(stats.compilation.assets)
70+
.filter(function(key) { return key.endsWith('.js'); })[0];
71+
72+
var manifestContents = fs.existsSync(manifestPath) ? JSON.parse(fs.readFileSync(manifestPath, 'utf8')) : {};
73+
manifestContents[bundleName] = hashedFileName;
74+
75+
fs.writeFileSync(manifestPath, JSON.stringify(manifestContents, null, ' '));
76+
};
77+
}
78+
79+
function createWebpackConfig(bundleName, entryScriptPath, watchMode, additionalWebpackConfig) {
80+
var webpackConfig = additionalWebpackConfig || {};
5681
var baseName = bundleName.replace(/(.*)\..*$/, '$1');
57-
58-
return gulp.src(entryScriptPath)
59-
.pipe(gulpif(watchMode, plumber()))
60-
.pipe(gulpWebpack({
61-
devtool: 'source-map',
62-
entry: { bundle: entryScriptPath },
63-
exclude: {},
64-
output: { filename: baseName + '-[chunkhash].js' },
65-
watch: watchMode,
66-
module: {
67-
loaders: [{
68-
test: /\.ts(x?)$/,
69-
loaders: ['ts'],
70-
exclude: /(node_modules)/
71-
}]
72-
},
73-
plugins: _.without([
82+
var loaders = _.concat([{
83+
test: /\.ts(x?)$/,
84+
loaders: ['ts'],
85+
exclude: /(node_modules)/
86+
}], webpackConfig.loaders || []);
87+
88+
return {
89+
devtool: 'source-map',
90+
entry: { bundle: entryScriptPath },
91+
exclude: {},
92+
output: { filename: baseName + '-[chunkhash].js' },
93+
watch: watchMode,
94+
module: {
95+
loaders: loaders
96+
},
97+
externals: webpackConfig.externals,
98+
plugins: _.chain(webpackConfig.plugins || [])
99+
.concat([
74100
new WebpackMd5Hash(),
75101
new webpack.NoErrorsPlugin(),
76102
new webpack.DefinePlugin({
@@ -82,35 +108,25 @@ function createWebpackBundle(bundleName, entryScriptPath, outputPath, watchMode)
82108
compress: { warnings: false }
83109
}),
84110
function() {
85-
this.plugin('done', function(stats) {
86-
if (!watchMode && stats.compilation.errors && stats.compilation.errors.length) {
87-
console.error('Failed to generate webpack: ' + bundleName);
88-
89-
stats.compilation.errors.forEach(function(error) {
90-
console.error('in ' + error.file + ':');
91-
console.error(error.message);
92-
});
93-
94-
throw new Error('Error generating webpack');
95-
}
96-
97-
// TODO: introduce a file lock here to avoid any concurrency issues (for now we just process these serially)
98-
var manifestPath = path.join(process.cwd(), 'rev-manifest.json');
99-
100-
var hashedFileName = Object.keys(stats.compilation.assets)
101-
.filter(function(key) { return key.endsWith('.js'); })[0];
102-
103-
var manifestContents = fs.existsSync(manifestPath) ? JSON.parse(fs.readFileSync(manifestPath, 'utf8')) : {};
104-
manifestContents[bundleName] = hashedFileName;
105-
106-
fs.writeFileSync(manifestPath, JSON.stringify(manifestContents, null, ' '));
107-
});
111+
this.plugin('done', createWebpackManifestWriter(bundleName, watchMode));
108112
}
109-
], undefined),
110-
resolve: {
111-
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.jsx']
112-
}
113-
}))
113+
])
114+
.without(undefined)
115+
.value(),
116+
resolve: {
117+
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.jsx']
118+
}
119+
};
120+
}
121+
122+
function createWebpackBundle(bundleName, entryScriptPath, outputPath, watchMode, additionalWebpackConfig) {
123+
console.log('Creating webpack script bundle: ' + bundleName);
124+
125+
var webpackConfig = createWebpackConfig(bundleName, entryScriptPath, watchMode, additionalWebpackConfig);
126+
127+
return gulp.src(entryScriptPath)
128+
.pipe(gulpif(watchMode, plumber()))
129+
.pipe(gulpWebpack(webpackConfig))
114130
.pipe(gulp.dest(outputPath));
115131
}
116132

@@ -130,6 +146,7 @@ function runKarmaTests(config, callback) {
130146
'**/*.tsx': ['webpack']
131147
},
132148
webpack: {
149+
plugins: config.webpack.plugins || [],
133150
module: {
134151
loaders: _.concat(
135152
[{

packages/olo-gulp-helpers/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ var currentDirectory = process.cwd();
1313
var bundleDefaults = {
1414
assetConfigPath: './asset-config.json',
1515
outputPath: './Content/bundles/',
16-
bundlesForFile: null
16+
bundlesForFile: null,
17+
webpack: {}
1718
};
1819
var karmaDefaults = {
1920
frameworks: ['mocha', 'chai', 'sinon-chai'],
2021
watch: false,
2122
webpack: {
2223
loaders: [],
23-
externals: {}
24+
externals: {},
25+
plugins: []
2426
}
2527
};
2628

@@ -79,7 +81,7 @@ function bundle(options, watchMode) {
7981

8082
var webpackBundles = getWebpackBundles(assetConfigFullPath);
8183
var webpackBundleTasks = config.bundlesForFile ? [] : Object.keys(webpackBundles).map(function (bundleName) {
82-
return scriptHelpers.createWebpackBundle(bundleName, webpackBundles[bundleName], config.outputPath, watchMode);
84+
return scriptHelpers.createWebpackBundle(bundleName, webpackBundles[bundleName], config.outputPath, watchMode, config.webpack[bundleName]);
8385
});
8486

8587
return merge.call(this, _.flatten([allBundleTasks, webpackBundleTasks]));
@@ -102,7 +104,7 @@ function watch(incrementalFilesToWatch, bundleOptions) {
102104
var webpackBundles = getWebpackBundles(assetConfigFullPath);
103105

104106
Object.keys(webpackBundles).forEach(function (bundleName) {
105-
scriptHelpers.createWebpackBundle(bundleName, webpackBundles[bundleName], config.outputPath, true);
107+
scriptHelpers.createWebpackBundle(bundleName, webpackBundles[bundleName], config.outputPath, true, config.webpack[bundleName]);
106108
});
107109
}
108110

packages/olo-gulp-helpers/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "olo-gulp-helpers",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "Helpers for Olo's gulp build pipeline",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)