-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from connect-foundation/coconut
Coconut worker build 기능 추가
- Loading branch information
Showing
11 changed files
with
210 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const scriptCodeTemplate = (code, newPath, parentPath) => /*javascript*/ ` | ||
exports['${newPath}'] = (() => { | ||
window.pathStack = []; | ||
pathStack.push('${parentPath}'); | ||
let exports = {}; | ||
let module = { exports: {} }; | ||
try { | ||
${code} | ||
return Object.keys(exports).length ? exports : module.exports; | ||
} catch (e) { | ||
const ignoreErrorList = [ | ||
'Cannot redefine property', | ||
'Cannot read property', | ||
'Cannot set property default of #<Object> which has only a getter' | ||
]; | ||
const errorType = e.message; | ||
const isExistInIgnoreList = ignoreErrorList.some(ignoreError => | ||
errorType.startsWith(ignoreError) | ||
); | ||
if(!isExistInIgnoreList) throw e; | ||
} | ||
})()`; | ||
|
||
const executeCodeTemplate = code => /*javascript*/ ` | ||
(() => { | ||
let exports = {}; | ||
try { | ||
${code} | ||
return Object.keys(exports).length ? exports : module.exports; | ||
} catch (e) { | ||
const ignoreErrorList = [ | ||
'Cannot redefine property', | ||
'Cannot read property', | ||
'Cannot set property default of #<Object> which has only a getter', | ||
'document is not defined' | ||
]; | ||
const errorType = e.message; | ||
const isExistInIgnoreList = ignoreErrorList.some(ignoreError => | ||
errorType.startsWith(ignoreError) | ||
); | ||
if(!isExistInIgnoreList) throw e; | ||
} | ||
})()`; | ||
|
||
export { scriptCodeTemplate, executeCodeTemplate }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import path from 'path'; | ||
|
||
const DEPENDENCY_PATH = '/node_modules/'; | ||
|
||
function pathParser(param) { | ||
const moduleName = param; | ||
|
||
if (param[0] !== '.' && param[0] !== '/') { | ||
param = `${DEPENDENCY_PATH}${param}`; | ||
} | ||
|
||
param = path.resolve(window.pathStack[window.pathStack.length - 1], param); | ||
const extension = param.split('.'); | ||
|
||
if (extension[extension.length - 1] === 'js') { | ||
return [param, path.dirname(param)]; | ||
} | ||
if (window.fileSystem[`${param}.js`]) { | ||
return [`${param}.js`, path.dirname(param)]; | ||
} | ||
if (window.fileSystem[`${param}/index.js`]) { | ||
return [`${param}/index.js`, param]; | ||
} | ||
|
||
throw Error(`Module not found: '${moduleName}'`); | ||
} | ||
|
||
export { pathParser }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { pathStack } from './global'; | ||
import { pathParser } from './pathParserInMain'; | ||
import { transformCode } from './core'; | ||
import { executeCodeTemplate } from './codeTemplate'; | ||
|
||
function requireInMain(path) { | ||
if (path === '.' || path === './') | ||
throw Error('Recursive path parsing error'); | ||
const [newPath, newPathParent] = pathParser(path); | ||
|
||
if (window.exports[newPath]) return window.exports[newPath]; | ||
|
||
pathStack.push(newPathParent); | ||
const code = transformCode(window.fileSystem[newPath].contents).value; | ||
|
||
let result = null; | ||
let stackLength = 0; | ||
try { | ||
stackLength = pathStack.length; | ||
result = eval(executeCodeTemplate(code)); | ||
} catch (error) { | ||
while (stackLength < pathStack.length) pathStack.pop(); | ||
result = eval(executeCodeTemplate(code)); | ||
} | ||
|
||
window.exports[newPath] = result; | ||
pathStack.pop(); | ||
|
||
return result; | ||
} | ||
|
||
export { requireInMain }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as bundler from 'bundler'; | ||
|
||
self.process = {}; | ||
self.process.env = {}; | ||
self.process.env.NODE_MODULE = 'development'; | ||
|
||
self.exports = {}; | ||
|
||
self.addEventListener('message', ({ data: { fileSystem } }) => { | ||
self.fileSystem = fileSystem; | ||
self.bundledCode = ''; | ||
|
||
buildProject(); | ||
}); | ||
|
||
function buildProject() { | ||
try { | ||
bundler.init(); | ||
bundler.require('./index.js'); | ||
|
||
self.postMessage({ bundledCode: self.bundledCode }); | ||
} catch (error) { | ||
self.postMessage({ error: error.stack }); | ||
} | ||
} |
Oops, something went wrong.