Skip to content
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

Refactor context #15

Open
wants to merge 39 commits into
base: feat-array-patch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
01b0c6c
refactor(cli): refactor compile context
Gcaufy Nov 5, 2019
70b334a
refactor(compiler-babel): compiler-babel refactor
Gcaufy Nov 5, 2019
f40990a
refactor(compiler-typescript): compiler-typescript refactor
Gcaufy Nov 5, 2019
e9d5bc2
Merge branch '2.0.x' into refactor-context
Gcaufy Dec 1, 2019
efb2a24
feat: script fixed
Gcaufy Dec 2, 2019
d208c80
refactor(cli): added chain & bead
Gcaufy Dec 2, 2019
cd6be5f
refactor(cli): fix vendor & asserts
Gcaufy Dec 2, 2019
cbea6fb
refactor(cli): added wepy-maker as a plugin
Gcaufy Dec 2, 2019
5311c96
fix(cli): initial default lang & fixed eslint
dlhandsome Dec 8, 2019
1217d5a
fix(cli): using compiled code at the parse stage
dlhandsome Dec 9, 2019
a9299b3
refactor(cli): refactor parse source
dlhandsome Dec 9, 2019
0847598
refactor(cli): using webpack-source at parse stage
dlhandsome Dec 10, 2019
182d8ba
refactor(cli): refactor bead & chain folder
dlhandsome Dec 10, 2019
637e3c1
refactor(cli): using code property instead of source at parse stage
dlhandsome Dec 10, 2019
87965d9
feat(cli): support weapp & wepy mode
dlhandsome Dec 11, 2019
1303843
fix(cli): fixed ast replacement
dlhandsome Dec 12, 2019
30087a4
fix(compiler-babel): register with chain level scopes
dlhandsome Dec 12, 2019
d02b67d
fix(plugin-define): register with chain level scopes
dlhandsome Dec 12, 2019
46cce00
feat(cli): added wxs bead class
dlhandsome Dec 13, 2019
e21cbaf
feat(cli): added ignore property of chain
dlhandsome Dec 13, 2019
bc5bbee
perf(cli): remove useless code
dlhandsome Dec 13, 2019
8231d4e
feat(cli): support wxs
dlhandsome Dec 13, 2019
b5e24b0
perf(cli): output vendor if necessary
dlhandsome Dec 13, 2019
ec16358
perf(cli): use replace source instead of raw source
dlhandsome Dec 13, 2019
792f590
perf(cli): use cache
dlhandsome Dec 13, 2019
dc0b537
fix(cli): fixed vendor output
dlhandsome Dec 16, 2019
244fb87
refactor(cli): refactor resolver
dlhandsome Dec 17, 2019
882b867
fix(cli): fixed vendor
dlhandsome Dec 17, 2019
72e49a4
fix(cli): fixed weappChain
dlhandsome Dec 17, 2019
53b0888
fix(cli): fixed producer typo
dlhandsome Dec 17, 2019
b996bd1
fix(cli): fixed replacement
dlhandsome Dec 18, 2019
34474a6
feat(cli): support watch
dlhandsome Dec 18, 2019
57e1f3e
fix(compiler-babel): added missing chain
Gcaufy Dec 19, 2019
28ef005
chore: eslint fix
Gcaufy Dec 19, 2019
7e13846
fix(cli): fixed custom sfc get wrong config lang
Gcaufy Dec 19, 2019
19df90d
test(compiler-test): fixed compiler-less test case
Gcaufy Dec 19, 2019
7b5df05
test(plugin-define): remove plugin-define test case
Gcaufy Dec 19, 2019
933017e
perf(cli): optimize compile
dlhandsome Dec 23, 2019
e532e24
feat(cli): circular dependency handler
dlhandsome Dec 23, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mocha": true
},
"rules": {
"no-debugger": 0,
"strict": [0],
"eqeqeq": 2,
"quotes": [2, "single", {"allowTemplateLiterals": true}],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "lib/wepy.js",
"scripts": {
"lint": "eslint ./ --ext .js",
"lint": "eslint ./ --fix --ext .js",
"dev:core": "rollup -w -c scripts/config.js --environment TARGET:core",
"dev:all": "node ./scripts/build.js",
"dev:watch": "chokidar '**/*.wpy' '**/*.js' -c 'npm run dev:all' -i '/dist/'",
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/core/CacheFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CacheFile {
constructor() {
this._data = {};
}

set(k, v) {
this._data[k] = v;
}
get(k) {
return this._data[k];
}
}

exports = module.exports = CacheFile;
58 changes: 58 additions & 0 deletions packages/cli/core/Producer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs');

class Producer {
constructor() {
this.sequence = 0;
this.beads = [];
this.beadsMap = {};
this._vendors = [];
this._assets = [];
}

make(BeadType, filepath, id, content) {
if (!id) {
id = filepath;
}
if (!content) {
try {
content = fs.readFileSync(filepath, 'utf-8');
} catch (err) {
content = '';
}
}
let bead;
if (this.beadsMap[id]) {
bead = this.beadsMap[id];
bead.reload(content);
} else {
bead = new BeadType(id, filepath, content);
bead.no = this.sequence;
this.sequence++;
this.beads.push(bead);
this.beadsMap[id] = bead;
}
return bead;
}

vendors(chain) {
if (chain !== undefined) {
if (!this._vendors.includes(chain)) {
this._vendors.push(chain);
}
} else {
return this._vendors;
}
}

assets(chain) {
if (chain !== undefined) {
if (!this._assets.includes(chain)) {
this._assets.push(chain);
}
} else {
return this._assets;
}
}
}

exports = module.exports = Producer;
58 changes: 58 additions & 0 deletions packages/cli/core/Resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const ResolverFactory = require('enhanced-resolve').ResolverFactory;
const node = require('enhanced-resolve/lib/node');
const NodeJsInputFileSystem = require('enhanced-resolve/lib/NodeJsInputFileSystem');
const CachedInputFileSystem = require('enhanced-resolve/lib/CachedInputFileSystem');

exports = module.exports = class Resolver {
constructor(options) {
this._options = options;
this.inputFileSystem = new CachedInputFileSystem(new NodeJsInputFileSystem(), 60000);
}

wrapper(context) {
let fnBak = context.resolve;

context.resolve = function(...args) {
return new Promise((resolve, reject) => {
args.push(function(err, filepath, meta) {
if (err) {
reject(err);
} else {
resolve({ path: filepath, meta: meta });
}
});
fnBak.apply(context, args);
});
};

return context;
}

create(options = {}) {
const context = ResolverFactory.createResolver(
Object.assign(
{
fileSystem: this.inputFileSystem
},
this._options,
options
)
);

return this.wrapper(context);
}

createSync(options = {}) {
const context = node.create.sync(
Object.assign(
{
fileSystem: this.inputFileSystem
},
this._options,
options
)
);

return context;
}
};
30 changes: 14 additions & 16 deletions packages/cli/core/ast/walker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class AstWalker {
constructor(compilation, ast, lang) {
constructor(ast, chain) {
this.ast = ast;
this.state = {};
this.deps = [];
this.chain = chain;
this.replacements = [];
this.compilation = compilation;
this.lang = lang;
}

run() {
Expand Down Expand Up @@ -234,7 +232,7 @@ class AstWalker {
module: source,
loc: statement.source.loc
};
this.deps.push(dep);
this.chain.bead.parsed.dependences.push(dep);
return;
/*
// this.applyPluginsBailResult("import", statement, source);
Expand Down Expand Up @@ -383,8 +381,8 @@ class AstWalker {
case 'VariableDeclarator': {
this.enterPattern(declarator.id, (name, decl) => {
// For old version compiler-babel, we can remove it later
if (this.compilation.hasHook('prewalk-' + declarator.type, this, declarator, name, decl)) {
this.compilation.hook('prewalk-' + declarator.type, this, declarator, name, decl);
if (this.chain.hasHook('prewalk-' + declarator.type, this, declarator, name, decl)) {
this.chain.hook('prewalk-' + declarator.type, this, declarator, name, decl);
} else {
// Ignore child scope
if (this.scope.instances && declarator.init && declarator.init.type === 'CallExpression') {
Expand Down Expand Up @@ -558,8 +556,8 @@ class AstWalker {
const exprName = this.getNameForExpression(expression.argument);
if (exprName && exprName.free) {
let hookName = 'walker-unary-expression-undefined';
if (this.compilation.hasHook(hookName)) {
this.compilation.hookSeq(hookName, this, expression, exprName);
if (this.chain.hasHook(hookName)) {
this.chain.hookSeq(hookName, this, expression, exprName);
}
}
}
Expand Down Expand Up @@ -732,8 +730,8 @@ class AstWalker {
if (expression.callee.type === 'MemberExpression') {
let exprName = this.getNameForExpression(expression.callee);
// For old version compiler-babel, we can remove it later
if (this.compilation.hasHook('walker-detect-entry')) {
this.compilation.hook('walker-detect-entry', this, expression, exprName);
if (this.chain.hasHook('walker-detect-entry')) {
this.chain.hook('walker-detect-entry', this, expression, exprName);
} else {
if (this.scope.instances && exprName && this.scope.instances.indexOf(exprName.instance) !== -1) {
// calling wepy instance
Expand Down Expand Up @@ -772,8 +770,8 @@ class AstWalker {
const exprName = this.getNameForExpression(expression);
if (exprName && exprName.free) {
let hookName = 'walker-member-expression-undefined';
if (this.compilation.hasHook(hookName)) {
this.compilation.hookSeq(hookName, this, expression, exprName);
if (this.chain.hasHook(hookName)) {
this.chain.hookSeq(hookName, this, expression, exprName);
}
}
this.walkExpression(expression.object);
Expand All @@ -783,8 +781,8 @@ class AstWalker {
walkIdentifier(expression) {
if (this.scope.definitions.indexOf(expression.name) === -1) {
let hookName = 'walker-identifier-undefined';
if (this.compilation.hasHook(hookName)) {
this.compilation.hookSeq(hookName, this, expression);
if (this.chain.hasHook(hookName)) {
this.chain.hookSeq(hookName, this, expression);
}
}
}
Expand Down Expand Up @@ -1195,7 +1193,7 @@ class AstWalker {
this.state.module,
expr.loc
);*/
this.deps.push(dep);
this.chain.bead.parsed.dependences.push(dep);
this.state.current = dep;
}

Expand Down
Loading