Skip to content

Commit d8c1450

Browse files
committed
Added the eval stripper. NOTE this might not be entirely necessary
1 parent 59be54e commit d8c1450

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
lines changed

generator/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = (api, { config }) => {
22
const eslintConfig = { env: { webextensions: true } }
33
const pkg = {
44
scripts: {
5-
'ext-serve': 'vue-cli-service ext-serve'
5+
'ext-serve': 'vue-cli-service ext-serve --mode development'
66
},
77
dependencies: {
88
'vue-router': '^3.0.1',

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
const path = require('path')
12
const { log } = require('@vue/cli-shared-utils')
23
const CopyWebpackPlugin = require('copy-webpack-plugin')
34
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader')
5+
const WebpackShellPlugin = require('webpack-shell-plugin-next')
46
const { version } = require('./package.json')
57

68
module.exports = (api) => {
@@ -28,6 +30,15 @@ module.exports = (api) => {
2830
}
2931
]))
3032

33+
const scriptPath = path.join(__dirname, 'scripts/remove-evals.js')
34+
webpackConfig.plugins.push(new WebpackShellPlugin({
35+
onBuildExit: {
36+
scripts: [`node ${scriptPath}`],
37+
blocking: true,
38+
parallel: false
39+
}
40+
}))
41+
3142
if (process.env.HMR === 'true') {
3243
webpackConfig.plugins = (webpackConfig.plugins || []).concat([
3344
new ChromeExtensionReloader()

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cli-plugin-browser-extension",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Browser extension development plugin for vue-cli 3.0",
55
"main": "index.js",
66
"scripts": {
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"@vue/cli-shared-utils": "^3.0.0-rc.3",
2828
"copy-webpack-plugin": "^4.5.2",
29-
"webpack-chrome-extension-reloader": "^0.8.3"
29+
"webpack-chrome-extension-reloader": "^0.8.3",
30+
"webpack-shell-plugin-next": "https://github.com/adambullmer/webpack-shell-plugin-next.git"
3031
}
3132
}

scripts/remove-evals.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env node
2+
3+
const path = require('path')
4+
const fs = require('fs')
5+
const BUNDLE_DIR = 'dist'
6+
const bundles = [
7+
'background.js',
8+
'popup/popup.js'
9+
]
10+
11+
const evalRegexForProduction = /;([a-z])=function\(\){return this}\(\);try{\1=\1\|\|Function\("return this"\)\(\)\|\|\(0,eval\)\("this"\)}catch\(t\){"object"==typeof window&&\(\1=window\)}/g
12+
const evalRegexForDevelopment = /;\\r\\n\\r\\n\/\/ This works in non-strict mode(?:.){1,304}/g
13+
14+
const removeEvals = (file) => {
15+
return new Promise((resolve, reject) => {
16+
fs.readFile(file, 'utf8', (err, data) => {
17+
if (err) {
18+
reject(err)
19+
return
20+
}
21+
22+
const regex = process.env.NODE_ENV === 'production' ? evalRegexForProduction : evalRegexForDevelopment
23+
24+
if (!regex.test(data)) {
25+
resolve(`No CSP specific code found in ${file}.`)
26+
return
27+
}
28+
29+
data = data.replace(regex, '=window;')
30+
31+
fs.writeFile(file, data, (err) => {
32+
if (err) {
33+
reject(err)
34+
return
35+
}
36+
37+
resolve()
38+
})
39+
})
40+
})
41+
}
42+
43+
const main = () => {
44+
bundles.forEach(bundle => {
45+
removeEvals(path.join(BUNDLE_DIR, bundle))
46+
.catch(console.error)
47+
})
48+
}
49+
50+
main()

yarn.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ babel-code-frame@^6.26.0:
146146
esutils "^2.0.2"
147147
js-tokens "^3.0.2"
148148

149+
babel-polyfill@^6.26.0:
150+
version "6.26.0"
151+
resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153"
152+
dependencies:
153+
babel-runtime "^6.26.0"
154+
core-js "^2.5.0"
155+
regenerator-runtime "^0.10.5"
156+
157+
babel-runtime@^6.26.0:
158+
version "6.26.0"
159+
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
160+
dependencies:
161+
core-js "^2.4.0"
162+
regenerator-runtime "^0.11.0"
163+
149164
balanced-match@^1.0.0:
150165
version "1.0.0"
151166
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
@@ -324,6 +339,10 @@ copy-webpack-plugin@^4.5.2:
324339
p-limit "^1.0.0"
325340
serialize-javascript "^1.4.0"
326341

342+
core-js@^2.4.0, core-js@^2.5.0:
343+
version "2.5.7"
344+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.7.tgz#f972608ff0cead68b841a16a932d0b183791814e"
345+
327346
core-util-is@1.0.2, core-util-is@~1.0.0:
328347
version "1.0.2"
329348
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
@@ -1472,6 +1491,14 @@ read-pkg@^2.0.0:
14721491
string_decoder "~1.1.1"
14731492
util-deprecate "~1.0.1"
14741493

1494+
regenerator-runtime@^0.10.5:
1495+
version "0.10.5"
1496+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"
1497+
1498+
regenerator-runtime@^0.11.0:
1499+
version "0.11.1"
1500+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
1501+
14751502
regexp.prototype.flags@^1.2.0:
14761503
version "1.2.0"
14771504
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.2.0.tgz#6b30724e306a27833eeb171b66ac8890ba37e41c"
@@ -1885,6 +1912,12 @@ webpack-chrome-extension-reloader@^0.8.3:
18851912
webpack-sources "^1.0.1"
18861913
ws "^3.3.3"
18871914

1915+
"webpack-shell-plugin-next@https://github.com/adambullmer/webpack-shell-plugin-next.git":
1916+
version "0.6.3"
1917+
resolved "https://github.com/adambullmer/webpack-shell-plugin-next.git#bfad45409bacb6f1554fce8909dd1326fa05a7f7"
1918+
dependencies:
1919+
babel-polyfill "^6.26.0"
1920+
18881921
webpack-sources@^1.0.1:
18891922
version "1.1.0"
18901923
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.1.0.tgz#a101ebae59d6507354d71d8013950a3a8b7a5a54"

0 commit comments

Comments
 (0)