Skip to content

Commit d075456

Browse files
committed
Inject script as a string in order to avoid delay in loading
1 parent 4f29180 commit d075456

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

gulpfile.babel.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import zip from 'gulp-zip';
77
import webpack from 'webpack';
88
import devConfig from './webpack/dev.config';
99
import prodConfig from './webpack/prod.config';
10+
import wrapConfig from './webpack/wrap.config';
1011

1112
/*
1213
* common tasks
@@ -56,13 +57,16 @@ gulp.task('copy:dev', () => {
5657
*/
5758

5859
gulp.task('webpack:build:extension', (callback) => {
59-
webpack(prodConfig, (err, stats) => {
60-
if (err) {
61-
throw new gutil.PluginError('webpack:build', err);
62-
}
63-
gutil.log('[webpack:build]', stats.toString({ colors: true }));
64-
callback();
65-
});
60+
function webpackProcess(config, next) {
61+
webpack(config, (err, stats) => {
62+
if (err) {
63+
throw new gutil.PluginError('webpack:build', err);
64+
}
65+
gutil.log('[webpack:build]', stats.toString({ colors: true }));
66+
next();
67+
});
68+
}
69+
webpackProcess(wrapConfig, () => { webpackProcess(prodConfig, callback); });
6670
});
6771

6872
gulp.task('views:build:extension', () => {

src/browser/extension/inject/pageScriptWrap.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
let s = document.createElement('script');
2-
s.src = chrome.extension.getURL('js/page.bundle.js');
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
const script = require('raw!tmp/page.bundle.js');
5+
s.appendChild(document.createTextNode(script));
6+
} else {
7+
s.src = chrome.extension.getURL('js/page.bundle.js');
8+
}
9+
10+
s.type = 'text/javascript';
311
s.onload = function() {
412
this.parentNode.removeChild(this);
513
};

webpack/base.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const baseConfig = (params) => ({
1111
devpanel: [ `${extpath}devpanel/index` ],
1212
devtools: [ `${extpath}devtools/index` ],
1313
content: [ `${extpath}inject/contentScript` ],
14-
page: [ `${extpath}inject/pageScript` ],
1514
pagewrap: [ `${extpath}inject/pageScriptWrap` ],
1615
inject: [ `${extpath}inject/index` ],
1716
...params.inputExtra

webpack/dev.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import webpack from 'webpack';
33
import baseConfig from './base.config';
44

55
let config = baseConfig({
6+
inputExtra: { page: [ path.join(__dirname, '../src/browser/extension/inject/pageScript') ] },
67
output: { path: path.join(__dirname, '../dev/js') },
78
globals: {
89
'process.env': {

webpack/wrap.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import path from 'path';
2+
import webpack from 'webpack';
3+
import baseConfig from './base.config';
4+
5+
export default baseConfig({
6+
input: { page: [ path.join(__dirname, '../src/browser/extension/inject/pageScript') ] },
7+
output: { path: path.join(__dirname, '../build/tmp') },
8+
globals: {
9+
'process.env': {
10+
NODE_ENV: '"production"'
11+
}
12+
}
13+
});

0 commit comments

Comments
 (0)