-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcd3c6f
commit ab12f5c
Showing
8 changed files
with
81 additions
and
81 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
* | ||
!index.cjs | ||
!index.js | ||
!index.mjs |
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,66 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const InertEntryPlugin = require('inert-entry-webpack-plugin'); | ||
const loaderUtils = require('loader-utils'); | ||
const { SingleEntryPlugin } = require('webpack'); | ||
|
||
module.exports = function () {}; | ||
|
||
module.exports.pitch = function (request) { | ||
const callback = this.async(); | ||
|
||
const options = loaderUtils.getOptions(this) || {}; | ||
const context = options.context || this.rootContext; | ||
const name = options.name || path.basename(this.resourcePath); | ||
const filename = loaderUtils.interpolateName(this, name, { | ||
context: context, | ||
}); | ||
const outputDir = options.path || '.'; | ||
const plugins = options.plugins || []; | ||
if (options.inert) { | ||
plugins.unshift(new InertEntryPlugin()); | ||
} | ||
|
||
// name of the entry and compiler (in logs) | ||
const debugName = loaderUtils.interpolateName(this, '[name]', {}); | ||
|
||
// create a child compiler (hacky) | ||
const compiler = this._compilation.createChildCompiler( | ||
debugName, | ||
{ filename: filename }, | ||
plugins, | ||
); | ||
new SingleEntryPlugin(this.context, '!!' + request, debugName).apply( | ||
compiler, | ||
); | ||
|
||
// add a dependency on the entry point of the child compiler, so watch mode works | ||
this.addDependency(request); | ||
|
||
// like compiler.runAsChild(), but remaps paths if necessary | ||
// https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L206 | ||
compiler.compile( | ||
function (err, compilation) { | ||
if (err) return callback(err); | ||
|
||
this.parentCompilation.children.push(compilation); | ||
for (const name of Object.keys(compilation.assets)) { | ||
this.parentCompilation.assets[path.join(outputDir, name)] = | ||
compilation.assets[name]; | ||
} | ||
|
||
// the first file in the first chunk of the first (should only be one) entry point is the real file | ||
// see https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L215 | ||
const outputFilename = compilation.entrypoints.values().next().value | ||
.chunks[0].files[0]; | ||
|
||
callback( | ||
null, | ||
'module.exports = __webpack_public_path__ + ' + | ||
JSON.stringify(path.join(outputDir, outputFilename)) + | ||
';', | ||
); | ||
}.bind(compiler), | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,66 +1,2 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const InertEntryPlugin = require('inert-entry-webpack-plugin'); | ||
const loaderUtils = require('loader-utils'); | ||
const { SingleEntryPlugin } = require('webpack'); | ||
|
||
module.exports = function () {}; | ||
|
||
module.exports.pitch = function (request) { | ||
const callback = this.async(); | ||
|
||
const options = loaderUtils.getOptions(this) || {}; | ||
const context = options.context || this.rootContext; | ||
const name = options.name || path.basename(this.resourcePath); | ||
const filename = loaderUtils.interpolateName(this, name, { | ||
context: context, | ||
}); | ||
const outputDir = options.path || '.'; | ||
const plugins = options.plugins || []; | ||
if (options.inert) { | ||
plugins.unshift(new InertEntryPlugin()); | ||
} | ||
|
||
// name of the entry and compiler (in logs) | ||
const debugName = loaderUtils.interpolateName(this, '[name]', {}); | ||
|
||
// create a child compiler (hacky) | ||
const compiler = this._compilation.createChildCompiler( | ||
debugName, | ||
{ filename: filename }, | ||
plugins, | ||
); | ||
new SingleEntryPlugin(this.context, '!!' + request, debugName).apply( | ||
compiler, | ||
); | ||
|
||
// add a dependency on the entry point of the child compiler, so watch mode works | ||
this.addDependency(request); | ||
|
||
// like compiler.runAsChild(), but remaps paths if necessary | ||
// https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L206 | ||
compiler.compile( | ||
function (err, compilation) { | ||
if (err) return callback(err); | ||
|
||
this.parentCompilation.children.push(compilation); | ||
for (const name of Object.keys(compilation.assets)) { | ||
this.parentCompilation.assets[path.join(outputDir, name)] = | ||
compilation.assets[name]; | ||
} | ||
|
||
// the first file in the first chunk of the first (should only be one) entry point is the real file | ||
// see https://github.com/webpack/webpack/blob/f6e366b4be1cfe2770251a890d93081824789209/lib/Compiler.js#L215 | ||
const outputFilename = compilation.entrypoints.values().next().value | ||
.chunks[0].files[0]; | ||
|
||
callback( | ||
null, | ||
'module.exports = __webpack_public_path__ + ' + | ||
JSON.stringify(path.join(outputDir, outputFilename)) + | ||
';', | ||
); | ||
}.bind(compiler), | ||
); | ||
}; | ||
export * from './index.cjs'; | ||
export { default } from './index.cjs'; |
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import 'file-loader?name=hi.jpeg!./hi.jpeg'; | ||
import '../../index.js?path=subchild&name=subchild.js!./child2.js'; | ||
import '../../index.cjs?path=subchild&name=subchild.js!./child2.js'; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import '../../index.js?path=children&name=child1.js!./child.js'; | ||
import '../../index.js?inert&path=inertOut!./manifest.notjson'; | ||
import '../../index.js?inert&name=[name]-inert.[hash:6].js!./defaults'; | ||
import '../../index.js?name=[name].[hash:6].js!./defaults'; | ||
import '../../index.js!./defaults'; | ||
import '../../index.cjs?path=children&name=child1.js!./child.js'; | ||
import '../../index.cjs?inert&path=inertOut!./manifest.notjson'; | ||
import '../../index.cjs?inert&name=[name]-inert.[hash:6].js!./defaults'; | ||
import '../../index.cjs?name=[name].[hash:6].js!./defaults'; | ||
import '../../index.cjs!./defaults'; | ||
import './other.entry.js'; |