-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Add npm library mode #855
Open
mizchi
wants to merge
1
commit into
ampproject:main
Choose a base branch
from
mizchi:lib-with-webpack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,68 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import compiler from '@ampproject/rollup-plugin-closure-compiler'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import replace from '@rollup/plugin-replace'; | ||
import { babelPlugin } from './rollup.plugins.js'; | ||
import path from 'path'; | ||
|
||
// Compile plugins should always be added at the end of the plugin list. | ||
const compilePlugins = [ | ||
compiler({ | ||
env: 'CUSTOM', | ||
}), | ||
terser(), | ||
]; | ||
|
||
export default [ | ||
{ | ||
input: path.join(__dirname, '../output/main-thread/lib.js'), | ||
output: { | ||
file: 'dist/lib/main.mjs', | ||
format: 'esm', | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
replace({ | ||
WORKER_DOM_DEBUG: false, | ||
}), | ||
babelPlugin({ | ||
transpileToES5: false, | ||
allowConsole: false, | ||
}), | ||
...compilePlugins, | ||
], | ||
}, | ||
{ | ||
input: path.join(__dirname, '../output/worker-thread/lib.js'), | ||
output: { | ||
file: 'dist/lib/worker.mjs', | ||
format: 'esm', | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
replace({ | ||
WORKER_DOM_DEBUG: false, | ||
}), | ||
babelPlugin({ | ||
transpileToES5: false, | ||
allowConsole: false, | ||
}), | ||
...compilePlugins, | ||
], | ||
}, | ||
]; |
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 @@ | ||
dist |
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,41 @@ | ||
# worker-dom as package with webpack | ||
|
||
## Webpack settings | ||
|
||
- Add `@ampproject/worker-dom` | ||
- Add `worker-plugin` or `worker-loader` to create worker on webpack | ||
|
||
In this README, we use `worker-plugin`. | ||
|
||
```js | ||
// webpack.config.js | ||
const WorkerPlugin = require("worker-plugin"); | ||
module.exports = { | ||
// ... | ||
plugins: [new WorkerPlugin()] | ||
} | ||
``` | ||
|
||
## Example | ||
|
||
```js | ||
// worker.js | ||
import { ready } from "@ampproject/worker-dom/dist/lib/worker"; | ||
|
||
ready.then(() =>{ | ||
document.body.firstChild.textContent = 'hello from worker mutate'; | ||
}); | ||
``` | ||
|
||
```js | ||
// main.js | ||
import { attachWorker } from "@ampproject/worker-dom/dist/lib/main"; | ||
|
||
// Create worker by your own way | ||
// This code uses worker-plugin | ||
const worker = new Worker("./worker.js", { type: "module" }); | ||
|
||
// attach worker to dom | ||
attachWorker(document.querySelector('#root'), worker); | ||
``` | ||
|
Empty file.
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,13 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"build": "webpack --mode development" | ||
}, | ||
"devDependencies": { | ||
"html-webpack-plugin": "^4.2.1", | ||
"webpack": "^4.43.0", | ||
"webpack-cli": "^3.3.11", | ||
"webpack-dev-server": "^3.10.3", | ||
"worker-plugin": "^4.0.2" | ||
} | ||
} |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
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,14 @@ | ||
import { attachWorker } from '@ampproject/worker-dom/dist/lib/main.mjs'; | ||
|
||
const el = document.createElement('main'); | ||
el.innerHTML = ` | ||
<div> | ||
<h1>main</h1> | ||
<button>click</button> | ||
</div> | ||
`; | ||
document.body.appendChild(el); | ||
|
||
const worker = new Worker('./worker.js', { type: 'module' }); | ||
|
||
attachWorker(el, worker); |
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,18 @@ | ||
import { ready } from '@ampproject/worker-dom/dist/lib/worker.mjs'; | ||
|
||
async function run() { | ||
await ready; | ||
|
||
// dom mutation | ||
const el = document.createElement('h2'); | ||
el.textContent = 'hello from worker'; | ||
document.body.appendChild(el); | ||
|
||
// handler | ||
const button = document.querySelector('button'); | ||
button.onclick = (ev) => { | ||
console.log('button:onclick', ev); | ||
}; | ||
} | ||
|
||
run(); |
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,34 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const path = require('path'); | ||
const HTMLPlugin = require('html-webpack-plugin'); | ||
const WorkerPlugin = require('worker-plugin'); | ||
|
||
module.exports = { | ||
mode: 'development', | ||
resolve: { | ||
alias: { | ||
'@ampproject/worker-dom': path.join(__dirname, '../../'), | ||
}, | ||
}, | ||
plugins: [ | ||
new WorkerPlugin(), | ||
new HTMLPlugin({ | ||
template: path.join(__dirname, 'src/index.html'), | ||
}), | ||
], | ||
}; |
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,138 @@ | ||
/** | ||
* Copyright 2018 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { MutationFromWorker, MessageType, MessageFromWorker, MessageToWorker } from '../transfer/Messages'; | ||
import { MutatorProcessor } from './mutator'; | ||
import { NodeContext } from './nodes'; | ||
import { StringContext } from './strings'; | ||
import { TransferrableKeys } from '../transfer/TransferrableKeys'; | ||
import { InboundWorkerDOMConfiguration, normalizeConfiguration, WorkerDOMConfiguration } from './configuration'; | ||
import { WorkerContext } from './worker'; | ||
import { ObjectContext } from './object-context'; | ||
import { readableMessageToWorker, readableHydrateableRootNode } from './debugging'; | ||
import { createHydrateableRootNode } from './serialize'; | ||
|
||
const ALLOWABLE_MESSAGE_TYPES = [MessageType.MUTATE, MessageType.HYDRATE]; | ||
|
||
export function attachWorker( | ||
baseElement: HTMLElement, | ||
worker: Worker, | ||
config: InboundWorkerDOMConfiguration = { | ||
authorURL: '[external-instance]', | ||
domURL: '[external-instance]', | ||
}, | ||
) { | ||
const stringContext = new StringContext(); | ||
const objectContext = new ObjectContext(); | ||
const nodeContext = new NodeContext(stringContext, baseElement); | ||
const normalizedConfig = normalizeConfiguration(config); | ||
const workerContext = (new WorkerContextFromInstance(baseElement as HTMLElement, nodeContext, worker, normalizedConfig) as any) as WorkerContext; | ||
const mutatorContext = new MutatorProcessor(stringContext, nodeContext, workerContext, normalizedConfig, objectContext); | ||
|
||
workerContext.worker.onmessage = (message: MessageFromWorker) => { | ||
const { data } = message; | ||
if (!ALLOWABLE_MESSAGE_TYPES.includes(data[TransferrableKeys.type])) { | ||
return; | ||
} | ||
mutatorContext.mutate( | ||
(data as MutationFromWorker)[TransferrableKeys.phase], | ||
(data as MutationFromWorker)[TransferrableKeys.nodes], | ||
(data as MutationFromWorker)[TransferrableKeys.strings], | ||
new Uint16Array(data[TransferrableKeys.mutations]), | ||
); | ||
|
||
if (config.onReceiveMessage) { | ||
config.onReceiveMessage(message); | ||
} | ||
}; | ||
} | ||
|
||
// TODO: Refactor with original | ||
class WorkerContextFromInstance { | ||
private [TransferrableKeys.worker]: Worker; | ||
private nodeContext: NodeContext; | ||
private config: WorkerDOMConfiguration; | ||
|
||
/** | ||
* @param baseElement | ||
* @param nodeContext | ||
* @param workerDOMScript | ||
* @param authorScript | ||
* @param config | ||
*/ | ||
constructor(baseElement: HTMLElement, nodeContext: NodeContext, worker: Worker, config: WorkerDOMConfiguration) { | ||
const selfAsWorkerContext = (this as any) as WorkerContext; | ||
this.nodeContext = nodeContext; | ||
this.config = config; | ||
|
||
const { skeleton, strings } = createHydrateableRootNode(baseElement, config, selfAsWorkerContext); | ||
const cssKeys: Array<string> = []; | ||
const globalEventHandlerKeys: Array<string> = []; | ||
// TODO(mizchi): Can not serialize on postMessage | ||
// TODO(choumx): Sync read of all localStorage and sessionStorage a possible performance bottleneck? | ||
// const localStorageData = config.sanitizer ? config.sanitizer.getStorage(StorageLocation.Local) : window.localStorage; | ||
// const sessionStorageData = config.sanitizer ? config.sanitizer.getStorage(StorageLocation.Session) : window.sessionStorage; | ||
|
||
for (const key in baseElement.style) { | ||
cssKeys.push(key); | ||
} | ||
for (const key in baseElement) { | ||
if (key.startsWith('on')) { | ||
globalEventHandlerKeys.push(key); | ||
} | ||
} | ||
|
||
this[TransferrableKeys.worker] = worker; | ||
worker.postMessage({ | ||
__init__: [ | ||
strings, | ||
skeleton, | ||
cssKeys, | ||
globalEventHandlerKeys, | ||
[window.innerWidth, window.innerHeight], | ||
{}, // localStorage | ||
{}, // sessionStorage | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe adding |
||
}); | ||
|
||
if (WORKER_DOM_DEBUG) { | ||
console.info('debug', 'hydratedNode', readableHydrateableRootNode(baseElement, config, selfAsWorkerContext)); | ||
} | ||
if (config.onCreateWorker) { | ||
config.onCreateWorker(baseElement, strings, skeleton, cssKeys); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the private worker. | ||
*/ | ||
get worker(): Worker { | ||
return this[TransferrableKeys.worker]; | ||
} | ||
|
||
/** | ||
* @param message | ||
*/ | ||
messageToWorker(message: MessageToWorker, transferables?: Transferable[]) { | ||
if (WORKER_DOM_DEBUG) { | ||
console.info('debug', 'messageToWorker', readableMessageToWorker(this.nodeContext, message)); | ||
} | ||
if (this.config.onSendMessage) { | ||
this.config.onSendMessage(message); | ||
} | ||
this.worker.postMessage(message, transferables || []); | ||
} | ||
} |
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,35 @@ | ||
import { hydrate, workerDOM } from './index'; | ||
import { TransferrableKeys } from '../transfer/TransferrableKeys'; | ||
import { HydrateableNode } from '../transfer/TransferrableNodes'; | ||
|
||
// @ts-ignore | ||
self.window = self; | ||
|
||
let _resolve: Function | null = null; | ||
export const ready = new Promise((resolve) => { | ||
_resolve = resolve; | ||
}); | ||
|
||
self.addEventListener( | ||
'message', | ||
(ev: any) => { | ||
const args = ev.data.__init__ as [ | ||
string[], | ||
HydrateableNode, | ||
string[], | ||
string[], | ||
[number, number], | ||
{ [key: string]: string }, | ||
{ [key: string]: string }, | ||
]; | ||
hydrate(workerDOM.document, ...args); | ||
|
||
workerDOM.document[TransferrableKeys.observe](); | ||
Object.keys(workerDOM).forEach((k) => { | ||
// @ts-ignore | ||
self[k] = workerDOM[k]; | ||
}); | ||
_resolve && _resolve(); | ||
}, | ||
{ once: true }, | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WIP: I copied it from WorkerContext and patched. Is there clear way?