Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit 2f2f603

Browse files
Merge pull request #34 from extplug/webpack
build: use Webpack to build ExtPlug core, closes #30
2 parents 27b48c1 + b1942d4 commit 2f2f603

6 files changed

Lines changed: 92 additions & 68 deletions

File tree

gulpfile.babel.js

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import gulp from 'gulp';
14
import babel from 'gulp-babel';
25
import babelHelpers from 'gulp-babel-external-helpers';
3-
import browserify from 'browserify';
46
import concat from 'gulp-concat';
57
import data from 'gulp-data';
68
import del from 'del';
7-
import fs from 'fs';
8-
import gulp from 'gulp';
99
import merge from 'merge-stream';
1010
import mkdirp from 'mkdirp';
1111
import rename from 'gulp-rename';
1212
import rjs from 'requirejs';
1313
import runseq from 'run-sequence';
14-
import source from 'vinyl-source-stream';
1514
import template from 'gulp-template';
1615
import zip from 'gulp-zip';
16+
import webpack from 'webpack';
1717
import packg from './package.json';
1818

1919
gulp.task('clean-lib', cb => {
@@ -36,80 +36,88 @@ gulp.task('babel', () =>
3636
.pipe(gulp.dest('lib/'))
3737
);
3838

39-
const nodelib = (entry, standalone, name = `${standalone}.js`) => {
40-
const opts = {
41-
entries: `./node_modules/${entry}`,
39+
function createWebpackConfig(options) {
40+
return {
41+
context: path.join(__dirname, './src'),
42+
entry: ['./ExtPlug'],
43+
watch: !!options.watch,
44+
45+
module: {
46+
loaders: [
47+
{ test: /\.json$/, loader: 'json' },
48+
{
49+
test: /\.js$/,
50+
exclude: /node_modules/,
51+
loader: 'babel',
52+
query: {
53+
presets: 'extplug',
54+
plugins: 'external-helpers',
55+
},
56+
},
57+
],
58+
},
59+
60+
output: {
61+
path: path.join(__dirname, './build'),
62+
filename: 'source.js',
63+
library: 'extplug/__internalExtPlug__',
64+
libraryTarget: 'amd',
65+
},
66+
67+
resolve: {
68+
alias: {
69+
extplug: path.join(__dirname, './src'),
70+
},
71+
},
72+
73+
externals: [
74+
'jquery',
75+
'underscore',
76+
'backbone',
77+
'plug-modules',
78+
'lang/Lang',
79+
(context, request, cb) => {
80+
if (/^plug\//.test(request)) {
81+
cb(null, `amd plug-modules!${request}`);
82+
} else {
83+
cb();
84+
}
85+
},
86+
],
4287
};
43-
if (standalone) {
44-
opts.standalone = standalone;
45-
}
46-
47-
return () => browserify(opts)
48-
.bundle()
49-
.pipe(source(name))
50-
.pipe(gulp.dest('build/_deps/'));
51-
};
52-
53-
gulp.task('lib-debug', nodelib('debug/browser.js', 'debug'));
54-
gulp.task('lib-semvercmp', nodelib('semver-compare/index.js', 'semvercmp'));
55-
gulp.task('lib-symbol', nodelib('es6-symbol/implement.js', false, 'es6-symbol.js'));
56-
gulp.task('lib-regexp-quote', nodelib('regexp-quote/regexp-quote.js', 'regexp-quote'));
57-
gulp.task('lib-sistyl', nodelib('sistyl/lib/sistyl.js', 'sistyl'));
58-
59-
gulp.task('dependencies', [
60-
'lib-debug',
61-
'lib-semvercmp',
62-
'lib-sistyl',
63-
'lib-symbol',
64-
'lib-regexp-quote',
65-
]);
66-
67-
gulp.task('rjs', done => {
68-
const npm = 'node_modules/';
69-
packg.builtAt = Date.now();
70-
const packgString = JSON.stringify(packg, null, 2);
71-
delete packg.builtAt;
88+
}
89+
90+
gulp.task('build:source', done => {
91+
webpack(createWebpackConfig({}), done);
92+
});
93+
94+
gulp.task('build:loader', ['babel'], done => {
7295
rjs.optimize({
7396
baseUrl: './',
74-
name: 'extplug/main',
75-
include: ['extplug/ExtPlug'],
97+
name: 'extplug/loader',
7698
paths: {
77-
// plug-modules defines, these are defined at runtime
78-
// so the r.js optimizer can't find them
79-
plug: 'empty:',
80-
lang: 'empty:',
81-
backbone: 'empty:',
8299
jquery: 'empty:',
83100
underscore: 'empty:',
84-
meld: `${npm}meld/meld`,
85-
sistyl: 'build/_deps/sistyl',
86-
extplug: 'lib',
87-
'plug-modules': `${npm}plug-modules/plug-modules`,
88-
debug: 'build/_deps/debug',
89-
onecolor: `${npm}onecolor/one-color-all`,
90-
'regexp-quote': 'build/_deps/regexp-quote',
91-
'semver-compare': 'build/_deps/semvercmp',
92-
},
93-
rawText: {
94-
'package.json': `define(${packgString})`,
95-
'extplug/package': `define(${packgString})`,
101+
backbone: 'empty:',
102+
plug: 'empty:',
103+
'extplug/loader': 'lib/main',
104+
'plug-modules': 'node_modules/plug-modules/plug-modules',
96105
},
97-
insertRequire: ['extplug/main'],
98106
optimize: 'none',
99107
out(text) {
100108
mkdirp('build', e => {
101109
if (e) {
102110
done(e);
103111
} else {
104-
fs.writeFile('build/build.rjs.js', text, done);
112+
fs.writeFile('build/loader.js', text, done);
105113
}
106114
});
107115
},
108116
});
109117
});
110118

111-
gulp.task('concat', () =>
112-
gulp.src(['build/_deps/es6-symbol.js', 'lib/_babelHelpers.js', 'build/build.rjs.js'])
119+
gulp.task('concat', ['babel', 'build:loader', 'build:source'], () =>
120+
gulp.src(['lib/_babelHelpers.js', 'build/loader.js', 'build/source.js'])
113121
.pipe(concat('extplug.code.js'))
114122
.pipe(gulp.dest('build/'))
115123
);
@@ -174,9 +182,7 @@ gulp.task('userscript', ['userscript-meta'], () =>
174182

175183
gulp.task('default', cb => {
176184
runseq(
177-
'clean',
178-
['babel', 'dependencies'],
179-
'rjs',
185+
'clean-build',
180186
'build',
181187
['chrome', 'firefox', 'userscript'],
182188
cb

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"babel-preset-es2015": "^6.9.0",
2222
"babel-preset-extplug": "^1.0.0",
2323
"babel-preset-stage-2": "^6.5.0",
24-
"browserify": "^11.0.1",
2524
"del": "^1.2.1",
2625
"eslint": "^3.0.1",
2726
"eslint-config-airbnb-base": "^4.0.0",
@@ -38,7 +37,7 @@
3837
"mkdirp": "^0.5.1",
3938
"requirejs": "^2.1.20",
4039
"run-sequence": "^1.1.2",
41-
"vinyl-source-stream": "^1.1.0"
40+
"webpack": "^1.13.1"
4241
},
4342
"scripts": {
4443
"prepublish": "npm run build",

src/ExtPlug.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import inlineChatStyles from './styles/inline-chat';
3131
import settingsPaneStyles from './styles/settings-pane';
3232
import pluginDialogStyles from './styles/install-plugin-dialog';
3333

34+
// Enable compatibility with AMD-based plugins.
35+
import './util/compatibility';
36+
3437
// LocalStorage key name for extplug
3538
const LS_NAME = 'extPlugins';
3639

src/loader.js.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
var require = window.requirejs;
1717
var define = window.define;
1818
<%= code %>
19+
require(['extplug/loader']);
1920
}
2021
else {
2122
setTimeout(load, 20);

src/main.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import plugModules from 'plug-modules';
22

3+
const EXTPLUG_MODULE = 'extplug/__internalExtPlug__';
4+
35
function waitFor(cond, fn) {
46
const i = setInterval(() => {
57
if (cond()) {
@@ -24,11 +26,13 @@ function appViewExists() {
2426
plugModules.run();
2527
plugModules.register();
2628

27-
window.require(['extplug/ExtPlug'], ExtPlug => {
28-
waitFor(appViewExists, () => {
29-
const ext = new ExtPlug();
30-
window.extp = ext;
29+
waitFor(appViewExists, () => {
30+
window.define('extplug', [EXTPLUG_MODULE], ExtPlug => new ExtPlug());
3131

32+
window.requirejs(['extplug'], ext => {
33+
window.requirejs.undef(EXTPLUG_MODULE);
34+
35+
window.extp = ext;
3236
ext.enable();
3337
});
3438
});

src/util/compatibility.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as meld from 'meld';
2+
import Plugin from '../Plugin';
3+
import * as request from './request';
4+
import getUserClasses from './getUserClasses';
5+
import Style from './Style';
6+
7+
window.define('meld', () => meld);
8+
window.define('extplug/Plugin', () => Plugin);
9+
window.define('extplug/util/request', () => request);
10+
window.define('extplug/util/getUserClasses', () => getUserClasses);
11+
window.define('extplug/util/Style', () => Style);

0 commit comments

Comments
 (0)