Skip to content

Commit

Permalink
Yinitial Ycommit
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Dec 18, 2017
0 parents commit 22600b9
Show file tree
Hide file tree
Showing 1,568 changed files with 625,029 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
.cache
5 changes: 5 additions & 0 deletions .prettierrc
@@ -0,0 +1,5 @@
{
"printWidth": 79,
"semi": false,
"bracketSpacing": false
}
86 changes: 86 additions & 0 deletions PHPAsset.js
@@ -0,0 +1,86 @@
const path = require('path')

const HTMLAsset = require('parcel-bundler/src/assets/HTMLAsset')
const PHPParser = require('php-parser')
const Include = require('php-parser/src/ast/include')

const parser = new PHPParser({
ast: {
withPositions: true
}
})

module.exports = class PHPAsset extends HTMLAsset {
constructor (name, pkg, options) {
super(name, pkg, options)
this.type = 'php'
}

parse (code) {
this.ast = parser.parseCode(code)
return this.ast
}

apologise (require) {
console.warn(`Sorry, hit an unsupported ${require ? 'require' : 'include'}.`)
}

collectDependencies () {
let offset = 0
this.ast.children.forEach(node => {
if (node.constructor !== Include) return

const {
require,
target: {
kind,
value,
loc: {
start,
end
},
inner
}
} = node

let phpPath

if (kind === 'string') {
phpPath = value
} else if (kind === 'parenthesis') {
if (inner.left.what.name !== 'dirname') {
return this.apologise(require)
}
const dirname = path.dirname(this.name)
phpPath = `${dirname}/${inner.right.value}`
} else {
return this.apologise(require)
}

const assetPath = this.addURLDependency(phpPath)

const untilStart = this.contents.slice(0, start.offset + offset)
const afterEnd = this.contents.slice(end.offset + offset)

const originalLength = end.offset - start.offset
const newLength = assetPath.length + 2

offset += (newLength - originalLength)

this.contents = `${untilStart}'${assetPath}'${afterEnd}`
})

this.ast = super.parse(this.contents)
super.collectDependencies()
}

generate () {
const {
html
} = super.generate()

return {
php: html
}
}
}
1 change: 1 addition & 0 deletions example/.tern-port
@@ -0,0 +1 @@
49207
3 changes: 3 additions & 0 deletions example/README.md
@@ -0,0 +1,3 @@
run `parcel watch index.php` in one terminal,
then `cd` into `dist` with another, and run
`php -S localhost:1234`
1 change: 1 addition & 0 deletions example/dist/1d4c9315d9a6b7c64df6c9ef66ccf029.php
@@ -0,0 +1 @@
you got me all wrong!
1 change: 1 addition & 0 deletions example/dist/386e43e1347f5a02fb335e8e2f2a967b.php
@@ -0,0 +1 @@
<script src="/482c40a07028f88c5f8719da73c62ab4.js"></script>
188 changes: 188 additions & 0 deletions example/dist/482c40a07028f88c5f8719da73c62ab4.js
@@ -0,0 +1,188 @@
// modules are defined as an array
// [ module function, map of requires ]
//
// map of requires is short require name -> numeric require
//
// anything defined in a previous bundle is accessed via the
// orig method which is the require for previous bundles

require = (function (modules, cache, entry) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof require === "function" && require;

function newRequire(name, jumped) {
if (!cache[name]) {
if (!modules[name]) {
// if we cannot find the module within our internal map or
// cache jump to the current global require ie. the last bundle
// that was added to the page.
var currentRequire = typeof require === "function" && require;
if (!jumped && currentRequire) {
return currentRequire(name, true);
}

// If there are other bundles on this page the require from the
// previous one is saved to 'previousRequire'. Repeat this as
// many times as there are bundles until the module is found or
// we exhaust the require chain.
if (previousRequire) {
return previousRequire(name, true);
}

var err = new Error('Cannot find module \'' + name + '\'');
err.code = 'MODULE_NOT_FOUND';
throw err;
}

function localRequire(x) {
return newRequire(localRequire.resolve(x));
}

localRequire.resolve = function (x) {
return modules[name][1][x] || x;
};

var module = cache[name] = new newRequire.Module;
modules[name][0].call(module.exports, localRequire, module, module.exports);
}

return cache[name].exports;
}

function Module() {
this.bundle = newRequire;
this.exports = {};
}

newRequire.Module = Module;
newRequire.modules = modules;
newRequire.cache = cache;
newRequire.parent = previousRequire;

for (var i = 0; i < entry.length; i++) {
newRequire(entry[i]);
}

// Override the current require with this new one
return newRequire;
})({2:[function(require,module,exports) {
console.log('come with me')
console.log('merry christmas')

},{}],0:[function(require,module,exports) {
var global = (1, eval)('this');
var OldModule = module.bundle.Module;
function Module() {
OldModule.call(this);
this.hot = {
accept: function (fn) {
this._acceptCallback = fn || function () {};
},
dispose: function (fn) {
this._disposeCallback = fn;
}
};
}

module.bundle.Module = Module;

if (!module.bundle.parent) {
var ws = new WebSocket('ws://localhost:49198/');
ws.onmessage = function(event) {
var data = JSON.parse(event.data);

if (data.type === 'update') {
data.assets.forEach(function (asset) {
hmrApply(global.require, asset);
});

data.assets.forEach(function (asset) {
if (!asset.isNew) {
hmrAccept(global.require, asset.id);
}
});
}

if (data.type === 'reload') {
window.location.reload();
}

if (data.type === 'error-resolved') {
console.log('[parcel] ✨ Error resolved');
}

if (data.type === 'error') {
console.error(`[parcel] 🚨 ${data.error.message}\n${data.error.stack}`);
}
};
}

function getParents(bundle, id) {
var modules = bundle.modules;
if (!modules) {
return [];
}

var parents = [];
var k, d, dep;

for (k in modules) {
for (d in modules[k][1]) {
dep = modules[k][1][d];
if (dep === id || (Array.isArray(dep) && dep[dep.length - 1] === id)) {
parents.push(+k);
}
}
}

if (bundle.parent) {
parents = parents.concat(getParents(bundle.parent, id));
}

return parents;
}

function hmrApply(bundle, asset) {
var modules = bundle.modules;
if (!modules) {
return;
}

if (modules[asset.id] || !bundle.parent) {
var fn = new Function('require', 'module', 'exports', asset.generated.js);
asset.isNew = !modules[asset.id];
modules[asset.id] = [fn, asset.deps];
} else if (bundle.parent) {
hmrApply(bundle.parent, asset);
}
}

function hmrAccept(bundle, id) {
var modules = bundle.modules;
if (!modules) {
return;
}

if (!modules[id] && bundle.parent) {
return hmrAccept(bundle.parent, id);
}

var cached = bundle.cache[id];
if (cached && cached.hot._disposeCallback) {
cached.hot._disposeCallback();
}

delete bundle.cache[id];
bundle(id);

cached = bundle.cache[id];
if (cached && cached.hot && cached.hot._acceptCallback) {
cached.hot._acceptCallback();
return true;
}

return getParents(global.require, id).some(function (id) {
return hmrAccept(global.require, id)
});
}
},{}]},{},[0,2])
3 changes: 3 additions & 0 deletions example/dist/c84ccbc7e186a9b29ee7d6fde41876ad.php
@@ -0,0 +1,3 @@
<?php
print time();
print "come on let's go!!!";
14 changes: 14 additions & 0 deletions example/dist/index.php
@@ -0,0 +1,14 @@
<!doctype html>

<div>I am php</div>
<div>
<?php
include '1d4c9315d9a6b7c64df6c9ef66ccf029.php';
?>
</div>

<div class="<?php date(); ?>">
goodbye
<div>
<p>
<?php include '386e43e1347f5a02fb335e8e2f2a967b.php'; ?>
3 changes: 3 additions & 0 deletions example/fox/dog.php
@@ -0,0 +1,3 @@
<?php
include '../wog.php';
?> <script src="./spring.js"></script>
2 changes: 2 additions & 0 deletions example/fox/spring.js
@@ -0,0 +1,2 @@
console.log('come with me')
console.log('merry christmas')
14 changes: 14 additions & 0 deletions example/index.php
@@ -0,0 +1,14 @@
<!doctype html>

<div>I am php</div>
<div>
<?php
include 'lol.php';
?>
</div>

<div class="<?php date(); ?>">
goodbye
<div>
<p>
<?php include 'fox/dog.php'; ?>
1 change: 1 addition & 0 deletions example/lol.php
@@ -0,0 +1 @@
you got me all wrong!
10 changes: 10 additions & 0 deletions example/package.json
@@ -0,0 +1,10 @@
{
"name": "parcel-plugin-php-example",
"version": "1.0.0",
"main": "index.js",
"author": "chee <chee@snake.dog>",
"license": "MIT",
"dependencies": {
"parcel-plugin-php": "1.0.0"
}
}
3 changes: 3 additions & 0 deletions example/wog.php
@@ -0,0 +1,3 @@
<?php
print time();
print "come on let's go!!!";

0 comments on commit 22600b9

Please sign in to comment.