1+ import * as path from 'path' ;
2+ import * as fs from 'fs' ;
3+ import gulp from 'gulp' ;
14import babel from 'gulp-babel' ;
25import babelHelpers from 'gulp-babel-external-helpers' ;
3- import browserify from 'browserify' ;
46import concat from 'gulp-concat' ;
57import data from 'gulp-data' ;
68import del from 'del' ;
7- import fs from 'fs' ;
8- import gulp from 'gulp' ;
99import merge from 'merge-stream' ;
1010import mkdirp from 'mkdirp' ;
1111import rename from 'gulp-rename' ;
1212import rjs from 'requirejs' ;
1313import runseq from 'run-sequence' ;
14- import source from 'vinyl-source-stream' ;
1514import template from 'gulp-template' ;
1615import zip from 'gulp-zip' ;
16+ import webpack from 'webpack' ;
1717import packg from './package.json' ;
1818
1919gulp . 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 : / \. j s o n $ / , loader : 'json' } ,
48+ {
49+ test : / \. j s $ / ,
50+ exclude : / n o d e _ m o d u l e s / ,
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 ( / ^ p l u g \/ / . 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
175183gulp . task ( 'default' , cb => {
176184 runseq (
177- 'clean' ,
178- [ 'babel' , 'dependencies' ] ,
179- 'rjs' ,
185+ 'clean-build' ,
180186 'build' ,
181187 [ 'chrome' , 'firefox' , 'userscript' ] ,
182188 cb
0 commit comments