Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
39b36e9
feat: ability to disable extract async modules
lsycxyj Jul 20, 2019
188cf5c
feat: ability to disable extract async modules, lint fix
lsycxyj Jul 20, 2019
4a7a1f6
feat: ability to disable extract async modules, add doc
lsycxyj Jul 20, 2019
399aa9a
feat: ability to disable extract async modules, ci fix
lsycxyj Jul 20, 2019
26646ae
feat: ability to disable extract async modules, order fix
lsycxyj Jul 20, 2019
08b8e52
feat: ability to disable extract async modules, wrong initial chunks fix
lsycxyj Jul 21, 2019
775cc0c
feat: ability to disable extract async modules, add manual test
lsycxyj Jul 21, 2019
f9d7df1
feat: ability to disable extract async modules, update lock file
lsycxyj Jul 21, 2019
c38cc8c
feat: ability to disable extract async modules, add tests for js files
lsycxyj Jul 21, 2019
e2177fd
feat: ability to disable extract modules, wip
lsycxyj Jul 22, 2019
4982afb
feat: ability to disable extract modules, remove duplicated dependencies
lsycxyj Jul 24, 2019
81517cc
feat: ability to disable extract modules, update doc
lsycxyj Jul 24, 2019
5b3fbef
feat: ability to disable extract modules, remove boolean type option
lsycxyj Jul 24, 2019
1da949e
feat: ability to disable extract modules, less code difference
lsycxyj Jul 24, 2019
e59a0e1
feat: ability to disable extract modules, less code difference
lsycxyj Jul 24, 2019
37ce619
Merge remote-tracking branch 'origin/feature/disable_async_20190719' …
lsycxyj Jul 24, 2019
6001569
feat: ability to disable extract modules, less regressions
lsycxyj Jul 26, 2019
b986c54
feat: ability to disable extract modules, ci fix
lsycxyj Jul 26, 2019
fa58310
feat: ability to disable extract modules, ci fix
lsycxyj Jul 26, 2019
5de6572
feat: ability to disable extract modules, replace private property
lsycxyj Jul 27, 2019
5b2c6d3
feat: ability to disable extract modules, remove useless code
lsycxyj Jul 31, 2019
2f29038
merge from master WIP
lsycxyj Feb 3, 2020
f0358d0
fix wrong modules after disable extraction
lsycxyj Feb 4, 2020
cbcfbc5
Merge remote-tracking branch 'upstream/master' into feature/disable_a…
lsycxyj Aug 6, 2020
40af212
fix wrong modification and update package-lock.json
lsycxyj Aug 6, 2020
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
Prev Previous commit
Next Next commit
feat: ability to disable extract modules, less code difference
  • Loading branch information
lsycxyj committed Jul 24, 2019
commit 1da949eab91a749737ed4542a2a290a887e2d6de
221 changes: 111 additions & 110 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,133 +69,134 @@ export function pitch(request) {

if (this[`${MODULE_TYPE}/disableExtract`]()) {
callback();
} else {
const childFilename = '*'; // eslint-disable-line no-path-concat
const publicPath =
typeof options.publicPath === 'string'
? options.publicPath === '' || options.publicPath.endsWith('/')
? options.publicPath
: `${options.publicPath}/`
: typeof options.publicPath === 'function'
? options.publicPath(this.resourcePath, this.rootContext)
: this._compilation.outputOptions.publicPath;
const outputOptions = {
filename: childFilename,
publicPath,
};
const childCompiler = this._compilation.createChildCompiler(
`${pluginName} ${request}`,
outputOptions
);

new NodeTemplatePlugin(outputOptions).apply(childCompiler);
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
new NodeTargetPlugin().apply(childCompiler);
new SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
childCompiler
);
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);

// We set loaderContext[MODULE_TYPE] = false to indicate we already in
// a child compiler so we don't spawn another child compilers from there.
childCompiler.hooks.thisCompilation.tap(
`${pluginName} loader`,
(compilation) => {
compilation.hooks.normalModuleLoader.tap(
`${pluginName} loader`,
(loaderContext, module) => {
return;
}

const childFilename = '*'; // eslint-disable-line no-path-concat
const publicPath =
typeof options.publicPath === 'string'
? options.publicPath === '' || options.publicPath.endsWith('/')
? options.publicPath
: `${options.publicPath}/`
: typeof options.publicPath === 'function'
? options.publicPath(this.resourcePath, this.rootContext)
: this._compilation.outputOptions.publicPath;
const outputOptions = {
filename: childFilename,
publicPath,
};
const childCompiler = this._compilation.createChildCompiler(
`${pluginName} ${request}`,
outputOptions
);

new NodeTemplatePlugin(outputOptions).apply(childCompiler);
new LibraryTemplatePlugin(null, 'commonjs2').apply(childCompiler);
new NodeTargetPlugin().apply(childCompiler);
new SingleEntryPlugin(this.context, `!!${request}`, pluginName).apply(
childCompiler
);
new LimitChunkCountPlugin({ maxChunks: 1 }).apply(childCompiler);

// We set loaderContext[MODULE_TYPE] = false to indicate we already in
// a child compiler so we don't spawn another child compilers from there.
childCompiler.hooks.thisCompilation.tap(
`${pluginName} loader`,
(compilation) => {
compilation.hooks.normalModuleLoader.tap(
`${pluginName} loader`,
(loaderContext, module) => {
// eslint-disable-next-line no-param-reassign
loaderContext.emitFile = this.emitFile;
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign

if (module.request === request) {
// eslint-disable-next-line no-param-reassign
loaderContext.emitFile = this.emitFile;
loaderContext[MODULE_TYPE] = false; // eslint-disable-line no-param-reassign

if (module.request === request) {
// eslint-disable-next-line no-param-reassign
module.loaders = loaders.map((loader) => {
return {
loader: loader.path,
options: loader.options,
ident: loader.ident,
};
});
}
module.loaders = loaders.map((loader) => {
return {
loader: loader.path,
options: loader.options,
ident: loader.ident,
};
});
}
);
}
);
}
);
}
);

let source;
let source;

childCompiler.hooks.afterCompile.tap(pluginName, (compilation) => {
source =
compilation.assets[childFilename] &&
compilation.assets[childFilename].source();
childCompiler.hooks.afterCompile.tap(pluginName, (compilation) => {
source =
compilation.assets[childFilename] &&
compilation.assets[childFilename].source();

// Remove all chunk assets
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
delete compilation.assets[file]; // eslint-disable-line no-param-reassign
});
// Remove all chunk assets
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
delete compilation.assets[file]; // eslint-disable-line no-param-reassign
});
});
});

childCompiler.runAsChild((err, entries, compilation) => {
if (err) {
return callback(err);
}
childCompiler.runAsChild((err, entries, compilation) => {
if (err) {
return callback(err);
}

if (compilation.errors.length > 0) {
return callback(compilation.errors[0]);
}
if (compilation.errors.length > 0) {
return callback(compilation.errors[0]);
}

compilation.fileDependencies.forEach((dep) => {
this.addDependency(dep);
}, this);
compilation.fileDependencies.forEach((dep) => {
this.addDependency(dep);
}, this);

compilation.contextDependencies.forEach((dep) => {
this.addContextDependency(dep);
}, this);
compilation.contextDependencies.forEach((dep) => {
this.addContextDependency(dep);
}, this);

if (!source) {
return callback(new Error("Didn't get a result from child compiler"));
}
if (!source) {
return callback(new Error("Didn't get a result from child compiler"));
}

let text;
let locals;

try {
text = exec(this, source, request);
locals = text && text.locals;
if (!Array.isArray(text)) {
text = [[null, text]];
} else {
text = text.map((line) => {
const module = findModuleById(compilation.modules, line[0]);

return {
identifier: module.identifier(),
content: line[1],
media: line[2],
sourceMap: line[3],
};
});
}
this[MODULE_TYPE](text);
} catch (e) {
return callback(e);
let text;
let locals;

try {
text = exec(this, source, request);
locals = text && text.locals;
if (!Array.isArray(text)) {
text = [[null, text]];
} else {
text = text.map((line) => {
const module = findModuleById(compilation.modules, line[0]);

return {
identifier: module.identifier(),
content: line[1],
media: line[2],
sourceMap: line[3],
};
});
}
this[MODULE_TYPE](text);
} catch (e) {
return callback(e);
}

let resultSource = `// extracted by ${pluginName}`;
const result = locals
? `\nmodule.exports = ${JSON.stringify(locals)};`
: '';
let resultSource = `// extracted by ${pluginName}`;
const result = locals
? `\nmodule.exports = ${JSON.stringify(locals)};`
: '';

resultSource += options.hmr
? hotLoader(result, { context: this.context, options, locals })
: result;
resultSource += options.hmr
? hotLoader(result, { context: this.context, options, locals })
: result;

return callback(null, resultSource);
});
}
return callback(null, resultSource);
});
}

export default function(source) {
Expand Down