Skip to content

Commit

Permalink
sync: update with merged changes
Browse files Browse the repository at this point in the history
  • Loading branch information
e-cloud committed Dec 9, 2016
1 parent 8b21571 commit 08e55b6
Show file tree
Hide file tree
Showing 41 changed files with 638 additions and 176 deletions.
15 changes: 14 additions & 1 deletion lib/CompatibilityPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import Compiler = require('./Compiler')
import Compilation = require('./Compilation')
import Parser = require('./Parser')
import { CallExpression } from 'estree'
import { CompilationParams, ParserOptions } from '../typings/webpack-types'
import { CompilationParams, ParserOptions, NMFAfterResolveResult } from '../typings/webpack-types'
import ContextDependency = require('./dependencies/ContextDependency')

const jsonLoaderPath = require.resolve('json-loader');
const matchJson = /\.json$/i;

class CompatibilityPlugin {
apply(compiler: Compiler) {
compiler.plugin('compilation', function (compilation: Compilation, params: CompilationParams) {
Expand Down Expand Up @@ -48,6 +51,16 @@ class CompatibilityPlugin {
return true;
});
});
params.normalModuleFactory.plugin('after-resolve', function (data: NMFAfterResolveResult, done) {
// if this is a json file and there are no loaders active, we use the json-loader in order to avoid
// parse errors @see https://github.com/webpack/webpack/issues/3363
if (matchJson.test(data.request) && data.loaders.length === 0) {
data.loaders.push({
loader: jsonLoaderPath
});
}
done(null, data);
});
});
}
}
Expand Down
22 changes: 13 additions & 9 deletions lib/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
ErrCallback,
PlainObject,
TimeStampMap,
WebpackError, AbstractInputFileSystem
WebpackError,
AbstractInputFileSystem,
PerformanceOptions
} from '../typings/webpack-types'
import { ResolveError } from 'enhanced-resolve/lib/common-types'
import async = require('async');
Expand All @@ -36,7 +38,6 @@ import ChunkRenderError = require('./ChunkRenderError');
import NormalModule = require('./NormalModule')
import DependenciesBlock = require('./DependenciesBlock')
import AsyncDependenciesBlock = require('./AsyncDependenciesBlock')
import * as Resolve from 'enhanced-resolve'

interface SlotChunk {
name: string
Expand Down Expand Up @@ -82,6 +83,7 @@ class Compilation extends Tapable {
options: WebpackOptions
outputOptions: WebpackOutputOptions
preparedChunks: SlotChunk[]
performance: PerformanceOptions
profile: boolean
records: Record
resolvers: Compiler.Resolvers
Expand All @@ -99,6 +101,7 @@ class Compilation extends Tapable {
this.outputOptions = options && options.output;
this.bail = options && options.bail;
this.profile = options && options.profile;
this.performance = options && options.performance;

this.mainTemplate = new MainTemplate(this.outputOptions);
this.chunkTemplate = new ChunkTemplate(this.outputOptions);
Expand Down Expand Up @@ -437,12 +440,12 @@ class Compilation extends Tapable {
const start = this.profile && +new Date();

const errorAndCallback = this.bail ? function errorAndCallback(err: ModuleNotFoundError) {
callback(err);
} : function errorAndCallback(err: ModuleNotFoundError) {
err.dependencies = [dependency];
this.errors.push(err);
callback();
}.bind(this);
callback(err);
} : function errorAndCallback(err: ModuleNotFoundError) {
err.dependencies = [dependency];
this.errors.push(err);
callback();
}.bind(this);

if (typeof dependency !== 'object' || dependency === null || !dependency.constructor) {
throw new Error('Parameter \'dependency\' must be a Dependency');
Expand Down Expand Up @@ -1083,7 +1086,7 @@ class Compilation extends Tapable {
let source;
let file;
const filenameTemplate = chunk.filenameTemplate
? chunk.filenameTemplate as string
? chunk.filenameTemplate
: chunk.isInitial() ? filename : chunkFilename;
try {
const useChunkHash = !chunk.hasRuntime() || this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk);
Expand Down Expand Up @@ -1156,6 +1159,7 @@ declare namespace Compilation {
__SourceMapDevToolData?: Dictionary<RawSource | ConcatSource>
emitted?: boolean
existsAt?: string
isOverSizeLimit?: boolean
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
TimeStampMap,
WatchFileSystem,
WatchOptions,
AbstractInputFileSystem, AbstractStats
AbstractInputFileSystem,
AbstractStats
} from '../typings/webpack-types'
import Parser = require('./Parser')
import Stats = require('./Stats')
Expand Down
6 changes: 4 additions & 2 deletions lib/ContextModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import AsyncDependenciesBlock = require('./AsyncDependenciesBlock');
import ModuleDependency = require('./dependencies/ModuleDependency');
import RequestShortener = require('./RequestShortener')
import Compilation = require('./Compilation')
import * as Resolve from 'enhanced-resolve'

class ContextModule extends Module {
async: boolean
Expand Down Expand Up @@ -90,7 +89,10 @@ class ContextModule extends Module {
super.disconnect();
}

build(options: WebpackOptions, compilation: Compilation, resolver: any, fs: AbstractInputFileSystem, callback: ErrCallback) {
build(
options: WebpackOptions, compilation: Compilation, resolver: any, fs: AbstractInputFileSystem,
callback: ErrCallback
) {
this.built = true;
this.builtTime = new Date().getTime();
const addon = this.addon;
Expand Down
13 changes: 10 additions & 3 deletions lib/ContextModuleFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import Tapable = require('tapable');
import ContextModule = require('./ContextModule');
import ContextElementDependency = require('./dependencies/ContextElementDependency');
import ContextDependency = require('./dependencies/ContextDependency')
import { CMFBeforeResolveResult, ErrCallback, AlternativeModule, AbstractInputFileSystem } from '../typings/webpack-types'
import {
CMFBeforeResolveResult,
ErrCallback,
AlternativeModule,
AbstractInputFileSystem
} from '../typings/webpack-types'
import { Stats } from 'fs'
import Compiler = require('./Compiler')
import * as Resolve from 'enhanced-resolve'

class ContextModuleFactory extends Tapable {
constructor(public resolvers: Compiler.Resolvers) {
Expand Down Expand Up @@ -126,7 +130,10 @@ class ContextModuleFactory extends Tapable {
});
}

resolveDependencies(fs: AbstractInputFileSystem, resource: string, recursive: boolean, regExp: RegExp, callback: ErrCallback) {
resolveDependencies(
fs: AbstractInputFileSystem, resource: string, recursive: boolean, regExp: RegExp,
callback: ErrCallback
) {
if (!regExp || !resource) {
return callback(null, []);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/ContextReplacementPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Compiler = require('./Compiler')
import ContextModuleFactory = require('./ContextModuleFactory')
import ContextDependency = require('./dependencies/ContextDependency')
import {
CMFAfterResolveResult, CMFBeforeResolveResult, ErrCallback,
CMFAfterResolveResult,
CMFBeforeResolveResult,
ErrCallback,
AbstractInputFileSystem
} from '../typings/webpack-types'
import * as Resolve from 'enhanced-resolve'
Expand Down
23 changes: 23 additions & 0 deletions lib/Entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Author Tobias Koppers @sokra
*/
import Chunk = require('./Chunk')
import Compilation = require('./Compilation')

class Entrypoint {
chunks: Chunk[]
Expand All @@ -26,6 +27,28 @@ class Entrypoint {
}
chunk.entrypoints.push(this);
}

getFiles() {
const files: string[] = [];

for (let chunk of this.chunks) {
for (let file of chunk.files) {
if (!files.includes(file)) {
files.push(file)
}
}
}

return files;
}

getSize(compilation: Compilation) {
const files = this.getFiles();

return files
.map(file => compilation.assets[file].size())
.reduce((currentSize, nextSize) => currentSize + nextSize, 0);
}
}

export = Entrypoint;
18 changes: 9 additions & 9 deletions lib/JsonpMainTemplatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ class JsonpMainTemplatePlugin {
'while(resolves.length)',
this.indent('resolves.shift()();'),
this.entryPointInChildren(chunk) ? [
'if(executeModules) {',
this.indent([
'for(i=0; i < executeModules.length; i++) {',
this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
'}'
]),
'}',
'return result;'
] : ''
'if(executeModules) {',
this.indent([
'for(i=0; i < executeModules.length; i++) {',
this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
'}'
]),
'}',
'return result;'
] : ''
]),
'};'
]);
Expand Down
42 changes: 23 additions & 19 deletions lib/MainTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ class MainTemplate extends Template {
'};',
'',
this.asString(outputOptions.strictModuleExceptionHandling ? [
'// Execute the module function',
'var threw = true;',
'try {',
this.indent([
'// Execute the module function',
'var threw = true;',
'try {',
this.indent([
`modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(hash, chunk, 'moduleId')});`,
'threw = false;'
]),
'} finally {',
this.indent([
'if(threw) delete installedModules[moduleId];'
]),
'}'
] : [
'// Execute the module function',
`modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(hash, chunk, 'moduleId')});`,
'threw = false;'
]),
'} finally {',
this.indent([
'if(threw) delete installedModules[moduleId];'
]),
'}'
] : [
'// Execute the module function',
`modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(hash, chunk, 'moduleId')});`,
]),
'',
'// Flag the module as loaded',
'module.l = true;',
Expand Down Expand Up @@ -113,16 +113,20 @@ class MainTemplate extends Template {
buf.push(`${this.requireFn}.c = installedModules;`);

buf.push('');
buf.push('// identity function for calling harmory imports with the correct context');
buf.push('// identity function for calling harmony imports with the correct context');
buf.push(`${this.requireFn}.i = function(value) { return value; };`);

buf.push('');
buf.push('// define getter function for harmory exports');
buf.push('// define getter function for harmony exports');
buf.push(`${this.requireFn}.d = function(exports, name, getter) {`);
buf.push(this.indent([
'Object.defineProperty(exports, name, {',
this.indent(['configurable: false,', 'enumerable: true,', 'get: getter']),
'});'
`if(!${this.requireFn}.o(exports, name)) \{`,
this.indent([
'Object.defineProperty(exports, name, {',
this.indent(['configurable: false,', 'enumerable: true,', 'get: getter']),
'});'
]),
'}'
]));
buf.push('};');

Expand Down
2 changes: 2 additions & 0 deletions lib/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ abstract class Module extends DependenciesBlock implements IRemoveAndDo {
meta: Module.Meta;
optional?: boolean
parent: Module
portableId: string
prefetched: boolean
profile?: Module.Profile
providedExports: string[] | boolean;
Expand All @@ -56,6 +57,7 @@ abstract class Module extends DependenciesBlock implements IRemoveAndDo {
this.debugId = debugId++;
this.lastId = -1;
this.id = null;
this.portableId = null;
this.index = null;
this.index2 = null;
this.used = null;
Expand Down
14 changes: 10 additions & 4 deletions lib/NormalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
WebpackOutputOptions,
TimeStampMap,
SourceRange,
ErrCallback, AbstractInputFileSystem
ErrCallback,
AbstractInputFileSystem
} from '../typings/webpack-types'
import crypto = require('crypto')
import path = require('path');
Expand All @@ -38,7 +39,6 @@ import DependenciesBlockVariable = require('./DependenciesBlockVariable')
import DependenciesBlock = require('./DependenciesBlock')
import Parser = require('./Parser')
import Resolver = require('enhanced-resolve/lib/Resolver')
import * as Resolve from 'enhanced-resolve'

function asString(buf: string | Buffer) {
if (Buffer.isBuffer(buf)) {
Expand Down Expand Up @@ -110,7 +110,10 @@ class NormalModule extends Module {
return this.resource;
}

doBuild(options: WebpackOptions, compilation: Compilation, resolver: Resolver, fs: AbstractInputFileSystem, callback: ErrCallback) {
doBuild(
options: WebpackOptions, compilation: Compilation, resolver: Resolver, fs: AbstractInputFileSystem,
callback: ErrCallback
) {
this.cacheable = false;
const self = this;
const loaderContext: LoaderContext = {
Expand Down Expand Up @@ -205,7 +208,10 @@ class NormalModule extends Module {
super.disconnect();
}

build(options: WebpackOptions, compilation: Compilation, resolver: Resolver, fs: AbstractInputFileSystem, callback: ErrCallback) {
build(
options: WebpackOptions, compilation: Compilation, resolver: Resolver, fs: AbstractInputFileSystem,
callback: ErrCallback
) {
const self = this;
self.buildTimestamp = new Date().getTime();
self.built = true;
Expand Down
Loading

0 comments on commit 08e55b6

Please sign in to comment.